Bug Summary

File:nnc/ccv_cnnp_model_addons.c
Warning:line 400, column 3
Declared variable-length array (VLA) has negative size

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ccv_cnnp_model_addons.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +sse2 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/liu/actions-runner/_work/ccv/ccv/lib/nnc -fcoverage-compilation-dir=/home/liu/actions-runner/_work/ccv/ccv/lib/nnc -resource-dir /usr/local/lib/clang/18 -I ../ -I /usr/local/cuda/include -D HAVE_CBLAS -D HAVE_LIBPNG -D HAVE_LIBJPEG -D HAVE_FFTW3 -D HAVE_PTHREAD -D HAVE_LIBLINEAR -D HAVE_TESSERACT -D HAVE_AVCODEC -D HAVE_AVFORMAT -D HAVE_AVUTIL -D HAVE_SWSCALE -D HAVE_SSE2 -D HAVE_GSL -D HAVE_CUDA -D HAVE_CUDNN -D HAVE_NCCL -D USE_SYSTEM_CUB -D HAVE_CUDA_SM80 -I /usr/local/include -internal-isystem /usr/local/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/liu/actions-runner/_work/ccv/ccv/_analyze/2024-06-10-094233-222984-1 -x c ccv_cnnp_model_addons.c
1#include "ccv_nnc.h"
2#include "ccv_nnc_easy.h"
3#include "ccv_nnc_internal.h"
4#include "ccv_internal.h"
5#include "_ccv_cnnp_model.h"
6
7// MARK - Add-on Functions
8
9static int _ccv_cnnp_model_clip_grad_norm_reduce_norm2(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)
10{
11 const int device_id = CCV_TENSOR_GET_DEVICE_ID(inputs[0]->info.type)(((inputs[0]->info.type) & 0xfff00) >> 8);
12 ccv_nnc_tensor_t* const old_norm2 = outputs[1 + device_id * 2];
13 ccv_nnc_tensor_t* const norm2 = outputs[1 + device_id * 2 + 1];
14 const int tensor_count = ccv_nnc_tensor_count(inputs[0]->info);
15 if (tensor_count == 1)
16 ccv_nnc_cmd_exec(CMD_MUL_FORWARD(1)ccv_nnc_cmd(CCV_NNC_MUL_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={1,}}}, 0)
, hint, flags, TENSOR_LIST(inputs[0], inputs[0])(ccv_nnc_tensor_t* []){inputs[0], inputs[0]}, (1 +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 -1)
, TENSOR_LIST(norm2)(ccv_nnc_tensor_t* []){norm2}, (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)
, stream_context);
17 else {
18 ccv_nnc_cmd_exec(CMD_REDUCE_NORM2_FORWARD()ccv_nnc_cmd(CCV_NNC_REDUCE_NORM2_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}}
), 0)
, hint, flags, TENSOR_LIST(inputs[0])(ccv_nnc_tensor_t* []){inputs[0]}, (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)
, TENSOR_LIST(norm2)(ccv_nnc_tensor_t* []){norm2}, (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)
, stream_context);
19 ccv_nnc_cmd_exec(CMD_MUL_FORWARD(1)ccv_nnc_cmd(CCV_NNC_MUL_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={1,}}}, 0)
, hint, flags, TENSOR_LIST(norm2, norm2)(ccv_nnc_tensor_t* []){norm2, norm2}, (1 +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 -1)
, TENSOR_LIST(norm2)(ccv_nnc_tensor_t* []){norm2}, (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)
, stream_context);
20 }
21 ccv_nnc_cmd_exec(CMD_ADD_FORWARD(1, 1)ccv_nnc_cmd(CCV_NNC_ADD_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={1, 1}}}, 0)
, hint, flags, TENSOR_LIST(old_norm2, norm2)(ccv_nnc_tensor_t* []){old_norm2, norm2}, (1 +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 -1)
, TENSOR_LIST(old_norm2)(ccv_nnc_tensor_t* []){old_norm2}, (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)
, stream_context);
22 return CCV_NNC_EXEC_SUCCESS;
23}
24
25static ccv_nnc_cmd_vtab_t clip_grad_norm_reduce_norm2_vtab = {
26 .exec = _ccv_cnnp_model_clip_grad_norm_reduce_norm2
27};
28
29static int _ccv_cnnp_model_clip_grad_norm_scatter_norm2(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)
30{
31 const int device_id = CCV_TENSOR_GET_DEVICE_ID(inputs[0]->info.type)(((inputs[0]->info.type) & 0xfff00) >> 8);
32 ccv_nnc_tensor_t* const norm2 = inputs[1 + device_id * 2];
33 ccv_nnc_cmd_exec(CMD_MUL_FORWARD(1)ccv_nnc_cmd(CCV_NNC_MUL_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={1,}}}, 0)
, hint, flags, TENSOR_LIST(inputs[0], norm2)(ccv_nnc_tensor_t* []){inputs[0], norm2}, (1 +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 -1)
, TENSOR_LIST(outputs[0])(ccv_nnc_tensor_t* []){outputs[0]}, (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)
, stream_context);
34 return CCV_NNC_EXEC_SUCCESS;
35}
36
37static ccv_nnc_cmd_vtab_t clip_grad_norm_scatter_norm2_vtab = {
38 .exec = _ccv_cnnp_model_clip_grad_norm_scatter_norm2
39};
40
41void ccv_cnnp_model_parameters_clip_grad_norm(ccv_cnnp_model_t* const model, const ccv_cnnp_model_io_t parameters, int norm_type, float max_norm, ccv_nnc_stream_context_t* const stream_context)
42{
43 assert(norm_type == 2)((void) sizeof ((norm_type == 2) ? 1 : 0), __extension__ ({ if
(norm_type == 2) ; else __assert_fail ("norm_type == 2", "ccv_cnnp_model_addons.c"
, 43, __extension__ __PRETTY_FUNCTION__); }))
;
44 ccv_cnnp_compiled_data_t* const compiled_data = model->compiled_data;
45 assert(compiled_data)((void) sizeof ((compiled_data) ? 1 : 0), __extension__ ({ if
(compiled_data) ; else __assert_fail ("compiled_data", "ccv_cnnp_model_addons.c"
, 45, __extension__ __PRETTY_FUNCTION__); }))
;
46 const int parallel_count = ccv_max(model->parallel_count, 1)({ typeof (model->parallel_count) _a = (model->parallel_count
); typeof (1) _b = (1); (_a > _b) ? _a : _b; })
;
47 ccv_nnc_tensor_t* norm2[parallel_count * 2];
48 ccv_nnc_tensor_t* max_normt[parallel_count];
49 const int stream_type = model->compiled_data->stream_type;
50 int i;
51 if (stream_type == CCV_STREAM_CONTEXT_GPU)
52 {
53 for (i = 0; i < parallel_count; i++)
54 {
55 ccv_nnc_tensor_param_t info = {
56 .type = CCV_TENSOR_GPU_MEMORY,
57 .format = CCV_TENSOR_FORMAT_NHWC,
58 .datatype = CCV_32F,
59 .dim = {1},
60 };
61 CCV_TENSOR_SET_DEVICE_ID(info.type, i)(info.type) = (((info.type) & ~0xfff00) | (((i) & 0xfff
) << 8))
;
62 norm2[i * 2] = ccv_nnc_tensor_new(ccv_nnc_xpu_alloc(&compiled_data->xpu_alloc, i, stream_context, ccv_nnc_tensor_data_size(info)), info, 0);
63 norm2[i * 2 + 1] = ccv_nnc_tensor_new(ccv_nnc_xpu_alloc(&compiled_data->xpu_alloc, i, stream_context, ccv_nnc_tensor_data_size(info)), info, 0);
64 max_normt[i] = ccv_nnc_tensor_new(ccv_nnc_xpu_alloc(&compiled_data->xpu_alloc, i, stream_context, ccv_nnc_tensor_data_size(info)), info, 0);
65 }
66 } else {
67 for (i = 0; i < parallel_count; i++)
68 {
69 ccv_nnc_tensor_param_t info = {
70 .type = CCV_TENSOR_CPU_MEMORY,
71 .format = CCV_TENSOR_FORMAT_NHWC,
72 .datatype = CCV_32F,
73 .dim = {1},
74 };
75 norm2[i * 2] = ccv_nnc_tensor_new(0, info, 0);
76 norm2[i * 2 + 1] = ccv_nnc_tensor_new(0, info, 0);
77 max_normt[i] = ccv_nnc_tensor_new(0, info, 0);
78 }
79 }
80 // zero out old norm2.
81 if (parallel_count > 1)
82 {
83 ccv_nnc_stream_context_t* streams[parallel_count];
84 ccv_nnc_stream_signal_t* signal;
85 if (stream_context)
86 signal = ccv_nnc_stream_context_emit_signal_new(stream_context);
87 for (i = 0; i < parallel_count; i++)
88 {
89 const int stream_type = CCV_TENSOR_GET_MEMORY(norm2[i * 2]->info.type)((norm2[i * 2]->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY ? CCV_STREAM_CONTEXT_GPU : CCV_STREAM_CONTEXT_CPU;
90 const int device_id = CCV_TENSOR_GET_DEVICE_ID(norm2[i * 2]->info.type)(((norm2[i * 2]->info.type) & 0xfff00) >> 8);
91 int type = stream_type;
92 CCV_STREAM_SET_DEVICE_ID(type, device_id)(type) = (((type) & ~0xfff00) | (((device_id) & 0xfff
) << 8))
;
93 ccv_nnc_stream_context_t* const stream_0 = ccv_cnnp_compiled_data_get_stream(compiled_data, type);
94 // Wait signal to finish.
95 if (stream_context)
96 ccv_nnc_stream_context_wait_signal(stream_0, signal);
97 ccv_nnc_cmd_exec(CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(norm2[i * 2])(ccv_nnc_tensor_t* []){norm2[i * 2]}, (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)
, stream_0);
98 if (stream_context)
99 {
100 ccv_nnc_stream_signal_t* const signal = ccv_nnc_stream_context_emit_signal_new(stream_0);
101 ccv_nnc_stream_context_wait_signal(stream_context, signal);
102 }
103 streams[i] = stream_0;
104 }
105 // If this should be blocking, blocking it.
106 if (!stream_context)
107 for (i = 0; i < parallel_count; i++)
108 if (streams[i])
109 ccv_nnc_stream_context_wait(streams[i]);
110 } else {
111 ccv_nnc_cmd_exec(CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(norm2[0])(ccv_nnc_tensor_t* []){norm2[0]}, (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)
, stream_context);
112 }
113 // Gather norm2.
114 ccv_nnc_cmd_t reduce_cmd = {
115 .cmd = CCV_NNC_CUSTOM_FORWARD,
116 .isa = &clip_grad_norm_reduce_norm2_vtab,
117 };
118 ccv_cnnp_model_parameter_gradients_map(model, parameters, reduce_cmd, ccv_nnc_no_hint, 0, 0, 0, norm2, parallel_count * 2, stream_context);
119 // Now compute max(max_norm / norm2, 1.0).
120 if (parallel_count > 1)
121 {
122 ccv_nnc_stream_context_t* streams[parallel_count];
123 ccv_nnc_stream_signal_t* signal;
124 if (stream_context)
125 signal = ccv_nnc_stream_context_emit_signal_new(stream_context);
126 for (i = 0; i < parallel_count; i++)
127 {
128 const int stream_type = CCV_TENSOR_GET_MEMORY(norm2[i * 2]->info.type)((norm2[i * 2]->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY ? CCV_STREAM_CONTEXT_GPU : CCV_STREAM_CONTEXT_CPU;
129 const int device_id = CCV_TENSOR_GET_DEVICE_ID(norm2[i * 2]->info.type)(((norm2[i * 2]->info.type) & 0xfff00) >> 8);
130 int type = stream_type;
131 CCV_STREAM_SET_DEVICE_ID(type, device_id)(type) = (((type) & ~0xfff00) | (((device_id) & 0xfff
) << 8))
;
132 ccv_nnc_stream_context_t* const stream_0 = ccv_cnnp_compiled_data_get_stream(compiled_data, type);
133 // Wait signal to finish.
134 if (stream_context)
135 ccv_nnc_stream_context_wait_signal(stream_0, signal);
136 ccv_nnc_cmd_exec(CMD_EWSQRT_FORWARD()ccv_nnc_cmd(CCV_NNC_EWSQRT_FORWARD, 0, ccv_nnc_cmd_auto, 0), ccv_nnc_no_hint, 0, TENSOR_LIST(norm2[i * 2])(ccv_nnc_tensor_t* []){norm2[i * 2]}, (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)
, TENSOR_LIST(norm2[i * 2])(ccv_nnc_tensor_t* []){norm2[i * 2]}, (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)
, stream_0);
137 ccv_nnc_cmd_exec(CMD_SET_FORWARD(max_norm)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={max_norm,}}}, 0)
, ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(max_normt[i])(ccv_nnc_tensor_t* []){max_normt[i]}, (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)
, stream_0);
138 ccv_nnc_cmd_exec(CMD_EWDIV_FORWARD()ccv_nnc_cmd(CCV_NNC_EWDIV_FORWARD, 0, ccv_nnc_cmd_auto, 0), ccv_nnc_no_hint, 0, TENSOR_LIST(max_normt[i], norm2[i * 2])(ccv_nnc_tensor_t* []){max_normt[i], norm2[i * 2]}, (1 +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 -1
)
, TENSOR_LIST(norm2[i * 2])(ccv_nnc_tensor_t* []){norm2[i * 2]}, (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)
, stream_0);
139 ccv_nnc_cmd_exec(CMD_CLAMP_FORWARD(NAN, 1)ccv_nnc_cmd(CCV_NNC_CLAMP_FORWARD, 0, (ccv_nnc_cmd_param_t){.
size={.dim={1,1,1}},.clamp={.min=(__builtin_nanf ("")),.max=1
}}, 0)
, ccv_nnc_no_hint, 0, TENSOR_LIST(norm2[i * 2])(ccv_nnc_tensor_t* []){norm2[i * 2]}, (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)
, TENSOR_LIST(norm2[i * 2])(ccv_nnc_tensor_t* []){norm2[i * 2]}, (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)
, stream_0);
140 if (stream_context)
141 {
142 ccv_nnc_stream_signal_t* const signal = ccv_nnc_stream_context_emit_signal_new(stream_0);
143 ccv_nnc_stream_context_wait_signal(stream_context, signal);
144 }
145 streams[i] = stream_0;
146 }
147 // If this should be blocking, blocking it.
148 if (!stream_context)
149 for (i = 0; i < parallel_count; i++)
150 if (streams[i])
151 ccv_nnc_stream_context_wait(streams[i]);
152 } else {
153 ccv_nnc_cmd_exec(CMD_EWSQRT_FORWARD()ccv_nnc_cmd(CCV_NNC_EWSQRT_FORWARD, 0, ccv_nnc_cmd_auto, 0), ccv_nnc_no_hint, 0, TENSOR_LIST(norm2[0])(ccv_nnc_tensor_t* []){norm2[0]}, (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)
, TENSOR_LIST(norm2[0])(ccv_nnc_tensor_t* []){norm2[0]}, (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)
, stream_context);
154 ccv_nnc_cmd_exec(CMD_SET_FORWARD(max_norm)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={max_norm,}}}, 0)
, ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(max_normt[0])(ccv_nnc_tensor_t* []){max_normt[0]}, (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)
, stream_context);
155 ccv_nnc_cmd_exec(CMD_EWDIV_FORWARD()ccv_nnc_cmd(CCV_NNC_EWDIV_FORWARD, 0, ccv_nnc_cmd_auto, 0), ccv_nnc_no_hint, 0, TENSOR_LIST(max_normt[0], norm2[0])(ccv_nnc_tensor_t* []){max_normt[0], norm2[0]}, (1 +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 -1)
, TENSOR_LIST(norm2[0])(ccv_nnc_tensor_t* []){norm2[0]}, (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)
, stream_context);
156 ccv_nnc_cmd_exec(CMD_CLAMP_FORWARD(NAN, 1)ccv_nnc_cmd(CCV_NNC_CLAMP_FORWARD, 0, (ccv_nnc_cmd_param_t){.
size={.dim={1,1,1}},.clamp={.min=(__builtin_nanf ("")),.max=1
}}, 0)
, ccv_nnc_no_hint, 0, TENSOR_LIST(norm2[0])(ccv_nnc_tensor_t* []){norm2[0]}, (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)
, TENSOR_LIST(norm2[0])(ccv_nnc_tensor_t* []){norm2[0]}, (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)
, stream_context);
157 }
158 ccv_nnc_cmd_t scatter_cmd = {
159 .cmd = CCV_NNC_CUSTOM_FORWARD,
160 .isa = &clip_grad_norm_scatter_norm2_vtab,
161 };
162 ccv_cnnp_model_parameter_gradients_map(model, parameters, scatter_cmd, ccv_nnc_no_hint, 0, norm2, parallel_count * 2, 0, 0, stream_context);
163 if (stream_type == CCV_STREAM_CONTEXT_GPU)
164 for (i = 0; i < parallel_count; i++)
165 {
166 ccv_nnc_xpu_free(&compiled_data->xpu_alloc, norm2[i * 2]->data.u8);
167 ccv_nnc_xpu_free(&compiled_data->xpu_alloc, norm2[i * 2 + 1]->data.u8);
168 ccv_nnc_xpu_free(&compiled_data->xpu_alloc, max_normt[i]->data.u8);
169 }
170 for (i = 0; i < parallel_count; i++)
171 {
172 ccv_nnc_tensor_free(norm2[i * 2]);
173 ccv_nnc_tensor_free(norm2[i * 2 + 1]);
174 ccv_nnc_tensor_free(max_normt[i]);
175 }
176}
177
178// MARK - Add-on Functions
179
180static int _ccv_cnnp_model_isnan(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)
181{
182 const int device_id = CCV_TENSOR_GET_DEVICE_ID(inputs[0]->info.type)(((inputs[0]->info.type) & 0xfff00) >> 8);
183 ccv_nnc_tensor_t* const old_isnanr = outputs[1 + device_id * 2];
184 ccv_nnc_tensor_t* const isnanr = outputs[1 + device_id * 2 + 1];
185 ccv_nnc_cmd_t reduce_cmd = CMD_REDUCE_ISNAN_FORWARD()ccv_nnc_cmd(CCV_NNC_REDUCE_ISNAN_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}}
), 0)
;
186 reduce_cmd.info.reduce.count = ccv_nnc_tensor_nd(inputs[0]->info.dim);
187 int i;
188 for (i = 0; i < cmd.info.reduce.count; i++)
189 reduce_cmd.info.reduce.axis[i] = i;
190 ccv_nnc_cmd_exec(reduce_cmd, hint, flags, TENSOR_LIST(inputs[0])(ccv_nnc_tensor_t* []){inputs[0]}, (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)
, TENSOR_LIST(isnanr)(ccv_nnc_tensor_t* []){isnanr}, (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)
, stream_context);
191 ccv_nnc_cmd_exec(CMD_EWSUM_FORWARD()ccv_nnc_cmd(CCV_NNC_EWSUM_FORWARD, 0, ccv_nnc_cmd_auto, 0), hint, flags, TENSOR_LIST(old_isnanr, isnanr)(ccv_nnc_tensor_t* []){old_isnanr, isnanr}, (1 +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 -1)
, TENSOR_LIST(old_isnanr)(ccv_nnc_tensor_t* []){old_isnanr}, (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)
, stream_context);
192 return CCV_NNC_EXEC_SUCCESS;
193}
194
195static ccv_nnc_cmd_vtab_t reduce_isnan_vtab = {
196 .exec = _ccv_cnnp_model_isnan
197};
198
199int ccv_cnnp_model_parameter_gradients_isnan(ccv_cnnp_model_t* const model, const ccv_cnnp_model_io_t parameters, ccv_nnc_stream_context_t* const stream_context)
200{
201 ccv_cnnp_compiled_data_t* const compiled_data = model->compiled_data;
202 assert(compiled_data)((void) sizeof ((compiled_data) ? 1 : 0), __extension__ ({ if
(compiled_data) ; else __assert_fail ("compiled_data", "ccv_cnnp_model_addons.c"
, 202, __extension__ __PRETTY_FUNCTION__); }))
;
203 const int parallel_count = ccv_max(model->parallel_count, 1)({ typeof (model->parallel_count) _a = (model->parallel_count
); typeof (1) _b = (1); (_a > _b) ? _a : _b; })
;
204 ccv_nnc_tensor_t* isnanr[parallel_count * 2];
205 const int stream_type = model->compiled_data->stream_type;
206 int i;
207 if (stream_type == CCV_STREAM_CONTEXT_GPU)
208 {
209 for (i = 0; i < parallel_count; i++)
210 {
211 ccv_nnc_tensor_param_t info = {
212 .type = CCV_TENSOR_GPU_MEMORY,
213 .format = CCV_TENSOR_FORMAT_NHWC,
214 .datatype = CCV_32S,
215 .dim = {1},
216 };
217 CCV_TENSOR_SET_DEVICE_ID(info.type, i)(info.type) = (((info.type) & ~0xfff00) | (((i) & 0xfff
) << 8))
;
218 isnanr[i * 2] = ccv_nnc_tensor_new(ccv_nnc_xpu_alloc(&compiled_data->xpu_alloc, i, stream_context, ccv_nnc_tensor_data_size(info)), info, 0);
219 isnanr[i * 2 + 1] = ccv_nnc_tensor_new(ccv_nnc_xpu_alloc(&compiled_data->xpu_alloc, i, stream_context, ccv_nnc_tensor_data_size(info)), info, 0);
220 }
221 } else {
222 for (i = 0; i < parallel_count; i++)
223 {
224 ccv_nnc_tensor_param_t info = {
225 .type = CCV_TENSOR_CPU_MEMORY,
226 .format = CCV_TENSOR_FORMAT_NHWC,
227 .datatype = CCV_32S,
228 .dim = {1},
229 };
230 isnanr[i * 2] = ccv_nnc_tensor_new(0, info, 0);
231 isnanr[i * 2 + 1] = ccv_nnc_tensor_new(0, info, 0);
232 }
233 }
234 // zero out old isnanr.
235 if (parallel_count > 1)
236 {
237 ccv_nnc_stream_context_t* streams[parallel_count];
238 ccv_nnc_stream_signal_t* signal;
239 if (stream_context)
240 signal = ccv_nnc_stream_context_emit_signal_new(stream_context);
241 for (i = 0; i < parallel_count; i++)
242 {
243 const int stream_type = CCV_TENSOR_GET_MEMORY(isnanr[i * 2]->info.type)((isnanr[i * 2]->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY ? CCV_STREAM_CONTEXT_GPU : CCV_STREAM_CONTEXT_CPU;
244 const int device_id = CCV_TENSOR_GET_DEVICE_ID(isnanr[i * 2]->info.type)(((isnanr[i * 2]->info.type) & 0xfff00) >> 8);
245 int type = stream_type;
246 CCV_STREAM_SET_DEVICE_ID(type, device_id)(type) = (((type) & ~0xfff00) | (((device_id) & 0xfff
) << 8))
;
247 ccv_nnc_stream_context_t* const stream_0 = ccv_cnnp_compiled_data_get_stream(compiled_data, type);
248 // Wait signal to finish.
249 if (stream_context)
250 ccv_nnc_stream_context_wait_signal(stream_0, signal);
251 ccv_nnc_cmd_exec(CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(isnanr[i * 2])(ccv_nnc_tensor_t* []){isnanr[i * 2]}, (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)
, stream_0);
252 if (stream_context)
253 {
254 ccv_nnc_stream_signal_t* const signal = ccv_nnc_stream_context_emit_signal_new(stream_0);
255 ccv_nnc_stream_context_wait_signal(stream_context, signal);
256 }
257 streams[i] = stream_0;
258 }
259 // If this should be blocking, blocking it.
260 if (!stream_context)
261 for (i = 0; i < parallel_count; i++)
262 if (streams[i])
263 ccv_nnc_stream_context_wait(streams[i]);
264 } else
265 ccv_nnc_cmd_exec(CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(isnanr[0])(ccv_nnc_tensor_t* []){isnanr[0]}, (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)
, stream_context);
266 // Gather isnanr.
267 ccv_nnc_cmd_t reduce_cmd = {
268 .cmd = CCV_NNC_CUSTOM_FORWARD,
269 .isa = &reduce_isnan_vtab,
270 };
271 ccv_cnnp_model_parameter_gradients_map(model, parameters, reduce_cmd, ccv_nnc_no_hint, 0, 0, 0, isnanr, parallel_count * 2, stream_context);
272 for (i = 0; i < parallel_count; i++)
273 ccv_nnc_tensor_free(isnanr[i * 2 + 1]);
274 int retval = 0;
275 if (stream_type == CCV_TENSOR_GPU_MEMORY)
276 {
277 ccv_nnc_tensor_param_t info = {
278 .type = CCV_TENSOR_CPU_MEMORY,
279 .format = CCV_TENSOR_FORMAT_NHWC,
280 .datatype = CCV_32S,
281 .dim = {1},
282 };
283 ccv_nnc_tensor_t* checknan = ccv_nnc_tensor_new(0, info, 0);
284 for (i = 0; i < parallel_count; i++)
285 {
286 ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD()ccv_nnc_cmd(CCV_NNC_DATA_TRANSFER_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, ccv_nnc_no_hint, 0, TENSOR_LIST(isnanr[i * 2])(ccv_nnc_tensor_t* []){isnanr[i * 2]}, (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)
, TENSOR_LIST(checknan)(ccv_nnc_tensor_t* []){checknan}, (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)
, 0);
287 if (checknan->data.i32[0] > 0)
288 {
289 retval = 1;
290 break;
291 }
292 }
293 ccv_nnc_tensor_free(checknan);
294 } else {
295 for (i = 0; i < parallel_count; i++)
296 if (isnanr[i * 2]->data.i32[0] > 0)
297 {
298 retval = 1;
299 break;
300 }
301 }
302 for (i = 0; i < parallel_count; i++)
303 ccv_nnc_tensor_free(isnanr[i * 2]);
304 return retval;
305}
306
307// MARK - Core Layers
308
309static void _ccv_cnnp_sum_build(ccv_cnnp_model_t* const self, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
310{
311 PRINT(CCV_CLI_VERBOSE, "[cnnp_sum_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_sum_build] -\n"); fflush(stdout); } } while (
0)
;
312 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 312, __extension__ __PRETTY_FUNCTION__
); }))
;
313 outputs[0] = ccv_nnc_tensor_symbol_new(graph, ccv_nnc_tensor_symbol_params(graph, inputs[0]), 0);
314 ccv_nnc_graph_exec_symbol_new(graph, CMD_EWSUM_FORWARD()ccv_nnc_cmd(CCV_NNC_EWSUM_FORWARD, 0, ccv_nnc_cmd_auto, 0), inputs, input_size, outputs, output_size, 0);
315}
316
317static ccv_cnnp_model_t* _ccv_cnnp_sum_copy(const ccv_cnnp_model_t* const self, void* const context);
318
319static const ccv_cnnp_model_vtab_t ccv_cnnp_sum_isa = {
320 .build = _ccv_cnnp_sum_build,
321 .copy = _ccv_cnnp_sum_copy,
322};
323
324typedef struct {
325 ccv_cnnp_model_t super;
326 ccv_nnc_tensor_symbol_t output;
327} ccv_cnnp_model_sum_t;
328
329ccv_cnnp_model_t* ccv_cnnp_sum(const char* const name)
330{
331 ccv_cnnp_model_sum_t* const model_sum = (ccv_cnnp_model_sum_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_sum_t));
332 model_sum->super.isa = &ccv_cnnp_sum_isa;
333 model_sum->super.input_size = 0;
334 model_sum->super.outputs = &model_sum->output;
335 model_sum->super.output_size = 1;
336 ccv_cnnp_model_copy_name(&model_sum->super, name);
337 return (ccv_cnnp_model_t*)model_sum;
338}
339
340static ccv_cnnp_model_t* _ccv_cnnp_sum_copy(const ccv_cnnp_model_t* const self, void* const context)
341{
342 return ccv_cnnp_sum(self->name);
343}
344
345typedef struct {
346 ccv_cnnp_model_t super;
347 int axis;
348 ccv_nnc_tensor_symbol_t output;
349} ccv_cnnp_model_concat_t;
350
351static void _ccv_cnnp_concat_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
352{
353 const ccv_cnnp_model_concat_t* const self = (const ccv_cnnp_model_concat_t*)super;
354 PRINT(CCV_CLI_VERBOSE, "[cnnp_concat_build] 1. -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_concat_build] 1. -\n"); fflush(stdout); } } while
(0)
;
1
Assuming the condition is false
2
Taking false branch
3
Loop condition is false. Exiting loop
355 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 355, __extension__ __PRETTY_FUNCTION__
); }))
;
4
Assuming 'output_size' is equal to 1
5
Taking true branch
356 ccv_nnc_tensor_param_t output_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
357 int i, j;
358 if (output_params.dim[0] == 0)
6
Assuming the condition is false
7
Taking false branch
359 for (i = 1; i < input_size; i++)
360 {
361 output_params = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
362 if (output_params.dim[0] != 0)
363 break;
364 }
365 const int nd = ccv_nnc_tensor_nd(output_params.dim);
366 const int axis = self->axis;
367 assert(axis < nd)((void) sizeof ((axis < nd) ? 1 : 0), __extension__ ({ if (
axis < nd) ; else __assert_fail ("axis < nd", "ccv_cnnp_model_addons.c"
, 367, __extension__ __PRETTY_FUNCTION__); }))
;
8
Assuming 'axis' is < 'nd'
9
Taking true branch
368 output_params.dim[axis] = 0;
369 int input_is_contiguous = 1;
370 for (i = 0; i < input_size; i++)
10
Assuming 'i' is >= 'input_size'
11
Loop condition is false. Execution continues on line 394
371 {
372 const ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
373 const int input_nd = ccv_nnc_tensor_nd(input_params.dim);
374 if (input_nd == 0)
375 {
376 PRINT(CCV_CLI_VERBOSE, "[cnnp_concat_build] %d. input[%d]: -\n", i + 2, i)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_concat_build] %d. input[%d]: -\n", i + 2, i)
; fflush(stdout); } } while (0)
;
377 input_is_contiguous = 0;
378 continue;
379 }
380 if (CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_VERBOSE)(CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
381 {
382 PRINT(CCV_CLI_VERBOSE, "[cnnp_concat_build] %d. input[%d]: (%d", i + 2, i, input_params.dim[0])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_concat_build] %d. input[%d]: (%d", i + 2, i,
input_params.dim[0]); fflush(stdout); } } while (0)
;
383 int i;
384 for (i = 1; i < CCV_NNC_MAX_DIM_ALLOC(12) && input_params.dim[i] > 0; i++)
385 PRINT(CCV_CLI_VERBOSE, ", %d", input_params.dim[i])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(", %d", input_params.dim[i]); fflush(stdout); } } while
(0)
;
386 PRINT(CCV_CLI_VERBOSE, ")\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(")\n"); fflush(stdout); } } while (0)
;
387 }
388 assert(input_nd == nd)((void) sizeof ((input_nd == nd) ? 1 : 0), __extension__ ({ if
(input_nd == nd) ; else __assert_fail ("input_nd == nd", "ccv_cnnp_model_addons.c"
, 388, __extension__ __PRETTY_FUNCTION__); }))
;
389 for (j = 0; j < nd; j++)
390 if (j != axis)
391 { assert(input_params.dim[j] == output_params.dim[j])((void) sizeof ((input_params.dim[j] == output_params.dim[j])
? 1 : 0), __extension__ ({ if (input_params.dim[j] == output_params
.dim[j]) ; else __assert_fail ("input_params.dim[j] == output_params.dim[j]"
, "ccv_cnnp_model_addons.c", 391, __extension__ __PRETTY_FUNCTION__
); }))
; }
392 output_params.dim[axis] += input_params.dim[axis];
393 }
394 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
395 int ofs[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
396 int stride[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
397 ccv_nnc_tensor_get_stride(output_params.dim, stride);
398 if (input_is_contiguous
11.1
'input_is_contiguous' is 1
)
12
Taking true branch
399 {
400 ccv_nnc_tensor_symbol_t aliases[input_size];
13
Declared variable-length array (VLA) has negative size
401 for (i = 0; i < input_size; i++)
402 {
403 const ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
404 aliases[i] = ccv_nnc_tensor_symbol_alias_new(graph, outputs[0], ofs, stride, input_params, 0);
405 ofs[axis] += input_params.dim[axis];
406 }
407 // Format transform is more flexible.
408 ccv_nnc_graph_exec_symbol_new(graph, CMD_FORMAT_TRANSFORM_FORWARD()ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, inputs, input_size, aliases, input_size, "concat");
409 } else {
410 ccv_nnc_tensor_symbol_t aliases[input_size];
411 for (i = 0; i < input_size; i++)
412 {
413 const ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
414 if (input_params.dim[0] == 0)
415 {
416 // Create a new alias anyway, but not going to use it, in this way, the alias count will match during absorb.
417 aliases[i] = ccv_nnc_tensor_symbol_alias_new(graph, outputs[0], ofs, stride, input_params, 0);
418 continue;
419 }
420 aliases[i] = ccv_nnc_tensor_symbol_alias_new(graph, outputs[0], ofs, stride, input_params, 0);
421 ofs[axis] += input_params.dim[axis];
422 }
423 // Format transform is more flexible.
424 ccv_nnc_graph_exec_symbol_new(graph, CMD_FORMAT_TRANSFORM_FORWARD()ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, inputs, input_size, aliases, input_size, "concat");
425 }
426}
427
428static ccv_cnnp_model_t* _ccv_cnnp_concat_copy(const ccv_cnnp_model_t* const self, void* const context);
429
430static const ccv_cnnp_model_vtab_t ccv_cnnp_concat_isa = {
431 .build = _ccv_cnnp_concat_build,
432 .copy = _ccv_cnnp_concat_copy,
433};
434
435ccv_cnnp_model_t* ccv_cnnp_concat(const int axis, const char* const name)
436{
437 ccv_cnnp_model_concat_t* const model_concat = (ccv_cnnp_model_concat_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_concat_t));
438 model_concat->super.isa = &ccv_cnnp_concat_isa;
439 model_concat->super.input_size = 0;
440 model_concat->super.outputs = &model_concat->output;
441 model_concat->super.output_size = 1;
442 model_concat->axis = axis;
443 ccv_cnnp_model_copy_name(&model_concat->super, name);
444 return (ccv_cnnp_model_t*)model_concat;
445}
446
447static ccv_cnnp_model_t* _ccv_cnnp_concat_copy(const ccv_cnnp_model_t* const super, void* const context)
448{
449 const ccv_cnnp_model_concat_t* const self = (const ccv_cnnp_model_concat_t*)super;
450 return ccv_cnnp_concat(self->axis, self->super.name);
451}
452
453typedef struct {
454 ccv_cnnp_model_t super;
455 int axis;
456 ccv_nnc_tensor_symbol_t outputs[1];
457} ccv_cnnp_model_chunk_t;
458
459static void _ccv_cnnp_chunk_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
460{
461 const ccv_cnnp_model_concat_t* const self = (const ccv_cnnp_model_concat_t*)super;
462 PRINT(CCV_CLI_VERBOSE, "[cnnp_chunk_build] 1. axis: %d\n", self->axis)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_chunk_build] 1. axis: %d\n", self->axis);
fflush(stdout); } } while (0)
;
463 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 463, __extension__ __PRETTY_FUNCTION__); }))
;
464 const ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
465 if (CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_VERBOSE)(CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
466 {
467 PRINT(CCV_CLI_VERBOSE, "[cnnp_chunk_build] 2. input: (%d", input_params.dim[0])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_chunk_build] 2. input: (%d", input_params.dim
[0]); fflush(stdout); } } while (0)
;
468 int i;
469 for (i = 1; i < CCV_NNC_MAX_DIM_ALLOC(12) && input_params.dim[i] > 0; i++)
470 PRINT(CCV_CLI_VERBOSE, ", %d", input_params.dim[i])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(", %d", input_params.dim[i]); fflush(stdout); } } while
(0)
;
471 PRINT(CCV_CLI_VERBOSE, ")\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(")\n"); fflush(stdout); } } while (0)
;
472 }
473 ccv_nnc_tensor_param_t output_params = input_params;
474 int i;
475 const int nd = ccv_nnc_tensor_nd(output_params.dim);
476 const int axis = self->axis;
477 assert(axis < nd)((void) sizeof ((axis < nd) ? 1 : 0), __extension__ ({ if (
axis < nd) ; else __assert_fail ("axis < nd", "ccv_cnnp_model_addons.c"
, 477, __extension__ __PRETTY_FUNCTION__); }))
;
478 const int n = self->super.output_size;
479 assert(n == output_size)((void) sizeof ((n == output_size) ? 1 : 0), __extension__ ({
if (n == output_size) ; else __assert_fail ("n == output_size"
, "ccv_cnnp_model_addons.c", 479, __extension__ __PRETTY_FUNCTION__
); }))
;
480 assert(output_params.dim[axis] % n == 0)((void) sizeof ((output_params.dim[axis] % n == 0) ? 1 : 0), __extension__
({ if (output_params.dim[axis] % n == 0) ; else __assert_fail
("output_params.dim[axis] % n == 0", "ccv_cnnp_model_addons.c"
, 480, __extension__ __PRETTY_FUNCTION__); }))
;
481 output_params.dim[axis] = output_params.dim[axis] / n;
482 int ofs[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
483 int stride[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
484 ccv_nnc_tensor_get_stride(input_params.dim, stride);
485 ccv_nnc_tensor_symbol_t to = ccv_nnc_tensor_symbol_alias_to(graph, inputs[0]);
486 if (to.d == CCV_NNC_NO_TENSOR_SYMBOL) // If we are not reshape an alias, it is straightforward.
487 {
488 for (i = 0; i < output_size; i++)
489 {
490 outputs[i] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], ofs, stride, output_params, 0);
491 ofs[axis] += output_params.dim[axis];
492 }
493 } else {
494 // Otherwise, we need to check if it is permute. For permute, we cannot do alias directly.
495 // We need to first materialize the permute and then run reshape on top of it, otherwise it will be wrong.
496 int old_stride[CCV_NNC_MAX_DIM_ALLOC(12)];
497 ccv_nnc_tensor_symbol_alias_params(graph, inputs[0], 0, old_stride);
498 // We identify permute by checking if the stride is not in descending order.
499 // This also covered "permute" through reshape, rather than using ccv_cnnp_permute directly.
500 int i, no_permute = 1;
501 for (i = 1; no_permute && i < nd; i++)
502 if (old_stride[i - 1] < old_stride[i])
503 no_permute = 0;
504 if (no_permute)
505 { // Just straightforward reshape if there is no no permute.
506 for (i = 0; i < output_size; i++)
507 {
508 outputs[i] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], ofs, old_stride, output_params, 0);
509 ofs[axis] += output_params.dim[axis];
510 }
511 } else {
512 // Otherwise, we first do format transform to plain tensor and then do reshape.
513 ccv_nnc_tensor_symbol_t permuted = ccv_nnc_tensor_symbol_new(graph, input_params, 0);
514 ccv_nnc_graph_exec_symbol_new(graph, CMD_FORMAT_TRANSFORM_FORWARD()ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(permuted)(const ccv_nnc_tensor_symbol_t []){permuted}, (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)
, "reshape");
515 for (i = 0; i < output_size; i++)
516 {
517 outputs[i] = ccv_nnc_tensor_symbol_alias_new(graph, permuted, ofs, stride, output_params, 0);
518 ofs[axis] += output_params.dim[axis];
519 }
520 }
521 }
522}
523
524static ccv_cnnp_model_t* _ccv_cnnp_chunk_copy(const ccv_cnnp_model_t* const self, void* const context);
525
526static const ccv_cnnp_model_vtab_t ccv_cnnp_chunk_isa = {
527 .build = _ccv_cnnp_chunk_build,
528 .copy = _ccv_cnnp_chunk_copy,
529};
530
531ccv_cnnp_model_t* ccv_cnnp_chunk(const int n, const int axis, const char* const name)
532{
533 assert(n >= 1)((void) sizeof ((n >= 1) ? 1 : 0), __extension__ ({ if (n >=
1) ; else __assert_fail ("n >= 1", "ccv_cnnp_model_addons.c"
, 533, __extension__ __PRETTY_FUNCTION__); }))
;
534 ccv_cnnp_model_chunk_t* const model_chunk = (ccv_cnnp_model_chunk_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_chunk_t) + sizeof(ccv_nnc_tensor_symbol_t) * (n - 1));
535 model_chunk->super.isa = &ccv_cnnp_chunk_isa;
536 model_chunk->super.input_size = 1;
537 model_chunk->super.outputs = model_chunk->outputs;
538 model_chunk->super.output_size = n;
539 model_chunk->axis = axis;
540 ccv_cnnp_model_copy_name(&model_chunk->super, name);
541 return (ccv_cnnp_model_t*)model_chunk;
542}
543
544static ccv_cnnp_model_t* _ccv_cnnp_chunk_copy(const ccv_cnnp_model_t* const super, void* const context)
545{
546 const ccv_cnnp_model_chunk_t* const self = (const ccv_cnnp_model_chunk_t*)super;
547 return ccv_cnnp_chunk(self->super.output_size, self->axis, self->super.name);
548}
549
550typedef struct {
551 ccv_cnnp_model_t super;
552 ccv_nnc_tensor_symbol_t output;
553 int format;
554 int dim[CCV_NNC_MAX_DIM_ALLOC(12)];
555 int ofs[CCV_NNC_MAX_DIM_ALLOC(12)];
556 int stride[CCV_NNC_MAX_DIM_ALLOC(12)];
557} ccv_cnnp_model_reshape_t;
558
559static void _ccv_cnnp_reshape_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
560{
561 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 561, __extension__ __PRETTY_FUNCTION__); }))
;
562 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 562, __extension__ __PRETTY_FUNCTION__
); }))
;
563 ccv_cnnp_model_reshape_t* const self = (ccv_cnnp_model_reshape_t*)super;
564 if (CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_VERBOSE)(CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
565 {
566 PRINT(CCV_CLI_VERBOSE, "[cnnp_reshape_build] 1. dim: (%d", self->dim[0])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_reshape_build] 1. dim: (%d", self->dim[0]
); fflush(stdout); } } while (0)
;
567 int i;
568 for (i = 1; i < CCV_NNC_MAX_DIM_ALLOC(12) && self->dim[i] > 0; i++)
569 PRINT(CCV_CLI_VERBOSE, ", %d", self->dim[i])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(", %d", self->dim[i]); fflush(stdout); } } while
(0)
;
570 const int count = i;
571 PRINT(CCV_CLI_VERBOSE, "), ofs: (%d", self->ofs[0])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("), ofs: (%d", self->ofs[0]); fflush(stdout); } }
while (0)
;
572 for (i = 1; i < count; i++)
573 PRINT(CCV_CLI_VERBOSE, ", %d", self->ofs[i])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(", %d", self->ofs[i]); fflush(stdout); } } while
(0)
;
574 PRINT(CCV_CLI_VERBOSE, "), stride: (%d", self->stride[0])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("), stride: (%d", self->stride[0]); fflush(stdout
); } } while (0)
;
575 for (i = 1; i < count; i++)
576 PRINT(CCV_CLI_VERBOSE, ", %d", self->stride[i])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(", %d", self->stride[i]); fflush(stdout); } } while
(0)
;
577 PRINT(CCV_CLI_VERBOSE, ")\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(")\n"); fflush(stdout); } } while (0)
;
578 }
579 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
580 if (CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_VERBOSE)(CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
581 {
582 PRINT(CCV_CLI_VERBOSE, "[cnnp_reshape_build] 2. input: (%d", params.dim[0])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_reshape_build] 2. input: (%d", params.dim[0]
); fflush(stdout); } } while (0)
;
583 int i;
584 for (i = 1; i < CCV_NNC_MAX_DIM_ALLOC(12) && params.dim[i] > 0; i++)
585 PRINT(CCV_CLI_VERBOSE, ", %d", params.dim[i])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(", %d", params.dim[i]); fflush(stdout); } } while (
0)
;
586 PRINT(CCV_CLI_VERBOSE, ")\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf(")\n"); fflush(stdout); } } while (0)
;
587 }
588 if (self->format > 0)
589 params.format = self->format;
590 assert(ccv_nnc_dimension_count(self->dim) <= ccv_nnc_tensor_count(params))((void) sizeof ((ccv_nnc_dimension_count(self->dim) <= ccv_nnc_tensor_count
(params)) ? 1 : 0), __extension__ ({ if (ccv_nnc_dimension_count
(self->dim) <= ccv_nnc_tensor_count(params)) ; else __assert_fail
("ccv_nnc_dimension_count(self->dim) <= ccv_nnc_tensor_count(params)"
, "ccv_cnnp_model_addons.c", 590, __extension__ __PRETTY_FUNCTION__
); }))
;
591 ccv_nnc_tensor_symbol_t to = ccv_nnc_tensor_symbol_alias_to(graph, inputs[0]);
592 int stride_from_dim[CCV_NNC_MAX_DIM_ALLOC(12)];
593 if (to.d == CCV_NNC_NO_TENSOR_SYMBOL) // If we are not reshape an alias, it is straightforward.
594 {
595 memcpy(params.dim, self->dim, sizeof(params.dim));
596 int* stride;
597 if (self->stride[0] == 0)
598 {
599 ccv_nnc_tensor_get_stride(self->dim, stride_from_dim);
600 stride = stride_from_dim;
601 } else
602 stride = self->stride;
603 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], self->ofs, stride, params, 0);
604 } else {
605 // Otherwise, we need to check if it is permute. For permute, we cannot do alias directly.
606 // We need to first materialize the permute and then run reshape on top of it, otherwise it will be wrong.
607 int old_stride[CCV_NNC_MAX_DIM_ALLOC(12)];
608 ccv_nnc_tensor_symbol_alias_params(graph, inputs[0], 0, old_stride);
609 // We identify permute by checking if the stride is not in descending order.
610 // This also covered "permute" through reshape, rather than using ccv_cnnp_permute directly.
611 const int nd = ccv_nnc_tensor_nd(params.dim);
612 const int new_nd = ccv_nnc_tensor_nd(self->dim);
613 int i, no_permute = 1;
614 // If the new dim has different nd, or we actually have a stride, we need to check if it is no permute or not.
615 if (new_nd != nd || (self->stride[0] != 0 && memcmp(self->stride, old_stride, sizeof(self->stride)) != 0))
616 for (i = 1; no_permute && i < nd; i++)
617 if (old_stride[i - 1] < old_stride[i])
618 no_permute = 0;
619 if (no_permute)
620 { // Just straightforward reshape if there is no no permute.
621 memcpy(params.dim, self->dim, sizeof(params.dim));
622 int* stride;
623 if (self->stride[0] == 0)
624 {
625 if (new_nd != nd) // Cannot use old stride.
626 {
627 ccv_nnc_tensor_get_stride(self->dim, stride_from_dim);
628 stride = stride_from_dim;
629 } else
630 stride = old_stride;
631 } else
632 stride = self->stride;
633 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], self->ofs, stride, params, 0);
634 } else {
635 // Otherwise, we first do format transform to plain tensor and then do reshape.
636 ccv_nnc_tensor_symbol_t permuted = ccv_nnc_tensor_symbol_new(graph, params, 0);
637 ccv_nnc_graph_exec_symbol_new(graph, CMD_FORMAT_TRANSFORM_FORWARD()ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(permuted)(const ccv_nnc_tensor_symbol_t []){permuted}, (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)
, "reshape");
638 memcpy(params.dim, self->dim, sizeof(params.dim));
639 int* stride;
640 if (self->stride[0] == 0)
641 {
642 ccv_nnc_tensor_get_stride(self->dim, stride_from_dim);
643 stride = stride_from_dim;
644 } else
645 stride = self->stride;
646 // And then we create alias against the permuted one.
647 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, permuted, self->ofs, stride, params, 0);
648 }
649 }
650}
651
652static ccv_cnnp_model_t* _ccv_cnnp_reshape_copy(const ccv_cnnp_model_t* const super, void* const context);
653
654static const ccv_cnnp_model_vtab_t ccv_cnnp_reshape_isa = {
655 .build = _ccv_cnnp_reshape_build,
656 .copy = _ccv_cnnp_reshape_copy,
657};
658
659ccv_cnnp_model_t* ccv_cnnp_reshape(const int format, const int dim[CCV_NNC_MAX_DIM_ALLOC(12)], const int ofs[CCV_NNC_MAX_DIM_ALLOC(12)], const int stride[CCV_NNC_MAX_DIM_ALLOC(12)], const char* const name)
660{
661 ccv_cnnp_model_reshape_t* const model_reshape = (ccv_cnnp_model_reshape_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_reshape_t));
662 model_reshape->super.isa = &ccv_cnnp_reshape_isa;
663 model_reshape->super.input_size = 1;
664 model_reshape->super.outputs = &model_reshape->output;
665 model_reshape->super.output_size = 1;
666 ccv_cnnp_model_copy_name(&model_reshape->super, name);
667 model_reshape->format = format;
668 memcpy(model_reshape->dim, dim, sizeof(model_reshape->dim));
669 memcpy(model_reshape->ofs, ofs, sizeof(model_reshape->ofs));
670 if (stride[0] != 0)
671 memcpy(model_reshape->stride, stride, sizeof(model_reshape->stride));
672 return (ccv_cnnp_model_t*)model_reshape;
673}
674
675static ccv_cnnp_model_t* _ccv_cnnp_reshape_copy(const ccv_cnnp_model_t* const super, void* const context)
676{
677 const ccv_cnnp_model_reshape_t* const self = (const ccv_cnnp_model_reshape_t*)super;
678 return ccv_cnnp_reshape(self->format, self->dim, self->ofs, self->stride, self->super.name);
679}
680
681typedef struct {
682 ccv_cnnp_model_t super;
683 ccv_nnc_tensor_symbol_t output;
684 int type;
685 int begin[CCV_NNC_MAX_DIM_ALLOC(12)];
686 int end[CCV_NNC_MAX_DIM_ALLOC(12)];
687} ccv_cnnp_model_pad_t;
688
689static void _ccv_cnnp_pad_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
690{
691 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 691, __extension__ __PRETTY_FUNCTION__); }))
;
692 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 692, __extension__ __PRETTY_FUNCTION__
); }))
;
693 ccv_cnnp_model_pad_t* const self = (ccv_cnnp_model_pad_t*)super;
694 PRINT(CCV_CLI_VERBOSE, "[cnnp_pad_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_pad_build] -\n"); fflush(stdout); } } while (
0)
;
695 const ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
696 const int nd = ccv_nnc_tensor_nd(input_params.dim);
697 ccv_nnc_tensor_param_t params = input_params;
698 int i;
699 for (i = 0 ; i < nd; i++)
700 params.dim[i] += self->begin[i] + self->end[i];
701 const ccv_nnc_tensor_symbol_t padded = ccv_nnc_tensor_symbol_new(graph, params, 0);
702 ccv_nnc_cmd_t pad = CMD_PAD_FORWARD(self->type, (), ())ccv_nnc_cmd(CCV_NNC_PAD_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size
={.dim={}},.pad={.type=self->type,.end={}}}), 0)
;
703 memcpy(pad.info.size.dim, self->begin, sizeof(pad.info.size.dim));
704 memcpy(pad.info.pad.end, self->end, sizeof(pad.info.pad.end));
705 ccv_nnc_graph_exec_symbol_new(graph, pad, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(padded)(const ccv_nnc_tensor_symbol_t []){padded}, (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)
, "pad");
706 outputs[0] = padded;
707}
708
709static ccv_cnnp_model_t* _ccv_cnnp_pad_copy(const ccv_cnnp_model_t* const super, void* const context);
710
711static const ccv_cnnp_model_vtab_t ccv_cnnp_pad_isa = {
712 .build = _ccv_cnnp_pad_build,
713 .copy = _ccv_cnnp_pad_copy,
714};
715
716ccv_cnnp_model_t* ccv_cnnp_pad(const int type, const int begin[CCV_NNC_MAX_DIM_ALLOC(12)], const int end[CCV_NNC_MAX_DIM_ALLOC(12)], const char* const name)
717{
718 ccv_cnnp_model_pad_t* const model_pad = (ccv_cnnp_model_pad_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_pad_t));
719 model_pad->super.isa = &ccv_cnnp_pad_isa;
720 model_pad->super.input_size = 1;
721 model_pad->super.outputs = &model_pad->output;
722 model_pad->super.output_size = 1;
723 ccv_cnnp_model_copy_name(&model_pad->super, name);
724 model_pad->type = type;
725 memcpy(model_pad->begin, begin, sizeof(model_pad->begin));
726 memcpy(model_pad->end, end, sizeof(model_pad->end));
727 return (ccv_cnnp_model_t*)model_pad;
728}
729
730static ccv_cnnp_model_t* _ccv_cnnp_pad_copy(const ccv_cnnp_model_t* const super, void* const context)
731{
732 const ccv_cnnp_model_pad_t* const self = (const ccv_cnnp_model_pad_t*)super;
733 return ccv_cnnp_pad(self->type, self->begin, self->end, self->super.name);
734}
735
736typedef struct {
737 ccv_cnnp_model_t super;
738 ccv_nnc_tensor_symbol_t output;
739} ccv_cnnp_model_identity_t;
740
741static void _ccv_cnnp_identity_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
742{
743 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 743, __extension__ __PRETTY_FUNCTION__); }))
;
744 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 744, __extension__ __PRETTY_FUNCTION__
); }))
;
745 PRINT(CCV_CLI_VERBOSE, "[cnnp_identity_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_identity_build] -\n"); fflush(stdout); } } while
(0)
;
746 outputs[0] = inputs[0];
747}
748
749static ccv_cnnp_model_t* _ccv_cnnp_identity_copy(const ccv_cnnp_model_t* const super, void* const context);
750
751static const ccv_cnnp_model_vtab_t ccv_cnnp_identity_isa = {
752 .build = _ccv_cnnp_identity_build,
753 .copy = _ccv_cnnp_identity_copy,
754};
755
756ccv_cnnp_model_t* ccv_cnnp_identity(const char* const name)
757{
758 ccv_cnnp_model_identity_t* const model_identity = (ccv_cnnp_model_identity_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_identity_t));
759 model_identity->super.isa = &ccv_cnnp_identity_isa;
760 model_identity->super.input_size = 1;
761 model_identity->super.outputs = &model_identity->output;
762 model_identity->super.output_size = 1;
763 ccv_cnnp_model_copy_name(&model_identity->super, name);
764 return (ccv_cnnp_model_t*)model_identity;
765}
766
767static ccv_cnnp_model_t* _ccv_cnnp_identity_copy(const ccv_cnnp_model_t* const super, void* const context)
768{
769 const ccv_cnnp_model_identity_t* const self = (const ccv_cnnp_model_identity_t*)super;
770 return ccv_cnnp_identity(self->super.name);
771}
772
773typedef struct {
774 ccv_cnnp_model_t super;
775 ccv_nnc_tensor_symbol_t output;
776 int index[CCV_NNC_MAX_DIM_ALLOC(12)];
777} ccv_cnnp_model_permute_t;
778
779static void _ccv_cnnp_permute_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
780{
781 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 781, __extension__ __PRETTY_FUNCTION__); }))
;
782 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 782, __extension__ __PRETTY_FUNCTION__
); }))
;
783 ccv_cnnp_model_permute_t* const self = (ccv_cnnp_model_permute_t*)super;
784 PRINT(CCV_CLI_VERBOSE, "[cnnp_permute_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_permute_build] -\n"); fflush(stdout); } } while
(0)
;
785 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
786 ccv_nnc_tensor_symbol_t to = ccv_nnc_tensor_symbol_alias_to(graph, inputs[0]);
787 const int nd = ccv_nnc_tensor_nd(params.dim);
788 int input_dim[CCV_NNC_MAX_DIM_ALLOC(12)];
789 memcpy(input_dim, params.dim, sizeof(params.dim));
790 int input_stride[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
791 int output_stride[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
792 if (to.d == CCV_NNC_NO_TENSOR_SYMBOL) // If it is not an alias. Find stride and permute.
793 {
794 ccv_nnc_tensor_get_stride(input_dim, input_stride);
795 int i;
796 for (i = 0; i < nd; i++)
797 {
798 const int idx = self->index[i];
799 assert(idx >= 0 && idx < nd)((void) sizeof ((idx >= 0 && idx < nd) ? 1 : 0)
, __extension__ ({ if (idx >= 0 && idx < nd) ; else
__assert_fail ("idx >= 0 && idx < nd", "ccv_cnnp_model_addons.c"
, 799, __extension__ __PRETTY_FUNCTION__); }))
;
800 params.dim[i] = input_dim[idx];
801 output_stride[i] = input_stride[idx];
802 }
803 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], ccv_nnc_no_ofs, output_stride, params, 0);
804 } else {
805 // if it is an alias, we can get the stride from it and use that.
806 int input_ofs[CCV_NNC_MAX_DIM_ALLOC(12)];
807 ccv_nnc_tensor_symbol_alias_params(graph, inputs[0], input_ofs, input_stride);
808 assert(input_stride[0] != 0)((void) sizeof ((input_stride[0] != 0) ? 1 : 0), __extension__
({ if (input_stride[0] != 0) ; else __assert_fail ("input_stride[0] != 0"
, "ccv_cnnp_model_addons.c", 808, __extension__ __PRETTY_FUNCTION__
); }))
;
809 int output_ofs[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
810 int i;
811 for (i = 0; i < nd; i++)
812 {
813 const int idx = self->index[i];
814 assert(idx >= 0 && idx < nd)((void) sizeof ((idx >= 0 && idx < nd) ? 1 : 0)
, __extension__ ({ if (idx >= 0 && idx < nd) ; else
__assert_fail ("idx >= 0 && idx < nd", "ccv_cnnp_model_addons.c"
, 814, __extension__ __PRETTY_FUNCTION__); }))
;
815 params.dim[i] = input_dim[idx];
816 output_stride[i] = input_stride[idx];
817 output_ofs[i] = input_ofs[idx];
818 }
819 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], output_ofs, output_stride, params, 0);
820 }
821}
822
823static ccv_cnnp_model_t* _ccv_cnnp_permute_copy(const ccv_cnnp_model_t* const super, void* const context);
824
825static const ccv_cnnp_model_vtab_t ccv_cnnp_permute_isa = {
826 .build = _ccv_cnnp_permute_build,
827 .copy = _ccv_cnnp_permute_copy,
828};
829
830ccv_cnnp_model_t* ccv_cnnp_permute(const int index[CCV_NNC_MAX_DIM_ALLOC(12)], const char* const name)
831{
832 ccv_cnnp_model_permute_t* const model_permute = (ccv_cnnp_model_permute_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_permute_t));
833 model_permute->super.isa = &ccv_cnnp_permute_isa;
834 model_permute->super.input_size = 1;
835 model_permute->super.outputs = &model_permute->output;
836 model_permute->super.output_size = 1;
837 ccv_cnnp_model_copy_name(&model_permute->super, name);
838 memcpy(model_permute->index, index, sizeof(model_permute->index));
839 return (ccv_cnnp_model_t*)model_permute;
840}
841
842static ccv_cnnp_model_t* _ccv_cnnp_permute_copy(const ccv_cnnp_model_t* const super, void* const context)
843{
844 const ccv_cnnp_model_permute_t* const self = (const ccv_cnnp_model_permute_t*)super;
845 return ccv_cnnp_permute(self->index, self->super.name);
846}
847
848typedef struct {
849 ccv_cnnp_model_t super;
850 int index;
851 ccv_nnc_tensor_symbol_t output;
852} ccv_cnnp_model_extract_t;
853
854static void _ccv_cnnp_extract_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
855{
856 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 856, __extension__ __PRETTY_FUNCTION__
); }))
;
857 ccv_cnnp_model_extract_t* const self = (ccv_cnnp_model_extract_t*)super;
858 PRINT(CCV_CLI_VERBOSE, "[cnnp_extract_build] index: %d\n", self->index)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_extract_build] index: %d\n", self->index)
; fflush(stdout); } } while (0)
;
859 outputs[0] = inputs[self->index];
860}
861
862static ccv_cnnp_model_t* _ccv_cnnp_extract_copy(const ccv_cnnp_model_t* const self, void* const context);
863
864static const ccv_cnnp_model_vtab_t ccv_cnnp_extract_isa = {
865 .build = _ccv_cnnp_extract_build,
866 .copy = _ccv_cnnp_extract_copy,
867};
868
869ccv_cnnp_model_t* ccv_cnnp_extract(const int index, const char* const name)
870{
871 ccv_cnnp_model_extract_t* const model_extract = (ccv_cnnp_model_extract_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_extract_t));
872 model_extract->index = index;
873 model_extract->super.isa = &ccv_cnnp_extract_isa;
874 model_extract->super.input_size = 0;
875 model_extract->super.outputs = &model_extract->output;
876 model_extract->super.output_size = 1;
877 ccv_cnnp_model_copy_name(&model_extract->super, name);
878 return (ccv_cnnp_model_t*)model_extract;
879}
880
881static ccv_cnnp_model_t* _ccv_cnnp_extract_copy(const ccv_cnnp_model_t* const super, void* const context)
882{
883 ccv_cnnp_model_extract_t* const self = (ccv_cnnp_model_extract_t*)super;
884 return ccv_cnnp_extract(self->index, self->super.name);
885}
886
887typedef struct {
888 ccv_cnnp_model_t super;
889 ccv_nnc_tensor_symbol_t output;
890} ccv_cnnp_model_flatten_t;
891
892static void _ccv_cnnp_flatten_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
893{
894 PRINT(CCV_CLI_VERBOSE, "[cnnp_flatten_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_flatten_build] -\n"); fflush(stdout); } } while
(0)
;
895 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 895, __extension__ __PRETTY_FUNCTION__); }))
;
896 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 896, __extension__ __PRETTY_FUNCTION__
); }))
;
897 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
898 ccv_nnc_tensor_param_t output_params = params;
899 memset(output_params.dim, 0, sizeof(output_params.dim));
900 output_params.dim[0] = ccv_nnc_tensor_get_n(params);
901 assert(output_params.dim[0] > 0)((void) sizeof ((output_params.dim[0] > 0) ? 1 : 0), __extension__
({ if (output_params.dim[0] > 0) ; else __assert_fail ("output_params.dim[0] > 0"
, "ccv_cnnp_model_addons.c", 901, __extension__ __PRETTY_FUNCTION__
); }))
;
902 output_params.dim[1] = ccv_nnc_tensor_count(params) / output_params.dim[0];
903 int stride[CCV_NNC_MAX_DIM_ALLOC(12)] = {};
904 ccv_nnc_tensor_get_stride(output_params.dim, stride);
905 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], DIM_ALLOC()(int [(12)]){}, stride, output_params, 0);
906}
907
908static ccv_cnnp_model_t* _ccv_cnnp_flatten_copy(const ccv_cnnp_model_t* const self, void* const context);
909
910static const ccv_cnnp_model_vtab_t ccv_cnnp_flatten_isa = {
911 .build = _ccv_cnnp_flatten_build,
912 .copy = _ccv_cnnp_flatten_copy,
913};
914
915ccv_cnnp_model_t* ccv_cnnp_flatten(const char* const name)
916{
917 ccv_cnnp_model_flatten_t* const model_flatten = (ccv_cnnp_model_flatten_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_flatten_t));
918 model_flatten->super.isa = &ccv_cnnp_flatten_isa;
919 model_flatten->super.input_size = 1;
920 model_flatten->super.outputs = &model_flatten->output;
921 model_flatten->super.output_size = 1;
922 ccv_cnnp_model_copy_name(&model_flatten->super, name);
923 return (ccv_cnnp_model_t*)model_flatten;
924}
925
926static ccv_cnnp_model_t* _ccv_cnnp_flatten_copy(const ccv_cnnp_model_t* const self, void* const context)
927{
928 return ccv_cnnp_flatten(self->name);
929}
930
931// MARK - Batch Norm Layer
932
933typedef struct {
934 ccv_cnnp_model_t super;
935 ccv_nnc_tensor_symbol_t output;
936 ccv_nnc_tensor_symbol_t bias;
937 ccv_nnc_tensor_symbol_t scale;
938 ccv_nnc_graph_exec_symbol_t batch_norm;
939 ccv_nnc_cmd_param_t params;
940 ccv_array_t* zero_inits;
941 ccv_array_t* retainables;
942} ccv_cnnp_model_batch_norm_t;
943
944static void _ccv_cnnp_batch_norm_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
945{
946 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 946, __extension__ __PRETTY_FUNCTION__); }))
;
947 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 947, __extension__ __PRETTY_FUNCTION__
); }))
;
948 ccv_cnnp_model_batch_norm_t* const self = (ccv_cnnp_model_batch_norm_t*)super;
949 PRINT(CCV_CLI_VERBOSE, "[cnnp_batch_norm_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_batch_norm_build] -\n"); fflush(stdout); } }
while (0)
;
950 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
951 const int nd = ccv_nnc_tensor_nd(params.dim);
952 ccv_nnc_tensor_param_t bias_params = params;
953 memset(bias_params.dim, 0, sizeof(bias_params.dim));
954 // If the accuracy is not enough, bump it to 32-bit floating point.
955 if (bias_params.datatype != CCV_32F && bias_params.datatype != CCV_64F)
956 bias_params.datatype = CCV_32F;
957 bias_params.dim[0] = nd > 1 ? ccv_nnc_tensor_get_c(params) : params.dim[0];
958 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, params, 0);
959 // Both scale and bias are shared between if this model is reused.
960 if (!self->scale.graph)
961 self->scale = ccv_nnc_tensor_symbol_new(graph, bias_params, "scale");
962 if (!self->bias.graph)
963 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
964 const ccv_nnc_tensor_symbol_t mean = ccv_nnc_tensor_symbol_new(graph, bias_params, "mean");
965 const ccv_nnc_tensor_symbol_t var = ccv_nnc_tensor_symbol_new(graph, bias_params, "var");
966 // Otherwise, notice mean, var, saved_mean, saved_inv_std are not reused.
967 if (!self->zero_inits)
968 self->zero_inits = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
969 ccv_array_push(self->zero_inits, &mean);
970 ccv_array_push(self->zero_inits, &var);
971 const ccv_nnc_tensor_symbol_t out_mean = ccv_nnc_tensor_symbol_new(graph, bias_params, "out_mean");
972 const ccv_nnc_tensor_symbol_t out_var = ccv_nnc_tensor_symbol_new(graph, bias_params, "out_var");
973 if (!self->retainables)
974 self->retainables = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
975 ccv_array_push(self->retainables, &out_mean);
976 ccv_array_push(self->retainables, &out_var);
977 const ccv_nnc_tensor_symbol_t saved_mean = ccv_nnc_tensor_symbol_new(graph, bias_params, "saved_mean");
978 const ccv_nnc_tensor_symbol_t saved_inv_std = ccv_nnc_tensor_symbol_new(graph, bias_params, "saved_inv_std");
979 const int hw = ccv_nnc_tensor_hw(params, ccv_nnc_tensor_nd(params.dim));
980 ccv_nnc_cmd_param_t batch_norm = self->params;
981 batch_norm.bnorm.count = hw >= 0 ? CCV_NNC_MAX_DIM(2) + 1 : 1;
982 int i;
983 batch_norm.bnorm.axis[0] = (params.format == CCV_TENSOR_FORMAT_CHWN) ? 3 : 0;
984 if (hw >= 0)
985 for (i = 0; i < CCV_NNC_MAX_DIM(2); i++)
986 batch_norm.bnorm.axis[i + 1] = i + hw;
987 self->params = batch_norm;
988 self->batch_norm = ccv_nnc_graph_exec_symbol_new(graph, ccv_nnc_cmd(CCV_NNC_BATCH_NORM_FORWARD, 0, batch_norm, 0), TENSOR_SYMBOL_LIST(inputs[0], self->scale, self->bias, mean, var)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->scale,
self->bias, mean, var}, (1 +1 +1 +1 +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 -1)
, TENSOR_SYMBOL_LIST(output, out_mean, out_var, saved_mean, saved_inv_std)(const ccv_nnc_tensor_symbol_t []){output, out_mean, out_var,
saved_mean, saved_inv_std}, (1 +1 +1 +1 +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 -1)
, "batch_norm");
989 outputs[0] = output;
990}
991
992static void _ccv_cnnp_batch_norm_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
993{
994 ccv_cnnp_model_batch_norm_t* const self = (ccv_cnnp_model_batch_norm_t*)super;
995 if (self->scale.graph)
996 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(0, 1)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={0, 1}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->scale);
997 if (self->bias.graph)
998 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->bias);
999 int i;
1000 if (self->zero_inits)
1001 for (i = 0; i < self->zero_inits->rnum; i++)
1002 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, *(ccv_nnc_tensor_symbol_t*)ccv_array_get(self->zero_inits, i)((void*)(((char*)((self->zero_inits)->data)) + (size_t)
(self->zero_inits)->rsize * (size_t)(i)))
);
1003}
1004
1005static void _ccv_cnnp_batch_norm_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
1006{
1007 ccv_cnnp_model_batch_norm_t* const self = (ccv_cnnp_model_batch_norm_t*)super;
1008 if (self->scale.graph)
1009 add_to_array(parameters, self->scale, is_trainable);
1010 if (self->bias.graph)
1011 add_to_array(parameters, self->bias, is_trainable);
1012}
1013
1014static void _ccv_cnnp_batch_norm_add_to_output(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const outputs)
1015{
1016 ccv_cnnp_model_batch_norm_t* const self = (ccv_cnnp_model_batch_norm_t*)super;
1017 int i;
1018 if (self->retainables)
1019 for (i = 0; i < self->retainables->rnum; i++)
1020 {
1021 const ccv_nnc_tensor_symbol_t symbol = *(ccv_nnc_tensor_symbol_t*)ccv_array_get(self->retainables, i)((void*)(((char*)((self->retainables)->data)) + (size_t
)(self->retainables)->rsize * (size_t)(i)))
;
1022 add_to_array(outputs, symbol, 0);
1023 }
1024}
1025
1026static void _ccv_cnnp_batch_norm_set_is_test(ccv_cnnp_model_t* const super, const int is_test, const ccv_cnnp_cmd_updater_f updater, void* const context)
1027{
1028 ccv_cnnp_model_batch_norm_t* const self = (ccv_cnnp_model_batch_norm_t*)super;
1029 if (self->batch_norm.graph)
1030 {
1031 self->params.bnorm.is_test = is_test;
1032 updater(context, self->batch_norm, ccv_nnc_cmd(CCV_NNC_BATCH_NORM_FORWARD, 0, self->params, 0), ccv_nnc_no_hint);
1033 }
1034}
1035
1036static void _ccv_cnnp_batch_norm_deinit(ccv_cnnp_model_t* const super)
1037{
1038 ccv_cnnp_model_batch_norm_t* const self = (ccv_cnnp_model_batch_norm_t*)super;
1039 if (self->zero_inits)
1040 ccv_array_free(self->zero_inits);
1041 if (self->retainables)
1042 ccv_array_free(self->retainables);
1043}
1044
1045static ccv_cnnp_model_t* _ccv_cnnp_batch_norm_copy(const ccv_cnnp_model_t* const super, void* const context);
1046
1047static const ccv_cnnp_model_vtab_t ccv_cnnp_batch_norm_isa = {
1048 .build = _ccv_cnnp_batch_norm_build,
1049 .init_states = _ccv_cnnp_batch_norm_init_states,
1050 .add_to_parameter = _ccv_cnnp_batch_norm_add_to_parameter,
1051 .add_to_output = _ccv_cnnp_batch_norm_add_to_output,
1052 .copy = _ccv_cnnp_batch_norm_copy,
1053 .set_is_test = _ccv_cnnp_batch_norm_set_is_test,
1054 .deinit = _ccv_cnnp_batch_norm_deinit,
1055};
1056
1057ccv_cnnp_model_t* ccv_cnnp_batch_norm(const float momentum, const float epsilon, const int is_trainable, const char* const name)
1058{
1059 ccv_cnnp_model_batch_norm_t* const model_batch_norm = (ccv_cnnp_model_batch_norm_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_batch_norm_t));
1060 model_batch_norm->super.isa = &ccv_cnnp_batch_norm_isa;
1061 model_batch_norm->super.input_size = 1;
1062 model_batch_norm->super.outputs = &model_batch_norm->output;
1063 model_batch_norm->super.output_size = 1;
1064 model_batch_norm->super.is_trainable = is_trainable;
1065 ccv_cnnp_model_copy_name(&model_batch_norm->super, name);
1066 model_batch_norm->scale.d = CCV_NNC_NO_TENSOR_SYMBOL;
1067 model_batch_norm->scale.graph = 0;
1068 model_batch_norm->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
1069 model_batch_norm->bias.graph = 0;
1070 model_batch_norm->params.bnorm.momentum = momentum;
1071 model_batch_norm->params.bnorm.epsilon = epsilon;
1072 return (ccv_cnnp_model_t*)model_batch_norm;
1073}
1074
1075static ccv_cnnp_model_t* _ccv_cnnp_batch_norm_copy(const ccv_cnnp_model_t* const super, void* const context)
1076{
1077 const ccv_cnnp_model_batch_norm_t* const self = (const ccv_cnnp_model_batch_norm_t*)super;
1078 return ccv_cnnp_batch_norm(self->params.bnorm.momentum, self->params.bnorm.epsilon, self->super.is_trainable, self->super.name);
1079}
1080
1081// MARK - Convolution Layer
1082
1083typedef struct {
1084 ccv_cnnp_model_t super;
1085 ccv_nnc_tensor_symbol_t output;
1086 ccv_nnc_tensor_symbol_t weights;
1087 ccv_nnc_tensor_symbol_t bias;
1088 int groups;
1089 int filters;
1090 int kdim[CCV_NNC_MAX_DIM_ALLOC(12)];
1091 int dilation[CCV_NNC_MAX_DIM_ALLOC(12)];
1092 int no_bias;
1093 int format;
1094 ccv_nnc_hint_t hint;
1095} ccv_cnnp_model_convolution_t;
1096
1097static void _ccv_cnnp_convolution_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1098{
1099 ccv_cnnp_model_convolution_t* const self = (ccv_cnnp_model_convolution_t*)super;
1100 PRINT(CCV_CLI_VERBOSE, "[cnnp_convolution_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_convolution_build] -\n"); fflush(stdout); } }
while (0)
;
1101 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1101, __extension__ __PRETTY_FUNCTION__); }))
;
1102 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1102, __extension__ __PRETTY_FUNCTION__
); }))
;
1103 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1104 int i;
1105 const int nd = CCV_NNC_MAX_DIM(2) + 2;
1106 ccv_nnc_tensor_param_t weights_params = params;
1107 if (self->format)
1108 weights_params.format = self->format;
1109 ccv_nnc_tensor_set_n(&weights_params, self->filters);
1110 assert(ccv_nnc_tensor_get_c(params) % self->groups == 0)((void) sizeof ((ccv_nnc_tensor_get_c(params) % self->groups
== 0) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_get_c(params
) % self->groups == 0) ; else __assert_fail ("ccv_nnc_tensor_get_c(params) % self->groups == 0"
, "ccv_cnnp_model_addons.c", 1110, __extension__ __PRETTY_FUNCTION__
); }))
;
1111 ccv_nnc_tensor_set_c(&weights_params, nd, ccv_nnc_tensor_get_c(params) / self->groups);
1112 const int hw = ccv_nnc_tensor_hw(weights_params, nd);
1113 assert(hw >= 0)((void) sizeof ((hw >= 0) ? 1 : 0), __extension__ ({ if (hw
>= 0) ; else __assert_fail ("hw >= 0", "ccv_cnnp_model_addons.c"
, 1113, __extension__ __PRETTY_FUNCTION__); }))
;
1114 for (i = 0; i < CCV_NNC_MAX_DIM(2); i++)
1115 weights_params.dim[i + hw] = self->kdim[i];
1116 if (!self->weights.graph)
1117 self->weights = ccv_nnc_tensor_symbol_new(graph, weights_params, "weights");
1118 assert(self->weights.graph == graph)((void) sizeof ((self->weights.graph == graph) ? 1 : 0), __extension__
({ if (self->weights.graph == graph) ; else __assert_fail
("self->weights.graph == graph", "ccv_cnnp_model_addons.c"
, 1118, __extension__ __PRETTY_FUNCTION__); }))
;
1119 ccv_nnc_tensor_param_t bias_params = params;
1120 if (self->format)
1121 bias_params.format = self->format;
1122 memset(bias_params.dim, 0, sizeof(bias_params.dim));
1123 bias_params.dim[0] = self->filters;
1124 ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_FORWARD(self->groups, self->filters)ccv_nnc_cmd(CCV_NNC_CONVOLUTION_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={}},.convolution={.count=self->filters,.groups
=self->groups}}), 0)
;
1125 for (i = 0; i < CCV_NNC_MAX_DIM(2); i++)
1126 cmd.info.size.dim[i] = self->kdim[i];
1127 memcpy(cmd.info.convolution.dilation, self->dilation, sizeof(self->dilation));
1128 ccv_nnc_tensor_param_t output_params;
1129 // Dilate weight size based on the dilation factor.
1130 for (i = 0; i < CCV_NNC_MAX_DIM(2); i++)
1131 weights_params.dim[i + hw] = (self->kdim[i] - 1) * ccv_max(self->dilation[i], 1)({ typeof (self->dilation[i]) _a = (self->dilation[i]);
typeof (1) _b = (1); (_a > _b) ? _a : _b; })
+ 1;
1132 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
1133 params,
1134 weights_params,
1135 bias_params,
1136 }, 3, self->hint, &output_params, 1);
1137 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1138 ccv_nnc_graph_exec_symbol_t convolution;
1139 if (self->no_bias)
1140 convolution = ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0], self->weights)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->weights
}, (1 +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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "convolution");
1141 else {
1142 if (!self->bias.graph)
1143 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
1144 convolution = ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0], self->weights, self->bias)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->weights
, self->bias}, (1 +1 +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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "convolution");
1145 }
1146 ccv_nnc_graph_exec_symbol_set_hint(graph, convolution, self->hint);
1147 outputs[0] = output;
1148}
1149
1150static void _ccv_cnnp_convolution_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
1151{
1152 ccv_cnnp_model_convolution_t* const self = (ccv_cnnp_model_convolution_t*)super;
1153 const ccv_nnc_tensor_param_t weight_params = ccv_nnc_tensor_symbol_params(graph, self->weights);
1154 const int n = ccv_max(ccv_nnc_tensor_get_n(weight_params), 1)({ typeof (ccv_nnc_tensor_get_n(weight_params)) _a = (ccv_nnc_tensor_get_n
(weight_params)); typeof (1) _b = (1); (_a > _b) ? _a : _b
; })
;
1155 const int count = ccv_nnc_tensor_count(weight_params);
1156 const float std = sqrtf(2) / sqrtf(count / n);
1157 const float bound = sqrtf(3) * std;
1158 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(-bound, bound)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={-bound, bound}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->weights);
1159 if (self->bias.graph)
1160 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->bias);
1161}
1162
1163static void _ccv_cnnp_convolution_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
1164{
1165 ccv_cnnp_model_convolution_t* const self = (ccv_cnnp_model_convolution_t*)super;
1166 add_to_array(parameters, self->weights, is_trainable);
1167 if (self->bias.graph)
1168 add_to_array(parameters, self->bias, is_trainable);
1169}
1170
1171static ccv_cnnp_model_t* _ccv_cnnp_convolution_copy(const ccv_cnnp_model_t* const super, void* const context);
1172
1173static const ccv_cnnp_model_vtab_t ccv_cnnp_convolution_isa = {
1174 .build = _ccv_cnnp_convolution_build,
1175 .init_states = _ccv_cnnp_convolution_init_states,
1176 .add_to_parameter = _ccv_cnnp_convolution_add_to_parameter,
1177 .copy = _ccv_cnnp_convolution_copy,
1178};
1179
1180ccv_cnnp_model_t* ccv_cnnp_convolution(const int groups, const int filters, const int kdim[CCV_NNC_MAX_DIM_ALLOC(12)], const int dilation[CCV_NNC_MAX_DIM_ALLOC(12)], const int no_bias, ccv_nnc_hint_t hint, const int format, const int is_trainable, const char* const name)
1181{
1182 ccv_cnnp_model_convolution_t* const model_convolution = (ccv_cnnp_model_convolution_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_convolution_t));
1183 model_convolution->super.isa = &ccv_cnnp_convolution_isa;
1184 model_convolution->super.input_size = 1;
1185 model_convolution->super.outputs = &model_convolution->output;
1186 model_convolution->super.output_size = 1;
1187 model_convolution->super.is_trainable = is_trainable;
1188 ccv_cnnp_model_copy_name(&model_convolution->super, name);
1189 model_convolution->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
1190 model_convolution->weights.graph = 0;
1191 model_convolution->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
1192 model_convolution->bias.graph = 0;
1193 model_convolution->groups = groups;
1194 model_convolution->filters = filters;
1195 memcpy(model_convolution->kdim, kdim, sizeof(model_convolution->kdim));
1196 memcpy(model_convolution->dilation, dilation, sizeof(model_convolution->dilation));
1197 model_convolution->no_bias = no_bias;
1198 model_convolution->hint = hint;
1199 model_convolution->format = format;
1200 return (ccv_cnnp_model_t*)model_convolution;
1201}
1202
1203static ccv_cnnp_model_t* _ccv_cnnp_convolution_copy(const ccv_cnnp_model_t* const super, void* const context)
1204{
1205 ccv_cnnp_model_convolution_t* const self = (ccv_cnnp_model_convolution_t*)super;
1206 return ccv_cnnp_convolution(self->groups, self->filters, self->kdim, self->dilation, self->no_bias, self->hint, self->format, self->super.is_trainable, self->super.name);
1207}
1208
1209// MARK - Convolution Transpose Layer
1210
1211typedef struct {
1212 ccv_cnnp_model_t super;
1213 ccv_nnc_tensor_symbol_t output;
1214 ccv_nnc_tensor_symbol_t weights;
1215 ccv_nnc_tensor_symbol_t bias;
1216 int groups;
1217 int filters;
1218 int kdim[CCV_NNC_MAX_DIM_ALLOC(12)];
1219 int dilation[CCV_NNC_MAX_DIM_ALLOC(12)];
1220 int output_padding;
1221 int no_bias;
1222 int format;
1223 ccv_nnc_hint_t hint;
1224} ccv_cnnp_model_convolution_transpose_t;
1225
1226static void _ccv_cnnp_convolution_transpose_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1227{
1228 ccv_cnnp_model_convolution_transpose_t* const self = (ccv_cnnp_model_convolution_transpose_t*)super;
1229 PRINT(CCV_CLI_VERBOSE, "[cnnp_convolution_transpose_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_convolution_transpose_build] -\n"); fflush(stdout
); } } while (0)
;
1230 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1230, __extension__ __PRETTY_FUNCTION__); }))
;
1231 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1231, __extension__ __PRETTY_FUNCTION__
); }))
;
1232 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1233 int i;
1234 const int nd = CCV_NNC_MAX_DIM(2) + 2;
1235 ccv_nnc_tensor_param_t weights_params = params;
1236 if (self->format)
1237 weights_params.format = self->format;
1238 ccv_nnc_tensor_set_n(&weights_params, ccv_nnc_tensor_get_c(params));
1239 assert(ccv_nnc_tensor_get_c(params) % self->groups == 0)((void) sizeof ((ccv_nnc_tensor_get_c(params) % self->groups
== 0) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_get_c(params
) % self->groups == 0) ; else __assert_fail ("ccv_nnc_tensor_get_c(params) % self->groups == 0"
, "ccv_cnnp_model_addons.c", 1239, __extension__ __PRETTY_FUNCTION__
); }))
;
1240 ccv_nnc_tensor_set_c(&weights_params, nd, self->filters / self->groups);
1241 const int hw = ccv_nnc_tensor_hw(weights_params, nd);
1242 assert(hw >= 0)((void) sizeof ((hw >= 0) ? 1 : 0), __extension__ ({ if (hw
>= 0) ; else __assert_fail ("hw >= 0", "ccv_cnnp_model_addons.c"
, 1242, __extension__ __PRETTY_FUNCTION__); }))
;
1243 for (i = 0; i < CCV_NNC_MAX_DIM(2); i++)
1244 weights_params.dim[i + hw] = self->kdim[i];
1245 if (!self->weights.graph)
1246 self->weights = ccv_nnc_tensor_symbol_new(graph, weights_params, "weights");
1247 assert(self->weights.graph == graph)((void) sizeof ((self->weights.graph == graph) ? 1 : 0), __extension__
({ if (self->weights.graph == graph) ; else __assert_fail
("self->weights.graph == graph", "ccv_cnnp_model_addons.c"
, 1247, __extension__ __PRETTY_FUNCTION__); }))
;
1248 ccv_nnc_tensor_param_t bias_params = params;
1249 if (self->format)
1250 bias_params.format = self->format;
1251 memset(bias_params.dim, 0, sizeof(bias_params.dim));
1252 bias_params.dim[0] = self->filters;
1253 ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_TRANSPOSE_FORWARD(self->groups, self->filters, self->output_padding)ccv_nnc_cmd(CCV_NNC_CONVOLUTION_TRANSPOSE_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={}},.convolution_transpose={.count=self->filters
,.groups=self->groups,.output_padding=self->output_padding
}}), 0)
;
1254 for (i = 0; i < CCV_NNC_MAX_DIM(2); i++)
1255 cmd.info.size.dim[i] = self->kdim[i];
1256 memcpy(cmd.info.convolution_transpose.dilation, self->dilation, sizeof(self->dilation));
1257 ccv_nnc_tensor_param_t output_params;
1258 // Dilate weight size based on the dilation factor.
1259 for (i = 0; i < CCV_NNC_MAX_DIM(2); i++)
1260 weights_params.dim[i + hw] = (self->kdim[i] - 1) * ccv_max(self->dilation[i], 1)({ typeof (self->dilation[i]) _a = (self->dilation[i]);
typeof (1) _b = (1); (_a > _b) ? _a : _b; })
+ 1;
1261 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
1262 params,
1263 weights_params,
1264 bias_params,
1265 }, 3, self->hint, &output_params, 1);
1266 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1267 ccv_nnc_graph_exec_symbol_t convolution_transpose;
1268 if (self->no_bias)
1269 convolution_transpose = ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0], self->weights)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->weights
}, (1 +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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "convolution_transpose");
1270 else {
1271 if (!self->bias.graph)
1272 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
1273 convolution_transpose = ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0], self->weights, self->bias)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->weights
, self->bias}, (1 +1 +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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "convolution_transpose");
1274 }
1275 ccv_nnc_graph_exec_symbol_set_hint(graph, convolution_transpose, self->hint);
1276 outputs[0] = output;
1277}
1278
1279static void _ccv_cnnp_convolution_transpose_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
1280{
1281 ccv_cnnp_model_convolution_transpose_t* const self = (ccv_cnnp_model_convolution_transpose_t*)super;
1282 const ccv_nnc_tensor_param_t weight_params = ccv_nnc_tensor_symbol_params(graph, self->weights);
1283 const int n = ccv_max(ccv_nnc_tensor_get_n(weight_params), 1)({ typeof (ccv_nnc_tensor_get_n(weight_params)) _a = (ccv_nnc_tensor_get_n
(weight_params)); typeof (1) _b = (1); (_a > _b) ? _a : _b
; })
;
1284 const int count = ccv_nnc_tensor_count(weight_params);
1285 const float std = sqrtf(2) / sqrtf(count / n);
1286 const float bound = sqrtf(3) * std;
1287 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(-bound, bound)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={-bound, bound}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->weights);
1288 if (self->bias.graph)
1289 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->bias);
1290}
1291
1292static void _ccv_cnnp_convolution_transpose_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
1293{
1294 ccv_cnnp_model_convolution_transpose_t* const self = (ccv_cnnp_model_convolution_transpose_t*)super;
1295 add_to_array(parameters, self->weights, is_trainable);
1296 if (self->bias.graph)
1297 add_to_array(parameters, self->bias, is_trainable);
1298}
1299
1300static ccv_cnnp_model_t* _ccv_cnnp_convolution_transpose_copy(const ccv_cnnp_model_t* const super, void* const context);
1301
1302static const ccv_cnnp_model_vtab_t ccv_cnnp_convolution_transpose_isa = {
1303 .build = _ccv_cnnp_convolution_transpose_build,
1304 .init_states = _ccv_cnnp_convolution_transpose_init_states,
1305 .add_to_parameter = _ccv_cnnp_convolution_transpose_add_to_parameter,
1306 .copy = _ccv_cnnp_convolution_transpose_copy,
1307};
1308
1309ccv_cnnp_model_t* ccv_cnnp_convolution_transpose(const int groups, const int filters, const int kdim[CCV_NNC_MAX_DIM_ALLOC(12)], const int dilation[CCV_NNC_MAX_DIM_ALLOC(12)], const int output_padding, const int no_bias, ccv_nnc_hint_t hint, const int format, const int is_trainable, const char* const name)
1310{
1311 ccv_cnnp_model_convolution_transpose_t* const model_convolution_transpose = (ccv_cnnp_model_convolution_transpose_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_convolution_transpose_t));
1312 model_convolution_transpose->super.isa = &ccv_cnnp_convolution_transpose_isa;
1313 model_convolution_transpose->super.input_size = 1;
1314 model_convolution_transpose->super.outputs = &model_convolution_transpose->output;
1315 model_convolution_transpose->super.output_size = 1;
1316 model_convolution_transpose->super.is_trainable = is_trainable;
1317 ccv_cnnp_model_copy_name(&model_convolution_transpose->super, name);
1318 model_convolution_transpose->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
1319 model_convolution_transpose->weights.graph = 0;
1320 model_convolution_transpose->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
1321 model_convolution_transpose->bias.graph = 0;
1322 model_convolution_transpose->groups = groups;
1323 model_convolution_transpose->filters = filters;
1324 memcpy(model_convolution_transpose->kdim, kdim, sizeof(model_convolution_transpose->kdim));
1325 memcpy(model_convolution_transpose->dilation, dilation, sizeof(model_convolution_transpose->dilation));
1326 model_convolution_transpose->output_padding = output_padding;
1327 model_convolution_transpose->no_bias = no_bias;
1328 model_convolution_transpose->hint = hint;
1329 model_convolution_transpose->format = format;
1330 return (ccv_cnnp_model_t*)model_convolution_transpose;
1331}
1332
1333static ccv_cnnp_model_t* _ccv_cnnp_convolution_transpose_copy(const ccv_cnnp_model_t* const super, void* const context)
1334{
1335 ccv_cnnp_model_convolution_transpose_t* const self = (ccv_cnnp_model_convolution_transpose_t*)super;
1336 return ccv_cnnp_convolution_transpose(self->groups, self->filters, self->kdim, self->dilation, self->output_padding, self->no_bias, self->hint, self->format, self->super.is_trainable, self->super.name);
1337}
1338
1339// MARK - Dense Layer
1340
1341typedef struct {
1342 ccv_cnnp_model_t super;
1343 ccv_nnc_tensor_symbol_t output;
1344 ccv_nnc_tensor_symbol_t weights;
1345 ccv_nnc_tensor_symbol_t bias;
1346 int count;
1347 int no_bias;
1348} ccv_cnnp_model_dense_t;
1349
1350static void _ccv_cnnp_dense_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1351{
1352 ccv_cnnp_model_dense_t* const self = (ccv_cnnp_model_dense_t*)super;
1353 PRINT(CCV_CLI_VERBOSE, "[cnnp_dense_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_dense_build] -\n"); fflush(stdout); } } while
(0)
;
1354 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1354, __extension__ __PRETTY_FUNCTION__); }))
;
1355 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1355, __extension__ __PRETTY_FUNCTION__
); }))
;
1356 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1357 ccv_nnc_tensor_param_t weights_params = params;
1358 memset(weights_params.dim, 0, sizeof(weights_params.dim));
1359 weights_params.dim[0] = self->count;
1360 weights_params.dim[1] = params.dim[ccv_nnc_tensor_nd(params.dim) - 1];
1361 if (!self->weights.graph)
1362 self->weights = ccv_nnc_tensor_symbol_new(graph, weights_params, "weights");
1363 assert(self->weights.graph == graph)((void) sizeof ((self->weights.graph == graph) ? 1 : 0), __extension__
({ if (self->weights.graph == graph) ; else __assert_fail
("self->weights.graph == graph", "ccv_cnnp_model_addons.c"
, 1363, __extension__ __PRETTY_FUNCTION__); }))
;
1364 ccv_nnc_tensor_param_t bias_params = params;
1365 memset(bias_params.dim, 0, sizeof(bias_params.dim));
1366 bias_params.dim[0] = self->count;
1367 ccv_nnc_cmd_t cmd = {0};
1368 cmd.cmd = CCV_NNC_GEMM_FORWARD;
1369 cmd.info.blas.a[0] = 1;
1370 cmd.info.blas.a[1] = 1;
1371 cmd.info.blas.transpose_b[0] = 0;
1372 cmd.info.blas.transpose_b[1] = 1;
1373 ccv_nnc_tensor_param_t output_params;
1374 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
1375 params,
1376 weights_params,
1377 bias_params,
1378 }, 3, ccv_nnc_no_hint, &output_params, 1);
1379 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1380 if (self->no_bias)
1381 ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0], self->weights)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->weights
}, (1 +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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "dense");
1382 else {
1383 if (!self->bias.graph)
1384 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
1385 ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0], self->weights, self->bias)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->weights
, self->bias}, (1 +1 +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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "dense");
1386 }
1387 outputs[0] = output;
1388}
1389
1390static void _ccv_cnnp_dense_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
1391{
1392 ccv_cnnp_model_dense_t* const self = (ccv_cnnp_model_dense_t*)super;
1393 const ccv_nnc_tensor_param_t weight_params = ccv_nnc_tensor_symbol_params(graph, self->weights);
1394 const int c = weight_params.dim[1];
1395 const float std = sqrtf(2) / sqrtf(c);
1396 const float bound = sqrtf(3) * std;
1397 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(-bound, bound)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={-bound, bound}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->weights);
1398 if (self->bias.graph)
1399 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->bias);
1400}
1401
1402static void _ccv_cnnp_dense_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
1403{
1404 ccv_cnnp_model_dense_t* const self = (ccv_cnnp_model_dense_t*)super;
1405 add_to_array(parameters, self->weights, is_trainable);
1406 if (self->bias.graph)
1407 add_to_array(parameters, self->bias, is_trainable);
1408}
1409
1410static ccv_cnnp_model_t* _ccv_cnnp_dense_copy(const ccv_cnnp_model_t* const super, void* const context);
1411
1412static const ccv_cnnp_model_vtab_t ccv_cnnp_dense_isa = {
1413 .build = _ccv_cnnp_dense_build,
1414 .init_states = _ccv_cnnp_dense_init_states,
1415 .add_to_parameter = _ccv_cnnp_dense_add_to_parameter,
1416 .copy = _ccv_cnnp_dense_copy,
1417};
1418
1419ccv_cnnp_model_t* ccv_cnnp_dense(const int count, const int no_bias, const int is_trainable, const char* const name)
1420{
1421 ccv_cnnp_model_dense_t* const model_dense = (ccv_cnnp_model_dense_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_dense_t));
1422 model_dense->super.isa = &ccv_cnnp_dense_isa;
1423 model_dense->super.input_size = 1;
1424 model_dense->super.outputs = &model_dense->output;
1425 model_dense->super.output_size = 1;
1426 model_dense->super.is_trainable = is_trainable;
1427 ccv_cnnp_model_copy_name(&model_dense->super, name);
1428 model_dense->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
1429 model_dense->weights.graph = 0;
1430 model_dense->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
1431 model_dense->bias.graph = 0;
1432 model_dense->count = count;
1433 model_dense->no_bias = no_bias;
1434 return (ccv_cnnp_model_t*)model_dense;
1435}
1436
1437static ccv_cnnp_model_t* _ccv_cnnp_dense_copy(const ccv_cnnp_model_t* const super, void* const context)
1438{
1439 const ccv_cnnp_model_dense_t* const self = (const ccv_cnnp_model_dense_t*)super;
1440 return ccv_cnnp_dense(self->count, self->no_bias, self->super.is_trainable, self->super.name);
1441}
1442
1443// MARK - Pool Layers
1444
1445typedef struct {
1446 ccv_cnnp_model_t super;
1447 ccv_nnc_tensor_symbol_t output;
1448 int kdim[CCV_NNC_MAX_DIM_ALLOC(12)];
1449 ccv_nnc_hint_t hint;
1450} ccv_cnnp_model_pool_t;
1451
1452static void _ccv_cnnp_max_pool_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1453{
1454 ccv_cnnp_model_pool_t* const self = (ccv_cnnp_model_pool_t*)super;
1455 PRINT(CCV_CLI_VERBOSE, "[cnnp_max_pool_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_max_pool_build] -\n"); fflush(stdout); } } while
(0)
;
1456 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1456, __extension__ __PRETTY_FUNCTION__); }))
;
1457 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1457, __extension__ __PRETTY_FUNCTION__
); }))
;
1458 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1459 const int hw = ccv_nnc_tensor_hw(params, ccv_nnc_tensor_nd(params.dim));
1460 ccv_nnc_cmd_t cmd;
1461 if (hw >= 0 && self->kdim[0] == 0 && self->kdim[1] == 0)
1462 cmd = CMD_MAX_POOL_FORWARD(params.dim[hw], params.dim[hw + 1])ccv_nnc_cmd(CCV_NNC_MAX_POOL_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={params.dim[hw], params.dim[hw + 1],1}}}), 0)
;
1463 else
1464 cmd = CMD_MAX_POOL_FORWARD(self->kdim[0], self->kdim[1])ccv_nnc_cmd(CCV_NNC_MAX_POOL_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={self->kdim[0], self->kdim[1],1}}}), 0)
;
1465 ccv_nnc_tensor_param_t output_params;
1466 ccv_nnc_hint_tensor_auto(cmd, &params, 1, self->hint, &output_params, 1);
1467 const ccv_nnc_tensor_symbol_t pool_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1468 const ccv_nnc_graph_exec_symbol_t exec = ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(pool_output)(const ccv_nnc_tensor_symbol_t []){pool_output}, (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)
, "max_pool");
1469 ccv_nnc_graph_exec_symbol_set_hint(graph, exec, self->hint);
1470 outputs[0] = pool_output;
1471}
1472
1473static ccv_cnnp_model_t* _ccv_cnnp_max_pool_copy(const ccv_cnnp_model_t* const super, void* const context);
1474
1475static const ccv_cnnp_model_vtab_t ccv_cnnp_max_pool_isa = {
1476 .build = _ccv_cnnp_max_pool_build,
1477 .copy = _ccv_cnnp_max_pool_copy,
1478};
1479
1480ccv_cnnp_model_t* ccv_cnnp_max_pool(const int kdim[CCV_NNC_MAX_DIM_ALLOC(12)], const ccv_nnc_hint_t hint, const char* const name)
1481{
1482 ccv_cnnp_model_pool_t* const model_pool = (ccv_cnnp_model_pool_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_pool_t));
1483 model_pool->super.isa = &ccv_cnnp_max_pool_isa;
1484 model_pool->super.input_size = 1;
1485 model_pool->super.outputs = &model_pool->output;
1486 model_pool->super.output_size = 1;
1487 ccv_cnnp_model_copy_name(&model_pool->super, name);
1488 memcpy(model_pool->kdim, kdim, sizeof(model_pool->kdim));
1489 model_pool->hint = hint;
1490 return (ccv_cnnp_model_t*)model_pool;
1491}
1492
1493static ccv_cnnp_model_t* _ccv_cnnp_max_pool_copy(const ccv_cnnp_model_t* const super, void* const context)
1494{
1495 const ccv_cnnp_model_pool_t* const self = (const ccv_cnnp_model_pool_t*)super;
1496 return ccv_cnnp_max_pool(self->kdim, self->hint, self->super.name);
1497}
1498
1499static void _ccv_cnnp_average_pool_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1500{
1501 ccv_cnnp_model_pool_t* const self = (ccv_cnnp_model_pool_t*)super;
1502 PRINT(CCV_CLI_VERBOSE, "[cnnp_average_pool_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_average_pool_build] -\n"); fflush(stdout); }
} while (0)
;
1503 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1503, __extension__ __PRETTY_FUNCTION__); }))
;
1504 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1504, __extension__ __PRETTY_FUNCTION__
); }))
;
1505 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1506 const int hw = ccv_nnc_tensor_hw(params, ccv_nnc_tensor_nd(params.dim));
1507 ccv_nnc_cmd_t cmd;
1508 if (hw >= 0 && self->kdim[0] == 0 && self->kdim[1] == 0)
1509 cmd = CMD_AVERAGE_POOL_FORWARD(params.dim[hw], params.dim[hw + 1])ccv_nnc_cmd(CCV_NNC_AVERAGE_POOL_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={params.dim[hw], params.dim[hw + 1],1}}}), 0)
;
1510 else
1511 cmd = CMD_AVERAGE_POOL_FORWARD(self->kdim[0], self->kdim[1])ccv_nnc_cmd(CCV_NNC_AVERAGE_POOL_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={self->kdim[0], self->kdim[1],1}}}), 0)
;
1512 ccv_nnc_tensor_param_t output_params;
1513 ccv_nnc_hint_tensor_auto(cmd, &params, 1, self->hint, &output_params, 1);
1514 const ccv_nnc_tensor_symbol_t pool_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1515 const ccv_nnc_graph_exec_symbol_t exec = ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(pool_output)(const ccv_nnc_tensor_symbol_t []){pool_output}, (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)
, "average_pool");
1516 ccv_nnc_graph_exec_symbol_set_hint(graph, exec, self->hint);
1517 outputs[0] = pool_output;
1518}
1519
1520static ccv_cnnp_model_t* _ccv_cnnp_average_pool_copy(const ccv_cnnp_model_t* const super, void* const context);
1521
1522static const ccv_cnnp_model_vtab_t ccv_cnnp_average_pool_isa = {
1523 .build = _ccv_cnnp_average_pool_build,
1524 .copy = _ccv_cnnp_average_pool_copy,
1525};
1526
1527ccv_cnnp_model_t* ccv_cnnp_average_pool(const int kdim[CCV_NNC_MAX_DIM_ALLOC(12)], const ccv_nnc_hint_t hint, const char* const name)
1528{
1529 ccv_cnnp_model_pool_t* const model_pool = (ccv_cnnp_model_pool_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_pool_t));
1530 model_pool->super.isa = &ccv_cnnp_average_pool_isa;
1531 model_pool->super.input_size = 1;
1532 model_pool->super.outputs = &model_pool->output;
1533 model_pool->super.output_size = 1;
1534 ccv_cnnp_model_copy_name(&model_pool->super, name);
1535 memcpy(model_pool->kdim, kdim, sizeof(model_pool->kdim));
1536 model_pool->hint = hint;
1537 return (ccv_cnnp_model_t*)model_pool;
1538}
1539
1540static ccv_cnnp_model_t* _ccv_cnnp_average_pool_copy(const ccv_cnnp_model_t* const super, void* const context)
1541{
1542 const ccv_cnnp_model_pool_t* const self = (const ccv_cnnp_model_pool_t*)super;
1543 return ccv_cnnp_average_pool(self->kdim, self->hint, self->super.name);
1544}
1545
1546// MARK - RELU Layer
1547
1548typedef struct {
1549 ccv_cnnp_model_t super;
1550 ccv_nnc_tensor_symbol_t output;
1551} ccv_cnnp_model_relu_t;
1552
1553static void _ccv_cnnp_relu_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1554{
1555 PRINT(CCV_CLI_VERBOSE, "[cnnp_relu_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_relu_build] -\n"); fflush(stdout); } } while
(0)
;
1556 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1556, __extension__ __PRETTY_FUNCTION__); }))
;
1557 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1557, __extension__ __PRETTY_FUNCTION__
); }))
;
1558 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1559 ccv_nnc_tensor_param_t output_params;
1560 const ccv_nnc_cmd_t relu = CMD_RELU_FORWARD()ccv_nnc_cmd(CCV_NNC_RELU_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1561 ccv_nnc_hint_tensor_auto(relu, (ccv_nnc_tensor_param_t []){
1562 params,
1563 }, 1, ccv_nnc_no_hint, &output_params, 1);
1564 const ccv_nnc_tensor_symbol_t relu_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1565 ccv_nnc_graph_exec_symbol_new(graph, relu, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(relu_output)(const ccv_nnc_tensor_symbol_t []){relu_output}, (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)
, "relu");
1566 outputs[0] = relu_output;
1567}
1568
1569static ccv_cnnp_model_t* _ccv_cnnp_relu_copy(const ccv_cnnp_model_t* const self, void* const context);
1570
1571static const ccv_cnnp_model_vtab_t ccv_cnnp_relu_isa = {
1572 .build = _ccv_cnnp_relu_build,
1573 .copy = _ccv_cnnp_relu_copy,
1574};
1575
1576ccv_cnnp_model_t* ccv_cnnp_relu(const char* const name)
1577{
1578 ccv_cnnp_model_relu_t* const model_relu = (ccv_cnnp_model_relu_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_relu_t));
1579 model_relu->super.isa = &ccv_cnnp_relu_isa;
1580 model_relu->super.input_size = 1;
1581 model_relu->super.outputs = &model_relu->output;
1582 model_relu->super.output_size = 1;
1583 ccv_cnnp_model_copy_name(&model_relu->super, name);
1584 return (ccv_cnnp_model_t*)model_relu;
1585}
1586
1587static ccv_cnnp_model_t* _ccv_cnnp_relu_copy(const ccv_cnnp_model_t* const self, void* const context)
1588{
1589 return ccv_cnnp_relu(self->name);
1590}
1591
1592// MARK - Sigmoid Layer
1593
1594typedef struct {
1595 ccv_cnnp_model_t super;
1596 ccv_nnc_tensor_symbol_t output;
1597} ccv_cnnp_model_sigmoid_t;
1598
1599static void _ccv_cnnp_sigmoid_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1600{
1601 PRINT(CCV_CLI_VERBOSE, "[cnnp_sigmoid_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_sigmoid_build] -\n"); fflush(stdout); } } while
(0)
;
1602 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1602, __extension__ __PRETTY_FUNCTION__); }))
;
1603 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1603, __extension__ __PRETTY_FUNCTION__
); }))
;
1604 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1605 ccv_nnc_tensor_param_t output_params;
1606 const ccv_nnc_cmd_t sigmoid = CMD_SIGMOID_FORWARD()ccv_nnc_cmd(CCV_NNC_SIGMOID_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1607 ccv_nnc_hint_tensor_auto(sigmoid, (ccv_nnc_tensor_param_t []){
1608 params,
1609 }, 1, ccv_nnc_no_hint, &output_params, 1);
1610 const ccv_nnc_tensor_symbol_t sigmoid_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1611 ccv_nnc_graph_exec_symbol_new(graph, sigmoid, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(sigmoid_output)(const ccv_nnc_tensor_symbol_t []){sigmoid_output}, (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
)
, "sigmoid");
1612 outputs[0] = sigmoid_output;
1613}
1614
1615static ccv_cnnp_model_t* _ccv_cnnp_sigmoid_copy(const ccv_cnnp_model_t* const self, void* const context);
1616
1617static const ccv_cnnp_model_vtab_t ccv_cnnp_sigmoid_isa = {
1618 .build = _ccv_cnnp_sigmoid_build,
1619 .copy = _ccv_cnnp_sigmoid_copy,
1620};
1621
1622ccv_cnnp_model_t* ccv_cnnp_sigmoid(const char* const name)
1623{
1624 ccv_cnnp_model_sigmoid_t* const model_sigmoid = (ccv_cnnp_model_sigmoid_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_sigmoid_t));
1625 model_sigmoid->super.isa = &ccv_cnnp_sigmoid_isa;
1626 model_sigmoid->super.input_size = 1;
1627 model_sigmoid->super.outputs = &model_sigmoid->output;
1628 model_sigmoid->super.output_size = 1;
1629 ccv_cnnp_model_copy_name(&model_sigmoid->super, name);
1630 return (ccv_cnnp_model_t*)model_sigmoid;
1631}
1632
1633static ccv_cnnp_model_t* _ccv_cnnp_sigmoid_copy(const ccv_cnnp_model_t* const self, void* const context)
1634{
1635 return ccv_cnnp_sigmoid(self->name);
1636}
1637
1638// MARK - Tanh Layer
1639
1640typedef struct {
1641 ccv_cnnp_model_t super;
1642 ccv_nnc_tensor_symbol_t output;
1643} ccv_cnnp_model_tanh_t;
1644
1645static void _ccv_cnnp_tanh_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1646{
1647 PRINT(CCV_CLI_VERBOSE, "[cnnp_tanh_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_tanh_build] -\n"); fflush(stdout); } } while
(0)
;
1648 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1648, __extension__ __PRETTY_FUNCTION__); }))
;
1649 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1649, __extension__ __PRETTY_FUNCTION__
); }))
;
1650 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1651 ccv_nnc_tensor_param_t output_params;
1652 const ccv_nnc_cmd_t tanh = CMD_TANH_FORWARD()ccv_nnc_cmd(CCV_NNC_TANH_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1653 ccv_nnc_hint_tensor_auto(tanh, (ccv_nnc_tensor_param_t []){
1654 params,
1655 }, 1, ccv_nnc_no_hint, &output_params, 1);
1656 const ccv_nnc_tensor_symbol_t tanh_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1657 ccv_nnc_graph_exec_symbol_new(graph, tanh, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(tanh_output)(const ccv_nnc_tensor_symbol_t []){tanh_output}, (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)
, "tanh");
1658 outputs[0] = tanh_output;
1659}
1660
1661static ccv_cnnp_model_t* _ccv_cnnp_tanh_copy(const ccv_cnnp_model_t* const self, void* const context);
1662
1663static const ccv_cnnp_model_vtab_t ccv_cnnp_tanh_isa = {
1664 .build = _ccv_cnnp_tanh_build,
1665 .copy = _ccv_cnnp_tanh_copy,
1666};
1667
1668ccv_cnnp_model_t* ccv_cnnp_tanh(const char* const name)
1669{
1670 ccv_cnnp_model_tanh_t* const model_tanh = (ccv_cnnp_model_tanh_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_tanh_t));
1671 model_tanh->super.isa = &ccv_cnnp_tanh_isa;
1672 model_tanh->super.input_size = 1;
1673 model_tanh->super.outputs = &model_tanh->output;
1674 model_tanh->super.output_size = 1;
1675 ccv_cnnp_model_copy_name(&model_tanh->super, name);
1676 return (ccv_cnnp_model_t*)model_tanh;
1677}
1678
1679static ccv_cnnp_model_t* _ccv_cnnp_tanh_copy(const ccv_cnnp_model_t* const self, void* const context)
1680{
1681 return ccv_cnnp_tanh(self->name);
1682}
1683
1684// MARK - Swish Layer
1685
1686typedef struct {
1687 ccv_cnnp_model_t super;
1688 ccv_nnc_tensor_symbol_t output;
1689} ccv_cnnp_model_swish_t;
1690
1691static void _ccv_cnnp_swish_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1692{
1693 PRINT(CCV_CLI_VERBOSE, "[cnnp_swish_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_swish_build] -\n"); fflush(stdout); } } while
(0)
;
1694 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1694, __extension__ __PRETTY_FUNCTION__); }))
;
1695 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1695, __extension__ __PRETTY_FUNCTION__
); }))
;
1696 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1697 ccv_nnc_tensor_param_t output_params;
1698 const ccv_nnc_cmd_t swish = CMD_SWISH_FORWARD()ccv_nnc_cmd(CCV_NNC_SWISH_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1699 ccv_nnc_hint_tensor_auto(swish, (ccv_nnc_tensor_param_t []){
1700 params,
1701 }, 1, ccv_nnc_no_hint, &output_params, 1);
1702 const ccv_nnc_tensor_symbol_t swish_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1703 ccv_nnc_graph_exec_symbol_new(graph, swish, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(swish_output)(const ccv_nnc_tensor_symbol_t []){swish_output}, (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)
, "swish");
1704 outputs[0] = swish_output;
1705}
1706
1707static ccv_cnnp_model_t* _ccv_cnnp_swish_copy(const ccv_cnnp_model_t* const self, void* const context);
1708
1709static const ccv_cnnp_model_vtab_t ccv_cnnp_swish_isa = {
1710 .build = _ccv_cnnp_swish_build,
1711 .copy = _ccv_cnnp_swish_copy,
1712};
1713
1714ccv_cnnp_model_t* ccv_cnnp_swish(const char* const name)
1715{
1716 ccv_cnnp_model_swish_t* const model_swish = (ccv_cnnp_model_swish_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_swish_t));
1717 model_swish->super.isa = &ccv_cnnp_swish_isa;
1718 model_swish->super.input_size = 1;
1719 model_swish->super.outputs = &model_swish->output;
1720 model_swish->super.output_size = 1;
1721 ccv_cnnp_model_copy_name(&model_swish->super, name);
1722 return (ccv_cnnp_model_t*)model_swish;
1723}
1724
1725static ccv_cnnp_model_t* _ccv_cnnp_swish_copy(const ccv_cnnp_model_t* const self, void* const context)
1726{
1727 return ccv_cnnp_swish(self->name);
1728}
1729
1730// MARK - GELU Layer
1731
1732typedef struct {
1733 ccv_cnnp_model_t super;
1734 ccv_nnc_tensor_symbol_t output;
1735 int tanh;
1736} ccv_cnnp_model_gelu_t;
1737
1738static void _ccv_cnnp_gelu_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1739{
1740 PRINT(CCV_CLI_VERBOSE, "[cnnp_gelu_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_gelu_build] -\n"); fflush(stdout); } } while
(0)
;
1741 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1741, __extension__ __PRETTY_FUNCTION__); }))
;
1742 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1742, __extension__ __PRETTY_FUNCTION__
); }))
;
1743 ccv_cnnp_model_gelu_t* const self = (ccv_cnnp_model_gelu_t*)super;
1744 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1745 ccv_nnc_tensor_param_t output_params;
1746 const ccv_nnc_cmd_t gelu = CMD_GELU_FORWARD(self->tanh)ccv_nnc_cmd(CCV_NNC_GELU_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.gelu={.tanh=self->tanh}}, 0)
;
1747 ccv_nnc_hint_tensor_auto(gelu, (ccv_nnc_tensor_param_t []){
1748 params,
1749 }, 1, ccv_nnc_no_hint, &output_params, 1);
1750 const ccv_nnc_tensor_symbol_t gelu_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1751 ccv_nnc_graph_exec_symbol_new(graph, gelu, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(gelu_output)(const ccv_nnc_tensor_symbol_t []){gelu_output}, (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)
, "gelu");
1752 outputs[0] = gelu_output;
1753}
1754
1755static ccv_cnnp_model_t* _ccv_cnnp_gelu_copy(const ccv_cnnp_model_t* const self, void* const context);
1756
1757static const ccv_cnnp_model_vtab_t ccv_cnnp_gelu_isa = {
1758 .build = _ccv_cnnp_gelu_build,
1759 .copy = _ccv_cnnp_gelu_copy,
1760};
1761
1762ccv_cnnp_model_t* ccv_cnnp_gelu(const int tanh, const char* const name)
1763{
1764 ccv_cnnp_model_gelu_t* const model_gelu = (ccv_cnnp_model_gelu_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_gelu_t));
1765 model_gelu->super.isa = &ccv_cnnp_gelu_isa;
1766 model_gelu->super.input_size = 1;
1767 model_gelu->super.outputs = &model_gelu->output;
1768 model_gelu->super.output_size = 1;
1769 model_gelu->tanh = tanh;
1770 ccv_cnnp_model_copy_name(&model_gelu->super, name);
1771 return (ccv_cnnp_model_t*)model_gelu;
1772}
1773
1774static ccv_cnnp_model_t* _ccv_cnnp_gelu_copy(const ccv_cnnp_model_t* const super, void* const context)
1775{
1776 ccv_cnnp_model_gelu_t* const self = (ccv_cnnp_model_gelu_t*)super;
1777 return ccv_cnnp_gelu(self->tanh, self->super.name);
1778}
1779
1780// MARK - Leaky ReLU Layer
1781
1782typedef struct {
1783 ccv_cnnp_model_t super;
1784 ccv_nnc_tensor_symbol_t output;
1785 float negative_slope;
1786} ccv_cnnp_model_leaky_relu_t;
1787
1788static void _ccv_cnnp_leaky_relu_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1789{
1790 PRINT(CCV_CLI_VERBOSE, "[cnnp_leaky_relu_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_leaky_relu_build] -\n"); fflush(stdout); } }
while (0)
;
1791 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1791, __extension__ __PRETTY_FUNCTION__); }))
;
1792 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1792, __extension__ __PRETTY_FUNCTION__
); }))
;
1793 ccv_cnnp_model_leaky_relu_t* const self = (ccv_cnnp_model_leaky_relu_t*)super;
1794 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1795 ccv_nnc_tensor_param_t output_params;
1796 const ccv_nnc_cmd_t leaky_relu = CMD_LEAKY_RELU_FORWARD(self->negative_slope)ccv_nnc_cmd(CCV_NNC_LEAKY_RELU_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.leaky_relu={.negative_slope=self->
negative_slope}}, 0)
;
1797 ccv_nnc_hint_tensor_auto(leaky_relu, (ccv_nnc_tensor_param_t []){
1798 params,
1799 }, 1, ccv_nnc_no_hint, &output_params, 1);
1800 const ccv_nnc_tensor_symbol_t leaky_relu_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1801 ccv_nnc_graph_exec_symbol_new(graph, leaky_relu, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(leaky_relu_output)(const ccv_nnc_tensor_symbol_t []){leaky_relu_output}, (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)
, "leaky_relu");
1802 outputs[0] = leaky_relu_output;
1803}
1804
1805static ccv_cnnp_model_t* _ccv_cnnp_leaky_relu_copy(const ccv_cnnp_model_t* const self, void* const context);
1806
1807static const ccv_cnnp_model_vtab_t ccv_cnnp_leaky_relu_isa = {
1808 .build = _ccv_cnnp_leaky_relu_build,
1809 .copy = _ccv_cnnp_leaky_relu_copy,
1810};
1811
1812ccv_cnnp_model_t* ccv_cnnp_leaky_relu(const float negative_slope, const char* const name)
1813{
1814 ccv_cnnp_model_leaky_relu_t* const model_leaky_relu = (ccv_cnnp_model_leaky_relu_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_leaky_relu_t));
1815 model_leaky_relu->super.isa = &ccv_cnnp_leaky_relu_isa;
1816 model_leaky_relu->super.input_size = 1;
1817 model_leaky_relu->super.outputs = &model_leaky_relu->output;
1818 model_leaky_relu->super.output_size = 1;
1819 model_leaky_relu->negative_slope = negative_slope;
1820 ccv_cnnp_model_copy_name(&model_leaky_relu->super, name);
1821 return (ccv_cnnp_model_t*)model_leaky_relu;
1822}
1823
1824static ccv_cnnp_model_t* _ccv_cnnp_leaky_relu_copy(const ccv_cnnp_model_t* const super, void* const context)
1825{
1826 ccv_cnnp_model_leaky_relu_t* const self = (ccv_cnnp_model_leaky_relu_t*)super;
1827 return ccv_cnnp_leaky_relu(self->negative_slope, self->super.name);
1828}
1829
1830// MARK - Softmax Layer
1831
1832typedef struct {
1833 ccv_cnnp_model_t super;
1834 ccv_nnc_tensor_symbol_t output;
1835} ccv_cnnp_model_softmax_t;
1836
1837static void _ccv_cnnp_softmax_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1838{
1839 PRINT(CCV_CLI_VERBOSE, "[cnnp_softmax_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_softmax_build] -\n"); fflush(stdout); } } while
(0)
;
1840 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1840, __extension__ __PRETTY_FUNCTION__); }))
;
1841 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1841, __extension__ __PRETTY_FUNCTION__
); }))
;
1842 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1843 ccv_nnc_tensor_param_t output_params;
1844 const ccv_nnc_cmd_t softmax = CMD_SOFTMAX_FORWARD()ccv_nnc_cmd(CCV_NNC_SOFTMAX_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1845 ccv_nnc_hint_tensor_auto(softmax, (ccv_nnc_tensor_param_t []){
1846 params,
1847 }, 1, ccv_nnc_no_hint, &output_params, 1);
1848 const ccv_nnc_tensor_symbol_t softmax_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1849 ccv_nnc_graph_exec_symbol_new(graph, softmax, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(softmax_output)(const ccv_nnc_tensor_symbol_t []){softmax_output}, (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
)
, "softmax");
1850 outputs[0] = softmax_output;
1851}
1852
1853static ccv_cnnp_model_t* _ccv_cnnp_softmax_copy(const ccv_cnnp_model_t* const self, void* const context);
1854
1855static const ccv_cnnp_model_vtab_t ccv_cnnp_softmax_isa = {
1856 .build = _ccv_cnnp_softmax_build,
1857 .copy = _ccv_cnnp_softmax_copy,
1858};
1859
1860ccv_cnnp_model_t* ccv_cnnp_softmax(const char* const name)
1861{
1862 ccv_cnnp_model_softmax_t* const model_softmax = (ccv_cnnp_model_softmax_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_softmax_t));
1863 model_softmax->super.isa = &ccv_cnnp_softmax_isa;
1864 model_softmax->super.input_size = 1;
1865 model_softmax->super.outputs = &model_softmax->output;
1866 model_softmax->super.output_size = 1;
1867 ccv_cnnp_model_copy_name(&model_softmax->super, name);
1868 return (ccv_cnnp_model_t*)model_softmax;
1869}
1870
1871static ccv_cnnp_model_t* _ccv_cnnp_softmax_copy(const ccv_cnnp_model_t* const self, void* const context)
1872{
1873 return ccv_cnnp_softmax(self->name);
1874}
1875
1876// MARK - Add Layer
1877
1878typedef struct {
1879 ccv_cnnp_model_t super;
1880 float p;
1881 float q;
1882 ccv_nnc_tensor_symbol_t output;
1883} ccv_cnnp_model_add_t;
1884
1885static void _ccv_cnnp_add_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1886{
1887 PRINT(CCV_CLI_VERBOSE, "[cnnp_add_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_add_build] -\n"); fflush(stdout); } } while (
0)
;
1888 const ccv_cnnp_model_add_t* const self = (const ccv_cnnp_model_add_t*)super;
1889 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 1889, __extension__ __PRETTY_FUNCTION__); }))
;
1890 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1890, __extension__ __PRETTY_FUNCTION__
); }))
;
1891 ccv_nnc_tensor_param_t input_params[2];
1892 int i;
1893 for (i = 0; i < 2; i++)
1894 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
1895 ccv_nnc_tensor_param_t output_params;
1896 const ccv_nnc_cmd_t add = CMD_ADD_FORWARD(self->p, self->q)ccv_nnc_cmd(CCV_NNC_ADD_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={self->p, self->q}}}, 0)
;
1897 ccv_nnc_hint_tensor_auto(add, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
1898 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1899 ccv_nnc_graph_exec_symbol_new(graph, add, inputs, input_size, outputs, output_size, "add");
1900}
1901
1902static ccv_cnnp_model_t* _ccv_cnnp_add_copy(const ccv_cnnp_model_t* const self, void* const context);
1903
1904static const ccv_cnnp_model_vtab_t ccv_cnnp_add_isa = {
1905 .build = _ccv_cnnp_add_build,
1906 .copy = _ccv_cnnp_add_copy,
1907};
1908
1909ccv_cnnp_model_t* ccv_cnnp_add(const float p, const float q, const char* const name)
1910{
1911 ccv_cnnp_model_add_t* const model_add = (ccv_cnnp_model_add_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_add_t));
1912 model_add->super.isa = &ccv_cnnp_add_isa;
1913 model_add->super.input_size = 2;
1914 model_add->super.outputs = &model_add->output;
1915 model_add->super.output_size = 1;
1916 model_add->p = p;
1917 model_add->q = q;
1918 ccv_cnnp_model_copy_name(&model_add->super, name);
1919 return (ccv_cnnp_model_t*)model_add;
1920}
1921
1922static ccv_cnnp_model_t* _ccv_cnnp_add_copy(const ccv_cnnp_model_t* const super, void* const context)
1923{
1924 const ccv_cnnp_model_add_t* const self = (const ccv_cnnp_model_add_t*)super;
1925 return ccv_cnnp_add(self->p, self->q, self->super.name);
1926}
1927
1928// MARK - Mul Layer
1929
1930typedef struct {
1931 ccv_cnnp_model_t super;
1932 ccv_nnc_tensor_symbol_t output;
1933 float p;
1934} ccv_cnnp_model_mul_t;
1935
1936static void _ccv_cnnp_mul_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1937{
1938 PRINT(CCV_CLI_VERBOSE, "[cnnp_mul_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_mul_build] -\n"); fflush(stdout); } } while (
0)
;
1939 const ccv_cnnp_model_mul_t* const self = (const ccv_cnnp_model_mul_t*)super;
1940 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 1940, __extension__ __PRETTY_FUNCTION__); }))
;
1941 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1941, __extension__ __PRETTY_FUNCTION__
); }))
;
1942 ccv_nnc_tensor_param_t input_params[2];
1943 int i;
1944 for (i = 0; i < 2; i++)
1945 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
1946 ccv_nnc_tensor_param_t output_params;
1947 const ccv_nnc_cmd_t mul = CMD_MUL_FORWARD(self->p)ccv_nnc_cmd(CCV_NNC_MUL_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={self->p,}}}, 0)
;
1948 ccv_nnc_hint_tensor_auto(mul, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
1949 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1950 ccv_nnc_graph_exec_symbol_new(graph, mul, inputs, input_size, outputs, output_size, "mul");
1951}
1952
1953static ccv_cnnp_model_t* _ccv_cnnp_mul_copy(const ccv_cnnp_model_t* const self, void* const context);
1954
1955static const ccv_cnnp_model_vtab_t ccv_cnnp_mul_isa = {
1956 .build = _ccv_cnnp_mul_build,
1957 .copy = _ccv_cnnp_mul_copy,
1958};
1959
1960ccv_cnnp_model_t* ccv_cnnp_mul(const float p, const char* const name)
1961{
1962 ccv_cnnp_model_mul_t* const model_mul = (ccv_cnnp_model_mul_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_mul_t));
1963 model_mul->super.isa = &ccv_cnnp_mul_isa;
1964 model_mul->super.input_size = 2;
1965 model_mul->super.outputs = &model_mul->output;
1966 model_mul->super.output_size = 1;
1967 model_mul->p = p;
1968 ccv_cnnp_model_copy_name(&model_mul->super, name);
1969 return (ccv_cnnp_model_t*)model_mul;
1970}
1971
1972static ccv_cnnp_model_t* _ccv_cnnp_mul_copy(const ccv_cnnp_model_t* const super, void* const context)
1973{
1974 const ccv_cnnp_model_mul_t* const self = (const ccv_cnnp_model_mul_t*)super;
1975 return ccv_cnnp_mul(self->p, self->super.name);
1976}
1977
1978// MARK - Scalar Mul Layer
1979
1980typedef struct {
1981 ccv_cnnp_model_t super;
1982 ccv_nnc_tensor_symbol_t output;
1983 float a;
1984} ccv_cnnp_model_scalar_mul_t;
1985
1986static void _ccv_cnnp_scalar_mul_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
1987{
1988 PRINT(CCV_CLI_VERBOSE, "[cnnp_scalar_mul_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_scalar_mul_build] -\n"); fflush(stdout); } }
while (0)
;
1989 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 1989, __extension__ __PRETTY_FUNCTION__); }))
;
1990 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 1990, __extension__ __PRETTY_FUNCTION__
); }))
;
1991 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1992 ccv_nnc_tensor_param_t output_params;
1993 ccv_cnnp_model_scalar_mul_t* const self = (ccv_cnnp_model_scalar_mul_t*)super;
1994 const ccv_nnc_cmd_t scalar_mul = CMD_SCALAR_MUL_FORWARD(self->a)ccv_nnc_cmd(CCV_NNC_SCALAR_MUL_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={self->a,}}}, 0)
;
1995 ccv_nnc_hint_tensor_auto(scalar_mul, (ccv_nnc_tensor_param_t []){
1996 params,
1997 }, 1, ccv_nnc_no_hint, &output_params, 1);
1998 const ccv_nnc_tensor_symbol_t scalar_mul_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1999 ccv_nnc_graph_exec_symbol_new(graph, scalar_mul, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(scalar_mul_output)(const ccv_nnc_tensor_symbol_t []){scalar_mul_output}, (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)
, "scalar_mul");
2000 outputs[0] = scalar_mul_output;
2001}
2002
2003static ccv_cnnp_model_t* _ccv_cnnp_scalar_mul_copy(const ccv_cnnp_model_t* const super, void* const context);
2004
2005static const ccv_cnnp_model_vtab_t ccv_cnnp_scalar_mul_isa = {
2006 .build = _ccv_cnnp_scalar_mul_build,
2007 .copy = _ccv_cnnp_scalar_mul_copy,
2008};
2009
2010ccv_cnnp_model_t* ccv_cnnp_scalar_mul(const float a, const char* const name)
2011{
2012 ccv_cnnp_model_scalar_mul_t* const model_scalar_mul = (ccv_cnnp_model_scalar_mul_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_scalar_mul_t));
2013 model_scalar_mul->super.isa = &ccv_cnnp_scalar_mul_isa;
2014 model_scalar_mul->super.input_size = 1;
2015 model_scalar_mul->super.outputs = &model_scalar_mul->output;
2016 model_scalar_mul->super.output_size = 1;
2017 model_scalar_mul->a = a;
2018 ccv_cnnp_model_copy_name(&model_scalar_mul->super, name);
2019 return (ccv_cnnp_model_t*)model_scalar_mul;
2020}
2021
2022static ccv_cnnp_model_t* _ccv_cnnp_scalar_mul_copy(const ccv_cnnp_model_t* const super, void* const context)
2023{
2024 const ccv_cnnp_model_scalar_mul_t* const self = (const ccv_cnnp_model_scalar_mul_t*)super;
2025 return ccv_cnnp_scalar_mul(self->a, self->super.name);
2026}
2027
2028// MARK - Div Layer
2029
2030typedef struct {
2031 ccv_cnnp_model_t super;
2032 ccv_nnc_tensor_symbol_t output;
2033 int reciprocal;
2034} ccv_cnnp_model_div_t;
2035
2036static void _ccv_cnnp_div_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2037{
2038 const ccv_cnnp_model_div_t* const self = (const ccv_cnnp_model_div_t*)super;
2039 PRINT(CCV_CLI_VERBOSE, "[cnnp_div_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_div_build] -\n"); fflush(stdout); } } while (
0)
;
2040 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2040, __extension__ __PRETTY_FUNCTION__
); }))
;
2041 ccv_nnc_tensor_param_t input_params[2];
2042 int i;
2043 ccv_nnc_tensor_param_t output_params;
2044 const ccv_nnc_cmd_t div = CMD_EWDIV_FORWARD()ccv_nnc_cmd(CCV_NNC_EWDIV_FORWARD, 0, ccv_nnc_cmd_auto, 0);
2045 if (self->reciprocal)
2046 {
2047 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2047, __extension__ __PRETTY_FUNCTION__); }))
;
2048 input_params[0] = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2049 input_params[1] = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2050 ccv_nnc_hint_tensor_auto(div, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
2051 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2052 ccv_nnc_graph_exec_symbol_new(graph, div, TENSOR_SYMBOL_LIST(NO_TENSOR_SYMBOL, inputs[0])(const ccv_nnc_tensor_symbol_t []){(const ccv_nnc_tensor_symbol_t
){.d = CCV_NNC_NO_TENSOR_SYMBOL}, inputs[0]}, (1 +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 -1)
, outputs, output_size, "div");
2053 } else {
2054 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 2054, __extension__ __PRETTY_FUNCTION__); }))
;
2055 for (i = 0; i < 2; i++)
2056 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
2057 ccv_nnc_hint_tensor_auto(div, input_params, input_size, ccv_nnc_no_hint, &output_params, 1);
2058 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2059 ccv_nnc_graph_exec_symbol_new(graph, div, inputs, input_size, outputs, output_size, "div");
2060 }
2061}
2062
2063static ccv_cnnp_model_t* _ccv_cnnp_div_copy(const ccv_cnnp_model_t* const self, void* const context);
2064
2065static const ccv_cnnp_model_vtab_t ccv_cnnp_div_isa = {
2066 .build = _ccv_cnnp_div_build,
2067 .copy = _ccv_cnnp_div_copy,
2068};
2069
2070ccv_cnnp_model_t* ccv_cnnp_div(const int reciprocal, const char* const name)
2071{
2072 ccv_cnnp_model_div_t* const model_div = (ccv_cnnp_model_div_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_div_t));
2073 model_div->super.isa = &ccv_cnnp_div_isa;
2074 model_div->super.input_size = reciprocal ? 1 : 2;
2075 model_div->super.outputs = &model_div->output;
2076 model_div->super.output_size = 1;
2077 model_div->reciprocal = reciprocal;
2078 ccv_cnnp_model_copy_name(&model_div->super, name);
2079 return (ccv_cnnp_model_t*)model_div;
2080}
2081
2082static ccv_cnnp_model_t* _ccv_cnnp_div_copy(const ccv_cnnp_model_t* const super, void* const context)
2083{
2084 const ccv_cnnp_model_div_t* const self = (const ccv_cnnp_model_div_t*)super;
2085 return ccv_cnnp_div(self->reciprocal, self->super.name);
2086}
2087
2088// MARK - Sqrt Layer
2089
2090typedef struct {
2091 ccv_cnnp_model_t super;
2092 ccv_nnc_tensor_symbol_t output;
2093} ccv_cnnp_model_sqrt_t;
2094
2095static void _ccv_cnnp_sqrt_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2096{
2097 PRINT(CCV_CLI_VERBOSE, "[cnnp_sqrt_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_sqrt_build] -\n"); fflush(stdout); } } while
(0)
;
2098 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2098, __extension__ __PRETTY_FUNCTION__
); }))
;
2099 ccv_nnc_tensor_param_t input_params[1];
2100 ccv_nnc_tensor_param_t output_params;
2101 const ccv_nnc_cmd_t sqrt = CMD_EWSQRT_FORWARD()ccv_nnc_cmd(CCV_NNC_EWSQRT_FORWARD, 0, ccv_nnc_cmd_auto, 0);
2102 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2102, __extension__ __PRETTY_FUNCTION__); }))
;
2103 input_params[0] = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2104 ccv_nnc_hint_tensor_auto(sqrt, input_params, 1, ccv_nnc_no_hint, &output_params, 1);
2105 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2106 ccv_nnc_graph_exec_symbol_new(graph, sqrt, inputs, 1, outputs, output_size, "sqrt");
2107}
2108
2109static ccv_cnnp_model_t* _ccv_cnnp_sqrt_copy(const ccv_cnnp_model_t* const self, void* const context);
2110
2111static const ccv_cnnp_model_vtab_t ccv_cnnp_sqrt_isa = {
2112 .build = _ccv_cnnp_sqrt_build,
2113 .copy = _ccv_cnnp_sqrt_copy,
2114};
2115
2116ccv_cnnp_model_t* ccv_cnnp_sqrt(const char* const name)
2117{
2118 ccv_cnnp_model_sqrt_t* const model_sqrt = (ccv_cnnp_model_sqrt_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_sqrt_t));
2119 model_sqrt->super.isa = &ccv_cnnp_sqrt_isa;
2120 model_sqrt->super.input_size = 1;
2121 model_sqrt->super.outputs = &model_sqrt->output;
2122 model_sqrt->super.output_size = 1;
2123 ccv_cnnp_model_copy_name(&model_sqrt->super, name);
2124 return (ccv_cnnp_model_t*)model_sqrt;
2125}
2126
2127static ccv_cnnp_model_t* _ccv_cnnp_sqrt_copy(const ccv_cnnp_model_t* const super, void* const context)
2128{
2129 const ccv_cnnp_model_sqrt_t* const self = (const ccv_cnnp_model_sqrt_t*)super;
2130 return ccv_cnnp_sqrt(self->super.name);
2131}
2132
2133// MARK - Cmul Layer
2134
2135typedef struct {
2136 ccv_cnnp_model_t super;
2137 ccv_nnc_tensor_symbol_t output;
2138} ccv_cnnp_model_cmul_t;
2139
2140static void _ccv_cnnp_cmul_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2141{
2142 PRINT(CCV_CLI_VERBOSE, "[cnnp_cmul_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_cmul_build] -\n"); fflush(stdout); } } while
(0)
;
2143 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 2143, __extension__ __PRETTY_FUNCTION__); }))
;
2144 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2144, __extension__ __PRETTY_FUNCTION__
); }))
;
2145 ccv_nnc_tensor_param_t input_params[2];
2146 int i;
2147 for (i = 0; i < 2; i++)
2148 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
2149 ccv_nnc_tensor_param_t output_params;
2150 const ccv_nnc_cmd_t mul = CMD_CMUL_FORWARD()ccv_nnc_cmd(CCV_NNC_CMUL_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}}}, 0)
;
2151 ccv_nnc_hint_tensor_auto(mul, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
2152 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2153 ccv_nnc_graph_exec_symbol_new(graph, mul, inputs, input_size, outputs, output_size, "cmul");
2154}
2155
2156static ccv_cnnp_model_t* _ccv_cnnp_cmul_copy(const ccv_cnnp_model_t* const self, void* const context);
2157
2158static const ccv_cnnp_model_vtab_t ccv_cnnp_cmul_isa = {
2159 .build = _ccv_cnnp_cmul_build,
2160 .copy = _ccv_cnnp_cmul_copy,
2161};
2162
2163ccv_cnnp_model_t* ccv_cnnp_cmul(const char* const name)
2164{
2165 ccv_cnnp_model_cmul_t* const model_cmul = (ccv_cnnp_model_cmul_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_cmul_t));
2166 model_cmul->super.isa = &ccv_cnnp_cmul_isa;
2167 model_cmul->super.input_size = 2;
2168 model_cmul->super.outputs = &model_cmul->output;
2169 model_cmul->super.output_size = 1;
2170 ccv_cnnp_model_copy_name(&model_cmul->super, name);
2171 return (ccv_cnnp_model_t*)model_cmul;
2172}
2173
2174static ccv_cnnp_model_t* _ccv_cnnp_cmul_copy(const ccv_cnnp_model_t* const super, void* const context)
2175{
2176 return ccv_cnnp_cmul(super->name);
2177}
2178
2179// MARK - Transpose Layer
2180
2181typedef struct {
2182 ccv_cnnp_model_t super;
2183 ccv_nnc_tensor_symbol_t output;
2184 int transpose[2];
2185} ccv_cnnp_model_transpose_t;
2186
2187static void _ccv_cnnp_transpose_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2188{
2189 ccv_cnnp_model_transpose_t* const self = (ccv_cnnp_model_transpose_t*)super;
2190 PRINT(CCV_CLI_VERBOSE, "[cnnp_transpose_build] (%d, %d)\n", self->transpose[0], self->transpose[1])do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_transpose_build] (%d, %d)\n", self->transpose
[0], self->transpose[1]); fflush(stdout); } } while (0)
;
2191 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2191, __extension__ __PRETTY_FUNCTION__); }))
;
2192 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2192, __extension__ __PRETTY_FUNCTION__
); }))
;
2193 if (self->transpose[0] == self->transpose[1])
2194 {
2195 outputs[0] = inputs[0];
2196 return;
2197 }
2198 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2199 ccv_nnc_tensor_param_t output_params;
2200 const ccv_nnc_cmd_t transpose = CMD_TRANSPOSE_FORWARD(self->transpose[0], self->transpose[1])ccv_nnc_cmd(CCV_NNC_TRANSPOSE_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.transpose={.axis={self->transpose[
0], self->transpose[1]}}}), 0)
;
2201 ccv_nnc_hint_tensor_auto(transpose, (ccv_nnc_tensor_param_t []){
2202 params,
2203 }, 1, ccv_nnc_no_hint, &output_params, 1);
2204 const ccv_nnc_tensor_symbol_t transpose_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2205 ccv_nnc_graph_exec_symbol_new(graph, transpose, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(transpose_output)(const ccv_nnc_tensor_symbol_t []){transpose_output}, (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)
, "transpose");
2206 outputs[0] = transpose_output;
2207}
2208
2209static ccv_cnnp_model_t* _ccv_cnnp_transpose_copy(const ccv_cnnp_model_t* const super, void* const context);
2210
2211static const ccv_cnnp_model_vtab_t ccv_cnnp_transpose_isa = {
2212 .build = _ccv_cnnp_transpose_build,
2213 .copy = _ccv_cnnp_transpose_copy,
2214};
2215
2216ccv_cnnp_model_t* ccv_cnnp_transpose(const int axis_a, const int axis_b, const char* const name)
2217{
2218 ccv_cnnp_model_transpose_t* const model_transpose = (ccv_cnnp_model_transpose_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_transpose_t));
2219 model_transpose->super.isa = &ccv_cnnp_transpose_isa;
2220 model_transpose->super.input_size = 1;
2221 model_transpose->super.outputs = &model_transpose->output;
2222 model_transpose->super.output_size = 1;
2223 model_transpose->transpose[0] = axis_a;
2224 model_transpose->transpose[1] = axis_b;
2225 ccv_cnnp_model_copy_name(&model_transpose->super, name);
2226 return (ccv_cnnp_model_t*)model_transpose;
2227}
2228
2229static ccv_cnnp_model_t* _ccv_cnnp_transpose_copy(const ccv_cnnp_model_t* const super, void* const context)
2230{
2231 const ccv_cnnp_model_transpose_t* const self = (const ccv_cnnp_model_transpose_t*)super;
2232 return ccv_cnnp_transpose(self->transpose[0], self->transpose[1], self->super.name);
2233}
2234
2235// MARK - Layer Norm Layer
2236
2237typedef struct {
2238 ccv_cnnp_model_t super;
2239 ccv_nnc_tensor_symbol_t output;
2240 ccv_nnc_tensor_symbol_t bias;
2241 ccv_nnc_tensor_symbol_t scale;
2242 ccv_nnc_cmd_param_t params;
2243} ccv_cnnp_model_layer_norm_t;
2244
2245static void _ccv_cnnp_layer_norm_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2246{
2247 PRINT(CCV_CLI_VERBOSE, "[cnnp_layer_norm_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_layer_norm_build] -\n"); fflush(stdout); } }
while (0)
;
2248 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2248, __extension__ __PRETTY_FUNCTION__); }))
;
2249 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2249, __extension__ __PRETTY_FUNCTION__
); }))
;
2250 ccv_cnnp_model_layer_norm_t* const self = (ccv_cnnp_model_layer_norm_t*)super;
2251 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2252 ccv_nnc_tensor_param_t bias_params = params;
2253 const int nd = ccv_nnc_tensor_nd(params.dim);
2254 int i;
2255 for (i = 0; i < nd; i++)
2256 bias_params.dim[i] = 1;
2257 for (i = 0; i < self->params.lnorm.count; i++)
2258 bias_params.dim[self->params.lnorm.axis[i]] = params.dim[self->params.lnorm.axis[i]];
2259 if (self->params.lnorm.elementwise_affine)
2260 {
2261 // Both scale and bias are shared between if this model is reused.
2262 if (!self->scale.graph)
2263 self->scale = ccv_nnc_tensor_symbol_new(graph, bias_params, "scale");
2264 if (!self->bias.graph)
2265 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
2266 }
2267 const ccv_nnc_cmd_t layer_norm = ccv_nnc_cmd(CCV_NNC_LAYER_NORM_FORWARD, 0, self->params, 0);
2268 ccv_nnc_tensor_param_t output_params[3];
2269 if (self->params.lnorm.elementwise_affine)
2270 ccv_nnc_hint_tensor_auto(layer_norm, (ccv_nnc_tensor_param_t []){
2271 params,
2272 bias_params,
2273 bias_params,
2274 }, 3, ccv_nnc_no_hint, output_params, 3);
2275 else
2276 ccv_nnc_hint_tensor_auto(layer_norm, (ccv_nnc_tensor_param_t []){
2277 params,
2278 }, 1, ccv_nnc_no_hint, output_params, 3);
2279 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2280 const ccv_nnc_tensor_symbol_t saved_mean = ccv_nnc_tensor_symbol_new(graph, output_params[1], "saved_mean");
2281 const ccv_nnc_tensor_symbol_t saved_inv_std = ccv_nnc_tensor_symbol_new(graph, output_params[2], "saved_inv_std");
2282 if (self->params.lnorm.elementwise_affine)
2283 ccv_nnc_graph_exec_symbol_new(graph, layer_norm, TENSOR_SYMBOL_LIST(inputs[0], self->scale, self->bias)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->scale,
self->bias}, (1 +1 +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 -1)
, TENSOR_SYMBOL_LIST(output, saved_mean, saved_inv_std)(const ccv_nnc_tensor_symbol_t []){output, saved_mean, saved_inv_std
}, (1 +1 +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 -1)
, "layer_norm");
2284 else
2285 ccv_nnc_graph_exec_symbol_new(graph, layer_norm, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(output, saved_mean, saved_inv_std)(const ccv_nnc_tensor_symbol_t []){output, saved_mean, saved_inv_std
}, (1 +1 +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 -1)
, "layer_norm");
2286 outputs[0] = output;
2287}
2288
2289static void _ccv_cnnp_layer_norm_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
2290{
2291 ccv_cnnp_model_layer_norm_t* const self = (ccv_cnnp_model_layer_norm_t*)super;
2292 if (self->scale.graph)
2293 initializer(context, CMD_SET_FORWARD(1)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={1,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->scale);
2294 if (self->bias.graph)
2295 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->bias);
2296}
2297
2298static void _ccv_cnnp_layer_norm_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
2299{
2300 ccv_cnnp_model_layer_norm_t* const self = (ccv_cnnp_model_layer_norm_t*)super;
2301 if (self->scale.graph)
2302 add_to_array(parameters, self->scale, is_trainable);
2303 if (self->bias.graph)
2304 add_to_array(parameters, self->bias, is_trainable);
2305}
2306
2307static ccv_cnnp_model_t* _ccv_cnnp_layer_norm_copy(const ccv_cnnp_model_t* const super, void* const context);
2308
2309static const ccv_cnnp_model_vtab_t ccv_cnnp_layer_norm_isa = {
2310 .build = _ccv_cnnp_layer_norm_build,
2311 .init_states = _ccv_cnnp_layer_norm_init_states,
2312 .add_to_parameter = _ccv_cnnp_layer_norm_add_to_parameter,
2313 .copy = _ccv_cnnp_layer_norm_copy,
2314};
2315
2316ccv_cnnp_model_t* ccv_cnnp_layer_norm(const float epsilon, const int axis[CCV_NNC_MAX_DIM_ALLOC(12)], const int axis_count, const int elementwise_affine, const int is_trainable, const char* const name)
2317{
2318 ccv_cnnp_model_layer_norm_t* const model_layer_norm = (ccv_cnnp_model_layer_norm_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_layer_norm_t));
2319 model_layer_norm->super.isa = &ccv_cnnp_layer_norm_isa;
2320 model_layer_norm->super.input_size = 1;
2321 model_layer_norm->super.outputs = &model_layer_norm->output;
2322 model_layer_norm->super.output_size = 1;
2323 model_layer_norm->super.is_trainable = is_trainable;
2324 ccv_cnnp_model_copy_name(&model_layer_norm->super, name);
2325 model_layer_norm->scale.d = CCV_NNC_NO_TENSOR_SYMBOL;
2326 model_layer_norm->scale.graph = 0;
2327 model_layer_norm->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
2328 model_layer_norm->bias.graph = 0;
2329 model_layer_norm->params.lnorm.epsilon = epsilon;
2330 model_layer_norm->params.lnorm.count = axis_count;
2331 model_layer_norm->params.lnorm.elementwise_affine = elementwise_affine;
2332 memcpy(model_layer_norm->params.lnorm.axis, axis, sizeof(int) * axis_count);
2333 return (ccv_cnnp_model_t*)model_layer_norm;
2334}
2335
2336static ccv_cnnp_model_t* _ccv_cnnp_layer_norm_copy(const ccv_cnnp_model_t* const super, void* const context)
2337{
2338 const ccv_cnnp_model_layer_norm_t* const self = (const ccv_cnnp_model_layer_norm_t*)super;
2339 return ccv_cnnp_layer_norm(self->params.lnorm.epsilon, self->params.lnorm.axis, self->params.lnorm.count, self->params.lnorm.elementwise_affine, self->super.is_trainable, self->super.name);
2340}
2341
2342// MARK - Group Norm Layer
2343
2344typedef struct {
2345 ccv_cnnp_model_t super;
2346 ccv_nnc_tensor_symbol_t output;
2347 ccv_nnc_tensor_symbol_t bias;
2348 ccv_nnc_tensor_symbol_t scale;
2349 ccv_nnc_cmd_param_t params;
2350} ccv_cnnp_model_group_norm_t;
2351
2352static void _ccv_cnnp_group_norm_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2353{
2354 PRINT(CCV_CLI_VERBOSE, "[cnnp_group_norm_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_group_norm_build] -\n"); fflush(stdout); } }
while (0)
;
2355 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2355, __extension__ __PRETTY_FUNCTION__); }))
;
2356 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2356, __extension__ __PRETTY_FUNCTION__
); }))
;
2357 ccv_cnnp_model_group_norm_t* const self = (ccv_cnnp_model_group_norm_t*)super;
2358 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2359 ccv_nnc_tensor_param_t bias_params = params;
2360 const int nd = ccv_nnc_tensor_nd(params.dim);
2361 int i;
2362 for (i = 0; i < nd; i++)
2363 bias_params.dim[i] = 1;
2364 bias_params.dim[self->params.gnorm.group_axis] = params.dim[self->params.gnorm.group_axis];
2365 if (self->params.gnorm.elementwise_affine)
2366 {
2367 // Both scale and bias are shared between if this model is reused.
2368 if (!self->scale.graph)
2369 self->scale = ccv_nnc_tensor_symbol_new(graph, bias_params, "scale");
2370 if (!self->bias.graph)
2371 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
2372 }
2373 const ccv_nnc_cmd_t group_norm = ccv_nnc_cmd(CCV_NNC_GROUP_NORM_FORWARD, 0, self->params, 0);
2374 ccv_nnc_tensor_param_t output_params[3];
2375 if (self->params.gnorm.elementwise_affine)
2376 ccv_nnc_hint_tensor_auto(group_norm, (ccv_nnc_tensor_param_t []){
2377 params,
2378 bias_params,
2379 bias_params,
2380 }, 3, ccv_nnc_no_hint, output_params, 3);
2381 else
2382 ccv_nnc_hint_tensor_auto(group_norm, (ccv_nnc_tensor_param_t []){
2383 params,
2384 }, 1, ccv_nnc_no_hint, output_params, 3);
2385 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2386 const ccv_nnc_tensor_symbol_t saved_mean = ccv_nnc_tensor_symbol_new(graph, output_params[1], "saved_mean");
2387 const ccv_nnc_tensor_symbol_t saved_inv_std = ccv_nnc_tensor_symbol_new(graph, output_params[2], "saved_inv_std");
2388 if (self->params.gnorm.elementwise_affine)
2389 ccv_nnc_graph_exec_symbol_new(graph, group_norm, TENSOR_SYMBOL_LIST(inputs[0], self->scale, self->bias)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->scale,
self->bias}, (1 +1 +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 -1)
, TENSOR_SYMBOL_LIST(output, saved_mean, saved_inv_std)(const ccv_nnc_tensor_symbol_t []){output, saved_mean, saved_inv_std
}, (1 +1 +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 -1)
, "group_norm");
2390 else
2391 ccv_nnc_graph_exec_symbol_new(graph, group_norm, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(output, saved_mean, saved_inv_std)(const ccv_nnc_tensor_symbol_t []){output, saved_mean, saved_inv_std
}, (1 +1 +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 -1)
, "group_norm");
2392 outputs[0] = output;
2393}
2394
2395static void _ccv_cnnp_group_norm_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
2396{
2397 ccv_cnnp_model_group_norm_t* const self = (ccv_cnnp_model_group_norm_t*)super;
2398 if (self->scale.graph)
2399 initializer(context, CMD_SET_FORWARD(1)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={1,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->scale);
2400 if (self->bias.graph)
2401 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->bias);
2402}
2403
2404static void _ccv_cnnp_group_norm_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
2405{
2406 ccv_cnnp_model_group_norm_t* const self = (ccv_cnnp_model_group_norm_t*)super;
2407 if (self->scale.graph)
2408 add_to_array(parameters, self->scale, is_trainable);
2409 if (self->bias.graph)
2410 add_to_array(parameters, self->bias, is_trainable);
2411}
2412
2413static ccv_cnnp_model_t* _ccv_cnnp_group_norm_copy(const ccv_cnnp_model_t* const super, void* const context);
2414
2415static const ccv_cnnp_model_vtab_t ccv_cnnp_group_norm_isa = {
2416 .build = _ccv_cnnp_group_norm_build,
2417 .init_states = _ccv_cnnp_group_norm_init_states,
2418 .add_to_parameter = _ccv_cnnp_group_norm_add_to_parameter,
2419 .copy = _ccv_cnnp_group_norm_copy,
2420};
2421
2422ccv_cnnp_model_t* ccv_cnnp_group_norm(const int group_axis, const int groups, const float epsilon, const int reduce_axis[CCV_NNC_MAX_DIM_ALLOC(12)], const int axis_count, const int elementwise_affine, const int is_trainable, const char* const name)
2423{
2424 ccv_cnnp_model_group_norm_t* const model_group_norm = (ccv_cnnp_model_group_norm_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_group_norm_t));
2425 model_group_norm->super.isa = &ccv_cnnp_group_norm_isa;
2426 model_group_norm->super.input_size = 1;
2427 model_group_norm->super.outputs = &model_group_norm->output;
2428 model_group_norm->super.output_size = 1;
2429 model_group_norm->super.is_trainable = is_trainable;
2430 ccv_cnnp_model_copy_name(&model_group_norm->super, name);
2431 model_group_norm->scale.d = CCV_NNC_NO_TENSOR_SYMBOL;
2432 model_group_norm->scale.graph = 0;
2433 model_group_norm->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
2434 model_group_norm->bias.graph = 0;
2435 model_group_norm->params.gnorm.group_axis = group_axis;
2436 model_group_norm->params.gnorm.groups = groups;
2437 model_group_norm->params.gnorm.epsilon = epsilon;
2438 model_group_norm->params.gnorm.reduce_count = axis_count;
2439 model_group_norm->params.gnorm.elementwise_affine = elementwise_affine;
2440 memcpy(model_group_norm->params.gnorm.reduce_axis, reduce_axis, sizeof(int) * axis_count);
2441 return (ccv_cnnp_model_t*)model_group_norm;
2442}
2443
2444static ccv_cnnp_model_t* _ccv_cnnp_group_norm_copy(const ccv_cnnp_model_t* const super, void* const context)
2445{
2446 const ccv_cnnp_model_group_norm_t* const self = (const ccv_cnnp_model_group_norm_t*)super;
2447 return ccv_cnnp_group_norm(self->params.gnorm.group_axis, self->params.gnorm.groups, self->params.gnorm.epsilon, self->params.gnorm.reduce_axis, self->params.gnorm.reduce_count, self->params.gnorm.elementwise_affine, self->super.is_trainable, self->super.name);
2448}
2449
2450// MARK - RMSNorm Layer
2451
2452typedef struct {
2453 ccv_cnnp_model_t super;
2454 ccv_nnc_tensor_symbol_t output;
2455 ccv_nnc_tensor_symbol_t scale;
2456 ccv_nnc_cmd_param_t params;
2457} ccv_cnnp_model_rmsnorm_t;
2458
2459static void _ccv_cnnp_rmsnorm_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2460{
2461 PRINT(CCV_CLI_VERBOSE, "[cnnp_rmsnorm_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_rmsnorm_build] -\n"); fflush(stdout); } } while
(0)
;
2462 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2462, __extension__ __PRETTY_FUNCTION__); }))
;
2463 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2463, __extension__ __PRETTY_FUNCTION__
); }))
;
2464 ccv_cnnp_model_rmsnorm_t* const self = (ccv_cnnp_model_rmsnorm_t*)super;
2465 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2466 ccv_nnc_tensor_param_t scale_params = params;
2467 const int nd = ccv_nnc_tensor_nd(params.dim);
2468 int i;
2469 for (i = 0; i < nd; i++)
2470 scale_params.dim[i] = 1;
2471 for (i = 0; i < self->params.rmsnorm.count; i++)
2472 scale_params.dim[self->params.rmsnorm.axis[i]] = params.dim[self->params.rmsnorm.axis[i]];
2473 // Both scale and bias are shared between if this model is reused.
2474 if (!self->scale.graph)
2475 self->scale = ccv_nnc_tensor_symbol_new(graph, scale_params, "scale");
2476 const ccv_nnc_cmd_t rmsnorm = ccv_nnc_cmd(CCV_NNC_RMSNORM_FORWARD, 0, self->params, 0);
2477 ccv_nnc_tensor_param_t output_params[2];
2478 ccv_nnc_hint_tensor_auto(rmsnorm, (ccv_nnc_tensor_param_t []){
2479 params,
2480 scale_params,
2481 }, 2, ccv_nnc_no_hint, output_params, 2);
2482 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2483 const ccv_nnc_tensor_symbol_t saved_inv_std = ccv_nnc_tensor_symbol_new(graph, output_params[1], "saved_inv_std");
2484 ccv_nnc_graph_exec_symbol_new(graph, rmsnorm, TENSOR_SYMBOL_LIST(inputs[0], self->scale)(const ccv_nnc_tensor_symbol_t []){inputs[0], self->scale}
, (1 +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 -1)
, TENSOR_SYMBOL_LIST(output, saved_inv_std)(const ccv_nnc_tensor_symbol_t []){output, saved_inv_std}, (1
+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 -1)
, "rmsnorm");
2485 outputs[0] = output;
2486}
2487
2488static void _ccv_cnnp_rmsnorm_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
2489{
2490 ccv_cnnp_model_rmsnorm_t* const self = (ccv_cnnp_model_rmsnorm_t*)super;
2491 if (self->scale.graph)
2492 initializer(context, CMD_SET_FORWARD(1)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={1,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->scale);
2493}
2494
2495static void _ccv_cnnp_rmsnorm_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
2496{
2497 ccv_cnnp_model_rmsnorm_t* const self = (ccv_cnnp_model_rmsnorm_t*)super;
2498 if (self->scale.graph)
2499 add_to_array(parameters, self->scale, is_trainable);
2500}
2501
2502static ccv_cnnp_model_t* _ccv_cnnp_rmsnorm_copy(const ccv_cnnp_model_t* const super, void* const context);
2503
2504static const ccv_cnnp_model_vtab_t ccv_cnnp_rmsnorm_isa = {
2505 .build = _ccv_cnnp_rmsnorm_build,
2506 .init_states = _ccv_cnnp_rmsnorm_init_states,
2507 .add_to_parameter = _ccv_cnnp_rmsnorm_add_to_parameter,
2508 .copy = _ccv_cnnp_rmsnorm_copy,
2509};
2510
2511ccv_cnnp_model_t* ccv_cnnp_rmsnorm(const float epsilon, const int axis[CCV_NNC_MAX_DIM_ALLOC(12)], const int axis_count, const int is_trainable, const char* const name)
2512{
2513 ccv_cnnp_model_rmsnorm_t* const model_rmsnorm = (ccv_cnnp_model_rmsnorm_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_rmsnorm_t));
2514 model_rmsnorm->super.isa = &ccv_cnnp_rmsnorm_isa;
2515 model_rmsnorm->super.input_size = 1;
2516 model_rmsnorm->super.outputs = &model_rmsnorm->output;
2517 model_rmsnorm->super.output_size = 1;
2518 model_rmsnorm->super.is_trainable = is_trainable;
2519 ccv_cnnp_model_copy_name(&model_rmsnorm->super, name);
2520 model_rmsnorm->scale.d = CCV_NNC_NO_TENSOR_SYMBOL;
2521 model_rmsnorm->scale.graph = 0;
2522 model_rmsnorm->params.rmsnorm.epsilon = epsilon;
2523 model_rmsnorm->params.rmsnorm.count = axis_count;
2524 memcpy(model_rmsnorm->params.lnorm.axis, axis, sizeof(int) * axis_count);
2525 return (ccv_cnnp_model_t*)model_rmsnorm;
2526}
2527
2528static ccv_cnnp_model_t* _ccv_cnnp_rmsnorm_copy(const ccv_cnnp_model_t* const super, void* const context)
2529{
2530 const ccv_cnnp_model_rmsnorm_t* const self = (const ccv_cnnp_model_rmsnorm_t*)super;
2531 return ccv_cnnp_rmsnorm(self->params.rmsnorm.epsilon, self->params.rmsnorm.axis, self->params.rmsnorm.count, self->super.is_trainable, self->super.name);
2532}
2533
2534// MARK - Batched Matrix Mul Layer
2535
2536typedef struct {
2537 ccv_cnnp_model_t super;
2538 ccv_nnc_tensor_symbol_t output;
2539 int transpose_a[2];
2540 int transpose_b[2];
2541} ccv_cnnp_model_matmul_t;
2542
2543static void _ccv_cnnp_matmul_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2544{
2545 PRINT(CCV_CLI_VERBOSE, "[cnnp_matmul_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_matmul_build] -\n"); fflush(stdout); } } while
(0)
;
2546 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 2546, __extension__ __PRETTY_FUNCTION__); }))
;
2547 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2547, __extension__ __PRETTY_FUNCTION__
); }))
;
2548 ccv_cnnp_model_matmul_t* const self = (ccv_cnnp_model_matmul_t*)super;
2549 ccv_nnc_tensor_param_t a_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2550 ccv_nnc_tensor_param_t b_params = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
2551 ccv_nnc_tensor_param_t output_params;
2552 const ccv_nnc_cmd_t matmul = CMD_GEMM_FORWARD(self->transpose_a, self->transpose_b)ccv_nnc_cmd(CCV_NNC_GEMM_FORWARD, 0, ((ccv_nnc_cmd_param_t){.
size={.dim={1,1,1}},.blas={.a={1,1},.transpose_a={self->transpose_a
[0],self->transpose_a[1]},.transpose_b={self->transpose_b
[0],self->transpose_b[1]},}}), 0)
;
2553 ccv_nnc_hint_tensor_auto(matmul, (ccv_nnc_tensor_param_t []){
2554 a_params,
2555 b_params,
2556 }, 2, ccv_nnc_no_hint, &output_params, 1);
2557 const ccv_nnc_tensor_symbol_t matmul_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2558 ccv_nnc_graph_exec_symbol_new(graph, matmul, inputs, input_size, TENSOR_SYMBOL_LIST(matmul_output)(const ccv_nnc_tensor_symbol_t []){matmul_output}, (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
)
, "matmul");
2559 outputs[0] = matmul_output;
2560}
2561
2562static ccv_cnnp_model_t* _ccv_cnnp_matmul_copy(const ccv_cnnp_model_t* const super, void* const context);
2563
2564static const ccv_cnnp_model_vtab_t ccv_cnnp_matmul_isa = {
2565 .build = _ccv_cnnp_matmul_build,
2566 .copy = _ccv_cnnp_matmul_copy,
2567};
2568
2569ccv_cnnp_model_t* ccv_cnnp_matmul(const int transpose_a[2], const int transpose_b[2], const char* const name)
2570{
2571 ccv_cnnp_model_matmul_t* const model_matmul = (ccv_cnnp_model_matmul_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_matmul_t));
2572 model_matmul->super.isa = &ccv_cnnp_matmul_isa;
2573 model_matmul->super.input_size = 2;
2574 model_matmul->super.outputs = &model_matmul->output;
2575 model_matmul->super.output_size = 1;
2576 model_matmul->transpose_a[0] = transpose_a[0];
2577 model_matmul->transpose_a[1] = transpose_a[1];
2578 model_matmul->transpose_b[0] = transpose_b[0];
2579 model_matmul->transpose_b[1] = transpose_b[1];
2580 ccv_cnnp_model_copy_name(&model_matmul->super, name);
2581 return (ccv_cnnp_model_t*)model_matmul;
2582}
2583
2584static ccv_cnnp_model_t* _ccv_cnnp_matmul_copy(const ccv_cnnp_model_t* const super, void* const context)
2585{
2586 const ccv_cnnp_model_matmul_t* const self = (const ccv_cnnp_model_matmul_t*)super;
2587 return ccv_cnnp_matmul(self->transpose_a, self->transpose_b, self->super.name);
2588}
2589
2590// MARK - Dropout Layer
2591
2592typedef struct {
2593 ccv_cnnp_model_t super;
2594 ccv_nnc_tensor_symbol_t output;
2595 ccv_nnc_graph_exec_symbol_t dropout;
2596 float p;
2597 int entirety;
2598} ccv_cnnp_model_dropout_t;
2599
2600static void _ccv_cnnp_dropout_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2601{
2602 PRINT(CCV_CLI_VERBOSE, "[cnnp_dropout_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_dropout_build] -\n"); fflush(stdout); } } while
(0)
;
2603 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2603, __extension__ __PRETTY_FUNCTION__); }))
;
2604 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2604, __extension__ __PRETTY_FUNCTION__
); }))
;
2605 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2606 ccv_nnc_tensor_param_t output_params[2];
2607 ccv_cnnp_model_dropout_t* const self = (ccv_cnnp_model_dropout_t*)super;
2608 const ccv_nnc_cmd_t dropout = CMD_DROPOUT_FORWARD(self->p, self->entirety)ccv_nnc_cmd(CCV_NNC_DROPOUT_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.dropout={.p=self->p,.entirety=self
->entirety}}), 0)
;
2609 ccv_nnc_hint_tensor_auto(dropout, (ccv_nnc_tensor_param_t []){
2610 params,
2611 }, 1, ccv_nnc_no_hint, output_params, 2);
2612 const ccv_nnc_tensor_symbol_t dropout_output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2613 const ccv_nnc_tensor_symbol_t mask = ccv_nnc_tensor_symbol_new(graph, output_params[1], "mask");
2614 self->dropout = ccv_nnc_graph_exec_symbol_new(graph, dropout, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(dropout_output, mask)(const ccv_nnc_tensor_symbol_t []){dropout_output, mask}, (1 +
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 -1)
, "dropout");
2615 outputs[0] = dropout_output;
2616}
2617
2618static void _ccv_cnnp_dropout_set_is_test(ccv_cnnp_model_t* const super, const int is_test, const ccv_cnnp_cmd_updater_f updater, void* const context)
2619{
2620 ccv_cnnp_model_dropout_t* const self = (ccv_cnnp_model_dropout_t*)super;
2621 if (self->dropout.graph)
2622 {
2623 if (is_test)
2624 // During test, the dropout is not applied. Data transfer is perfect because if these are the same tensor, it will skip.
2625 updater(context, self->dropout, CMD_DATA_TRANSFER_FORWARD()ccv_nnc_cmd(CCV_NNC_DATA_TRANSFER_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, ccv_nnc_no_hint);
2626 else
2627 updater(context, self->dropout, CMD_DROPOUT_FORWARD(self->p, self->entirety)ccv_nnc_cmd(CCV_NNC_DROPOUT_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.dropout={.p=self->p,.entirety=self
->entirety}}), 0)
, ccv_nnc_no_hint);
2628 }
2629}
2630
2631static ccv_cnnp_model_t* _ccv_cnnp_dropout_copy(const ccv_cnnp_model_t* const super, void* const context);
2632
2633static const ccv_cnnp_model_vtab_t ccv_cnnp_dropout_isa = {
2634 .build = _ccv_cnnp_dropout_build,
2635 .set_is_test = _ccv_cnnp_dropout_set_is_test,
2636 .copy = _ccv_cnnp_dropout_copy,
2637};
2638
2639ccv_cnnp_model_t* ccv_cnnp_dropout(const float p, const int entirety, const char* const name)
2640{
2641 ccv_cnnp_model_dropout_t* const model_dropout = (ccv_cnnp_model_dropout_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_dropout_t));
2642 model_dropout->super.isa = &ccv_cnnp_dropout_isa;
2643 model_dropout->super.input_size = 1;
2644 model_dropout->super.outputs = &model_dropout->output;
2645 model_dropout->super.output_size = 1;
2646 model_dropout->p = p;
2647 model_dropout->entirety = entirety;
2648 ccv_cnnp_model_copy_name(&model_dropout->super, name);
2649 return (ccv_cnnp_model_t*)model_dropout;
2650}
2651
2652static ccv_cnnp_model_t* _ccv_cnnp_dropout_copy(const ccv_cnnp_model_t* const super, void* const context)
2653{
2654 const ccv_cnnp_model_dropout_t* const self = (const ccv_cnnp_model_dropout_t*)super;
2655 return ccv_cnnp_dropout(self->p, self->entirety, self->super.name);
2656}
2657
2658// MARK - Masked Fill Layer
2659
2660typedef struct {
2661 ccv_cnnp_model_t super;
2662 ccv_nnc_tensor_symbol_t output;
2663 float eq;
2664 float fill;
2665} ccv_cnnp_model_masked_fill_t;
2666
2667static void _ccv_cnnp_masked_fill_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2668{
2669 PRINT(CCV_CLI_VERBOSE, "[cnnp_masked_fill_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_masked_fill_build] -\n"); fflush(stdout); } }
while (0)
;
2670 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 2670, __extension__ __PRETTY_FUNCTION__); }))
;
2671 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2671, __extension__ __PRETTY_FUNCTION__
); }))
;
2672 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2673 ccv_cnnp_model_masked_fill_t* const self = (ccv_cnnp_model_masked_fill_t*)super;
2674 const ccv_nnc_tensor_symbol_t masked_fill_output = ccv_nnc_tensor_symbol_new(graph, params, 0);
2675 ccv_nnc_graph_exec_symbol_new(graph, CMD_MASKED_FILL_FORWARD(self->eq, self->fill)ccv_nnc_cmd(CCV_NNC_MASKED_FILL_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={self->eq, self->fill}
}}, 0)
, TENSOR_SYMBOL_LIST(inputs[0], inputs[1])(const ccv_nnc_tensor_symbol_t []){inputs[0], inputs[1]}, (1 +
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 -1)
, TENSOR_SYMBOL_LIST(masked_fill_output)(const ccv_nnc_tensor_symbol_t []){masked_fill_output}, (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)
, "masked_fill");
2676 outputs[0] = masked_fill_output;
2677}
2678
2679static ccv_cnnp_model_t* _ccv_cnnp_masked_fill_copy(const ccv_cnnp_model_t* const super, void* const context);
2680
2681static const ccv_cnnp_model_vtab_t ccv_cnnp_masked_fill_isa = {
2682 .build = _ccv_cnnp_masked_fill_build,
2683 .copy = _ccv_cnnp_masked_fill_copy,
2684};
2685
2686ccv_cnnp_model_t* ccv_cnnp_masked_fill(const float eq, const float fill, const char* const name)
2687{
2688 ccv_cnnp_model_masked_fill_t* const model_masked_fill = (ccv_cnnp_model_masked_fill_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_masked_fill_t));
2689 model_masked_fill->super.isa = &ccv_cnnp_masked_fill_isa;
2690 model_masked_fill->super.input_size = 2;
2691 model_masked_fill->super.outputs = &model_masked_fill->output;
2692 model_masked_fill->super.output_size = 1;
2693 model_masked_fill->eq = eq;
2694 model_masked_fill->fill = fill;
2695 ccv_cnnp_model_copy_name(&model_masked_fill->super, name);
2696 return (ccv_cnnp_model_t*)model_masked_fill;
2697}
2698
2699static ccv_cnnp_model_t* _ccv_cnnp_masked_fill_copy(const ccv_cnnp_model_t* const super, void* const context)
2700{
2701 const ccv_cnnp_model_masked_fill_t* const self = (const ccv_cnnp_model_masked_fill_t*)super;
2702 return ccv_cnnp_masked_fill(self->eq, self->fill, self->super.name);
2703}
2704
2705// MARK - Index Select Layer
2706
2707typedef struct {
2708 ccv_cnnp_model_t super;
2709 ccv_nnc_tensor_symbol_t output;
2710} ccv_cnnp_model_index_select_t;
2711
2712static void _ccv_cnnp_index_select_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2713{
2714 PRINT(CCV_CLI_VERBOSE, "[cnnp_index_select_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_index_select_build] -\n"); fflush(stdout); }
} while (0)
;
2715 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 2715, __extension__ __PRETTY_FUNCTION__); }))
;
2716 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2716, __extension__ __PRETTY_FUNCTION__
); }))
;
2717 const ccv_nnc_tensor_param_t vocab_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2718 const ccv_nnc_tensor_param_t index_params = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
2719 ccv_nnc_tensor_param_t output_params;
2720 const ccv_nnc_cmd_t index_select = CMD_INDEX_SELECT_FORWARD()ccv_nnc_cmd(CCV_NNC_INDEX_SELECT_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
;
2721 ccv_nnc_hint_tensor_auto(index_select, (ccv_nnc_tensor_param_t []){
2722 vocab_params,
2723 index_params,
2724 }, 2, ccv_nnc_no_hint, &output_params, 1);
2725 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2726 ccv_nnc_graph_exec_symbol_new(graph, index_select, TENSOR_SYMBOL_LIST(inputs[0], inputs[1])(const ccv_nnc_tensor_symbol_t []){inputs[0], inputs[1]}, (1 +
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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "index_select");
2727 outputs[0] = output;
2728}
2729
2730static ccv_cnnp_model_t* _ccv_cnnp_index_select_copy(const ccv_cnnp_model_t* const super, void* const context);
2731
2732static const ccv_cnnp_model_vtab_t ccv_cnnp_index_select_isa = {
2733 .build = _ccv_cnnp_index_select_build,
2734 .copy = _ccv_cnnp_index_select_copy,
2735};
2736
2737ccv_cnnp_model_t* ccv_cnnp_index_select(const char* const name)
2738{
2739 ccv_cnnp_model_index_select_t* const model_index_select = (ccv_cnnp_model_index_select_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_index_select_t));
2740 model_index_select->super.isa = &ccv_cnnp_index_select_isa;
2741 model_index_select->super.input_size = 2;
2742 model_index_select->super.outputs = &model_index_select->output;
2743 model_index_select->super.output_size = 1;
2744 ccv_cnnp_model_copy_name(&model_index_select->super, name);
2745 return (ccv_cnnp_model_t*)model_index_select;
2746}
2747
2748static ccv_cnnp_model_t* _ccv_cnnp_index_select_copy(const ccv_cnnp_model_t* const super, void* const context)
2749{
2750 ccv_cnnp_model_index_select_t* const self = (ccv_cnnp_model_index_select_t*)super;
2751 return ccv_cnnp_index_select(self->super.name);
2752}
2753
2754// MARK - Embedding Layer
2755
2756typedef struct {
2757 ccv_cnnp_model_t super;
2758 ccv_nnc_tensor_symbol_t output;
2759 ccv_nnc_tensor_symbol_t vocab;
2760 int datatype;
2761 int vocab_size;
2762 int embed_size;
2763} ccv_cnnp_model_embedding_t;
2764
2765static void _ccv_cnnp_embedding_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2766{
2767 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2768 PRINT(CCV_CLI_VERBOSE, "[cnnp_embedding_build] vocab_size: %d, embed_size: %d\n", self->vocab_size, self->embed_size)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_embedding_build] vocab_size: %d, embed_size: %d\n"
, self->vocab_size, self->embed_size); fflush(stdout); }
} while (0)
;
2769 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2769, __extension__ __PRETTY_FUNCTION__); }))
;
2770 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2770, __extension__ __PRETTY_FUNCTION__
); }))
;
2771 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2772 ccv_nnc_tensor_param_t vocab_params = params;
2773 memset(vocab_params.dim, 0, sizeof(vocab_params.dim));
2774 vocab_params.datatype = self->datatype;
2775 vocab_params.dim[0] = self->vocab_size;
2776 vocab_params.dim[1] = self->embed_size;
2777 if (!self->vocab.graph)
2778 self->vocab = ccv_nnc_tensor_symbol_new(graph, vocab_params, "vocab");
2779 assert(self->vocab.graph == graph)((void) sizeof ((self->vocab.graph == graph) ? 1 : 0), __extension__
({ if (self->vocab.graph == graph) ; else __assert_fail (
"self->vocab.graph == graph", "ccv_cnnp_model_addons.c", 2779
, __extension__ __PRETTY_FUNCTION__); }))
;
2780 ccv_nnc_tensor_param_t output_params;
2781 const ccv_nnc_cmd_t embedding = CMD_INDEX_SELECT_FORWARD()ccv_nnc_cmd(CCV_NNC_INDEX_SELECT_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
;
2782 ccv_nnc_hint_tensor_auto(embedding, (ccv_nnc_tensor_param_t []){
2783 vocab_params,
2784 params,
2785 }, 2, ccv_nnc_no_hint, &output_params, 1);
2786 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2787 ccv_nnc_graph_exec_symbol_new(graph, embedding, TENSOR_SYMBOL_LIST(self->vocab, inputs[0])(const ccv_nnc_tensor_symbol_t []){self->vocab, inputs[0]}
, (1 +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 -1)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "embedding");
2788 outputs[0] = output;
2789}
2790
2791static void _ccv_cnnp_embedding_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
2792{
2793 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2794 const float std = sqrtf(2) / sqrtf(self->vocab_size + self->embed_size);
2795 const float bound = sqrtf(3) * std;
2796 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(-bound, bound)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={-bound, bound}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->vocab);
2797}
2798
2799static void _ccv_cnnp_embedding_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
2800{
2801 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2802 add_to_array(parameters, self->vocab, is_trainable);
2803}
2804
2805static ccv_cnnp_model_t* _ccv_cnnp_embedding_copy(const ccv_cnnp_model_t* const super, void* const context);
2806
2807static const ccv_cnnp_model_vtab_t ccv_cnnp_embedding_isa = {
2808 .build = _ccv_cnnp_embedding_build,
2809 .init_states = _ccv_cnnp_embedding_init_states,
2810 .add_to_parameter = _ccv_cnnp_embedding_add_to_parameter,
2811 .copy = _ccv_cnnp_embedding_copy,
2812};
2813
2814ccv_cnnp_model_t* ccv_cnnp_embedding(const int datatype, const int vocab_size, const int embed_size, const int is_trainable, const char* const name)
2815{
2816 ccv_cnnp_model_embedding_t* const model_embedding = (ccv_cnnp_model_embedding_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_embedding_t));
2817 model_embedding->super.isa = &ccv_cnnp_embedding_isa;
2818 model_embedding->super.input_size = 1;
2819 model_embedding->super.outputs = &model_embedding->output;
2820 model_embedding->super.output_size = 1;
2821 model_embedding->super.is_trainable = is_trainable;
2822 ccv_cnnp_model_copy_name(&model_embedding->super, name);
2823 model_embedding->vocab.d = CCV_NNC_NO_TENSOR_SYMBOL;
2824 model_embedding->vocab.graph = 0;
2825 assert(datatype == CCV_32F || datatype == CCV_16F)((void) sizeof ((datatype == CCV_32F || datatype == CCV_16F) ?
1 : 0), __extension__ ({ if (datatype == CCV_32F || datatype
== CCV_16F) ; else __assert_fail ("datatype == CCV_32F || datatype == CCV_16F"
, "ccv_cnnp_model_addons.c", 2825, __extension__ __PRETTY_FUNCTION__
); }))
;
2826 model_embedding->datatype = datatype;
2827 assert(vocab_size > 0)((void) sizeof ((vocab_size > 0) ? 1 : 0), __extension__ (
{ if (vocab_size > 0) ; else __assert_fail ("vocab_size > 0"
, "ccv_cnnp_model_addons.c", 2827, __extension__ __PRETTY_FUNCTION__
); }))
;
2828 model_embedding->vocab_size = vocab_size;
2829 assert(embed_size > 0)((void) sizeof ((embed_size > 0) ? 1 : 0), __extension__ (
{ if (embed_size > 0) ; else __assert_fail ("embed_size > 0"
, "ccv_cnnp_model_addons.c", 2829, __extension__ __PRETTY_FUNCTION__
); }))
;
2830 model_embedding->embed_size = embed_size;
2831 return (ccv_cnnp_model_t*)model_embedding;
2832}
2833
2834static ccv_cnnp_model_t* _ccv_cnnp_embedding_copy(const ccv_cnnp_model_t* const super, void* const context)
2835{
2836 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2837 return ccv_cnnp_embedding(self->datatype, self->vocab_size, self->embed_size, self->super.is_trainable, self->super.name);
2838}
2839
2840// MARK - Pool Layers
2841
2842typedef struct {
2843 ccv_cnnp_model_t super;
2844 ccv_nnc_tensor_symbol_t output;
2845 int type;
2846 float width_scale;
2847 float height_scale;
2848 int align_corners;
2849} ccv_cnnp_model_upsample_t;
2850
2851static void _ccv_cnnp_upsample_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2852{
2853 PRINT(CCV_CLI_VERBOSE, "[cnnp_upsample_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_upsample_build] -\n"); fflush(stdout); } } while
(0)
;
2854 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2854, __extension__ __PRETTY_FUNCTION__); }))
;
2855 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2855, __extension__ __PRETTY_FUNCTION__
); }))
;
2856 ccv_cnnp_model_upsample_t* const self = (ccv_cnnp_model_upsample_t*)super;
2857 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2858 ccv_nnc_cmd_t cmd = CMD_UPSAMPLE_FORWARD(self->type, self->width_scale, self->height_scale, self->align_corners)ccv_nnc_cmd(CCV_NNC_UPSAMPLE_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.upsample={.type=self->type,.width_scale
=self->width_scale,.height_scale=self->height_scale,.align_corners
=self->align_corners}}), 0)
;
2859 ccv_nnc_tensor_param_t output_params;
2860 ccv_nnc_hint_tensor_auto(cmd, &params, 1, ccv_nnc_no_hint, &output_params, 1);
2861 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2862 ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0])(const ccv_nnc_tensor_symbol_t []){inputs[0]}, (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)
, TENSOR_SYMBOL_LIST(output)(const ccv_nnc_tensor_symbol_t []){output}, (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)
, "upsample");
2863 outputs[0] = output;
2864}
2865
2866static ccv_cnnp_model_t* _ccv_cnnp_upsample_copy(const ccv_cnnp_model_t* const super, void* const context);
2867
2868static const ccv_cnnp_model_vtab_t ccv_cnnp_upsample_isa = {
2869 .build = _ccv_cnnp_upsample_build,
2870 .copy = _ccv_cnnp_upsample_copy,
2871};
2872
2873ccv_cnnp_model_t* ccv_cnnp_upsample(const int type, const float width_scale, const float height_scale, const int align_corners, const char* const name)
2874{
2875 ccv_cnnp_model_upsample_t* const model_upsample = (ccv_cnnp_model_upsample_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_upsample_t));
2876 model_upsample->super.isa = &ccv_cnnp_upsample_isa;
2877 model_upsample->super.input_size = 1;
2878 model_upsample->super.outputs = &model_upsample->output;
2879 model_upsample->super.output_size = 1;
2880 ccv_cnnp_model_copy_name(&model_upsample->super, name);
2881 assert(type == CCV_NNC_UPSAMPLE_NEAREST || type == CCV_NNC_UPSAMPLE_BILINEAR)((void) sizeof ((type == CCV_NNC_UPSAMPLE_NEAREST || type == CCV_NNC_UPSAMPLE_BILINEAR
) ? 1 : 0), __extension__ ({ if (type == CCV_NNC_UPSAMPLE_NEAREST
|| type == CCV_NNC_UPSAMPLE_BILINEAR) ; else __assert_fail (
"type == CCV_NNC_UPSAMPLE_NEAREST || type == CCV_NNC_UPSAMPLE_BILINEAR"
, "ccv_cnnp_model_addons.c", 2881, __extension__ __PRETTY_FUNCTION__
); }))
;
2882 model_upsample->type = type;
2883 model_upsample->width_scale = width_scale;
2884 model_upsample->height_scale = height_scale;
2885 model_upsample->align_corners = align_corners;
2886 return (ccv_cnnp_model_t*)model_upsample;
2887}
2888
2889static ccv_cnnp_model_t* _ccv_cnnp_upsample_copy(const ccv_cnnp_model_t* const super, void* const context)
2890{
2891 const ccv_cnnp_model_upsample_t* const self = (const ccv_cnnp_model_upsample_t*)super;
2892 return ccv_cnnp_upsample(self->type, self->width_scale, self->height_scale, self->align_corners, self->super.name);
2893}
2894
2895// MARK - Reduce Sum Layer
2896
2897typedef struct {
2898 ccv_cnnp_model_t super;
2899 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
2900 int count;
2901 ccv_nnc_tensor_symbol_t output;
2902} ccv_cnnp_model_reduce_sum_t;
2903
2904static void _ccv_cnnp_reduce_sum_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2905{
2906 PRINT(CCV_CLI_VERBOSE, "[cnnp_reduce_sum_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_reduce_sum_build] -\n"); fflush(stdout); } }
while (0)
;
2907 const ccv_cnnp_model_reduce_sum_t* const self = (const ccv_cnnp_model_reduce_sum_t*)super;
2908 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2908, __extension__ __PRETTY_FUNCTION__); }))
;
2909 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2909, __extension__ __PRETTY_FUNCTION__
); }))
;
2910 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2911 ccv_nnc_tensor_param_t output_params;
2912 ccv_nnc_cmd_t reduce_sum = CMD_REDUCE_SUM_FORWARD()ccv_nnc_cmd(CCV_NNC_REDUCE_SUM_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}}
), 0)
;
2913 int i;
2914 for (i = 0; i < self->count; i++)
2915 reduce_sum.info.reduce.axis[i] = self->axis[i];
2916 reduce_sum.info.reduce.count = self->count;
2917 ccv_nnc_hint_tensor_auto(reduce_sum, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
2918 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2919 ccv_nnc_graph_exec_symbol_new(graph, reduce_sum, inputs, input_size, outputs, output_size, "reduce_sum");
2920}
2921
2922static ccv_cnnp_model_t* _ccv_cnnp_reduce_sum_copy(const ccv_cnnp_model_t* const self, void* const context);
2923
2924static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_sum_isa = {
2925 .build = _ccv_cnnp_reduce_sum_build,
2926 .copy = _ccv_cnnp_reduce_sum_copy,
2927};
2928
2929ccv_cnnp_model_t* ccv_cnnp_reduce_sum(const int* const axis, const int axis_count, const char* const name)
2930{
2931 ccv_cnnp_model_reduce_sum_t* const model_reduce_sum = (ccv_cnnp_model_reduce_sum_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_reduce_sum_t));
2932 model_reduce_sum->super.isa = &ccv_cnnp_reduce_sum_isa;
2933 model_reduce_sum->super.input_size = 1;
2934 model_reduce_sum->super.outputs = &model_reduce_sum->output;
2935 model_reduce_sum->super.output_size = 1;
2936 ccv_cnnp_model_copy_name(&model_reduce_sum->super, name);
2937 assert(axis_count <= CCV_NNC_MAX_DIM_ALLOC)((void) sizeof ((axis_count <= (12)) ? 1 : 0), __extension__
({ if (axis_count <= (12)) ; else __assert_fail ("axis_count <= CCV_NNC_MAX_DIM_ALLOC"
, "ccv_cnnp_model_addons.c", 2937, __extension__ __PRETTY_FUNCTION__
); }))
;
2938 int i;
2939 for (i = 0; i < axis_count; i++)
2940 model_reduce_sum->axis[i] = axis[i];
2941 model_reduce_sum->count = axis_count;
2942 return (ccv_cnnp_model_t*)model_reduce_sum;
2943}
2944
2945static ccv_cnnp_model_t* _ccv_cnnp_reduce_sum_copy(const ccv_cnnp_model_t* const super, void* const context)
2946{
2947 const ccv_cnnp_model_reduce_sum_t* const self = (const ccv_cnnp_model_reduce_sum_t*)super;
2948 return ccv_cnnp_reduce_sum(self->axis, self->count, self->super.name);
2949}
2950
2951// MARK - Reduce Mean Layer
2952
2953typedef struct {
2954 ccv_cnnp_model_t super;
2955 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
2956 int count;
2957 ccv_nnc_tensor_symbol_t output;
2958} ccv_cnnp_model_reduce_mean_t;
2959
2960static void _ccv_cnnp_reduce_mean_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
2961{
2962 PRINT(CCV_CLI_VERBOSE, "[cnnp_reduce_mean_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_reduce_mean_build] -\n"); fflush(stdout); } }
while (0)
;
2963 const ccv_cnnp_model_reduce_mean_t* const self = (const ccv_cnnp_model_reduce_mean_t*)super;
2964 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 2964, __extension__ __PRETTY_FUNCTION__); }))
;
2965 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 2965, __extension__ __PRETTY_FUNCTION__
); }))
;
2966 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2967 ccv_nnc_tensor_param_t output_params;
2968 ccv_nnc_cmd_t reduce_mean = CMD_REDUCE_MEAN_FORWARD()ccv_nnc_cmd(CCV_NNC_REDUCE_MEAN_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}}
), 0)
;
2969 int i;
2970 for (i = 0; i < self->count; i++)
2971 reduce_mean.info.reduce.axis[i] = self->axis[i];
2972 reduce_mean.info.reduce.count = self->count;
2973 ccv_nnc_hint_tensor_auto(reduce_mean, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
2974 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2975 ccv_nnc_graph_exec_symbol_new(graph, reduce_mean, inputs, input_size, outputs, output_size, "reduce_mean");
2976}
2977
2978static ccv_cnnp_model_t* _ccv_cnnp_reduce_mean_copy(const ccv_cnnp_model_t* const self, void* const context);
2979
2980static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_mean_isa = {
2981 .build = _ccv_cnnp_reduce_mean_build,
2982 .copy = _ccv_cnnp_reduce_mean_copy,
2983};
2984
2985ccv_cnnp_model_t* ccv_cnnp_reduce_mean(const int* const axis, const int axis_count, const char* const name)
2986{
2987 ccv_cnnp_model_reduce_mean_t* const model_reduce_mean = (ccv_cnnp_model_reduce_mean_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_reduce_mean_t));
2988 model_reduce_mean->super.isa = &ccv_cnnp_reduce_mean_isa;
2989 model_reduce_mean->super.input_size = 1;
2990 model_reduce_mean->super.outputs = &model_reduce_mean->output;
2991 model_reduce_mean->super.output_size = 1;
2992 ccv_cnnp_model_copy_name(&model_reduce_mean->super, name);
2993 assert(axis_count <= CCV_NNC_MAX_DIM_ALLOC)((void) sizeof ((axis_count <= (12)) ? 1 : 0), __extension__
({ if (axis_count <= (12)) ; else __assert_fail ("axis_count <= CCV_NNC_MAX_DIM_ALLOC"
, "ccv_cnnp_model_addons.c", 2993, __extension__ __PRETTY_FUNCTION__
); }))
;
2994 int i;
2995 for (i = 0; i < axis_count; i++)
2996 model_reduce_mean->axis[i] = axis[i];
2997 model_reduce_mean->count = axis_count;
2998 return (ccv_cnnp_model_t*)model_reduce_mean;
2999}
3000
3001static ccv_cnnp_model_t* _ccv_cnnp_reduce_mean_copy(const ccv_cnnp_model_t* const super, void* const context)
3002{
3003 const ccv_cnnp_model_reduce_mean_t* const self = (const ccv_cnnp_model_reduce_mean_t*)super;
3004 return ccv_cnnp_reduce_mean(self->axis, self->count, self->super.name);
3005}
3006
3007// MARK - Reduce Max Layer
3008
3009typedef struct {
3010 ccv_cnnp_model_t super;
3011 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
3012 int count;
3013 ccv_nnc_tensor_symbol_t output;
3014} ccv_cnnp_model_reduce_max_t;
3015
3016static void _ccv_cnnp_reduce_max_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3017{
3018 PRINT(CCV_CLI_VERBOSE, "[cnnp_reduce_max_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_reduce_max_build] -\n"); fflush(stdout); } }
while (0)
;
3019 const ccv_cnnp_model_reduce_max_t* const self = (const ccv_cnnp_model_reduce_max_t*)super;
3020 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 3020, __extension__ __PRETTY_FUNCTION__); }))
;
3021 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3021, __extension__ __PRETTY_FUNCTION__
); }))
;
3022 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3023 ccv_nnc_tensor_param_t output_params;
3024 ccv_nnc_cmd_t reduce_max = CMD_REDUCE_MAX_FORWARD()ccv_nnc_cmd(CCV_NNC_REDUCE_MAX_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}}
), 0)
;
3025 int i;
3026 for (i = 0; i < self->count; i++)
3027 reduce_max.info.reduce.axis[i] = self->axis[i];
3028 reduce_max.info.reduce.count = self->count;
3029 ccv_nnc_hint_tensor_auto(reduce_max, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3030 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3031 ccv_nnc_graph_exec_symbol_new(graph, reduce_max, inputs, input_size, outputs, output_size, "reduce_max");
3032}
3033
3034static ccv_cnnp_model_t* _ccv_cnnp_reduce_max_copy(const ccv_cnnp_model_t* const self, void* const context);
3035
3036static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_max_isa = {
3037 .build = _ccv_cnnp_reduce_max_build,
3038 .copy = _ccv_cnnp_reduce_max_copy,
3039};
3040
3041ccv_cnnp_model_t* ccv_cnnp_reduce_max(const int* const axis, const int axis_count, const char* const name)
3042{
3043 ccv_cnnp_model_reduce_max_t* const model_reduce_max = (ccv_cnnp_model_reduce_max_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_reduce_max_t));
3044 model_reduce_max->super.isa = &ccv_cnnp_reduce_max_isa;
3045 model_reduce_max->super.input_size = 1;
3046 model_reduce_max->super.outputs = &model_reduce_max->output;
3047 model_reduce_max->super.output_size = 1;
3048 ccv_cnnp_model_copy_name(&model_reduce_max->super, name);
3049 assert(axis_count <= CCV_NNC_MAX_DIM_ALLOC)((void) sizeof ((axis_count <= (12)) ? 1 : 0), __extension__
({ if (axis_count <= (12)) ; else __assert_fail ("axis_count <= CCV_NNC_MAX_DIM_ALLOC"
, "ccv_cnnp_model_addons.c", 3049, __extension__ __PRETTY_FUNCTION__
); }))
;
3050 int i;
3051 for (i = 0; i < axis_count; i++)
3052 model_reduce_max->axis[i] = axis[i];
3053 model_reduce_max->count = axis_count;
3054 return (ccv_cnnp_model_t*)model_reduce_max;
3055}
3056
3057static ccv_cnnp_model_t* _ccv_cnnp_reduce_max_copy(const ccv_cnnp_model_t* const super, void* const context)
3058{
3059 const ccv_cnnp_model_reduce_max_t* const self = (const ccv_cnnp_model_reduce_max_t*)super;
3060 return ccv_cnnp_reduce_max(self->axis, self->count, self->super.name);
3061}
3062
3063// MARK - Reduce Min Layer
3064
3065typedef struct {
3066 ccv_cnnp_model_t super;
3067 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
3068 int count;
3069 ccv_nnc_tensor_symbol_t output;
3070} ccv_cnnp_model_reduce_min_t;
3071
3072static void _ccv_cnnp_reduce_min_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3073{
3074 PRINT(CCV_CLI_VERBOSE, "[cnnp_reduce_min_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_reduce_min_build] -\n"); fflush(stdout); } }
while (0)
;
3075 const ccv_cnnp_model_reduce_min_t* const self = (const ccv_cnnp_model_reduce_min_t*)super;
3076 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 3076, __extension__ __PRETTY_FUNCTION__); }))
;
3077 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3077, __extension__ __PRETTY_FUNCTION__
); }))
;
3078 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3079 ccv_nnc_tensor_param_t output_params;
3080 ccv_nnc_cmd_t reduce_min = CMD_REDUCE_MIN_FORWARD()ccv_nnc_cmd(CCV_NNC_REDUCE_MIN_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}}
), 0)
;
3081 int i;
3082 for (i = 0; i < self->count; i++)
3083 reduce_min.info.reduce.axis[i] = self->axis[i];
3084 reduce_min.info.reduce.count = self->count;
3085 ccv_nnc_hint_tensor_auto(reduce_min, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3086 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3087 ccv_nnc_graph_exec_symbol_new(graph, reduce_min, inputs, input_size, outputs, output_size, "reduce_min");
3088}
3089
3090static ccv_cnnp_model_t* _ccv_cnnp_reduce_min_copy(const ccv_cnnp_model_t* const self, void* const context);
3091
3092static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_min_isa = {
3093 .build = _ccv_cnnp_reduce_min_build,
3094 .copy = _ccv_cnnp_reduce_min_copy,
3095};
3096
3097ccv_cnnp_model_t* ccv_cnnp_reduce_min(const int* const axis, const int axis_count, const char* const name)
3098{
3099 ccv_cnnp_model_reduce_min_t* const model_reduce_min = (ccv_cnnp_model_reduce_min_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_reduce_min_t));
3100 model_reduce_min->super.isa = &ccv_cnnp_reduce_min_isa;
3101 model_reduce_min->super.input_size = 1;
3102 model_reduce_min->super.outputs = &model_reduce_min->output;
3103 model_reduce_min->super.output_size = 1;
3104 ccv_cnnp_model_copy_name(&model_reduce_min->super, name);
3105 assert(axis_count <= CCV_NNC_MAX_DIM_ALLOC)((void) sizeof ((axis_count <= (12)) ? 1 : 0), __extension__
({ if (axis_count <= (12)) ; else __assert_fail ("axis_count <= CCV_NNC_MAX_DIM_ALLOC"
, "ccv_cnnp_model_addons.c", 3105, __extension__ __PRETTY_FUNCTION__
); }))
;
3106 int i;
3107 for (i = 0; i < axis_count; i++)
3108 model_reduce_min->axis[i] = axis[i];
3109 model_reduce_min->count = axis_count;
3110 return (ccv_cnnp_model_t*)model_reduce_min;
3111}
3112
3113static ccv_cnnp_model_t* _ccv_cnnp_reduce_min_copy(const ccv_cnnp_model_t* const super, void* const context)
3114{
3115 const ccv_cnnp_model_reduce_min_t* const self = (const ccv_cnnp_model_reduce_min_t*)super;
3116 return ccv_cnnp_reduce_min(self->axis, self->count, self->super.name);
3117}
3118
3119// MARK - Reduce Norm2 Layer
3120
3121typedef struct {
3122 ccv_cnnp_model_t super;
3123 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
3124 int count;
3125 ccv_nnc_tensor_symbol_t output;
3126} ccv_cnnp_model_reduce_norm2_t;
3127
3128static void _ccv_cnnp_reduce_norm2_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3129{
3130 const ccv_cnnp_model_reduce_norm2_t* const self = (const ccv_cnnp_model_reduce_norm2_t*)super;
3131 PRINT(CCV_CLI_VERBOSE, "[cnnp_reduce_norm2_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_reduce_norm2_build] -\n"); fflush(stdout); }
} while (0)
;
3132 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 3132, __extension__ __PRETTY_FUNCTION__); }))
;
3133 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3133, __extension__ __PRETTY_FUNCTION__
); }))
;
3134 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3135 ccv_nnc_tensor_param_t output_params;
3136 ccv_nnc_cmd_t reduce_norm2 = CMD_REDUCE_NORM2_FORWARD()ccv_nnc_cmd(CCV_NNC_REDUCE_NORM2_FORWARD, 0, ((ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}}
), 0)
;
3137 int i;
3138 for (i = 0; i < self->count; i++)
3139 reduce_norm2.info.reduce.axis[i] = self->axis[i];
3140 reduce_norm2.info.reduce.count = self->count;
3141 ccv_nnc_hint_tensor_auto(reduce_norm2, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3142 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3143 ccv_nnc_graph_exec_symbol_new(graph, reduce_norm2, inputs, input_size, outputs, output_size, "reduce_norm2");
3144}
3145
3146static ccv_cnnp_model_t* _ccv_cnnp_reduce_norm2_copy(const ccv_cnnp_model_t* const self, void* const context);
3147
3148static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_norm2_isa = {
3149 .build = _ccv_cnnp_reduce_norm2_build,
3150 .copy = _ccv_cnnp_reduce_norm2_copy,
3151};
3152
3153ccv_cnnp_model_t* ccv_cnnp_reduce_norm2(const int* const axis, const int axis_count, const char* const name)
3154{
3155 ccv_cnnp_model_reduce_norm2_t* const model_reduce_norm2 = (ccv_cnnp_model_reduce_norm2_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_reduce_norm2_t));
3156 model_reduce_norm2->super.isa = &ccv_cnnp_reduce_norm2_isa;
3157 model_reduce_norm2->super.input_size = 1;
3158 model_reduce_norm2->super.outputs = &model_reduce_norm2->output;
3159 model_reduce_norm2->super.output_size = 1;
3160 ccv_cnnp_model_copy_name(&model_reduce_norm2->super, name);
3161 assert(axis_count <= CCV_NNC_MAX_DIM_ALLOC)((void) sizeof ((axis_count <= (12)) ? 1 : 0), __extension__
({ if (axis_count <= (12)) ; else __assert_fail ("axis_count <= CCV_NNC_MAX_DIM_ALLOC"
, "ccv_cnnp_model_addons.c", 3161, __extension__ __PRETTY_FUNCTION__
); }))
;
3162 int i;
3163 for (i = 0; i < axis_count; i++)
3164 model_reduce_norm2->axis[i] = axis[i];
3165 model_reduce_norm2->count = axis_count;
3166 return (ccv_cnnp_model_t*)model_reduce_norm2;
3167}
3168
3169static ccv_cnnp_model_t* _ccv_cnnp_reduce_norm2_copy(const ccv_cnnp_model_t* const super, void* const context)
3170{
3171 const ccv_cnnp_model_reduce_norm2_t* const self = (const ccv_cnnp_model_reduce_norm2_t*)super;
3172 return ccv_cnnp_reduce_norm2(self->axis, self->count, self->super.name);
3173}
3174
3175// MARK - Argmax Layer
3176
3177typedef struct {
3178 ccv_cnnp_model_t super;
3179 int axis;
3180 ccv_nnc_tensor_symbol_t output;
3181} ccv_cnnp_model_argmax_t;
3182
3183static void _ccv_cnnp_argmax_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3184{
3185 const ccv_cnnp_model_argmax_t* const self = (const ccv_cnnp_model_argmax_t*)super;
3186 PRINT(CCV_CLI_VERBOSE, "[cnnp_argmax_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_argmax_build] -\n"); fflush(stdout); } } while
(0)
;
3187 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 3187, __extension__ __PRETTY_FUNCTION__); }))
;
3188 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3188, __extension__ __PRETTY_FUNCTION__
); }))
;
3189 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3190 ccv_nnc_tensor_param_t output_params;
3191 ccv_nnc_cmd_t argmax = CMD_ARGMAX_FORWARD()ccv_nnc_cmd(CCV_NNC_ARGMAX_FORWARD, 0, ((ccv_nnc_cmd_param_t)
{.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}})
, 0)
;
3192 argmax.info.reduce.axis[0] = self->axis;
3193 argmax.info.reduce.count = 1;
3194 ccv_nnc_hint_tensor_auto(argmax, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3195 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3196 ccv_nnc_graph_exec_symbol_new(graph, argmax, inputs, input_size, outputs, output_size, "argmax");
3197}
3198
3199static ccv_cnnp_model_t* _ccv_cnnp_argmax_copy(const ccv_cnnp_model_t* const self, void* const context);
3200
3201static const ccv_cnnp_model_vtab_t ccv_cnnp_argmax_isa = {
3202 .build = _ccv_cnnp_argmax_build,
3203 .copy = _ccv_cnnp_argmax_copy,
3204};
3205
3206ccv_cnnp_model_t* ccv_cnnp_argmax(const int axis, const char* const name)
3207{
3208 ccv_cnnp_model_argmax_t* const model_argmax = (ccv_cnnp_model_argmax_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_argmax_t));
3209 model_argmax->super.isa = &ccv_cnnp_argmax_isa;
3210 model_argmax->super.input_size = 1;
3211 model_argmax->super.outputs = &model_argmax->output;
3212 model_argmax->super.output_size = 1;
3213 ccv_cnnp_model_copy_name(&model_argmax->super, name);
3214 model_argmax->axis = axis;
3215 return (ccv_cnnp_model_t*)model_argmax;
3216}
3217
3218static ccv_cnnp_model_t* _ccv_cnnp_argmax_copy(const ccv_cnnp_model_t* const super, void* const context)
3219{
3220 const ccv_cnnp_model_argmax_t* const self = (const ccv_cnnp_model_argmax_t*)super;
3221 return ccv_cnnp_argmax(self->axis, self->super.name);
3222}
3223
3224// MARK - Argmin Layer
3225
3226typedef struct {
3227 ccv_cnnp_model_t super;
3228 int axis;
3229 ccv_nnc_tensor_symbol_t output;
3230} ccv_cnnp_model_argmin_t;
3231
3232static void _ccv_cnnp_argmin_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3233{
3234 const ccv_cnnp_model_argmin_t* const self = (const ccv_cnnp_model_argmin_t*)super;
3235 PRINT(CCV_CLI_VERBOSE, "[cnnp_argmin_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_argmin_build] -\n"); fflush(stdout); } } while
(0)
;
3236 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 3236, __extension__ __PRETTY_FUNCTION__); }))
;
3237 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3237, __extension__ __PRETTY_FUNCTION__
); }))
;
3238 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3239 ccv_nnc_tensor_param_t output_params;
3240 ccv_nnc_cmd_t argmin = CMD_ARGMIN_FORWARD()ccv_nnc_cmd(CCV_NNC_ARGMIN_FORWARD, 0, ((ccv_nnc_cmd_param_t)
{.size={.dim={1,1,1}},.reduce={.count=(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 +0 -1),.axis={}}})
, 0)
;
3241 argmin.info.reduce.axis[0] = self->axis;
3242 argmin.info.reduce.count = 1;
3243 ccv_nnc_hint_tensor_auto(argmin, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3244 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3245 ccv_nnc_graph_exec_symbol_new(graph, argmin, inputs, input_size, outputs, output_size, "argmin");
3246}
3247
3248static ccv_cnnp_model_t* _ccv_cnnp_argmin_copy(const ccv_cnnp_model_t* const self, void* const context);
3249
3250static const ccv_cnnp_model_vtab_t ccv_cnnp_argmin_isa = {
3251 .build = _ccv_cnnp_argmin_build,
3252 .copy = _ccv_cnnp_argmin_copy,
3253};
3254
3255ccv_cnnp_model_t* ccv_cnnp_argmin(const int axis, const char* const name)
3256{
3257 ccv_cnnp_model_argmin_t* const model_argmin = (ccv_cnnp_model_argmin_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_argmin_t));
3258 model_argmin->super.isa = &ccv_cnnp_argmin_isa;
3259 model_argmin->super.input_size = 1;
3260 model_argmin->super.outputs = &model_argmin->output;
3261 model_argmin->super.output_size = 1;
3262 ccv_cnnp_model_copy_name(&model_argmin->super, name);
3263 model_argmin->axis = axis;
3264 return (ccv_cnnp_model_t*)model_argmin;
3265}
3266
3267static ccv_cnnp_model_t* _ccv_cnnp_argmin_copy(const ccv_cnnp_model_t* const super, void* const context)
3268{
3269 const ccv_cnnp_model_argmin_t* const self = (const ccv_cnnp_model_argmin_t*)super;
3270 return ccv_cnnp_argmin(self->axis, self->super.name);
3271}
3272
3273// MARK - Min Layer
3274
3275typedef struct {
3276 ccv_cnnp_model_t super;
3277 ccv_nnc_tensor_symbol_t output;
3278} ccv_cnnp_model_min_t;
3279
3280static void _ccv_cnnp_min_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3281{
3282 PRINT(CCV_CLI_VERBOSE, "[cnnp_min_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_min_build] -\n"); fflush(stdout); } } while (
0)
;
3283 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 3283, __extension__ __PRETTY_FUNCTION__); }))
;
3284 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3284, __extension__ __PRETTY_FUNCTION__
); }))
;
3285 ccv_nnc_tensor_param_t input_params[2];
3286 int i;
3287 for (i = 0; i < 2; i++)
3288 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
3289 ccv_nnc_tensor_param_t output_params;
3290 const ccv_nnc_cmd_t min = CMD_MIN_FORWARD()ccv_nnc_cmd(CCV_NNC_MIN_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}}}, 0)
;
3291 ccv_nnc_hint_tensor_auto(min, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
3292 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3293 ccv_nnc_graph_exec_symbol_new(graph, min, inputs, input_size, outputs, output_size, "min");
3294}
3295
3296static ccv_cnnp_model_t* _ccv_cnnp_min_copy(const ccv_cnnp_model_t* const self, void* const context);
3297
3298static const ccv_cnnp_model_vtab_t ccv_cnnp_min_isa = {
3299 .build = _ccv_cnnp_min_build,
3300 .copy = _ccv_cnnp_min_copy,
3301};
3302
3303ccv_cnnp_model_t* ccv_cnnp_min(const char* const name)
3304{
3305 ccv_cnnp_model_min_t* const model_min = (ccv_cnnp_model_min_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_min_t));
3306 model_min->super.isa = &ccv_cnnp_min_isa;
3307 model_min->super.input_size = 2;
3308 model_min->super.outputs = &model_min->output;
3309 model_min->super.output_size = 1;
3310 ccv_cnnp_model_copy_name(&model_min->super, name);
3311 return (ccv_cnnp_model_t*)model_min;
3312}
3313
3314static ccv_cnnp_model_t* _ccv_cnnp_min_copy(const ccv_cnnp_model_t* const super, void* const context)
3315{
3316 const ccv_cnnp_model_min_t* const self = (const ccv_cnnp_model_min_t*)super;
3317 return ccv_cnnp_min(self->super.name);
3318}
3319
3320// MARK - Max Layer
3321
3322typedef struct {
3323 ccv_cnnp_model_t super;
3324 ccv_nnc_tensor_symbol_t output;
3325} ccv_cnnp_model_max_t;
3326
3327static void _ccv_cnnp_max_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3328{
3329 PRINT(CCV_CLI_VERBOSE, "[cnnp_max_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_max_build] -\n"); fflush(stdout); } } while (
0)
;
3330 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 3330, __extension__ __PRETTY_FUNCTION__); }))
;
3331 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3331, __extension__ __PRETTY_FUNCTION__
); }))
;
3332 ccv_nnc_tensor_param_t input_params[2];
3333 int i;
3334 for (i = 0; i < 2; i++)
3335 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
3336 ccv_nnc_tensor_param_t output_params;
3337 const ccv_nnc_cmd_t max = CMD_MAX_FORWARD()ccv_nnc_cmd(CCV_NNC_MAX_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}}}, 0)
;
3338 ccv_nnc_hint_tensor_auto(max, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
3339 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3340 ccv_nnc_graph_exec_symbol_new(graph, max, inputs, input_size, outputs, output_size, "max");
3341}
3342
3343static ccv_cnnp_model_t* _ccv_cnnp_max_copy(const ccv_cnnp_model_t* const self, void* const context);
3344
3345static const ccv_cnnp_model_vtab_t ccv_cnnp_max_isa = {
3346 .build = _ccv_cnnp_max_build,
3347 .copy = _ccv_cnnp_max_copy,
3348};
3349
3350ccv_cnnp_model_t* ccv_cnnp_max(const char* const name)
3351{
3352 ccv_cnnp_model_max_t* const model_max = (ccv_cnnp_model_max_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_max_t));
3353 model_max->super.isa = &ccv_cnnp_max_isa;
3354 model_max->super.input_size = 2;
3355 model_max->super.outputs = &model_max->output;
3356 model_max->super.output_size = 1;
3357 ccv_cnnp_model_copy_name(&model_max->super, name);
3358 return (ccv_cnnp_model_t*)model_max;
3359}
3360
3361static ccv_cnnp_model_t* _ccv_cnnp_max_copy(const ccv_cnnp_model_t* const super, void* const context)
3362{
3363 const ccv_cnnp_model_max_t* const self = (const ccv_cnnp_model_max_t*)super;
3364 return ccv_cnnp_max(self->super.name);
3365}
3366
3367// MARK - LSTM Layer
3368
3369typedef struct {
3370 ccv_cnnp_model_t super;
3371 int masked;
3372 ccv_nnc_tensor_symbol_t output;
3373 ccv_nnc_tensor_symbol_t weights;
3374 ccv_nnc_tensor_symbol_t reserves;
3375 ccv_nnc_cmd_param_t params;
3376 ccv_nnc_graph_exec_symbol_t lstm;
3377} ccv_cnnp_model_lstm_t;
3378
3379static int _ccv_cnnp_lstm_weight_dim(int bidirectional, int num_layers, int input_size, int hidden_size, int proj_size, int bias)
3380{
3381 const int D = !!bidirectional + 1;
3382 if (hidden_size == proj_size)
3383 return (num_layers * (bias ? 8 : 0) + (num_layers - 1) * (hidden_size * 4 * D + hidden_size * 4) + input_size * 4 + hidden_size * 4) * D;
3384 else
3385 return (num_layers * (bias ? 8 : 0) + (num_layers - 1) * (proj_size * 4 * D + proj_size * 4) + (proj_size * 4 + input_size * 4) + num_layers * proj_size) * D;
3386}
3387
3388static void _ccv_cnnp_lstm_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3389{
3390 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3391 PRINT(CCV_CLI_VERBOSE, "[cnnp_lstm_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_lstm_build] -\n"); fflush(stdout); } } while
(0)
;
3392 assert(input_size == self->super.input_size)((void) sizeof ((input_size == self->super.input_size) ? 1
: 0), __extension__ ({ if (input_size == self->super.input_size
) ; else __assert_fail ("input_size == self->super.input_size"
, "ccv_cnnp_model_addons.c", 3392, __extension__ __PRETTY_FUNCTION__
); }))
;
3393 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3393, __extension__ __PRETTY_FUNCTION__
); }))
;
3394 const int proj_size = self->params.rnn.proj_size == 0 ? self->params.rnn.hidden_size : self->params.rnn.proj_size;
3395 ccv_nnc_tensor_param_t input_params[5];
3396 input_params[0]= ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3397 if (input_size == 2)
3398 input_params[1] = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
3399 input_params[4] = input_params[0];
3400 memset(input_params[4].dim, 0, sizeof(input_params[4].dim));
3401 const int x_nd = ccv_nnc_tensor_nd(input_params[0].dim);
3402 const int feature_count = input_params[0].dim[x_nd - 1];
3403 input_params[4].dim[0] = _ccv_cnnp_lstm_weight_dim(self->params.rnn.bidirectional, self->params.rnn.num_layers, feature_count, self->params.rnn.hidden_size, proj_size, self->params.rnn.bias);
3404 input_params[4].dim[1] = self->params.rnn.hidden_size;
3405 const ccv_nnc_cmd_t lstm = ccv_nnc_cmd(CCV_NNC_LSTM_FORWARD, 0, self->params, 0);
3406 ccv_nnc_tensor_param_t output_params[4];
3407 ccv_nnc_hint_tensor_auto(lstm, input_params, 5, ccv_nnc_no_hint, output_params, 4);
3408 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
3409 if (!self->weights.graph)
3410 self->weights = ccv_nnc_tensor_symbol_new(graph, input_params[4], "weights");
3411 if (!self->reserves.graph)
3412 self->reserves = ccv_nnc_tensor_symbol_new(graph, output_params[3], "reserves");
3413 const ccv_nnc_tensor_symbol_t mask = input_size == 2 ? inputs[1] : NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3414 self->lstm = ccv_nnc_graph_exec_symbol_new(graph, lstm, TENSOR_SYMBOL_LIST(inputs[0], mask, NO_TENSOR_SYMBOL, NO_TENSOR_SYMBOL, self->weights)(const ccv_nnc_tensor_symbol_t []){inputs[0], mask, (const ccv_nnc_tensor_symbol_t
){.d = CCV_NNC_NO_TENSOR_SYMBOL}, (const ccv_nnc_tensor_symbol_t
){.d = CCV_NNC_NO_TENSOR_SYMBOL}, self->weights}, (1 +1 +1
+1 +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 -
1)
, TENSOR_SYMBOL_LIST(outputs[0], NO_TENSOR_SYMBOL, NO_TENSOR_SYMBOL, self->reserves)(const ccv_nnc_tensor_symbol_t []){outputs[0], (const ccv_nnc_tensor_symbol_t
){.d = CCV_NNC_NO_TENSOR_SYMBOL}, (const ccv_nnc_tensor_symbol_t
){.d = CCV_NNC_NO_TENSOR_SYMBOL}, self->reserves}, (1 +1 +
1 +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
-1)
, "lstm");
3415}
3416
3417static void _ccv_cnnp_lstm_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
3418{
3419 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3420 if (self->weights.graph)
3421 {
3422 const float stdv = 1.0 / sqrt(self->params.rnn.hidden_size);
3423 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(-stdv, stdv)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={-stdv, stdv}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->weights);
3424 }
3425}
3426
3427static void _ccv_cnnp_lstm_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
3428{
3429 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3430 if (self->weights.graph)
3431 add_to_array(parameters, self->weights, is_trainable);
3432}
3433
3434static void _ccv_cnnp_lstm_set_is_test(ccv_cnnp_model_t* const super, const int is_test, const ccv_cnnp_cmd_updater_f updater, void* const context)
3435{
3436 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3437 if (self->lstm.graph)
3438 {
3439 self->params.rnn.is_test = is_test;
3440 updater(context, self->lstm, ccv_nnc_cmd(CCV_NNC_LSTM_FORWARD, 0, self->params, 0), ccv_nnc_no_hint);
3441 }
3442}
3443
3444static ccv_cnnp_model_t* _ccv_cnnp_lstm_copy(const ccv_cnnp_model_t* const self, void* const context);
3445
3446static const ccv_cnnp_model_vtab_t ccv_cnnp_lstm_isa = {
3447 .build = _ccv_cnnp_lstm_build,
3448 .init_states = _ccv_cnnp_lstm_init_states,
3449 .add_to_parameter = _ccv_cnnp_lstm_add_to_parameter,
3450 .copy = _ccv_cnnp_lstm_copy,
3451 .set_is_test = _ccv_cnnp_lstm_set_is_test,
3452};
3453
3454ccv_cnnp_model_t* ccv_cnnp_lstm(const int masked, const int hidden_size, const int proj_size, const int num_layers, const int bias, const int batch_first, const int bidirectional, const float dropout, const int is_trainable, const char* const name)
3455{
3456 ccv_cnnp_model_lstm_t* const model_lstm = (ccv_cnnp_model_lstm_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_lstm_t));
3457 model_lstm->super.isa = &ccv_cnnp_lstm_isa;
3458 model_lstm->super.input_size = masked ? 2 : 1;
3459 model_lstm->super.outputs = &model_lstm->output;
3460 model_lstm->super.output_size = 1;
3461 model_lstm->super.is_trainable = is_trainable;
3462 ccv_cnnp_model_copy_name(&model_lstm->super, name);
3463 model_lstm->masked = masked;
3464 model_lstm->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
3465 model_lstm->weights.graph = 0;
3466 model_lstm->params.rnn.hidden_size = hidden_size;
3467 model_lstm->params.rnn.proj_size = proj_size;
3468 model_lstm->params.rnn.num_layers = num_layers;
3469 model_lstm->params.rnn.bias = bias;
3470 model_lstm->params.rnn.batch_first = batch_first;
3471 model_lstm->params.rnn.bidirectional = bidirectional;
3472 model_lstm->params.rnn.dropout = dropout;
3473 return (ccv_cnnp_model_t*)model_lstm;
3474}
3475
3476static ccv_cnnp_model_t* _ccv_cnnp_lstm_copy(const ccv_cnnp_model_t* const super, void* const context)
3477{
3478 const ccv_cnnp_model_lstm_t* const self = (const ccv_cnnp_model_lstm_t*)super;
3479 return ccv_cnnp_lstm(self->masked, self->params.rnn.hidden_size, self->params.rnn.proj_size, self->params.rnn.num_layers, self->params.rnn.bias, self->params.rnn.batch_first, self->params.rnn.bidirectional, self->params.rnn.dropout, self->super.is_trainable, self->super.name);
3480}
3481
3482/// MARK - Datatype conversion layer.
3483
3484typedef struct {
3485 ccv_cnnp_model_t super;
3486 ccv_nnc_tensor_symbol_t output;
3487 int datatype;
3488 int ref_to_last;
3489} ccv_cnnp_model_datatype_conversion_t;
3490
3491static void _ccv_cnnp_datatype_conversion_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3492{
3493 ccv_cnnp_model_datatype_conversion_t* const self = (ccv_cnnp_model_datatype_conversion_t*)super;
3494 PRINT(CCV_CLI_VERBOSE, "[cnnp_datatype_conversion_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_datatype_conversion_build] -\n"); fflush(stdout
); } } while (0)
;
3495 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3496 if (self->ref_to_last)
3497 {
3498 assert(input_size > 1)((void) sizeof ((input_size > 1) ? 1 : 0), __extension__ (
{ if (input_size > 1) ; else __assert_fail ("input_size > 1"
, "ccv_cnnp_model_addons.c", 3498, __extension__ __PRETTY_FUNCTION__
); }))
;
3499 const ccv_nnc_tensor_param_t last_params = ccv_nnc_tensor_symbol_params(graph, inputs[input_size - 1]);
3500 params.datatype = last_params.datatype;
3501 } else
3502 params.datatype = self->datatype;
3503 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3503, __extension__ __PRETTY_FUNCTION__
); }))
;
3504 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3505 ccv_nnc_graph_exec_symbol_new(graph, CMD_DATATYPE_CONVERSION_FORWARD()ccv_nnc_cmd(CCV_NNC_DATATYPE_CONVERSION_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, inputs, output_size, outputs, output_size, 0);
3506}
3507
3508static ccv_cnnp_model_t* _ccv_cnnp_datatype_conversion_copy(const ccv_cnnp_model_t* const self, void* const context);
3509
3510static const ccv_cnnp_model_vtab_t ccv_cnnp_datatype_conversion_isa = {
3511 .build = _ccv_cnnp_datatype_conversion_build,
3512 .copy = _ccv_cnnp_datatype_conversion_copy,
3513};
3514
3515ccv_cnnp_model_t* ccv_cnnp_datatype_conversion(const int datatype, const int ref_to_last, const char* const name)
3516{
3517 ccv_cnnp_model_datatype_conversion_t* const model_datatype_conversion = (ccv_cnnp_model_datatype_conversion_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_datatype_conversion_t));
3518 model_datatype_conversion->super.isa = &ccv_cnnp_datatype_conversion_isa;
3519 model_datatype_conversion->super.input_size = 0;
3520 model_datatype_conversion->super.outputs = &model_datatype_conversion->output;
3521 model_datatype_conversion->super.output_size = 1;
3522 model_datatype_conversion->datatype = datatype;
3523 model_datatype_conversion->ref_to_last = ref_to_last;
3524 ccv_cnnp_model_copy_name(&model_datatype_conversion->super, name);
3525 return (ccv_cnnp_model_t*)model_datatype_conversion;
3526}
3527
3528static ccv_cnnp_model_t* _ccv_cnnp_datatype_conversion_copy(const ccv_cnnp_model_t* const super, void* const context)
3529{
3530 ccv_cnnp_model_datatype_conversion_t* const self = (ccv_cnnp_model_datatype_conversion_t*)super;
3531 return ccv_cnnp_datatype_conversion(self->datatype, self->ref_to_last, self->super.name);
3532}
3533
3534/// MARK - Clamp layer.
3535
3536typedef struct {
3537 ccv_cnnp_model_t super;
3538 ccv_nnc_tensor_symbol_t output;
3539 float min;
3540 float max;
3541} ccv_cnnp_model_clamp_t;
3542
3543static void _ccv_cnnp_clamp_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3544{
3545 ccv_cnnp_model_clamp_t* const self = (ccv_cnnp_model_clamp_t*)super;
3546 PRINT(CCV_CLI_VERBOSE, "[cnnp_clamp_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_clamp_build] -\n"); fflush(stdout); } } while
(0)
;
3547 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3548 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3548, __extension__ __PRETTY_FUNCTION__
); }))
;
3549 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3550 ccv_nnc_graph_exec_symbol_new(graph, CMD_CLAMP_FORWARD(self->min, self->max)ccv_nnc_cmd(CCV_NNC_CLAMP_FORWARD, 0, (ccv_nnc_cmd_param_t){.
size={.dim={1,1,1}},.clamp={.min=self->min,.max=self->max
}}, 0)
, inputs, output_size, outputs, output_size, 0);
3551}
3552
3553static ccv_cnnp_model_t* _ccv_cnnp_clamp_copy(const ccv_cnnp_model_t* const self, void* const context);
3554
3555static const ccv_cnnp_model_vtab_t ccv_cnnp_clamp_isa = {
3556 .build = _ccv_cnnp_clamp_build,
3557 .copy = _ccv_cnnp_clamp_copy,
3558};
3559
3560ccv_cnnp_model_t* ccv_cnnp_clamp(const float min, const float max, const char* const name)
3561{
3562 ccv_cnnp_model_clamp_t* const model_clamp = (ccv_cnnp_model_clamp_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_clamp_t));
3563 model_clamp->super.isa = &ccv_cnnp_clamp_isa;
3564 model_clamp->super.input_size = 0;
3565 model_clamp->super.outputs = &model_clamp->output;
3566 model_clamp->super.output_size = 1;
3567 model_clamp->min = min;
3568 model_clamp->max = max;
3569 ccv_cnnp_model_copy_name(&model_clamp->super, name);
3570 return (ccv_cnnp_model_t*)model_clamp;
3571}
3572
3573static ccv_cnnp_model_t* _ccv_cnnp_clamp_copy(const ccv_cnnp_model_t* const super, void* const context)
3574{
3575 ccv_cnnp_model_clamp_t* const self = (ccv_cnnp_model_clamp_t*)super;
3576 return ccv_cnnp_clamp(self->min, self->max, self->super.name);
3577}
3578
3579// MARK - Parameter Layer
3580
3581typedef struct {
3582 ccv_cnnp_model_t super;
3583 float init_bound;
3584 ccv_nnc_tensor_symbol_t weights;
3585 ccv_nnc_tensor_param_t weights_params;
3586 ccv_nnc_tensor_symbol_t output;
3587} ccv_cnnp_model_parameter_t;
3588
3589static void _ccv_cnnp_parameter_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3590{
3591 PRINT(CCV_CLI_VERBOSE, "[cnnp_parameter_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_parameter_build] -\n"); fflush(stdout); } } while
(0)
;
3592 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3592, __extension__ __PRETTY_FUNCTION__
); }))
;
3593 ccv_cnnp_model_parameter_t* const self = (ccv_cnnp_model_parameter_t*)super;
3594 if (!self->weights.graph)
3595 self->weights = ccv_nnc_tensor_symbol_new(graph, self->weights_params, "weights");
3596 assert(self->weights.graph == graph)((void) sizeof ((self->weights.graph == graph) ? 1 : 0), __extension__
({ if (self->weights.graph == graph) ; else __assert_fail
("self->weights.graph == graph", "ccv_cnnp_model_addons.c"
, 3596, __extension__ __PRETTY_FUNCTION__); }))
;
3597 outputs[0] = self->weights;
3598}
3599
3600static void _ccv_cnnp_parameter_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
3601{
3602 ccv_cnnp_model_parameter_t* const self = (ccv_cnnp_model_parameter_t*)super;
3603 if (self->init_bound > 0)
3604 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(-self->init_bound, self->init_bound)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={-self->init_bound, self->
init_bound}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->weights);
3605 else
3606 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->weights);
3607}
3608
3609static void _ccv_cnnp_parameter_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
3610{
3611 ccv_cnnp_model_parameter_t* const self = (ccv_cnnp_model_parameter_t*)super;
3612 add_to_array(parameters, self->weights, is_trainable);
3613}
3614
3615static ccv_cnnp_model_t* _ccv_cnnp_parameter_copy(const ccv_cnnp_model_t* const super, void* const context);
3616
3617static const ccv_cnnp_model_vtab_t ccv_cnnp_parameter_isa = {
3618 .build = _ccv_cnnp_parameter_build,
3619 .init_states = _ccv_cnnp_parameter_init_states,
3620 .add_to_parameter = _ccv_cnnp_parameter_add_to_parameter,
3621 .copy = _ccv_cnnp_parameter_copy,
3622};
3623
3624ccv_cnnp_model_t* ccv_cnnp_parameter(const ccv_nnc_tensor_param_t params, const float init_bound, const int is_trainable, const char* const name)
3625{
3626 ccv_cnnp_model_parameter_t* const model_parameter = (ccv_cnnp_model_parameter_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_parameter_t));
3627 model_parameter->super.isa = &ccv_cnnp_parameter_isa;
3628 model_parameter->super.input_size = 0;
3629 model_parameter->super.outputs = &model_parameter->output;
3630 model_parameter->super.output_size = 1;
3631 model_parameter->super.is_trainable = is_trainable;
3632 ccv_cnnp_model_copy_name(&model_parameter->super, name);
3633 model_parameter->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
3634 model_parameter->weights.graph = 0;
3635 model_parameter->weights_params = params;
3636 return (ccv_cnnp_model_t*)model_parameter;
3637}
3638
3639static ccv_cnnp_model_t* _ccv_cnnp_parameter_copy(const ccv_cnnp_model_t* const super, void* const context)
3640{
3641 const ccv_cnnp_model_parameter_t* const self = (const ccv_cnnp_model_parameter_t*)super;
3642 return ccv_cnnp_parameter(self->weights_params, self->init_bound, self->super.is_trainable, self->super.name);
3643}
3644
3645// MARK - Scalar Layer
3646
3647typedef struct {
3648 ccv_cnnp_model_t super;
3649 int type;
3650 int format;
3651 int datatype;
3652 float value;
3653 ccv_nnc_tensor_symbol_t output;
3654} ccv_cnnp_model_scalar_t;
3655
3656static void _ccv_cnnp_scalar_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3657{
3658 PRINT(CCV_CLI_VERBOSE, "[cnnp_scalar_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_scalar_build] -\n"); fflush(stdout); } } while
(0)
;
3659 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3659, __extension__ __PRETTY_FUNCTION__
); }))
;
3660 ccv_cnnp_model_scalar_t* const self = (ccv_cnnp_model_scalar_t*)super;
3661 ccv_nnc_tensor_param_t params = {
3662 .type = self->type,
3663 .format = self->format,
3664 .datatype = self->datatype,
3665 .dim = {
3666 1
3667 }
3668 };
3669 if (input_size > 0)
3670 {
3671 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3672 params.type = input_params.type;
3673 params.format = input_params.format;
3674 params.datatype = input_params.datatype;
3675 }
3676 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3677 ccv_nnc_graph_exec_symbol_new(graph, CMD_SET_FORWARD(self->value)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={self->value,}}}, 0)
, 0, 0, outputs, 1, 0);
3678}
3679
3680static ccv_cnnp_model_t* _ccv_cnnp_scalar_copy(const ccv_cnnp_model_t* const super, void* const context);
3681
3682static const ccv_cnnp_model_vtab_t ccv_cnnp_scalar_isa = {
3683 .build = _ccv_cnnp_scalar_build,
3684 .copy = _ccv_cnnp_scalar_copy,
3685};
3686
3687ccv_cnnp_model_t* ccv_cnnp_scalar(const int type, const int format, const int datatype, const float value, const char* const name)
3688{
3689 ccv_cnnp_model_scalar_t* const model_scalar = (ccv_cnnp_model_scalar_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_scalar_t));
3690 model_scalar->super.isa = &ccv_cnnp_scalar_isa;
3691 model_scalar->super.input_size = 0;
3692 model_scalar->super.outputs = &model_scalar->output;
3693 model_scalar->super.output_size = 1;
3694 ccv_cnnp_model_copy_name(&model_scalar->super, name);
3695 model_scalar->type = type;
3696 model_scalar->format = format;
3697 model_scalar->datatype = datatype;
3698 model_scalar->value = value;
3699 return (ccv_cnnp_model_t*)model_scalar;
3700}
3701
3702static ccv_cnnp_model_t* _ccv_cnnp_scalar_copy(const ccv_cnnp_model_t* const super, void* const context)
3703{
3704 const ccv_cnnp_model_scalar_t* const self = (const ccv_cnnp_model_scalar_t*)super;
3705 return ccv_cnnp_scalar(self->type, self->format, self->datatype, self->value, self->super.name);
3706}
3707
3708// MARK - Variable Layer
3709
3710typedef struct {
3711 ccv_cnnp_model_t super;
3712 ccv_nnc_tensor_param_t params;
3713 ccv_nnc_tensor_symbol_t output;
3714} ccv_cnnp_model_variable_t;
3715
3716static void _ccv_cnnp_variable_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3717{
3718 PRINT(CCV_CLI_VERBOSE, "[cnnp_variable_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_variable_build] -\n"); fflush(stdout); } } while
(0)
;
3719 assert(input_size == 0)((void) sizeof ((input_size == 0) ? 1 : 0), __extension__ ({ if
(input_size == 0) ; else __assert_fail ("input_size == 0", "ccv_cnnp_model_addons.c"
, 3719, __extension__ __PRETTY_FUNCTION__); }))
;
3720 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3720, __extension__ __PRETTY_FUNCTION__
); }))
;
3721 ccv_cnnp_model_variable_t* const self = (ccv_cnnp_model_variable_t*)super;
3722 outputs[0] = ccv_nnc_tensor_symbol_new(graph, self->params, 0);
3723}
3724
3725static ccv_cnnp_model_t* _ccv_cnnp_variable_copy(const ccv_cnnp_model_t* const super, void* const context);
3726
3727static const ccv_cnnp_model_vtab_t ccv_cnnp_variable_isa = {
3728 .build = _ccv_cnnp_variable_build,
3729 .copy = _ccv_cnnp_variable_copy,
3730};
3731
3732ccv_cnnp_model_t* ccv_cnnp_variable(const ccv_nnc_tensor_param_t params, const char* const name)
3733{
3734 ccv_cnnp_model_variable_t* const model_variable = (ccv_cnnp_model_variable_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_variable_t));
3735 model_variable->super.isa = &ccv_cnnp_variable_isa;
3736 model_variable->super.input_size = 0;
3737 model_variable->super.outputs = &model_variable->output;
3738 model_variable->super.output_size = 1;
3739 ccv_cnnp_model_copy_name(&model_variable->super, name);
3740 model_variable->params = params;
3741 return (ccv_cnnp_model_t*)model_variable;
3742}
3743
3744static ccv_cnnp_model_t* _ccv_cnnp_variable_copy(const ccv_cnnp_model_t* const super, void* const context)
3745{
3746 const ccv_cnnp_model_variable_t* const self = (const ccv_cnnp_model_variable_t*)super;
3747 return ccv_cnnp_variable(self->params, self->super.name);
3748}
3749
3750// MARK - Move Layer
3751
3752typedef struct {
3753 ccv_cnnp_model_t super;
3754 ccv_nnc_tensor_symbol_t output;
3755} ccv_cnnp_model_move_t;
3756
3757static void _ccv_cnnp_move_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3758{
3759 PRINT(CCV_CLI_VERBOSE, "[cnnp_move_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_move_build] -\n"); fflush(stdout); } } while
(0)
;
3760 assert(input_size == 2)((void) sizeof ((input_size == 2) ? 1 : 0), __extension__ ({ if
(input_size == 2) ; else __assert_fail ("input_size == 2", "ccv_cnnp_model_addons.c"
, 3760, __extension__ __PRETTY_FUNCTION__); }))
;
3761 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3761, __extension__ __PRETTY_FUNCTION__
); }))
;
3762 outputs[0] = inputs[1];
3763 ccv_nnc_graph_exec_symbol_new(graph, CMD_FORMAT_TRANSFORM_FORWARD()ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, inputs, 1, outputs, 1, "move");
3764}
3765
3766static ccv_cnnp_model_t* _ccv_cnnp_move_copy(const ccv_cnnp_model_t* const super, void* const context);
3767
3768static const ccv_cnnp_model_vtab_t ccv_cnnp_move_isa = {
3769 .build = _ccv_cnnp_move_build,
3770 .copy = _ccv_cnnp_move_copy,
3771};
3772
3773ccv_cnnp_model_t* ccv_cnnp_move(const char* const name)
3774{
3775 ccv_cnnp_model_move_t* const model_move = (ccv_cnnp_model_move_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_move_t));
3776 model_move->super.isa = &ccv_cnnp_move_isa;
3777 model_move->super.input_size = 2;
3778 model_move->super.outputs = &model_move->output;
3779 model_move->super.output_size = 1;
3780 ccv_cnnp_model_copy_name(&model_move->super, name);
3781 return (ccv_cnnp_model_t*)model_move;
3782}
3783
3784static ccv_cnnp_model_t* _ccv_cnnp_move_copy(const ccv_cnnp_model_t* const super, void* const context)
3785{
3786 const ccv_cnnp_model_move_t* const self = (const ccv_cnnp_model_move_t*)super;
3787 return ccv_cnnp_move(self->super.name);
3788}
3789
3790// MARK - "Making" Contiguous Layer
3791
3792typedef struct {
3793 ccv_cnnp_model_t super;
3794 ccv_nnc_tensor_symbol_t output;
3795} ccv_cnnp_model_contiguous_t;
3796
3797static void _ccv_cnnp_contiguous_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3798{
3799 PRINT(CCV_CLI_VERBOSE, "[cnnp_contiguous_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_contiguous_build] -\n"); fflush(stdout); } }
while (0)
;
3800 assert(input_size == 1)((void) sizeof ((input_size == 1) ? 1 : 0), __extension__ ({ if
(input_size == 1) ; else __assert_fail ("input_size == 1", "ccv_cnnp_model_addons.c"
, 3800, __extension__ __PRETTY_FUNCTION__); }))
;
3801 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3801, __extension__ __PRETTY_FUNCTION__
); }))
;
3802 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3803 ccv_nnc_tensor_symbol_t to = ccv_nnc_tensor_symbol_alias_to(graph, inputs[0]);
3804 if (to.d == CCV_NNC_NO_TENSOR_SYMBOL) // If we are not reshape an alias, it is straightforward.
3805 {
3806 outputs[0] = inputs[0];
3807 return;
3808 }
3809 // Otherwise, we need to check its stride to know if it is contiguous.
3810 int old_stride[CCV_NNC_MAX_DIM_ALLOC(12)];
3811 ccv_nnc_tensor_symbol_alias_params(graph, inputs[0], 0, old_stride);
3812 // We identify permute by checking if the stride is not in descending order.
3813 // This also covered "permute" through reshape, rather than using ccv_cnnp_permute directly.
3814 if (ccv_nnc_is_tensor_stride_packed(old_stride, params.dim))
3815 {
3816 outputs[0] = inputs[0];
3817 return;
3818 }
3819 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3820 ccv_nnc_graph_exec_symbol_t make_contiguous = ccv_nnc_graph_exec_symbol_new(graph, CMD_FORMAT_TRANSFORM_FORWARD()ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
, inputs, 1, outputs, 1, "contiguous");
3821 ccv_nnc_graph_exec_symbol_set_flags(graph, make_contiguous, CCV_NNC_GRAPH_EXEC_DISABLE_OPT);
3822}
3823
3824static ccv_cnnp_model_t* _ccv_cnnp_contiguous_copy(const ccv_cnnp_model_t* const super, void* const context);
3825
3826static const ccv_cnnp_model_vtab_t ccv_cnnp_contiguous_isa = {
3827 .build = _ccv_cnnp_contiguous_build,
3828 .copy = _ccv_cnnp_contiguous_copy,
3829};
3830
3831ccv_cnnp_model_t* ccv_cnnp_contiguous(const char* const name)
3832{
3833 ccv_cnnp_model_contiguous_t* const model_contiguous = (ccv_cnnp_model_contiguous_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_contiguous_t));
3834 model_contiguous->super.isa = &ccv_cnnp_contiguous_isa;
3835 model_contiguous->super.input_size = 1;
3836 model_contiguous->super.outputs = &model_contiguous->output;
3837 model_contiguous->super.output_size = 1;
3838 ccv_cnnp_model_copy_name(&model_contiguous->super, name);
3839 return (ccv_cnnp_model_t*)model_contiguous;
3840}
3841
3842static ccv_cnnp_model_t* _ccv_cnnp_contiguous_copy(const ccv_cnnp_model_t* const super, void* const context)
3843{
3844 const ccv_cnnp_model_contiguous_t* const self = (const ccv_cnnp_model_contiguous_t*)super;
3845 return ccv_cnnp_contiguous(self->super.name);
3846}
3847
3848// MARK - Scaled-Dot Product Attention Layer
3849
3850typedef struct {
3851 ccv_cnnp_model_t super;
3852 ccv_nnc_tensor_symbol_t output;
3853 ccv_nnc_tensor_symbol_t weights;
3854 ccv_nnc_tensor_symbol_t bias;
3855 float scale;
3856 int is_causal;
3857 int has_attn_mask;
3858 int upcast;
3859 int fused_unify_head_weights;
3860 int no_bias;
3861} ccv_cnnp_model_scaled_dot_product_attention_t;
3862
3863static void _ccv_cnnp_scaled_dot_product_attention_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size)
3864{
3865 PRINT(CCV_CLI_VERBOSE, "[cnnp_scaled_dot_product_attention_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_scaled_dot_product_attention_build] -\n"); fflush
(stdout); } } while (0)
;
3866 assert(input_size == 3 || input_size == 4)((void) sizeof ((input_size == 3 || input_size == 4) ? 1 : 0)
, __extension__ ({ if (input_size == 3 || input_size == 4) ; else
__assert_fail ("input_size == 3 || input_size == 4", "ccv_cnnp_model_addons.c"
, 3866, __extension__ __PRETTY_FUNCTION__); }))
;
3867 assert(output_size == 1)((void) sizeof ((output_size == 1) ? 1 : 0), __extension__ ({
if (output_size == 1) ; else __assert_fail ("output_size == 1"
, "ccv_cnnp_model_addons.c", 3867, __extension__ __PRETTY_FUNCTION__
); }))
;
3868 ccv_cnnp_model_scaled_dot_product_attention_t* const self = (ccv_cnnp_model_scaled_dot_product_attention_t*)super;
3869 const ccv_nnc_tensor_param_t q_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3870 const ccv_nnc_tensor_param_t k_params = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
3871 const ccv_nnc_tensor_param_t v_params = ccv_nnc_tensor_symbol_params(graph, inputs[2]);
3872 const int v_nd = ccv_nnc_tensor_nd(v_params.dim);
3873 assert(v_nd == 3 || v_nd == 4)((void) sizeof ((v_nd == 3 || v_nd == 4) ? 1 : 0), __extension__
({ if (v_nd == 3 || v_nd == 4) ; else __assert_fail ("v_nd == 3 || v_nd == 4"
, "ccv_cnnp_model_addons.c", 3873, __extension__ __PRETTY_FUNCTION__
); }))
;
3874 const int hEv = (v_nd == 3 ? 1 : v_params.dim[2]) * v_params.dim[v_nd - 1];
3875 ccv_nnc_tensor_param_t weights_params = q_params;
3876 memset(weights_params.dim, 0, sizeof(weights_params.dim));
3877 weights_params.dim[0] = hEv;
3878 weights_params.dim[1] = hEv;
3879 ccv_nnc_tensor_param_t bias_params = q_params;
3880 memset(bias_params.dim, 0, sizeof(bias_params.dim));
3881 bias_params.dim[0] = hEv;
3882 ccv_nnc_cmd_t cmd = {0};
3883 cmd.cmd = CCV_NNC_SCALED_DOT_PRODUCT_ATTENTION_FORWARD;
3884 cmd.info.scaled_dot_product_attention.scale = self->scale;
3885 cmd.info.scaled_dot_product_attention.is_causal = self->is_causal;
3886 cmd.info.scaled_dot_product_attention.upcast = self->upcast;
3887 ccv_nnc_tensor_param_t output_params[3];
3888 ccv_nnc_tensor_symbol_t output;
3889 ccv_nnc_tensor_symbol_t saved_softmax;
3890 ccv_nnc_tensor_symbol_t saved_v_proj = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3891 ccv_nnc_tensor_symbol_t attn_mask = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3892 ccv_nnc_tensor_symbol_t weights = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3893 ccv_nnc_tensor_symbol_t bias = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3894 if (self->has_attn_mask)
3895 attn_mask = inputs[3];
3896 if (self->fused_unify_head_weights)
3897 {
3898 if (!self->weights.graph)
3899 self->weights = ccv_nnc_tensor_symbol_new(graph, weights_params, "weights");
3900 weights = self->weights;
3901 if (!self->no_bias)
3902 {
3903 if (!self->bias.graph)
3904 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
3905 bias = self->bias;
3906 }
3907 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
3908 q_params,
3909 k_params,
3910 v_params,
3911 (ccv_nnc_tensor_param_t){},
3912 weights_params,
3913 bias_params,
3914 }, 6, ccv_nnc_no_hint, output_params, 3);
3915 output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
3916 saved_softmax = ccv_nnc_tensor_symbol_new(graph, output_params[1], 0);
3917 saved_v_proj = ccv_nnc_tensor_symbol_new(graph, output_params[2], 0);
3918 } else {
3919 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
3920 q_params,
3921 k_params,
3922 v_params,
3923 }, 3, ccv_nnc_no_hint, output_params, 2);
3924 output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
3925 saved_softmax = ccv_nnc_tensor_symbol_new(graph, output_params[1], 0);
3926 }
3927 ccv_nnc_graph_exec_symbol_new(graph, cmd, TENSOR_SYMBOL_LIST(inputs[0], inputs[1], inputs[2], attn_mask, weights, bias)(const ccv_nnc_tensor_symbol_t []){inputs[0], inputs[1], inputs
[2], attn_mask, weights, bias}, (1 +1 +1 +1 +1 +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 -1)
, TENSOR_SYMBOL_LIST(output, saved_softmax, saved_v_proj)(const ccv_nnc_tensor_symbol_t []){output, saved_softmax, saved_v_proj
}, (1 +1 +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 -1)
, "scaled_dot_product_attention");
3928 outputs[0] = output;
3929}
3930
3931static void _ccv_cnnp_scaled_dot_product_attention_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context)
3932{
3933 ccv_cnnp_model_scaled_dot_product_attention_t* const self = (ccv_cnnp_model_scaled_dot_product_attention_t*)super;
3934 if (self->weights.graph)
3935 {
3936 assert(self->fused_unify_head_weights)((void) sizeof ((self->fused_unify_head_weights) ? 1 : 0),
__extension__ ({ if (self->fused_unify_head_weights) ; else
__assert_fail ("self->fused_unify_head_weights", "ccv_cnnp_model_addons.c"
, 3936, __extension__ __PRETTY_FUNCTION__); }))
;
3937 const ccv_nnc_tensor_param_t weight_params = ccv_nnc_tensor_symbol_params(graph, self->weights);
3938 const int c = weight_params.dim[1];
3939 const float std = sqrtf(2) / sqrtf(c);
3940 const float bound = sqrtf(3) * std;
3941 initializer(context, CMD_RANDOM_UNIFORM_FORWARD(-bound, bound)ccv_nnc_cmd(CCV_NNC_RANDOM_UNIFORM_FORWARD, 0, (ccv_nnc_cmd_param_t
){.size={.dim={1,1,1}},.blas={.a={-bound, bound}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->weights);
3942 if (self->bias.graph)
3943 initializer(context, CMD_SET_FORWARD(0)ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, (ccv_nnc_cmd_param_t){.size
={.dim={1,1,1}},.blas={.a={0,}}}, 0)
, ccv_nnc_no_hint, 0, 0, self->bias);
3944 }
3945}
3946
3947static void _ccv_cnnp_scaled_dot_product_attention_add_to_parameter(ccv_cnnp_model_t* const super, const ccv_cnnp_add_to_array_f add_to_array, void* const parameters, const int is_trainable)
3948{
3949 ccv_cnnp_model_scaled_dot_product_attention_t* const self = (ccv_cnnp_model_scaled_dot_product_attention_t*)super;
3950 if (self->weights.graph)
3951 {
3952 assert(self->fused_unify_head_weights)((void) sizeof ((self->fused_unify_head_weights) ? 1 : 0),
__extension__ ({ if (self->fused_unify_head_weights) ; else
__assert_fail ("self->fused_unify_head_weights", "ccv_cnnp_model_addons.c"
, 3952, __extension__ __PRETTY_FUNCTION__); }))
;
3953 add_to_array(parameters, self->weights, is_trainable);
3954 if (self->bias.graph)
3955 add_to_array(parameters, self->bias, is_trainable);
3956 }
3957}
3958
3959static ccv_cnnp_model_t* _ccv_cnnp_scaled_dot_product_attention_copy(const ccv_cnnp_model_t* const super, void* const context);
3960
3961static const ccv_cnnp_model_vtab_t ccv_cnnp_scaled_dot_product_attention_isa = {
3962 .build = _ccv_cnnp_scaled_dot_product_attention_build,
3963 .copy = _ccv_cnnp_scaled_dot_product_attention_copy,
3964};
3965
3966static const ccv_cnnp_model_vtab_t ccv_cnnp_scaled_dot_product_attention_fused_isa = {
3967 .build = _ccv_cnnp_scaled_dot_product_attention_build,
3968 .init_states = _ccv_cnnp_scaled_dot_product_attention_init_states,
3969 .add_to_parameter = _ccv_cnnp_scaled_dot_product_attention_add_to_parameter,
3970 .copy = _ccv_cnnp_scaled_dot_product_attention_copy,
3971};
3972
3973ccv_cnnp_model_t* ccv_cnnp_scaled_dot_product_attention(const float scale, const int is_causal, const int has_attn_mask, const int upcast, const int fused_unify_head_weights, const int no_bias, const int is_trainable, const char* const name)
3974{
3975 ccv_cnnp_model_scaled_dot_product_attention_t* const model_scaled_dot_product_attention = (ccv_cnnp_model_scaled_dot_product_attention_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_scaled_dot_product_attention_t));
3976 model_scaled_dot_product_attention->super.isa = fused_unify_head_weights ? &ccv_cnnp_scaled_dot_product_attention_fused_isa : &ccv_cnnp_scaled_dot_product_attention_isa;
3977 model_scaled_dot_product_attention->super.input_size = has_attn_mask ? 4 : 3;
3978 model_scaled_dot_product_attention->super.outputs = &model_scaled_dot_product_attention->output;
3979 model_scaled_dot_product_attention->super.output_size = 1;
3980 model_scaled_dot_product_attention->super.is_trainable = is_trainable;
3981 ccv_cnnp_model_copy_name(&model_scaled_dot_product_attention->super, name);
3982 model_scaled_dot_product_attention->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
3983 model_scaled_dot_product_attention->weights.graph = 0;
3984 model_scaled_dot_product_attention->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
3985 model_scaled_dot_product_attention->bias.graph = 0;
3986 model_scaled_dot_product_attention->scale = scale;
3987 model_scaled_dot_product_attention->is_causal = is_causal;
3988 model_scaled_dot_product_attention->has_attn_mask = has_attn_mask;
3989 model_scaled_dot_product_attention->upcast = upcast;
3990 model_scaled_dot_product_attention->fused_unify_head_weights = fused_unify_head_weights;
3991 model_scaled_dot_product_attention->no_bias = no_bias;
3992 return (ccv_cnnp_model_t*)model_scaled_dot_product_attention;
3993}
3994
3995static ccv_cnnp_model_t* _ccv_cnnp_scaled_dot_product_attention_copy(const ccv_cnnp_model_t* const super, void* const context)
3996{
3997 const ccv_cnnp_model_scaled_dot_product_attention_t* const self = (const ccv_cnnp_model_scaled_dot_product_attention_t*)super;
3998 return ccv_cnnp_scaled_dot_product_attention(self->scale, self->is_causal, self->has_attn_mask, self->upcast, self->fused_unify_head_weights, self->no_bias, self->super.is_trainable, self->super.name);
3999}