| File: | nnc/cmd/attention_residual/ccv_nnc_attention_residual_cpu_ref.c |
| Warning: | line 33, column 61 Division by zero |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include "ccv.h" | ||||
| 2 | #include "ccv_internal.h" | ||||
| 3 | #include "nnc/ccv_nnc.h" | ||||
| 4 | #include "nnc/ccv_nnc_easy.h" | ||||
| 5 | #include "nnc/ccv_nnc_internal.h" | ||||
| 6 | #include <math.h> | ||||
| 7 | #ifdef USE_OPENMP | ||||
| 8 | #include <omp.h> | ||||
| 9 | #endif | ||||
| 10 | #ifdef USE_DISPATCH | ||||
| 11 | #include <dispatch/dispatch.h> | ||||
| 12 | #endif | ||||
| 13 | |||||
| 14 | // Each output is, for one query, a softmax mixture of the RMS-normalized states: the block | ||||
| 15 | // states and then the partial state. A state's score is the query's dot product with it, scaled. | ||||
| 16 | static int _ccv_nnc_attention_residual_forw(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context) | ||||
| 17 | { | ||||
| 18 | assert(input_size == 3)((void) sizeof ((input_size == 3) ? 1 : 0), __extension__ ({ if (input_size == 3) ; else __assert_fail ("input_size == 3", "attention_residual/ccv_nnc_attention_residual_cpu_ref.c" , 18, __extension__ __PRETTY_FUNCTION__); })); | ||||
| |||||
| 19 | assert(output_size >= 1)((void) sizeof ((output_size >= 1) ? 1 : 0), __extension__ ({ if (output_size >= 1) ; else __assert_fail ("output_size >= 1" , "attention_residual/ccv_nnc_attention_residual_cpu_ref.c", 19 , __extension__ __PRETTY_FUNCTION__); })); | ||||
| 20 | const ccv_nnc_tensor_t* const blocks = inputs[0]; | ||||
| 21 | const ccv_nnc_tensor_t* const partial = inputs[1]; | ||||
| 22 | const ccv_nnc_tensor_t* const queries = inputs[2]; | ||||
| 23 | int i; | ||||
| 24 | for (i = 0; i < 3; i++) | ||||
| 25 | { assert(inputs[i]->info.datatype == CCV_32F && CCV_IS_TENSOR_CONTIGUOUS(inputs[i]))((void) sizeof ((inputs[i]->info.datatype == CCV_32F && (!((*(int*)(inputs[i])) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t *)inputs[i])->contiguous == 1))) ? 1 : 0), __extension__ ( { if (inputs[i]->info.datatype == CCV_32F && (!((* (int*)(inputs[i])) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t *)inputs[i])->contiguous == 1))) ; else __assert_fail ("inputs[i]->info.datatype == CCV_32F && CCV_IS_TENSOR_CONTIGUOUS(inputs[i])" , "attention_residual/ccv_nnc_attention_residual_cpu_ref.c", 25 , __extension__ __PRETTY_FUNCTION__); })); } | ||||
| 26 | for (i = 0; i
| ||||
| 27 | { assert(outputs[i]->info.datatype == CCV_32F && CCV_IS_TENSOR_CONTIGUOUS(outputs[i]))((void) sizeof ((outputs[i]->info.datatype == CCV_32F && (!((*(int*)(outputs[i])) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t *)outputs[i])->contiguous == 1))) ? 1 : 0), __extension__ ( { if (outputs[i]->info.datatype == CCV_32F && (!(( *(int*)(outputs[i])) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t *)outputs[i])->contiguous == 1))) ; else __assert_fail ("outputs[i]->info.datatype == CCV_32F && CCV_IS_TENSOR_CONTIGUOUS(outputs[i])" , "attention_residual/ccv_nnc_attention_residual_cpu_ref.c", 27 , __extension__ __PRETTY_FUNCTION__); })); } | ||||
| 28 | // Shapes only matter through their counts: C is the partial state's last axis, every block | ||||
| 29 | // state has as many values as the partial state, and every query has C. | ||||
| 30 | const int channels = partial->info.dim[ccv_nnc_tensor_nd(partial->info.dim) - 1]; | ||||
| 31 | const size_t state_size = ccv_nnc_tensor_count(partial->info); | ||||
| 32 | const int rows = state_size / channels; | ||||
| 33 | const int block_count = ccv_nnc_tensor_count(blocks->info) / state_size; | ||||
| |||||
| 34 | assert(ccv_nnc_tensor_count(blocks->info) == (size_t)block_count * state_size)((void) sizeof ((ccv_nnc_tensor_count(blocks->info) == (size_t )block_count * state_size) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_count (blocks->info) == (size_t)block_count * state_size) ; else __assert_fail ("ccv_nnc_tensor_count(blocks->info) == (size_t)block_count * state_size" , "attention_residual/ccv_nnc_attention_residual_cpu_ref.c", 34 , __extension__ __PRETTY_FUNCTION__); })); | ||||
| 35 | assert(ccv_nnc_tensor_count(queries->info) == (size_t)output_size * channels)((void) sizeof ((ccv_nnc_tensor_count(queries->info) == (size_t )output_size * channels) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_count (queries->info) == (size_t)output_size * channels) ; else __assert_fail ("ccv_nnc_tensor_count(queries->info) == (size_t)output_size * channels" , "attention_residual/ccv_nnc_attention_residual_cpu_ref.c", 35 , __extension__ __PRETTY_FUNCTION__); })); | ||||
| 36 | for (i = 0; i < output_size; i++) | ||||
| 37 | { assert(ccv_nnc_tensor_count(outputs[i]->info) == state_size)((void) sizeof ((ccv_nnc_tensor_count(outputs[i]->info) == state_size) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_count (outputs[i]->info) == state_size) ; else __assert_fail ("ccv_nnc_tensor_count(outputs[i]->info) == state_size" , "attention_residual/ccv_nnc_attention_residual_cpu_ref.c", 37 , __extension__ __PRETTY_FUNCTION__); })); } | ||||
| 38 | const float epsilon = cmd.info.attention_residual.epsilon; | ||||
| 39 | const float scale = cmd.info.attention_residual.scale; | ||||
| 40 | const int state_count = block_count + 1; | ||||
| 41 | parallel_for(t, rows){ int t; for ((t) = 0; (t) < (rows); (t)++) { { | ||||
| 42 | // A state's reciprocal RMS, then its weight for the query at hand. | ||||
| 43 | float* const reciprocal = (float*)ccmallocmalloc(sizeof(float) * state_count * 2); | ||||
| 44 | float* const weight = reciprocal + state_count; | ||||
| 45 | int s, c, q; | ||||
| 46 | #define STATE(s) ((s) < block_count ? blocks->data.f32 + ((size_t)(s) * rows + t) * channels : partial->data.f32 + (size_t)t * channels) | ||||
| 47 | for (s = 0; s < state_count; s++) | ||||
| 48 | { | ||||
| 49 | const float* const x = STATE(s); | ||||
| 50 | // The sums of thousands of channels are in double. | ||||
| 51 | double sum = 0; | ||||
| 52 | for (c = 0; c < channels; c++) | ||||
| 53 | sum += (double)x[c] * x[c]; | ||||
| 54 | reciprocal[s] = 1 / sqrt(sum / channels + epsilon); | ||||
| 55 | } | ||||
| 56 | for (q = 0; q < output_size; q++) | ||||
| 57 | { | ||||
| 58 | const float* const query = queries->data.f32 + (size_t)q * channels; | ||||
| 59 | float maximum = 0; | ||||
| 60 | for (s = 0; s < state_count; s++) | ||||
| 61 | { | ||||
| 62 | const float* const x = STATE(s); | ||||
| 63 | double dot = 0; | ||||
| 64 | for (c = 0; c < channels; c++) | ||||
| 65 | dot += (double)query[c] * x[c]; | ||||
| 66 | weight[s] = dot * reciprocal[s] * scale; | ||||
| 67 | if (s == 0 || weight[s] > maximum) | ||||
| 68 | maximum = weight[s]; | ||||
| 69 | } | ||||
| 70 | float sum = 0; | ||||
| 71 | for (s = 0; s < state_count; s++) | ||||
| 72 | sum += (weight[s] = expf(weight[s] - maximum)); | ||||
| 73 | float* const y = outputs[q]->data.f32 + (size_t)t * channels; | ||||
| 74 | for (c = 0; c < channels; c++) | ||||
| 75 | y[c] = 0; | ||||
| 76 | for (s = 0; s < state_count; s++) | ||||
| 77 | { | ||||
| 78 | const float* const x = STATE(s); | ||||
| 79 | // The mixture is of the normalized states. | ||||
| 80 | const float w = weight[s] / sum * reciprocal[s]; | ||||
| 81 | for (c = 0; c < channels; c++) | ||||
| 82 | y[c] += w * x[c]; | ||||
| 83 | } | ||||
| 84 | } | ||||
| 85 | #undef STATE | ||||
| 86 | ccfreefree(reciprocal); | ||||
| 87 | } parallel_endfor} } | ||||
| 88 | return CCV_NNC_EXEC_SUCCESS; | ||||
| 89 | } | ||||
| 90 | |||||
| 91 | static int _ccv_nnc_attention_residual_back(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context) | ||||
| 92 | { | ||||
| 93 | return CCV_NNC_EXEC_INVALID; | ||||
| 94 | } | ||||
| 95 | |||||
| 96 | REGISTER_COMMAND_BACKEND(CCV_NNC_ATTENTION_RESIDUAL_FORWARD, CCV_NNC_BACKEND_CPU_REF)void _register_command_CCV_NNC_ATTENTION_RESIDUAL_FORWARD_backend_CCV_NNC_BACKEND_CPU_REF(ccv_nnc_cmd_backend_registry_t* const registry) | ||||
| 97 | { | ||||
| 98 | registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN; | ||||
| 99 | registry->tensor_datatypes = CCV_32F; | ||||
| 100 | registry->tensor_memory = CCV_TENSOR_CPU_MEMORY; | ||||
| 101 | registry->algorithms = 1; | ||||
| 102 | registry->exec = _ccv_nnc_attention_residual_forw; | ||||
| 103 | } | ||||
| 104 | |||||
| 105 | REGISTER_COMMAND_BACKEND(CCV_NNC_ATTENTION_RESIDUAL_BACKWARD, CCV_NNC_BACKEND_CPU_REF)void _register_command_CCV_NNC_ATTENTION_RESIDUAL_BACKWARD_backend_CCV_NNC_BACKEND_CPU_REF(ccv_nnc_cmd_backend_registry_t* const registry) | ||||
| 106 | { | ||||
| 107 | registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN; | ||||
| 108 | registry->tensor_datatypes = CCV_32F; | ||||
| 109 | registry->tensor_memory = CCV_TENSOR_CPU_MEMORY; | ||||
| 110 | registry->algorithms = 1; | ||||
| 111 | registry->exec = _ccv_nnc_attention_residual_back; | ||||
| 112 | } |
| 1 | /********************************************************** |
| 2 | * C-based/Cached/Core Computer Vision Library |
| 3 | * Liu Liu, 2010-02-01 |
| 4 | **********************************************************/ |
| 5 | |
| 6 | /********************************************************** |
| 7 | * CCV - Neural Network Collection |
| 8 | **********************************************************/ |
| 9 | |
| 10 | #ifndef GUARD_ccv_nnc_easy_h |
| 11 | #define GUARD_ccv_nnc_easy_h |
| 12 | |
| 13 | #include "ccv.h" |
| 14 | #include "ccv_internal.h" |
| 15 | #include "nnc/ccv_nnc.h" |
| 16 | #ifdef HAVE_MPS |
| 17 | #ifdef __APPLE__ |
| 18 | #include "TargetConditionals.h" |
| 19 | #if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR |
| 20 | #include <mach/mach_vm.h> |
| 21 | #else |
| 22 | #define PAGE_SIZE (16384) |
| 23 | #endif |
| 24 | #endif |
| 25 | #endif |
| 26 | |
| 27 | /** |
| 28 | * Convenience API |
| 29 | * |
| 30 | * This header provides convenience APIs for nnc usage. Being convenience API, |
| 31 | * it is optimized for shorthand coding, and may collide the naming space with |
| 32 | * others. |
| 33 | * |
| 34 | */ |
| 35 | // c99 only, make sure your compiler supports that. |
| 36 | |
| 37 | #define NOOP_GRAPH_WHILE_EXPR(ccv_nnc_graph_while_f)(1) (ccv_nnc_graph_while_f)(1) |
| 38 | #define NOOP_GRAPH_CASE_OF_EXPR(ccv_nnc_graph_case_of_f)(1) (ccv_nnc_graph_case_of_f)(1) |
| 39 | |
| 40 | // This is a better LIST_COUNT macro, it generates a list of 1+1+0+0+0 where it is 1 if the parameter presents, and 0 otherwise. |
| 41 | // This works better for cases such as LIST_COUNT(1, 2, 3,) where previous macro will get 4 and this one will have correctly |
| 42 | // computed result. |
| 43 | #define LIST_COUNT_01(_0,_1,_2,...)_2 _2 |
| 44 | #define LIST_COUNT_E(...)1 LIST_COUNT_01(_0,##__VA_ARGS__,1,0)1 |
| 45 | #define LIST_COUNT_N(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62,_63,...)(1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 + 1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 + 1 +1 +1 -1) (LIST_COUNT_E(_0)1+LIST_COUNT_E(_1)1+LIST_COUNT_E(_2)1+LIST_COUNT_E(_3)1+LIST_COUNT_E(_4)1+LIST_COUNT_E(_5)1+LIST_COUNT_E(_6)1+LIST_COUNT_E(_7)1+LIST_COUNT_E(_8)1+LIST_COUNT_E(_9)1+LIST_COUNT_E(_10)1+LIST_COUNT_E(_11)1+LIST_COUNT_E(_12)1+LIST_COUNT_E(_13)1+LIST_COUNT_E(_14)1+LIST_COUNT_E(_15)1+LIST_COUNT_E(_16)1+LIST_COUNT_E(_17)1+LIST_COUNT_E(_18)1+LIST_COUNT_E(_19)1+LIST_COUNT_E(_20)1+LIST_COUNT_E(_21)1+LIST_COUNT_E(_22)1+LIST_COUNT_E(_23)1+LIST_COUNT_E(_24)1+LIST_COUNT_E(_25)1+LIST_COUNT_E(_26)1+LIST_COUNT_E(_27)1+LIST_COUNT_E(_28)1+LIST_COUNT_E(_29)1+LIST_COUNT_E(_30)1+LIST_COUNT_E(_31)1+LIST_COUNT_E(_32)1+LIST_COUNT_E(_33)1+LIST_COUNT_E(_34)1+LIST_COUNT_E(_35)1+LIST_COUNT_E(_36)1+LIST_COUNT_E(_37)1+LIST_COUNT_E(_38)1+LIST_COUNT_E(_39)1+LIST_COUNT_E(_40)1+LIST_COUNT_E(_41)1+LIST_COUNT_E(_42)1+LIST_COUNT_E(_43)1+LIST_COUNT_E(_44)1+LIST_COUNT_E(_45)1+LIST_COUNT_E(_46)1+LIST_COUNT_E(_47)1+LIST_COUNT_E(_48)1+LIST_COUNT_E(_49)1+LIST_COUNT_E(_50)1+LIST_COUNT_E(_51)1+LIST_COUNT_E(_52)1+LIST_COUNT_E(_53)1+LIST_COUNT_E(_54)1+LIST_COUNT_E(_55)1+LIST_COUNT_E(_56)1+LIST_COUNT_E(_57)1+LIST_COUNT_E(_58)1+LIST_COUNT_E(_59)1+LIST_COUNT_E(_60)1+LIST_COUNT_E(_61)1+LIST_COUNT_E(_62)1+LIST_COUNT_E(_63)1-1) |
| 46 | #define LIST_COUNT(...)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) LIST_COUNT_N(_0,##__VA_ARGS__,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 47 | |
| 48 | #define LIST_X(_type, ...)(_type []){...} (_type []){__VA_ARGS__} |
| 49 | |
| 50 | #define KV_X_2(_x, _y, ...){(_x), (_y)} {(_x), (_y)} |
| 51 | #define KV_X_1(_x, ...){(_x)} {(_x)} |
| 52 | #define KV_X_SEL(_1, _2, _FX, ...)_FX _FX |
| 53 | #define KV(...){(...)} KV_X_SEL(__VA_ARGS__, KV_X_2, KV_X_1)(__VA_ARGS__){(__VA_ARGS__)} |
| 54 | |
| 55 | #define LIST_SIZEOF_COUNT(_type, ...)(sizeof((_type []){...}) / sizeof(_type)) (sizeof(LIST_X(_type, __VA_ARGS__)(_type []){__VA_ARGS__}) / sizeof(_type)) |
| 56 | |
| 57 | /** |
| 58 | * @defgroup convenience_api Convenience API |
| 59 | * @{ |
| 60 | */ |
| 61 | /** |
| 62 | * Pass a list of tensors to NNC functions that accepts (tensor array, tensor array size). |
| 63 | * This method effectively gives two parameters as one. |
| 64 | */ |
| 65 | #define TENSOR_LIST(...)(ccv_nnc_tensor_t* []){...}, (1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(ccv_nnc_tensor_t*, __VA_ARGS__)(ccv_nnc_tensor_t* []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 66 | /** |
| 67 | * Pass a list of tensor parameters to NNC functions that accepts (parameter array, parameter array size). |
| 68 | * This method effectively gives two parameters as one. |
| 69 | */ |
| 70 | #define TENSOR_PARAM_LIST(...)(const ccv_nnc_tensor_param_t []){...}, (1 +1 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const ccv_nnc_tensor_param_t, __VA_ARGS__)(const ccv_nnc_tensor_param_t []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 71 | /** |
| 72 | * This represents a tensor symbol that is empty (tensor = nil) |
| 73 | */ |
| 74 | #define NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL } (const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL} |
| 75 | /** |
| 76 | * This represents a graph exec symbol that is empty (exec = nil) |
| 77 | */ |
| 78 | #define NO_GRAPH_EXEC_SYMBOL(const ccv_nnc_graph_exec_symbol_t){.d = CCV_NNC_NO_GRAPH_EXEC_SYMBOL } (const ccv_nnc_graph_exec_symbol_t){.d = CCV_NNC_NO_GRAPH_EXEC_SYMBOL} |
| 79 | /** |
| 80 | * Pass a list of tensor symbols to NNC functions that accepts (tensor symbol array, tensor symbol array size). |
| 81 | * This method effectively gives two parameters as one. |
| 82 | */ |
| 83 | #define TENSOR_SYMBOL_LIST(...)(const ccv_nnc_tensor_symbol_t []){...}, (1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const ccv_nnc_tensor_symbol_t, __VA_ARGS__)(const ccv_nnc_tensor_symbol_t []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 84 | /** |
| 85 | * Pass a list of tensor variables to NNC functions that accepts (tensor variable array, tensor variable array size). |
| 86 | * This method effectively gives two parameters as one. |
| 87 | */ |
| 88 | #define TENSOR_VARIABLE_LIST(...)(ccv_nnc_tensor_variable_t []){...}, (1 +1 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(ccv_nnc_tensor_variable_t, __VA_ARGS__)(ccv_nnc_tensor_variable_t []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 89 | /** |
| 90 | * Pass a list of tensor bindings to NNC functions that accepts (tensor binding array, tensor binding array size). |
| 91 | * This method effectively gives two parameters as one. Since tensor binding requires two: symbol and a tensor, |
| 92 | * you should use this like: TENSOR_BIND_MAP(KV(symbol1, tensor1), KV(symbol2, tensor2)). |
| 93 | */ |
| 94 | #define TENSOR_BIND_MAP(...)(const ccv_nnc_tensor_bind_t []){...}, (sizeof((ccv_nnc_tensor_bind_t []){...}) / sizeof(ccv_nnc_tensor_bind_t)) LIST_X(const ccv_nnc_tensor_bind_t, __VA_ARGS__)(const ccv_nnc_tensor_bind_t []){__VA_ARGS__}, LIST_SIZEOF_COUNT(ccv_nnc_tensor_bind_t, __VA_ARGS__)(sizeof((ccv_nnc_tensor_bind_t []){__VA_ARGS__}) / sizeof(ccv_nnc_tensor_bind_t )) |
| 95 | /** |
| 96 | * Pass a list of tensor symbol pairs to NNC functions that accepts (tensor symbol pair array, tensor symbol pair array size). |
| 97 | * This method effectively gives two parameters as one. Since tensor symbol pair requires two: source symbol and destination symbol, |
| 98 | * you should use this like: TENSOR_SYMBOL_MAP(KV(symbol1, symbol2), KV(symbol3, symbol4)). |
| 99 | */ |
| 100 | #define TENSOR_SYMBOL_MAP(...)(const ccv_nnc_tensor_symbol_map_t []){...}, (sizeof((ccv_nnc_tensor_symbol_map_t []){...}) / sizeof(ccv_nnc_tensor_symbol_map_t)) LIST_X(const ccv_nnc_tensor_symbol_map_t, __VA_ARGS__)(const ccv_nnc_tensor_symbol_map_t []){__VA_ARGS__}, LIST_SIZEOF_COUNT(ccv_nnc_tensor_symbol_map_t, __VA_ARGS__)(sizeof((ccv_nnc_tensor_symbol_map_t []){__VA_ARGS__}) / sizeof (ccv_nnc_tensor_symbol_map_t)) |
| 101 | /** |
| 102 | * Pass a list of execution nodes to NNC functions that accepts (execution node array, execution node array size). |
| 103 | * This method effectively gives two parameters as one. |
| 104 | */ |
| 105 | #define GRAPH_EXEC_LIST(...)(const ccv_nnc_graph_exec_t []){...}, (1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const ccv_nnc_graph_exec_t, __VA_ARGS__)(const ccv_nnc_graph_exec_t []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 106 | /** |
| 107 | * Pass a list of execution node symbols to NNC functions that accepts (execution node symbol array, execution node symbol array size). |
| 108 | * This method effectively gives two parameters as one. |
| 109 | */ |
| 110 | #define GRAPH_EXEC_SYMBOL_LIST(...)(const ccv_nnc_graph_exec_symbol_t []){...}, (1 +1 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const ccv_nnc_graph_exec_symbol_t, __VA_ARGS__)(const ccv_nnc_graph_exec_symbol_t []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 111 | /** |
| 112 | * Pass both default sources and default sources size to function that accepts (sources, source size). |
| 113 | * @param x A given symbolic graph. |
| 114 | */ |
| 115 | #define SYMBOLIC_GRAPH_SOURCES(x)ccv_nnc_symbolic_graph_sources(x), ccv_nnc_symbolic_graph_source_size (x) ccv_nnc_symbolic_graph_sources(x), ccv_nnc_symbolic_graph_source_size(x) |
| 116 | /** |
| 117 | * Pass both default destinations and default destinations size to function that accepts (destinations, destination size). |
| 118 | * @param x A given symbolic graph. |
| 119 | */ |
| 120 | #define SYMBOLIC_GRAPH_DESTINATIONS(x)ccv_nnc_symbolic_graph_destinations(x), ccv_nnc_symbolic_graph_destination_size (x) ccv_nnc_symbolic_graph_destinations(x), ccv_nnc_symbolic_graph_destination_size(x) |
| 121 | /** |
| 122 | * Pass a list of simplification passes to NNC functions that accepts (pass array, pass array size). |
| 123 | * This method effectively gives two parameters as one. |
| 124 | */ |
| 125 | #define SYMBOLIC_GRAPH_PASSES(...)(const int []){...}, (1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const int, __VA_ARGS__)(const int []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 126 | /** |
| 127 | * Pass a list of CNNP models to NNC functions that accepts (model array, model array size). |
| 128 | * This method effectively gives two parameters as one. |
| 129 | */ |
| 130 | #define MODEL_LIST(...)(ccv_cnnp_model_t* []){...}, (1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(ccv_cnnp_model_t*, __VA_ARGS__)(ccv_cnnp_model_t* []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 131 | /** |
| 132 | * Pass a list of CNNP model IOs to NNC functions that accepts (model IO array, model IO array size). |
| 133 | * This method effectively gives two parameters as one. |
| 134 | */ |
| 135 | #define MODEL_IO_LIST(...)(const ccv_cnnp_model_io_t []){...}, (1 +1 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const ccv_cnnp_model_io_t, __VA_ARGS__)(const ccv_cnnp_model_io_t []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 136 | /** |
| 137 | * Pass a list of CNNP tensor params to ccv_cnnp_cmd_exec which accepts (tensor params array, tensor params array size). |
| 138 | * This method effectively gives two parameters as one. |
| 139 | */ |
| 140 | #define MODEL_CMD_EXEC_IO_MAP(...)(const ccv_cnnp_cmd_exec_io_t []){...}, (sizeof((ccv_cnnp_cmd_exec_io_t []){...}) / sizeof(ccv_cnnp_cmd_exec_io_t)) LIST_X(const ccv_cnnp_cmd_exec_io_t, __VA_ARGS__)(const ccv_cnnp_cmd_exec_io_t []){__VA_ARGS__}, LIST_SIZEOF_COUNT(ccv_cnnp_cmd_exec_io_t, __VA_ARGS__)(sizeof((ccv_cnnp_cmd_exec_io_t []){__VA_ARGS__}) / sizeof(ccv_cnnp_cmd_exec_io_t )) |
| 141 | /** |
| 142 | * Pass a list of CNNP tensor type to ccv_cnnp_cmd_exec which accepts (tensor type array, tensor type array size). |
| 143 | * This method effectively gives two parameters as one. |
| 144 | */ |
| 145 | #define MODEL_CMD_EXEC_IO_LIST(...)(const int []){...}, (1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const int, __VA_ARGS__)(const int []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 146 | /** |
| 147 | * Pass a list of dataframe column ids to iteration function that accepts (column id array, column id array size). |
| 148 | * This method effectively gives two parameters as one. |
| 149 | */ |
| 150 | #define COLUMN_ID_LIST(...)(const int []){...}, (1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1) LIST_X(const int, __VA_ARGS__)(const int []){__VA_ARGS__}, LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1) |
| 151 | |
| 152 | #define TRAVERSE_FULL0,0,0,0 0,0,0,0 |
| 153 | |
| 154 | #define ALL_PARAMETERS-1 -1 |
| 155 | |
| 156 | // We will support NUMA allocation on CPU in the future. Currently, this is not very meaningful (except enforce no memory reuse between tensors). |
| 157 | #define CPU_NUMA_TENSOR_NHWC(device_id, dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_device_id ) | CCV_TENSOR_CPU_MEMORY,.format=CCV_TENSOR_FORMAT_NHWC,.datatype =CCV_dt,.dim={...}}) ((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_##device_id) | CCV_TENSOR_CPU_MEMORY,.format=CCV_TENSOR_FORMAT_NHWC,.datatype=CCV_##dt,.dim={__VA_ARGS__}}) |
| 158 | #define CPU_NUMA_TENSOR_NCHW(device_id, dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_device_id ) | CCV_TENSOR_CPU_MEMORY,.format=CCV_TENSOR_FORMAT_NCHW,.datatype =CCV_dt,.dim={...}}) ((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_##device_id) | CCV_TENSOR_CPU_MEMORY,.format=CCV_TENSOR_FORMAT_NCHW,.datatype=CCV_##dt,.dim={__VA_ARGS__}}) |
| 159 | #define CPU_NUMA_TENSOR_CHWN(device_id, dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_device_id ) | CCV_TENSOR_CPU_MEMORY,.format=CCV_TENSOR_FORMAT_CHWN,.datatype =CCV_dt,.dim={...}}) ((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_##device_id) | CCV_TENSOR_CPU_MEMORY,.format=CCV_TENSOR_FORMAT_CHWN,.datatype=CCV_##dt,.dim={__VA_ARGS__}}) |
| 160 | #define CPU_TENSOR_NHWC(dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_ANY) | CCV_TENSOR_CPU_MEMORY ,.format=CCV_TENSOR_FORMAT_NHWC,.datatype=CCV_dt,.dim={...}}) CPU_NUMA_TENSOR_NHWC(ANY, dt, __VA_ARGS__)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_ANY) | CCV_TENSOR_CPU_MEMORY ,.format=CCV_TENSOR_FORMAT_NHWC,.datatype=CCV_dt,.dim={__VA_ARGS__ }}) |
| 161 | #define CPU_TENSOR_NCHW(dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_ANY) | CCV_TENSOR_CPU_MEMORY ,.format=CCV_TENSOR_FORMAT_NCHW,.datatype=CCV_dt,.dim={...}}) CPU_NUMA_TENSOR_NCHW(ANY, dt, __VA_ARGS__)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_ANY) | CCV_TENSOR_CPU_MEMORY ,.format=CCV_TENSOR_FORMAT_NCHW,.datatype=CCV_dt,.dim={__VA_ARGS__ }}) |
| 162 | #define CPU_TENSOR_CHWN(dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_ANY) | CCV_TENSOR_CPU_MEMORY ,.format=CCV_TENSOR_FORMAT_CHWN,.datatype=CCV_dt,.dim={...}}) CPU_NUMA_TENSOR_CHWN(ANY, dt, __VA_ARGS__)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_ANY) | CCV_TENSOR_CPU_MEMORY ,.format=CCV_TENSOR_FORMAT_CHWN,.datatype=CCV_dt,.dim={__VA_ARGS__ }}) |
| 163 | // This way, we can do error check on the device type :) |
| 164 | #define GPU_TENSOR_NHWC(device_id, dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_device_id ) | CCV_TENSOR_GPU_MEMORY,.format=CCV_TENSOR_FORMAT_NHWC,.datatype =CCV_dt,.dim={...}}) ((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_##device_id) | CCV_TENSOR_GPU_MEMORY,.format=CCV_TENSOR_FORMAT_NHWC,.datatype=CCV_##dt,.dim={__VA_ARGS__}}) |
| 165 | #define GPU_TENSOR_NCHW(device_id, dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_device_id ) | CCV_TENSOR_GPU_MEMORY,.format=CCV_TENSOR_FORMAT_NCHW,.datatype =CCV_dt,.dim={...}}) ((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_##device_id) | CCV_TENSOR_GPU_MEMORY,.format=CCV_TENSOR_FORMAT_NCHW,.datatype=CCV_##dt,.dim={__VA_ARGS__}}) |
| 166 | #define GPU_TENSOR_CHWN(device_id, dt, ...)((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_device_id ) | CCV_TENSOR_GPU_MEMORY,.format=CCV_TENSOR_FORMAT_CHWN,.datatype =CCV_dt,.dim={...}}) ((ccv_nnc_tensor_param_t){.type=(CCV_COMPUTE_DEVICE_##device_id) | CCV_TENSOR_GPU_MEMORY,.format=CCV_TENSOR_FORMAT_CHWN,.datatype=CCV_##dt,.dim={__VA_ARGS__}}) |
| 167 | /** @} */ |
| 168 | |
| 169 | #define DIM_ALLOC(...)(int [(12)]){...} (int [CCV_NNC_MAX_DIM_ALLOC(12)]){__VA_ARGS__} |
| 170 | |
| 171 | #define ESCAPE_X(...)... __VA_ARGS__ |
| 172 | #define HINT_X_1(_stride_)((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X _stride_}}, .border ={.begin={0},.end={0}}}) ((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X _stride_}}, .border={.begin={0},.end={0}}}) |
| 173 | #define HINT_X_2(_stride_, _border_)((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X _stride_}}, .border ={.begin={ESCAPE_X _border_},.end={ESCAPE_X _border_}}}) ((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X _stride_}}, .border={.begin={ESCAPE_X _border_},.end={ESCAPE_X _border_}}}) |
| 174 | #define HINT_X_3(_stride_, _begin_, _end_)((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X _stride_}}, .border ={.begin={ESCAPE_X _begin_},.end={ESCAPE_X _end_}}}) ((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X _stride_}}, .border={.begin={ESCAPE_X _begin_},.end={ESCAPE_X _end_}}}) |
| 175 | #define HINT_X_SEL(_1, _2, _3, _FX, ...)_FX _FX |
| 176 | /** |
| 177 | * @ingroup convenience_api |
| 178 | * Simpler method to create hint. |
| 179 | * HINT(stride), HINT(stride, border), HINT(stride, border begin, border end) |
| 180 | */ |
| 181 | #define HINT(...)((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X ...}}, .border={.begin ={0},.end={0}}}) HINT_X_SEL(__VA_ARGS__, HINT_X_3, HINT_X_2, HINT_X_1)(__VA_ARGS__)((ccv_nnc_hint_t){.stride={.dim={ESCAPE_X __VA_ARGS__}}, .border ={.begin={0},.end={0}}}) |
| 182 | |
| 183 | static inline size_t ccv_nnc_dimension_count(const int dim[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 184 | { |
| 185 | if (dim[0] == 0) |
| 186 | return 0; |
| 187 | int i; |
| 188 | size_t count = dim[0]; |
| 189 | for (i = 1; i < CCV_NNC_MAX_DIM_ALLOC(12) && dim[i] > 0; i++) |
| 190 | count *= dim[i]; |
| 191 | return count; |
| 192 | } |
| 193 | |
| 194 | static inline size_t ccv_nnc_dimension_upper_bound(const int dim[CCV_NNC_MAX_DIM_ALLOC(12)], const int stride[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 195 | { |
| 196 | if (dim[0] == 0 || stride[0] == 0) |
| 197 | return 0; |
| 198 | int i; |
| 199 | size_t count = 1 + (dim[0] - 1) * stride[0]; |
| 200 | for (i = 1; i < CCV_NNC_MAX_DIM_ALLOC(12) && dim[i] > 0 && stride[i] > 0; i++) |
| 201 | count += (dim[i] - 1) * stride[i]; |
| 202 | return count; |
| 203 | } |
| 204 | |
| 205 | static inline size_t ccv_nnc_tensor_count(const ccv_nnc_tensor_param_t params) |
| 206 | { |
| 207 | return ccv_nnc_dimension_count(params.dim); |
| 208 | } |
| 209 | |
| 210 | static inline ccv_nnc_tensor_param_t ccv_nnc_tensor_palettize(const ccv_nnc_tensor_param_t params, const int qbits, const int number_in_blocks) |
| 211 | { |
| 212 | assert(params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF)((void) sizeof ((params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF) ? 1 : 0), __extension__ ({ if (params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF) ; else __assert_fail ("params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF" , "../../nnc/ccv_nnc_easy.h", 212, __extension__ __PRETTY_FUNCTION__ ); })); |
| 213 | ccv_nnc_tensor_param_t new_params = params; |
| 214 | assert(qbits >= 4 && qbits <= 8)((void) sizeof ((qbits >= 4 && qbits <= 8) ? 1 : 0), __extension__ ({ if (qbits >= 4 && qbits <= 8) ; else __assert_fail ("qbits >= 4 && qbits <= 8" , "../../nnc/ccv_nnc_easy.h", 214, __extension__ __PRETTY_FUNCTION__ ); })); |
| 215 | new_params.datatype = ((params.datatype >> 12) & 0xff) | CCV_QX | ((qbits << 8) & 0xf00); |
| 216 | new_params.reserved = number_in_blocks; |
| 217 | return new_params; |
| 218 | } |
| 219 | |
| 220 | static inline ccv_nnc_tensor_param_t ccv_nnc_tensor_8i_rowwise(const ccv_nnc_tensor_param_t params) |
| 221 | { |
| 222 | assert(params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF)((void) sizeof ((params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF) ? 1 : 0), __extension__ ({ if (params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF) ; else __assert_fail ("params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF" , "../../nnc/ccv_nnc_easy.h", 222, __extension__ __PRETTY_FUNCTION__ ); })); |
| 223 | ccv_nnc_tensor_param_t new_params = params; |
| 224 | new_params.datatype = ((params.datatype >> 12) & 0xff) | CCV_QX | CCV_NNC_QX_8I_ROWWISE; |
| 225 | new_params.reserved = 0; |
| 226 | return new_params; |
| 227 | } |
| 228 | |
| 229 | static inline int ccv_nnc_8i_rowwise_x_group_size(const int format) |
| 230 | { |
| 231 | switch (format & CCV_NNC_QX_8I_ROWWISE_FORMAT_MASK) |
| 232 | { |
| 233 | case CCV_NNC_QX_8I_ROWWISE_Q5_K: |
| 234 | case CCV_NNC_QX_8I_ROWWISE_Q4_K: |
| 235 | case CCV_NNC_QX_8I_ROWWISE_Q3_K: |
| 236 | case CCV_NNC_QX_8I_ROWWISE_Q2_K: |
| 237 | case CCV_NNC_QX_8I_ROWWISE_IQ2_S: |
| 238 | case CCV_NNC_QX_8I_ROWWISE_IQ3_S: |
| 239 | case CCV_NNC_QX_8I_ROWWISE_IQ1_M: |
| 240 | return 16; |
| 241 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XXS: |
| 242 | case CCV_NNC_QX_8I_ROWWISE_IQ1_S: |
| 243 | return 32; |
| 244 | case CCV_NNC_QX_8I_ROWWISE_Q6_K: |
| 245 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XS: |
| 246 | case CCV_NNC_QX_8I_ROWWISE_IQ3_XXS: |
| 247 | return 8; |
| 248 | default: |
| 249 | assert(0)((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "../../nnc/ccv_nnc_easy.h", 249, __extension__ __PRETTY_FUNCTION__ ); })); |
| 250 | return 0; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static inline int ccv_nnc_8i_rowwise_x_group_bits(const int format) |
| 255 | { |
| 256 | switch (format & CCV_NNC_QX_8I_ROWWISE_FORMAT_MASK) |
| 257 | { |
| 258 | case CCV_NNC_QX_8I_ROWWISE_Q5_K: |
| 259 | return 88; |
| 260 | case CCV_NNC_QX_8I_ROWWISE_Q4_K: |
| 261 | return 72; |
| 262 | case CCV_NNC_QX_8I_ROWWISE_Q3_K: |
| 263 | case CCV_NNC_QX_8I_ROWWISE_IQ3_S: |
| 264 | return 56; |
| 265 | case CCV_NNC_QX_8I_ROWWISE_Q2_K: |
| 266 | case CCV_NNC_QX_8I_ROWWISE_IQ2_S: |
| 267 | return 42; |
| 268 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XS: |
| 269 | return 21; |
| 270 | case CCV_NNC_QX_8I_ROWWISE_IQ3_XXS: |
| 271 | case CCV_NNC_QX_8I_ROWWISE_IQ1_M: |
| 272 | return 28; |
| 273 | case CCV_NNC_QX_8I_ROWWISE_IQ1_S: |
| 274 | return 50; |
| 275 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XXS: |
| 276 | return 64; |
| 277 | case CCV_NNC_QX_8I_ROWWISE_Q6_K: |
| 278 | return 52; |
| 279 | default: |
| 280 | assert(0)((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "../../nnc/ccv_nnc_easy.h", 280, __extension__ __PRETTY_FUNCTION__ ); })); |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static inline ccv_nnc_tensor_param_t ccv_nnc_tensor_8i_rowwise_x(const ccv_nnc_tensor_param_t params, const int format) |
| 286 | { |
| 287 | assert(params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF)((void) sizeof ((params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF) ? 1 : 0), __extension__ ({ if (params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF) ; else __assert_fail ("params.datatype == CCV_16F || params.datatype == CCV_32F || params.datatype == CCV_64F || params.datatype == CCV_16BF" , "../../nnc/ccv_nnc_easy.h", 287, __extension__ __PRETTY_FUNCTION__ ); })); |
| 288 | const int nd = ccv_nnc_tensor_nd(params.dim); |
| 289 | if (format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) |
| 290 | { |
| 291 | assert(nd > 0 && params.dim[nd - 1] > 0 && params.dim[nd - 1] % 256 == 0)((void) sizeof ((nd > 0 && params.dim[nd - 1] > 0 && params.dim[nd - 1] % 256 == 0) ? 1 : 0), __extension__ ({ if (nd > 0 && params.dim[nd - 1] > 0 && params.dim[nd - 1] % 256 == 0) ; else __assert_fail ("nd > 0 && params.dim[nd - 1] > 0 && params.dim[nd - 1] % 256 == 0" , "../../nnc/ccv_nnc_easy.h", 291, __extension__ __PRETTY_FUNCTION__ ); })); |
| 292 | } |
| 293 | ccv_nnc_tensor_param_t new_params = params; |
| 294 | new_params.datatype = ((params.datatype >> 12) & 0xff) | CCV_QX | CCV_NNC_QX_8I_ROWWISE_X; |
| 295 | new_params.reserved = format; |
| 296 | return new_params; |
| 297 | } |
| 298 | |
| 299 | static inline size_t ccv_nnc_tensor_data_size_without_padding(const ccv_nnc_tensor_param_t params) |
| 300 | { |
| 301 | const ssize_t count = (ssize_t)ccv_nnc_tensor_count(params); |
| 302 | ssize_t data_size; |
| 303 | if (CCV_GET_DATA_TYPE(params.datatype)((params.datatype) & 0xFF000) == CCV_QX) |
| 304 | { |
| 305 | if (count <= 0) |
| 306 | return 0; |
| 307 | const int qx_datatype = (params.datatype & 0xff) << 12; |
| 308 | const int qx_subtype = params.datatype & 0xf00; |
| 309 | if (qx_subtype == CCV_NNC_QX_EPHERMAL_STAGING) |
| 310 | data_size = 1; |
| 311 | else if (qx_subtype >= 0x400 && qx_subtype <= 0x800) |
| 312 | { |
| 313 | const int number_in_blocks = params.reserved; |
| 314 | const int num_blocks = (int)((count + number_in_blocks - 1) / number_in_blocks); |
| 315 | const int qbits = qx_subtype >> 8; |
| 316 | assert(qbits >= 4 && qbits <= 8)((void) sizeof ((qbits >= 4 && qbits <= 8) ? 1 : 0), __extension__ ({ if (qbits >= 4 && qbits <= 8) ; else __assert_fail ("qbits >= 4 && qbits <= 8" , "../../nnc/ccv_nnc_easy.h", 316, __extension__ __PRETTY_FUNCTION__ ); })); |
| 317 | data_size = (ssize_t)(1 << qbits) * CCV_GET_DATA_TYPE_SIZE(qx_datatype)_ccv_get_data_type_size[((qx_datatype) & 0xFF000) >> 12] * num_blocks + (count * qbits + 7) / 8; |
| 318 | } else if (qx_subtype == CCV_NNC_QX_8I_ROWWISE) { |
| 319 | const int nd = ccv_nnc_tensor_nd(params.dim); |
| 320 | const int row_length = params.dim[nd - 1]; |
| 321 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "../../nnc/ccv_nnc_easy.h", 321, __extension__ __PRETTY_FUNCTION__ ); })); |
| 322 | assert(count % row_length == 0)((void) sizeof ((count % row_length == 0) ? 1 : 0), __extension__ ({ if (count % row_length == 0) ; else __assert_fail ("count % row_length == 0" , "../../nnc/ccv_nnc_easy.h", 322, __extension__ __PRETTY_FUNCTION__ ); })); |
| 323 | const ssize_t row_count = count / row_length; |
| 324 | const ssize_t scale_offset = (count + 127) & -128; |
| 325 | data_size = scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(qx_datatype)_ccv_get_data_type_size[((qx_datatype) & 0xFF000) >> 12]; |
| 326 | } else if (qx_subtype == CCV_NNC_QX_8I_ROWWISE_X) { |
| 327 | const int nd = ccv_nnc_tensor_nd(params.dim); |
| 328 | const int row_length = params.dim[nd - 1]; |
| 329 | if (params.reserved & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) |
| 330 | { |
| 331 | assert(row_length > 0 && row_length % 256 == 0)((void) sizeof ((row_length > 0 && row_length % 256 == 0) ? 1 : 0), __extension__ ({ if (row_length > 0 && row_length % 256 == 0) ; else __assert_fail ("row_length > 0 && row_length % 256 == 0" , "../../nnc/ccv_nnc_easy.h", 331, __extension__ __PRETTY_FUNCTION__ ); })); |
| 332 | } |
| 333 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "../../nnc/ccv_nnc_easy.h", 333, __extension__ __PRETTY_FUNCTION__ ); })); |
| 334 | assert(count % row_length == 0)((void) sizeof ((count % row_length == 0) ? 1 : 0), __extension__ ({ if (count % row_length == 0) ; else __assert_fail ("count % row_length == 0" , "../../nnc/ccv_nnc_easy.h", 334, __extension__ __PRETTY_FUNCTION__ ); })); |
| 335 | const ssize_t row_count = count / row_length; |
| 336 | const ssize_t group_size = ccv_nnc_8i_rowwise_x_group_size(params.reserved); |
| 337 | const ssize_t groups_per_row = (row_length + group_size - 1) / group_size; |
| 338 | const ssize_t group_bits = ccv_nnc_8i_rowwise_x_group_bits(params.reserved); |
| 339 | const ssize_t payload_size = (row_count * groups_per_row * group_bits + 7) / 8; |
| 340 | const ssize_t scale_offset = (payload_size + 127) & -128; |
| 341 | data_size = scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(qx_datatype)_ccv_get_data_type_size[((qx_datatype) & 0xFF000) >> 12]; |
| 342 | } else { |
| 343 | assert(0)((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "../../nnc/ccv_nnc_easy.h", 343, __extension__ __PRETTY_FUNCTION__ ); })); |
| 344 | data_size = 0; |
| 345 | } |
| 346 | } else |
| 347 | data_size = CCV_GET_DATA_TYPE_SIZE(params.datatype)_ccv_get_data_type_size[((params.datatype) & 0xFF000) >> 12] * count; |
| 348 | return data_size; |
| 349 | } |
| 350 | |
| 351 | static inline size_t ccv_nnc_tensor_data_size(const ccv_nnc_tensor_param_t params) |
| 352 | { |
| 353 | ssize_t data_size = ccv_nnc_tensor_data_size_without_padding(params); |
| 354 | #ifdef HAVE_CUDA1 // For CUDA, we align to 128-bytes. |
| 355 | if (CCV_TENSOR_GET_MEMORY(params.type)((params.type) & 0x3) == CCV_TENSOR_GPU_MEMORY) |
| 356 | return ((data_size + 127) & -128); |
| 357 | else |
| 358 | #elif defined(HAVE_MPS) // For MPS, we have to align to PAGE_SIZE. |
| 359 | if (CCV_TENSOR_GET_MEMORY(params.type)((params.type) & 0x3) == CCV_TENSOR_GPU_MEMORY) |
| 360 | return ((data_size + PAGE_SIZE - 1) & -PAGE_SIZE); |
| 361 | else |
| 362 | #endif |
| 363 | return ((data_size + 63) & -64); |
| 364 | } |
| 365 | |
| 366 | static inline void ccv_nnc_tensor_view_get_dim(const ccv_nnc_tensor_view_t* const tv, int dim[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 367 | { |
| 368 | int x; |
| 369 | const int nd = ccv_nnc_tensor_nd(tv->info.dim); |
| 370 | const int offset = ccv_max(CCV_NNC_MAX_DIM + 2 - nd, 0)({ typeof ((2) + 2 - nd) _a = ((2) + 2 - nd); typeof (0) _b = (0); (_a > _b) ? _a : _b; }); |
| 371 | for (x = 0; x < offset; x++) |
| 372 | dim[x] = 1; |
| 373 | for (x = offset; x < ccv_max(CCV_NNC_MAX_DIM + 2, nd)({ typeof ((2) + 2) _a = ((2) + 2); typeof (nd) _b = (nd); (_a > _b) ? _a : _b; }); x++) |
| 374 | dim[x] = tv->info.dim[x - offset]; |
| 375 | dim[ccv_max(CCV_NNC_MAX_DIM + 2, nd)({ typeof ((2) + 2) _a = ((2) + 2); typeof (nd) _b = (nd); (_a > _b) ? _a : _b; })] = 0; |
| 376 | } |
| 377 | |
| 378 | static inline CCV_WARN_UNUSED(int)int __attribute__((warn_unused_result)) ccv_nnc_is_tensor_stride_packed(const int stride[CCV_NNC_MAX_DIM_ALLOC(12)], const int dim[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 379 | { |
| 380 | const int nd = ccv_nnc_tensor_nd(stride); |
| 381 | int i; |
| 382 | int cstride = 1; |
| 383 | for (i = nd - 1; i >= 0; i--) |
| 384 | { |
| 385 | if (stride[i] != cstride) |
| 386 | return 0; |
| 387 | cstride *= dim[i]; |
| 388 | } |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | static inline CCV_WARN_UNUSED(int)int __attribute__((warn_unused_result)) ccv_nnc_tensor_view_check_dim(const ccv_nnc_tensor_view_t* const tv, const int dim[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 393 | { |
| 394 | int x; |
| 395 | const int nd = ccv_nnc_tensor_nd(tv->info.dim); |
| 396 | const int offset = CCV_NNC_MAX_DIM(2) + 2 - nd; |
| 397 | for (x = 0; x < offset; x++) |
| 398 | if (dim[x] != 1) |
| 399 | return 0; |
| 400 | for (x = offset; x < CCV_NNC_MAX_DIM(2) + 2; x++) |
| 401 | if (dim[x] != tv->info.dim[x - offset]) |
| 402 | return 0; |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | static inline void ccv_nnc_tensor_view_get_broadcast_dim(const ccv_nnc_tensor_view_t* const tv, int dim[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 407 | { |
| 408 | int x; |
| 409 | const int nd = ccv_nnc_tensor_nd(tv->info.dim); |
| 410 | const int offset = CCV_NNC_MAX_DIM(2) + 2 - nd; |
| 411 | for (x = 0; x < offset; x++) |
| 412 | dim[x] = ccv_max(1, dim[x])({ typeof (1) _a = (1); typeof (dim[x]) _b = (dim[x]); (_a > _b) ? _a : _b; }); |
| 413 | for (x = offset; x < CCV_NNC_MAX_DIM(2) + 2; x++) |
| 414 | dim[x] = ccv_max(dim[x], tv->info.dim[x - offset])({ typeof (dim[x]) _a = (dim[x]); typeof (tv->info.dim[x - offset]) _b = (tv->info.dim[x - offset]); (_a > _b) ? _a : _b; }); |
| 415 | } |
| 416 | |
| 417 | static inline CCV_WARN_UNUSED(int)int __attribute__((warn_unused_result)) ccv_nnc_tensor_view_check_broadcast_dim(const ccv_nnc_tensor_view_t* const tv, int dim[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 418 | { |
| 419 | int x; |
| 420 | const int nd = ccv_nnc_tensor_nd(tv->info.dim); |
| 421 | const int offset = CCV_NNC_MAX_DIM(2) + 2 - nd; |
| 422 | for (x = offset; x < CCV_NNC_MAX_DIM(2) + 2; x++) |
| 423 | if (dim[x] != tv->info.dim[x - offset] && tv->info.dim[x - offset] != 1) |
| 424 | return 0; |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | static inline void ccv_nnc_tensor_view_get_stride(const ccv_nnc_tensor_view_t* const tv, int stride[CCV_NNC_MAX_DIM_ALLOC(12)]) |
| 429 | { |
| 430 | int x; |
| 431 | const int nd = ccv_nnc_tensor_nd(tv->info.dim); |
| 432 | const int offset = ccv_max(CCV_NNC_MAX_DIM + 2 - nd, 0)({ typeof ((2) + 2 - nd) _a = ((2) + 2 - nd); typeof (0) _b = (0); (_a > _b) ? _a : _b; }); |
| 433 | stride[ccv_max(nd, CCV_NNC_MAX_DIM + 2)({ typeof (nd) _a = (nd); typeof ((2) + 2) _b = ((2) + 2); (_a > _b) ? _a : _b; })] = 0; |
| 434 | if (CCV_IS_TENSOR_VIEW(tv)((*(int*)(tv)) & CCV_TENSOR_VIEW)) |
| 435 | { |
| 436 | for (x = offset; x < ccv_max(nd, CCV_NNC_MAX_DIM + 2)({ typeof (nd) _a = (nd); typeof ((2) + 2) _b = ((2) + 2); (_a > _b) ? _a : _b; }); x++) |
| 437 | stride[x] = tv->stride[x - offset]; |
| 438 | for (x = 0; x < offset; x++) |
| 439 | stride[x] = stride[offset]; |
| 440 | } else { |
| 441 | int cstride = 1; |
| 442 | for (x = ccv_max(CCV_NNC_MAX_DIM + 1, nd - 1)({ typeof ((2) + 1) _a = ((2) + 1); typeof (nd - 1) _b = (nd - 1); (_a > _b) ? _a : _b; }); x >= offset; x--) |
| 443 | { |
| 444 | stride[x] = cstride; |
| 445 | cstride *= tv->info.dim[x - offset]; |
| 446 | } |
| 447 | for (x = 0; x < offset; x++) |
| 448 | stride[x] = cstride; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | static inline int ccv_nnc_tensor_get_n(const ccv_nnc_tensor_param_t params) |
| 453 | { |
| 454 | switch (params.format) |
| 455 | { |
| 456 | case CCV_TENSOR_FORMAT_NHWC: |
| 457 | case CCV_TENSOR_FORMAT_NCHW: |
| 458 | if (ccv_nnc_tensor_nd(params.dim) == CCV_NNC_MAX_DIM(2) + 1) |
| 459 | return 1; |
| 460 | else |
| 461 | return params.dim[0]; |
| 462 | case CCV_TENSOR_FORMAT_CHWN: |
| 463 | return params.dim[CCV_NNC_MAX_DIM(2) + 1]; |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static inline int ccv_nnc_tensor_get_c(const ccv_nnc_tensor_param_t params) |
| 469 | { |
| 470 | const int nd = ccv_nnc_tensor_nd(params.dim); |
| 471 | switch (params.format) |
| 472 | { |
| 473 | case CCV_TENSOR_FORMAT_NHWC: |
| 474 | return params.dim[nd - 1]; |
| 475 | case CCV_TENSOR_FORMAT_NCHW: |
| 476 | if (nd == CCV_NNC_MAX_DIM(2) + 1) |
| 477 | return params.dim[0]; |
| 478 | else |
| 479 | return params.dim[nd <= 1 ? 0 : 1]; |
| 480 | case CCV_TENSOR_FORMAT_CHWN: |
| 481 | return params.dim[0]; |
| 482 | } |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static inline void ccv_nnc_tensor_set_n(ccv_nnc_tensor_param_t* const params, const int n) |
| 487 | { |
| 488 | switch (params->format) |
| 489 | { |
| 490 | case CCV_TENSOR_FORMAT_NHWC: |
| 491 | case CCV_TENSOR_FORMAT_NCHW: |
| 492 | params->dim[0] = n; |
| 493 | break; |
| 494 | case CCV_TENSOR_FORMAT_CHWN: |
| 495 | params->dim[CCV_NNC_MAX_DIM(2) + 1] = n; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | static inline void ccv_nnc_tensor_set_c(ccv_nnc_tensor_param_t* const params, const int nd, const int c) |
| 501 | { |
| 502 | switch (params->format) |
| 503 | { |
| 504 | case CCV_TENSOR_FORMAT_NHWC: |
| 505 | params->dim[nd - 1] = c; |
| 506 | break; |
| 507 | case CCV_TENSOR_FORMAT_NCHW: |
| 508 | if (nd == CCV_NNC_MAX_DIM(2) + 1) |
| 509 | params->dim[0] = c; |
| 510 | else |
| 511 | params->dim[nd <= 1 ? 0 : 1] = c; |
| 512 | break; |
| 513 | case CCV_TENSOR_FORMAT_CHWN: |
| 514 | params->dim[0] = c; |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static inline int ccv_nnc_is_matrix_transpose(const ccv_nnc_tensor_param_t params, const int transpose[2]) |
| 520 | { |
| 521 | const int nd = ccv_nnc_tensor_nd(params.dim); |
| 522 | assert(nd >= 1)((void) sizeof ((nd >= 1) ? 1 : 0), __extension__ ({ if (nd >= 1) ; else __assert_fail ("nd >= 1", "../../nnc/ccv_nnc_easy.h" , 522, __extension__ __PRETTY_FUNCTION__); })); |
| 523 | if (transpose[0] != transpose[1]) |
| 524 | { |
| 525 | assert(nd > 1)((void) sizeof ((nd > 1) ? 1 : 0), __extension__ ({ if (nd > 1) ; else __assert_fail ("nd > 1", "../../nnc/ccv_nnc_easy.h" , 525, __extension__ __PRETTY_FUNCTION__); })); |
| 526 | assert(((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) ||((void) sizeof ((((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1 ] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == (( nd == 2) ? 1 : nd - 1)))) ? 1 : 0), __extension__ ({ if (((transpose [0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ( (nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))) ; else __assert_fail ("((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))" , "../../nnc/ccv_nnc_easy.h", 527, __extension__ __PRETTY_FUNCTION__ ); })) |
| 527 | ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1))))((void) sizeof ((((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1 ] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == (( nd == 2) ? 1 : nd - 1)))) ? 1 : 0), __extension__ ({ if (((transpose [0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ( (nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))) ; else __assert_fail ("((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))" , "../../nnc/ccv_nnc_easy.h", 527, __extension__ __PRETTY_FUNCTION__ ); })); |
| 528 | return 1; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | // Assuming this is batched matrix. Getting relevant parameters. |
| 534 | static inline void ccv_nnc_tensor_get_matrix_params(const ccv_nnc_tensor_param_t params, const int* const stride, const int* const dim, const int transpose[2], int* const batch_size_ref, int* const rows_ref, int* const cols_ref, int* const batch_inc_ref, int* const rows_inc_ref, int* const cols_inc_ref) |
| 535 | { |
| 536 | const int nd = ccv_nnc_tensor_nd(params.dim); |
| 537 | assert(nd >= 1)((void) sizeof ((nd >= 1) ? 1 : 0), __extension__ ({ if (nd >= 1) ; else __assert_fail ("nd >= 1", "../../nnc/ccv_nnc_easy.h" , 537, __extension__ __PRETTY_FUNCTION__); })); |
| 538 | *batch_size_ref = nd < 3 ? 1 : params.dim[nd - 3]; |
| 539 | *batch_inc_ref = nd < 3 ? 0 : stride ? stride[nd - 3] : dim[nd - 2] * dim[nd - 1]; |
| 540 | int rows = nd == 1 ? 1 : (nd == 2 ? params.dim[0] : params.dim[nd - 2]); |
| 541 | int rows_inc = stride ? (nd >= 2 ? stride[nd - 2] : stride[0] * dim[0]) : dim[nd - 1]; |
| 542 | int cols = params.dim[nd - 1]; |
| 543 | int cols_inc = 1; |
| 544 | if (transpose[0] != transpose[1]) |
| 545 | { |
| 546 | assert(nd > 1)((void) sizeof ((nd > 1) ? 1 : 0), __extension__ ({ if (nd > 1) ; else __assert_fail ("nd > 1", "../../nnc/ccv_nnc_easy.h" , 546, __extension__ __PRETTY_FUNCTION__); })); |
| 547 | assert(((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) ||((void) sizeof ((((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1 ] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == (( nd == 2) ? 1 : nd - 1)))) ? 1 : 0), __extension__ ({ if (((transpose [0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ( (nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))) ; else __assert_fail ("((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))" , "../../nnc/ccv_nnc_easy.h", 548, __extension__ __PRETTY_FUNCTION__ ); })) |
| 548 | ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1))))((void) sizeof ((((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1 ] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == (( nd == 2) ? 1 : nd - 1)))) ? 1 : 0), __extension__ ({ if (((transpose [0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ( (nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))) ; else __assert_fail ("((transpose[0] == ((nd == 2) ? 0 : nd - 2)) && (transpose[1] == ((nd == 2) ? 1 : nd - 1))) || ((transpose[1] == ((nd == 2) ? 0 : nd - 2)) && (transpose[0] == ((nd == 2) ? 1 : nd - 1)))" , "../../nnc/ccv_nnc_easy.h", 548, __extension__ __PRETTY_FUNCTION__ ); })); |
| 549 | int t; |
| 550 | CCV_SWAP(rows, cols, t)((t) = (rows), (rows) = (cols), (cols) = (t)); |
| 551 | CCV_SWAP(rows_inc, cols_inc, t)((t) = (rows_inc), (rows_inc) = (cols_inc), (cols_inc) = (t)); |
| 552 | } |
| 553 | *rows_ref = rows; |
| 554 | *cols_ref = cols; |
| 555 | *rows_inc_ref = rows_inc; |
| 556 | *cols_inc_ref = cols_inc; |
| 557 | } |
| 558 | |
| 559 | static inline CCV_WARN_UNUSED(ccv_nnc_tensor_view_t)ccv_nnc_tensor_view_t __attribute__((warn_unused_result)) ccv_nnc_get_tensor_view(const ccv_nnc_tensor_t* const tensor) |
| 560 | { |
| 561 | if (CCV_IS_TENSOR_VIEW(tensor)((*(int*)(tensor)) & CCV_TENSOR_VIEW)) |
| 562 | return (ccv_nnc_tensor_view_t)*(const ccv_nnc_tensor_view_t*)tensor; |
| 563 | ccv_nnc_tensor_view_t tv = {0}; |
| 564 | memcpy(&tv, tensor, sizeof(ccv_nnc_tensor_t)); |
| 565 | return tv; |
| 566 | } |
| 567 | |
| 568 | static inline void ccv_nnc_tensor_view_alignment(ccv_nnc_tensor_view_t** const tvs, const int tv_size) |
| 569 | { |
| 570 | int i, j; |
| 571 | int max_nd = 0; |
| 572 | for (i = 0; i < tv_size; i++) |
| 573 | max_nd = ccv_max(ccv_nnc_tensor_nd(tvs[i]->info.dim), max_nd)({ typeof (ccv_nnc_tensor_nd(tvs[i]->info.dim)) _a = (ccv_nnc_tensor_nd (tvs[i]->info.dim)); typeof (max_nd) _b = (max_nd); (_a > _b) ? _a : _b; }); |
| 574 | for (i = 0; i < tv_size; i++) |
| 575 | { |
| 576 | const int nd = ccv_nnc_tensor_nd(tvs[i]->info.dim); |
| 577 | for (j = max_nd - 1; j >= max_nd - nd; j--) |
| 578 | tvs[i]->info.dim[j] = tvs[i]->info.dim[j - max_nd + nd]; |
| 579 | for (j = 0; j < max_nd - nd; j++) |
| 580 | tvs[i]->info.dim[j] = 1; |
| 581 | if (!CCV_IS_TENSOR_VIEW(tvs[i])((*(int*)(tvs[i])) & CCV_TENSOR_VIEW)) |
| 582 | continue; |
| 583 | for (j = max_nd - 1; j >= max_nd - nd; j--) |
| 584 | tvs[i]->stride[j] = tvs[i]->stride[j - max_nd + nd]; |
| 585 | for (j = 0; j < max_nd - nd; j++) |
| 586 | tvs[i]->stride[j] = tvs[i]->stride[max_nd - nd]; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | |
| 591 | #define TRANSPOSE(_X, _Y)((int[]){(_X),(_Y)}) ((int[]){(_X),(_Y)}) |
| 592 | #define NO_TRANSPOSE((int[]){(0),(0)}) TRANSPOSE(0, 0)((int[]){(0),(0)}) |
| 593 | #define CMD_GEMM_X(_0, _TA, _TB, ...)((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.blas={.a={1,1},. transpose_a={_TA[0],_TA[1]},.transpose_b={_TB[0],_TB[1]},}}) ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.blas={.a={1,1},.transpose_a={_TA[0],_TA[1]},.transpose_b={_TB[0],_TB[1]},}}) // We default to alpha = 1 and beta = 1 |
| 594 | #define CMD_GEMM(...)((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.blas={.a={1,1},. transpose_a={...[0],...[1]},.transpose_b={((int[]){(0),(0)})[ 0],((int[]){(0),(0)})[1]},}}) CMD_GEMM_X(_0, ##__VA_ARGS__, NO_TRANSPOSE, NO_TRANSPOSE)((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.blas={.a={1,1},. transpose_a={##__VA_ARGS__[0],##__VA_ARGS__[1]},.transpose_b= {((int[]){(0),(0)})[0],((int[]){(0),(0)})[1]},}}) |
| 595 | #define CMD_GENERIC_X_0()((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}}}) ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}}}) |
| 596 | #define CMD_GENERIC_X_F(...)("This should not be used, you should have either 0 parameter or 3 parameters for CMD_GENERIC" ) ("This should not be used, you should have either 0 parameter or 3 parameters for CMD_GENERIC") |
| 597 | #define CMD_GENERIC_X_3(...)((ccv_nnc_cmd_param_t){.size={.dim={...}}}) ((ccv_nnc_cmd_param_t){.size={.dim={__VA_ARGS__}}}) |
| 598 | #define CMD_GENERIC_X_SEL(_0, _1, _2, _3, _FX, ...)_FX _FX |
| 599 | // Using ## so that if it is empty, we omit one comma. |
| 600 | #define CMD_GENERIC(...)("This should not be used, you should have either 0 parameter or 3 parameters for CMD_GENERIC" ) CMD_GENERIC_X_SEL(CMD_GENERIC_X_F, ##__VA_ARGS__, CMD_GENERIC_X_3, CMD_GENERIC_X_F, CMD_GENERIC_X_F, CMD_GENERIC_X_0)(__VA_ARGS__)("This should not be used, you should have either 0 parameter or 3 parameters for CMD_GENERIC" ) |
| 601 | #define CMD_REDUCE(...)((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.reduce={.count=( 1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 -1),.axis={...}}}) ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.reduce={.count=LIST_COUNT(__VA_ARGS__)(1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + 0 +0 +0 -1),.axis={__VA_ARGS__}}}) |
| 602 | /** |
| 603 | * @defgroup available_commands Available Commands |
| 604 | * @{ |
| 605 | */ |
| 606 | #define CMD_NOOP()ccv_nnc_cmd(CCV_NNC_NOOP, 0, ccv_nnc_cmd_auto, 0) ccv_nnc_cmd(CCV_NNC_NOOP, 0, ccv_nnc_cmd_auto, 0) |
| 607 | #define CMD_CUSTOM_FORWARD(f)ccv_nnc_cmd(CCV_NNC_CUSTOM_FORWARD, f, ccv_nnc_cmd_auto, 0) ccv_nnc_cmd(CCV_NNC_CUSTOM_FORWARD, f, ccv_nnc_cmd_auto, 0) |
| 608 | /** @} */ |
| 609 | |
| 610 | int ccv_nnc_is_no_hint(const ccv_nnc_hint_t hint); |
| 611 | int ccv_nnc_is_cmd_auto(const ccv_nnc_cmd_param_t params); |
| 612 | int ccv_nnc_is_tensor_auto(const ccv_nnc_tensor_param_t params); |
| 613 | |
| 614 | /** |
| 615 | * @addtogroup convenience_api |
| 616 | * @{ |
| 617 | */ |
| 618 | /** |
| 619 | * Offsets all zero. |
| 620 | */ |
| 621 | extern const int ccv_nnc_no_ofs[CCV_NNC_MAX_DIM_ALLOC(12)]; |
| 622 | /** |
| 623 | * No hint available. |
| 624 | */ |
| 625 | extern const ccv_nnc_hint_t ccv_nnc_no_hint; |
| 626 | /** |
| 627 | * The default symbolic graph compile parameters. |
| 628 | */ |
| 629 | extern const ccv_nnc_symbolic_graph_compile_param_t ccv_nnc_default_compile_params; |
| 630 | /** |
| 631 | * Derive the command parameters automatically if possible. |
| 632 | */ |
| 633 | extern const ccv_nnc_cmd_param_t ccv_nnc_cmd_auto; |
| 634 | /** |
| 635 | * Derive the tensor parameters automatically if possible. |
| 636 | */ |
| 637 | extern const ccv_nnc_tensor_param_t ccv_nnc_tensor_auto; |
| 638 | /** @} */ |
| 639 | |
| 640 | // Generated command flags for easy creation of ccv_nnc_cmd_t objects. |
| 641 | #include "cmd/ccv_nnc_cmd_easy.h" |
| 642 | |
| 643 | #endif |