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/19 -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 -I /usr/local/include -internal-isystem /usr/local/lib/clang/19/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-11-08-093000-160011-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 int flags;
1349} ccv_cnnp_model_dense_t;
1350
1351static 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)
1352{
1353 ccv_cnnp_model_dense_t* const self = (ccv_cnnp_model_dense_t*)super;
1354 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)
;
1355 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"
, 1355, __extension__ __PRETTY_FUNCTION__); }))
;
1356 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", 1356, __extension__ __PRETTY_FUNCTION__
); }))
;
1357 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1358 ccv_nnc_tensor_param_t weights_params = params;
1359 memset(weights_params.dim, 0, sizeof(weights_params.dim));
1360 weights_params.dim[0] = self->count;
1361 weights_params.dim[1] = params.dim[ccv_nnc_tensor_nd(params.dim) - 1];
1362 if (!self->weights.graph)
1363 self->weights = ccv_nnc_tensor_symbol_new(graph, weights_params, "weights");
1364 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"
, 1364, __extension__ __PRETTY_FUNCTION__); }))
;
1365 ccv_nnc_tensor_param_t bias_params = params;
1366 memset(bias_params.dim, 0, sizeof(bias_params.dim));
1367 bias_params.dim[0] = self->count;
1368 ccv_nnc_cmd_t cmd = {0};
1369 cmd.cmd = CCV_NNC_GEMM_FORWARD;
1370 cmd.info.blas.a[0] = 1;
1371 cmd.info.blas.a[1] = 1;
1372 cmd.info.blas.transpose_b[0] = 0;
1373 cmd.info.blas.transpose_b[1] = 1;
1374 cmd.info.blas.flags = self->flags;
1375 ccv_nnc_tensor_param_t output_params;
1376 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
1377 params,
1378 weights_params,
1379 bias_params,
1380 }, 3, ccv_nnc_no_hint, &output_params, 1);
1381 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1382 if (self->no_bias)
1383 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");
1384 else {
1385 if (!self->bias.graph)
1386 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
1387 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");
1388 }
1389 outputs[0] = output;
1390}
1391
1392static 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)
1393{
1394 ccv_cnnp_model_dense_t* const self = (ccv_cnnp_model_dense_t*)super;
1395 const ccv_nnc_tensor_param_t weight_params = ccv_nnc_tensor_symbol_params(graph, self->weights);
1396 const int c = weight_params.dim[1];
1397 const float std = sqrtf(2) / sqrtf(c);
1398 const float bound = sqrtf(3) * std;
1399 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);
1400 if (self->bias.graph)
1401 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);
1402}
1403
1404static 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)
1405{
1406 ccv_cnnp_model_dense_t* const self = (ccv_cnnp_model_dense_t*)super;
1407 add_to_array(parameters, self->weights, is_trainable);
1408 if (self->bias.graph)
1409 add_to_array(parameters, self->bias, is_trainable);
1410}
1411
1412static ccv_cnnp_model_t* _ccv_cnnp_dense_copy(const ccv_cnnp_model_t* const super, void* const context);
1413
1414static const ccv_cnnp_model_vtab_t ccv_cnnp_dense_isa = {
1415 .build = _ccv_cnnp_dense_build,
1416 .init_states = _ccv_cnnp_dense_init_states,
1417 .add_to_parameter = _ccv_cnnp_dense_add_to_parameter,
1418 .copy = _ccv_cnnp_dense_copy,
1419};
1420
1421ccv_cnnp_model_t* ccv_cnnp_dense(const int count, const int no_bias, const int flags, const int is_trainable, const char* const name)
1422{
1423 ccv_cnnp_model_dense_t* const model_dense = (ccv_cnnp_model_dense_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_dense_t));
1424 model_dense->super.isa = &ccv_cnnp_dense_isa;
1425 model_dense->super.input_size = 1;
1426 model_dense->super.outputs = &model_dense->output;
1427 model_dense->super.output_size = 1;
1428 model_dense->super.is_trainable = is_trainable;
1429 ccv_cnnp_model_copy_name(&model_dense->super, name);
1430 model_dense->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
1431 model_dense->weights.graph = 0;
1432 model_dense->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
1433 model_dense->bias.graph = 0;
1434 model_dense->count = count;
1435 model_dense->no_bias = no_bias;
1436 model_dense->flags = flags;
1437 return (ccv_cnnp_model_t*)model_dense;
1438}
1439
1440static ccv_cnnp_model_t* _ccv_cnnp_dense_copy(const ccv_cnnp_model_t* const super, void* const context)
1441{
1442 const ccv_cnnp_model_dense_t* const self = (const ccv_cnnp_model_dense_t*)super;
1443 return ccv_cnnp_dense(self->count, self->no_bias, self->flags, self->super.is_trainable, self->super.name);
1444}
1445
1446// MARK - Pool Layers
1447
1448typedef struct {
1449 ccv_cnnp_model_t super;
1450 ccv_nnc_tensor_symbol_t output;
1451 int kdim[CCV_NNC_MAX_DIM_ALLOC(12)];
1452 ccv_nnc_hint_t hint;
1453} ccv_cnnp_model_pool_t;
1454
1455static 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)
1456{
1457 ccv_cnnp_model_pool_t* const self = (ccv_cnnp_model_pool_t*)super;
1458 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)
;
1459 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"
, 1459, __extension__ __PRETTY_FUNCTION__); }))
;
1460 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", 1460, __extension__ __PRETTY_FUNCTION__
); }))
;
1461 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1462 const int hw = ccv_nnc_tensor_hw(params, ccv_nnc_tensor_nd(params.dim));
1463 ccv_nnc_cmd_t cmd;
1464 if (hw >= 0 && self->kdim[0] == 0 && self->kdim[1] == 0)
1465 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)
;
1466 else
1467 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)
;
1468 ccv_nnc_tensor_param_t output_params;
1469 ccv_nnc_hint_tensor_auto(cmd, &params, 1, self->hint, &output_params, 1);
1470 const ccv_nnc_tensor_symbol_t pool_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1471 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");
1472 ccv_nnc_graph_exec_symbol_set_hint(graph, exec, self->hint);
1473 outputs[0] = pool_output;
1474}
1475
1476static ccv_cnnp_model_t* _ccv_cnnp_max_pool_copy(const ccv_cnnp_model_t* const super, void* const context);
1477
1478static const ccv_cnnp_model_vtab_t ccv_cnnp_max_pool_isa = {
1479 .build = _ccv_cnnp_max_pool_build,
1480 .copy = _ccv_cnnp_max_pool_copy,
1481};
1482
1483ccv_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)
1484{
1485 ccv_cnnp_model_pool_t* const model_pool = (ccv_cnnp_model_pool_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_pool_t));
1486 model_pool->super.isa = &ccv_cnnp_max_pool_isa;
1487 model_pool->super.input_size = 1;
1488 model_pool->super.outputs = &model_pool->output;
1489 model_pool->super.output_size = 1;
1490 ccv_cnnp_model_copy_name(&model_pool->super, name);
1491 memcpy(model_pool->kdim, kdim, sizeof(model_pool->kdim));
1492 model_pool->hint = hint;
1493 return (ccv_cnnp_model_t*)model_pool;
1494}
1495
1496static ccv_cnnp_model_t* _ccv_cnnp_max_pool_copy(const ccv_cnnp_model_t* const super, void* const context)
1497{
1498 const ccv_cnnp_model_pool_t* const self = (const ccv_cnnp_model_pool_t*)super;
1499 return ccv_cnnp_max_pool(self->kdim, self->hint, self->super.name);
1500}
1501
1502static 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)
1503{
1504 ccv_cnnp_model_pool_t* const self = (ccv_cnnp_model_pool_t*)super;
1505 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)
;
1506 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"
, 1506, __extension__ __PRETTY_FUNCTION__); }))
;
1507 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", 1507, __extension__ __PRETTY_FUNCTION__
); }))
;
1508 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1509 const int hw = ccv_nnc_tensor_hw(params, ccv_nnc_tensor_nd(params.dim));
1510 ccv_nnc_cmd_t cmd;
1511 if (hw >= 0 && self->kdim[0] == 0 && self->kdim[1] == 0)
1512 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)
;
1513 else
1514 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)
;
1515 ccv_nnc_tensor_param_t output_params;
1516 ccv_nnc_hint_tensor_auto(cmd, &params, 1, self->hint, &output_params, 1);
1517 const ccv_nnc_tensor_symbol_t pool_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1518 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");
1519 ccv_nnc_graph_exec_symbol_set_hint(graph, exec, self->hint);
1520 outputs[0] = pool_output;
1521}
1522
1523static ccv_cnnp_model_t* _ccv_cnnp_average_pool_copy(const ccv_cnnp_model_t* const super, void* const context);
1524
1525static const ccv_cnnp_model_vtab_t ccv_cnnp_average_pool_isa = {
1526 .build = _ccv_cnnp_average_pool_build,
1527 .copy = _ccv_cnnp_average_pool_copy,
1528};
1529
1530ccv_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)
1531{
1532 ccv_cnnp_model_pool_t* const model_pool = (ccv_cnnp_model_pool_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_pool_t));
1533 model_pool->super.isa = &ccv_cnnp_average_pool_isa;
1534 model_pool->super.input_size = 1;
1535 model_pool->super.outputs = &model_pool->output;
1536 model_pool->super.output_size = 1;
1537 ccv_cnnp_model_copy_name(&model_pool->super, name);
1538 memcpy(model_pool->kdim, kdim, sizeof(model_pool->kdim));
1539 model_pool->hint = hint;
1540 return (ccv_cnnp_model_t*)model_pool;
1541}
1542
1543static ccv_cnnp_model_t* _ccv_cnnp_average_pool_copy(const ccv_cnnp_model_t* const super, void* const context)
1544{
1545 const ccv_cnnp_model_pool_t* const self = (const ccv_cnnp_model_pool_t*)super;
1546 return ccv_cnnp_average_pool(self->kdim, self->hint, self->super.name);
1547}
1548
1549// MARK - RELU Layer
1550
1551typedef struct {
1552 ccv_cnnp_model_t super;
1553 ccv_nnc_tensor_symbol_t output;
1554} ccv_cnnp_model_relu_t;
1555
1556static 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)
1557{
1558 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)
;
1559 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"
, 1559, __extension__ __PRETTY_FUNCTION__); }))
;
1560 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", 1560, __extension__ __PRETTY_FUNCTION__
); }))
;
1561 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1562 ccv_nnc_tensor_param_t output_params;
1563 const ccv_nnc_cmd_t relu = CMD_RELU_FORWARD()ccv_nnc_cmd(CCV_NNC_RELU_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1564 ccv_nnc_hint_tensor_auto(relu, (ccv_nnc_tensor_param_t []){
1565 params,
1566 }, 1, ccv_nnc_no_hint, &output_params, 1);
1567 const ccv_nnc_tensor_symbol_t relu_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1568 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");
1569 outputs[0] = relu_output;
1570}
1571
1572static ccv_cnnp_model_t* _ccv_cnnp_relu_copy(const ccv_cnnp_model_t* const self, void* const context);
1573
1574static const ccv_cnnp_model_vtab_t ccv_cnnp_relu_isa = {
1575 .build = _ccv_cnnp_relu_build,
1576 .copy = _ccv_cnnp_relu_copy,
1577};
1578
1579ccv_cnnp_model_t* ccv_cnnp_relu(const char* const name)
1580{
1581 ccv_cnnp_model_relu_t* const model_relu = (ccv_cnnp_model_relu_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_relu_t));
1582 model_relu->super.isa = &ccv_cnnp_relu_isa;
1583 model_relu->super.input_size = 1;
1584 model_relu->super.outputs = &model_relu->output;
1585 model_relu->super.output_size = 1;
1586 ccv_cnnp_model_copy_name(&model_relu->super, name);
1587 return (ccv_cnnp_model_t*)model_relu;
1588}
1589
1590static ccv_cnnp_model_t* _ccv_cnnp_relu_copy(const ccv_cnnp_model_t* const self, void* const context)
1591{
1592 return ccv_cnnp_relu(self->name);
1593}
1594
1595// MARK - Sigmoid Layer
1596
1597typedef struct {
1598 ccv_cnnp_model_t super;
1599 ccv_nnc_tensor_symbol_t output;
1600} ccv_cnnp_model_sigmoid_t;
1601
1602static 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)
1603{
1604 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)
;
1605 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"
, 1605, __extension__ __PRETTY_FUNCTION__); }))
;
1606 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", 1606, __extension__ __PRETTY_FUNCTION__
); }))
;
1607 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1608 ccv_nnc_tensor_param_t output_params;
1609 const ccv_nnc_cmd_t sigmoid = CMD_SIGMOID_FORWARD()ccv_nnc_cmd(CCV_NNC_SIGMOID_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1610 ccv_nnc_hint_tensor_auto(sigmoid, (ccv_nnc_tensor_param_t []){
1611 params,
1612 }, 1, ccv_nnc_no_hint, &output_params, 1);
1613 const ccv_nnc_tensor_symbol_t sigmoid_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1614 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");
1615 outputs[0] = sigmoid_output;
1616}
1617
1618static ccv_cnnp_model_t* _ccv_cnnp_sigmoid_copy(const ccv_cnnp_model_t* const self, void* const context);
1619
1620static const ccv_cnnp_model_vtab_t ccv_cnnp_sigmoid_isa = {
1621 .build = _ccv_cnnp_sigmoid_build,
1622 .copy = _ccv_cnnp_sigmoid_copy,
1623};
1624
1625ccv_cnnp_model_t* ccv_cnnp_sigmoid(const char* const name)
1626{
1627 ccv_cnnp_model_sigmoid_t* const model_sigmoid = (ccv_cnnp_model_sigmoid_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_sigmoid_t));
1628 model_sigmoid->super.isa = &ccv_cnnp_sigmoid_isa;
1629 model_sigmoid->super.input_size = 1;
1630 model_sigmoid->super.outputs = &model_sigmoid->output;
1631 model_sigmoid->super.output_size = 1;
1632 ccv_cnnp_model_copy_name(&model_sigmoid->super, name);
1633 return (ccv_cnnp_model_t*)model_sigmoid;
1634}
1635
1636static ccv_cnnp_model_t* _ccv_cnnp_sigmoid_copy(const ccv_cnnp_model_t* const self, void* const context)
1637{
1638 return ccv_cnnp_sigmoid(self->name);
1639}
1640
1641// MARK - Tanh Layer
1642
1643typedef struct {
1644 ccv_cnnp_model_t super;
1645 ccv_nnc_tensor_symbol_t output;
1646} ccv_cnnp_model_tanh_t;
1647
1648static 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)
1649{
1650 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)
;
1651 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"
, 1651, __extension__ __PRETTY_FUNCTION__); }))
;
1652 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", 1652, __extension__ __PRETTY_FUNCTION__
); }))
;
1653 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1654 ccv_nnc_tensor_param_t output_params;
1655 const ccv_nnc_cmd_t tanh = CMD_TANH_FORWARD()ccv_nnc_cmd(CCV_NNC_TANH_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1656 ccv_nnc_hint_tensor_auto(tanh, (ccv_nnc_tensor_param_t []){
1657 params,
1658 }, 1, ccv_nnc_no_hint, &output_params, 1);
1659 const ccv_nnc_tensor_symbol_t tanh_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1660 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");
1661 outputs[0] = tanh_output;
1662}
1663
1664static ccv_cnnp_model_t* _ccv_cnnp_tanh_copy(const ccv_cnnp_model_t* const self, void* const context);
1665
1666static const ccv_cnnp_model_vtab_t ccv_cnnp_tanh_isa = {
1667 .build = _ccv_cnnp_tanh_build,
1668 .copy = _ccv_cnnp_tanh_copy,
1669};
1670
1671ccv_cnnp_model_t* ccv_cnnp_tanh(const char* const name)
1672{
1673 ccv_cnnp_model_tanh_t* const model_tanh = (ccv_cnnp_model_tanh_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_tanh_t));
1674 model_tanh->super.isa = &ccv_cnnp_tanh_isa;
1675 model_tanh->super.input_size = 1;
1676 model_tanh->super.outputs = &model_tanh->output;
1677 model_tanh->super.output_size = 1;
1678 ccv_cnnp_model_copy_name(&model_tanh->super, name);
1679 return (ccv_cnnp_model_t*)model_tanh;
1680}
1681
1682static ccv_cnnp_model_t* _ccv_cnnp_tanh_copy(const ccv_cnnp_model_t* const self, void* const context)
1683{
1684 return ccv_cnnp_tanh(self->name);
1685}
1686
1687// MARK - Swish Layer
1688
1689typedef struct {
1690 ccv_cnnp_model_t super;
1691 ccv_nnc_tensor_symbol_t output;
1692} ccv_cnnp_model_swish_t;
1693
1694static 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)
1695{
1696 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)
;
1697 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"
, 1697, __extension__ __PRETTY_FUNCTION__); }))
;
1698 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", 1698, __extension__ __PRETTY_FUNCTION__
); }))
;
1699 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1700 ccv_nnc_tensor_param_t output_params;
1701 const ccv_nnc_cmd_t swish = CMD_SWISH_FORWARD()ccv_nnc_cmd(CCV_NNC_SWISH_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1702 ccv_nnc_hint_tensor_auto(swish, (ccv_nnc_tensor_param_t []){
1703 params,
1704 }, 1, ccv_nnc_no_hint, &output_params, 1);
1705 const ccv_nnc_tensor_symbol_t swish_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1706 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");
1707 outputs[0] = swish_output;
1708}
1709
1710static ccv_cnnp_model_t* _ccv_cnnp_swish_copy(const ccv_cnnp_model_t* const self, void* const context);
1711
1712static const ccv_cnnp_model_vtab_t ccv_cnnp_swish_isa = {
1713 .build = _ccv_cnnp_swish_build,
1714 .copy = _ccv_cnnp_swish_copy,
1715};
1716
1717ccv_cnnp_model_t* ccv_cnnp_swish(const char* const name)
1718{
1719 ccv_cnnp_model_swish_t* const model_swish = (ccv_cnnp_model_swish_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_swish_t));
1720 model_swish->super.isa = &ccv_cnnp_swish_isa;
1721 model_swish->super.input_size = 1;
1722 model_swish->super.outputs = &model_swish->output;
1723 model_swish->super.output_size = 1;
1724 ccv_cnnp_model_copy_name(&model_swish->super, name);
1725 return (ccv_cnnp_model_t*)model_swish;
1726}
1727
1728static ccv_cnnp_model_t* _ccv_cnnp_swish_copy(const ccv_cnnp_model_t* const self, void* const context)
1729{
1730 return ccv_cnnp_swish(self->name);
1731}
1732
1733// MARK - GELU Layer
1734
1735typedef struct {
1736 ccv_cnnp_model_t super;
1737 ccv_nnc_tensor_symbol_t output;
1738 int tanh;
1739} ccv_cnnp_model_gelu_t;
1740
1741static 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)
1742{
1743 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)
;
1744 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"
, 1744, __extension__ __PRETTY_FUNCTION__); }))
;
1745 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", 1745, __extension__ __PRETTY_FUNCTION__
); }))
;
1746 ccv_cnnp_model_gelu_t* const self = (ccv_cnnp_model_gelu_t*)super;
1747 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1748 ccv_nnc_tensor_param_t output_params;
1749 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)
;
1750 ccv_nnc_hint_tensor_auto(gelu, (ccv_nnc_tensor_param_t []){
1751 params,
1752 }, 1, ccv_nnc_no_hint, &output_params, 1);
1753 const ccv_nnc_tensor_symbol_t gelu_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1754 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");
1755 outputs[0] = gelu_output;
1756}
1757
1758static ccv_cnnp_model_t* _ccv_cnnp_gelu_copy(const ccv_cnnp_model_t* const self, void* const context);
1759
1760static const ccv_cnnp_model_vtab_t ccv_cnnp_gelu_isa = {
1761 .build = _ccv_cnnp_gelu_build,
1762 .copy = _ccv_cnnp_gelu_copy,
1763};
1764
1765ccv_cnnp_model_t* ccv_cnnp_gelu(const int tanh, const char* const name)
1766{
1767 ccv_cnnp_model_gelu_t* const model_gelu = (ccv_cnnp_model_gelu_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_gelu_t));
1768 model_gelu->super.isa = &ccv_cnnp_gelu_isa;
1769 model_gelu->super.input_size = 1;
1770 model_gelu->super.outputs = &model_gelu->output;
1771 model_gelu->super.output_size = 1;
1772 model_gelu->tanh = tanh;
1773 ccv_cnnp_model_copy_name(&model_gelu->super, name);
1774 return (ccv_cnnp_model_t*)model_gelu;
1775}
1776
1777static ccv_cnnp_model_t* _ccv_cnnp_gelu_copy(const ccv_cnnp_model_t* const super, void* const context)
1778{
1779 ccv_cnnp_model_gelu_t* const self = (ccv_cnnp_model_gelu_t*)super;
1780 return ccv_cnnp_gelu(self->tanh, self->super.name);
1781}
1782
1783// MARK - Leaky ReLU Layer
1784
1785typedef struct {
1786 ccv_cnnp_model_t super;
1787 ccv_nnc_tensor_symbol_t output;
1788 float negative_slope;
1789} ccv_cnnp_model_leaky_relu_t;
1790
1791static 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)
1792{
1793 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)
;
1794 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"
, 1794, __extension__ __PRETTY_FUNCTION__); }))
;
1795 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", 1795, __extension__ __PRETTY_FUNCTION__
); }))
;
1796 ccv_cnnp_model_leaky_relu_t* const self = (ccv_cnnp_model_leaky_relu_t*)super;
1797 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1798 ccv_nnc_tensor_param_t output_params;
1799 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)
;
1800 ccv_nnc_hint_tensor_auto(leaky_relu, (ccv_nnc_tensor_param_t []){
1801 params,
1802 }, 1, ccv_nnc_no_hint, &output_params, 1);
1803 const ccv_nnc_tensor_symbol_t leaky_relu_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1804 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");
1805 outputs[0] = leaky_relu_output;
1806}
1807
1808static ccv_cnnp_model_t* _ccv_cnnp_leaky_relu_copy(const ccv_cnnp_model_t* const self, void* const context);
1809
1810static const ccv_cnnp_model_vtab_t ccv_cnnp_leaky_relu_isa = {
1811 .build = _ccv_cnnp_leaky_relu_build,
1812 .copy = _ccv_cnnp_leaky_relu_copy,
1813};
1814
1815ccv_cnnp_model_t* ccv_cnnp_leaky_relu(const float negative_slope, const char* const name)
1816{
1817 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));
1818 model_leaky_relu->super.isa = &ccv_cnnp_leaky_relu_isa;
1819 model_leaky_relu->super.input_size = 1;
1820 model_leaky_relu->super.outputs = &model_leaky_relu->output;
1821 model_leaky_relu->super.output_size = 1;
1822 model_leaky_relu->negative_slope = negative_slope;
1823 ccv_cnnp_model_copy_name(&model_leaky_relu->super, name);
1824 return (ccv_cnnp_model_t*)model_leaky_relu;
1825}
1826
1827static ccv_cnnp_model_t* _ccv_cnnp_leaky_relu_copy(const ccv_cnnp_model_t* const super, void* const context)
1828{
1829 ccv_cnnp_model_leaky_relu_t* const self = (ccv_cnnp_model_leaky_relu_t*)super;
1830 return ccv_cnnp_leaky_relu(self->negative_slope, self->super.name);
1831}
1832
1833// MARK - Softmax Layer
1834
1835typedef struct {
1836 ccv_cnnp_model_t super;
1837 ccv_nnc_tensor_symbol_t output;
1838} ccv_cnnp_model_softmax_t;
1839
1840static 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)
1841{
1842 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)
;
1843 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"
, 1843, __extension__ __PRETTY_FUNCTION__); }))
;
1844 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", 1844, __extension__ __PRETTY_FUNCTION__
); }))
;
1845 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1846 ccv_nnc_tensor_param_t output_params;
1847 const ccv_nnc_cmd_t softmax = CMD_SOFTMAX_FORWARD()ccv_nnc_cmd(CCV_NNC_SOFTMAX_FORWARD, 0, ccv_nnc_cmd_auto, 0);
1848 ccv_nnc_hint_tensor_auto(softmax, (ccv_nnc_tensor_param_t []){
1849 params,
1850 }, 1, ccv_nnc_no_hint, &output_params, 1);
1851 const ccv_nnc_tensor_symbol_t softmax_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1852 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");
1853 outputs[0] = softmax_output;
1854}
1855
1856static ccv_cnnp_model_t* _ccv_cnnp_softmax_copy(const ccv_cnnp_model_t* const self, void* const context);
1857
1858static const ccv_cnnp_model_vtab_t ccv_cnnp_softmax_isa = {
1859 .build = _ccv_cnnp_softmax_build,
1860 .copy = _ccv_cnnp_softmax_copy,
1861};
1862
1863ccv_cnnp_model_t* ccv_cnnp_softmax(const char* const name)
1864{
1865 ccv_cnnp_model_softmax_t* const model_softmax = (ccv_cnnp_model_softmax_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_softmax_t));
1866 model_softmax->super.isa = &ccv_cnnp_softmax_isa;
1867 model_softmax->super.input_size = 1;
1868 model_softmax->super.outputs = &model_softmax->output;
1869 model_softmax->super.output_size = 1;
1870 ccv_cnnp_model_copy_name(&model_softmax->super, name);
1871 return (ccv_cnnp_model_t*)model_softmax;
1872}
1873
1874static ccv_cnnp_model_t* _ccv_cnnp_softmax_copy(const ccv_cnnp_model_t* const self, void* const context)
1875{
1876 return ccv_cnnp_softmax(self->name);
1877}
1878
1879// MARK - Add Layer
1880
1881typedef struct {
1882 ccv_cnnp_model_t super;
1883 float p;
1884 float q;
1885 ccv_nnc_tensor_symbol_t output;
1886} ccv_cnnp_model_add_t;
1887
1888static 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)
1889{
1890 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)
;
1891 const ccv_cnnp_model_add_t* const self = (const ccv_cnnp_model_add_t*)super;
1892 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"
, 1892, __extension__ __PRETTY_FUNCTION__); }))
;
1893 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", 1893, __extension__ __PRETTY_FUNCTION__
); }))
;
1894 ccv_nnc_tensor_param_t input_params[2];
1895 int i;
1896 for (i = 0; i < 2; i++)
1897 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
1898 ccv_nnc_tensor_param_t output_params;
1899 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)
;
1900 ccv_nnc_hint_tensor_auto(add, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
1901 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1902 ccv_nnc_graph_exec_symbol_new(graph, add, inputs, input_size, outputs, output_size, "add");
1903}
1904
1905static ccv_cnnp_model_t* _ccv_cnnp_add_copy(const ccv_cnnp_model_t* const self, void* const context);
1906
1907static const ccv_cnnp_model_vtab_t ccv_cnnp_add_isa = {
1908 .build = _ccv_cnnp_add_build,
1909 .copy = _ccv_cnnp_add_copy,
1910};
1911
1912ccv_cnnp_model_t* ccv_cnnp_add(const float p, const float q, const char* const name)
1913{
1914 ccv_cnnp_model_add_t* const model_add = (ccv_cnnp_model_add_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_add_t));
1915 model_add->super.isa = &ccv_cnnp_add_isa;
1916 model_add->super.input_size = 2;
1917 model_add->super.outputs = &model_add->output;
1918 model_add->super.output_size = 1;
1919 model_add->p = p;
1920 model_add->q = q;
1921 ccv_cnnp_model_copy_name(&model_add->super, name);
1922 return (ccv_cnnp_model_t*)model_add;
1923}
1924
1925static ccv_cnnp_model_t* _ccv_cnnp_add_copy(const ccv_cnnp_model_t* const super, void* const context)
1926{
1927 const ccv_cnnp_model_add_t* const self = (const ccv_cnnp_model_add_t*)super;
1928 return ccv_cnnp_add(self->p, self->q, self->super.name);
1929}
1930
1931// MARK - Mul Layer
1932
1933typedef struct {
1934 ccv_cnnp_model_t super;
1935 ccv_nnc_tensor_symbol_t output;
1936 float p;
1937} ccv_cnnp_model_mul_t;
1938
1939static 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)
1940{
1941 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)
;
1942 const ccv_cnnp_model_mul_t* const self = (const ccv_cnnp_model_mul_t*)super;
1943 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"
, 1943, __extension__ __PRETTY_FUNCTION__); }))
;
1944 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", 1944, __extension__ __PRETTY_FUNCTION__
); }))
;
1945 ccv_nnc_tensor_param_t input_params[2];
1946 int i;
1947 for (i = 0; i < 2; i++)
1948 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
1949 ccv_nnc_tensor_param_t output_params;
1950 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)
;
1951 ccv_nnc_hint_tensor_auto(mul, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
1952 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
1953 ccv_nnc_graph_exec_symbol_new(graph, mul, inputs, input_size, outputs, output_size, "mul");
1954}
1955
1956static ccv_cnnp_model_t* _ccv_cnnp_mul_copy(const ccv_cnnp_model_t* const self, void* const context);
1957
1958static const ccv_cnnp_model_vtab_t ccv_cnnp_mul_isa = {
1959 .build = _ccv_cnnp_mul_build,
1960 .copy = _ccv_cnnp_mul_copy,
1961};
1962
1963ccv_cnnp_model_t* ccv_cnnp_mul(const float p, const char* const name)
1964{
1965 ccv_cnnp_model_mul_t* const model_mul = (ccv_cnnp_model_mul_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_mul_t));
1966 model_mul->super.isa = &ccv_cnnp_mul_isa;
1967 model_mul->super.input_size = 2;
1968 model_mul->super.outputs = &model_mul->output;
1969 model_mul->super.output_size = 1;
1970 model_mul->p = p;
1971 ccv_cnnp_model_copy_name(&model_mul->super, name);
1972 return (ccv_cnnp_model_t*)model_mul;
1973}
1974
1975static ccv_cnnp_model_t* _ccv_cnnp_mul_copy(const ccv_cnnp_model_t* const super, void* const context)
1976{
1977 const ccv_cnnp_model_mul_t* const self = (const ccv_cnnp_model_mul_t*)super;
1978 return ccv_cnnp_mul(self->p, self->super.name);
1979}
1980
1981// MARK - Scalar Mul Layer
1982
1983typedef struct {
1984 ccv_cnnp_model_t super;
1985 ccv_nnc_tensor_symbol_t output;
1986 float a;
1987} ccv_cnnp_model_scalar_mul_t;
1988
1989static 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)
1990{
1991 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)
;
1992 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"
, 1992, __extension__ __PRETTY_FUNCTION__); }))
;
1993 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", 1993, __extension__ __PRETTY_FUNCTION__
); }))
;
1994 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
1995 ccv_nnc_tensor_param_t output_params;
1996 ccv_cnnp_model_scalar_mul_t* const self = (ccv_cnnp_model_scalar_mul_t*)super;
1997 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)
;
1998 ccv_nnc_hint_tensor_auto(scalar_mul, (ccv_nnc_tensor_param_t []){
1999 params,
2000 }, 1, ccv_nnc_no_hint, &output_params, 1);
2001 const ccv_nnc_tensor_symbol_t scalar_mul_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2002 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");
2003 outputs[0] = scalar_mul_output;
2004}
2005
2006static ccv_cnnp_model_t* _ccv_cnnp_scalar_mul_copy(const ccv_cnnp_model_t* const super, void* const context);
2007
2008static const ccv_cnnp_model_vtab_t ccv_cnnp_scalar_mul_isa = {
2009 .build = _ccv_cnnp_scalar_mul_build,
2010 .copy = _ccv_cnnp_scalar_mul_copy,
2011};
2012
2013ccv_cnnp_model_t* ccv_cnnp_scalar_mul(const float a, const char* const name)
2014{
2015 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));
2016 model_scalar_mul->super.isa = &ccv_cnnp_scalar_mul_isa;
2017 model_scalar_mul->super.input_size = 1;
2018 model_scalar_mul->super.outputs = &model_scalar_mul->output;
2019 model_scalar_mul->super.output_size = 1;
2020 model_scalar_mul->a = a;
2021 ccv_cnnp_model_copy_name(&model_scalar_mul->super, name);
2022 return (ccv_cnnp_model_t*)model_scalar_mul;
2023}
2024
2025static ccv_cnnp_model_t* _ccv_cnnp_scalar_mul_copy(const ccv_cnnp_model_t* const super, void* const context)
2026{
2027 const ccv_cnnp_model_scalar_mul_t* const self = (const ccv_cnnp_model_scalar_mul_t*)super;
2028 return ccv_cnnp_scalar_mul(self->a, self->super.name);
2029}
2030
2031// MARK - Div Layer
2032
2033typedef struct {
2034 ccv_cnnp_model_t super;
2035 ccv_nnc_tensor_symbol_t output;
2036 int reciprocal;
2037} ccv_cnnp_model_div_t;
2038
2039static 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)
2040{
2041 const ccv_cnnp_model_div_t* const self = (const ccv_cnnp_model_div_t*)super;
2042 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)
;
2043 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", 2043, __extension__ __PRETTY_FUNCTION__
); }))
;
2044 ccv_nnc_tensor_param_t input_params[2];
2045 int i;
2046 ccv_nnc_tensor_param_t output_params;
2047 const ccv_nnc_cmd_t div = CMD_EWDIV_FORWARD()ccv_nnc_cmd(CCV_NNC_EWDIV_FORWARD, 0, ccv_nnc_cmd_auto, 0);
2048 if (self->reciprocal)
2049 {
2050 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"
, 2050, __extension__ __PRETTY_FUNCTION__); }))
;
2051 input_params[0] = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2052 input_params[1] = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2053 ccv_nnc_hint_tensor_auto(div, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
2054 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2055 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");
2056 } else {
2057 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"
, 2057, __extension__ __PRETTY_FUNCTION__); }))
;
2058 for (i = 0; i < 2; i++)
2059 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
2060 ccv_nnc_hint_tensor_auto(div, input_params, input_size, ccv_nnc_no_hint, &output_params, 1);
2061 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2062 ccv_nnc_graph_exec_symbol_new(graph, div, inputs, input_size, outputs, output_size, "div");
2063 }
2064}
2065
2066static ccv_cnnp_model_t* _ccv_cnnp_div_copy(const ccv_cnnp_model_t* const self, void* const context);
2067
2068static const ccv_cnnp_model_vtab_t ccv_cnnp_div_isa = {
2069 .build = _ccv_cnnp_div_build,
2070 .copy = _ccv_cnnp_div_copy,
2071};
2072
2073ccv_cnnp_model_t* ccv_cnnp_div(const int reciprocal, const char* const name)
2074{
2075 ccv_cnnp_model_div_t* const model_div = (ccv_cnnp_model_div_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_div_t));
2076 model_div->super.isa = &ccv_cnnp_div_isa;
2077 model_div->super.input_size = reciprocal ? 1 : 2;
2078 model_div->super.outputs = &model_div->output;
2079 model_div->super.output_size = 1;
2080 model_div->reciprocal = reciprocal;
2081 ccv_cnnp_model_copy_name(&model_div->super, name);
2082 return (ccv_cnnp_model_t*)model_div;
2083}
2084
2085static ccv_cnnp_model_t* _ccv_cnnp_div_copy(const ccv_cnnp_model_t* const super, void* const context)
2086{
2087 const ccv_cnnp_model_div_t* const self = (const ccv_cnnp_model_div_t*)super;
2088 return ccv_cnnp_div(self->reciprocal, self->super.name);
2089}
2090
2091// MARK - Sqrt Layer
2092
2093typedef struct {
2094 ccv_cnnp_model_t super;
2095 ccv_nnc_tensor_symbol_t output;
2096} ccv_cnnp_model_sqrt_t;
2097
2098static 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)
2099{
2100 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)
;
2101 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", 2101, __extension__ __PRETTY_FUNCTION__
); }))
;
2102 ccv_nnc_tensor_param_t input_params[1];
2103 ccv_nnc_tensor_param_t output_params;
2104 const ccv_nnc_cmd_t sqrt = CMD_EWSQRT_FORWARD()ccv_nnc_cmd(CCV_NNC_EWSQRT_FORWARD, 0, ccv_nnc_cmd_auto, 0);
2105 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"
, 2105, __extension__ __PRETTY_FUNCTION__); }))
;
2106 input_params[0] = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2107 ccv_nnc_hint_tensor_auto(sqrt, input_params, 1, ccv_nnc_no_hint, &output_params, 1);
2108 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2109 ccv_nnc_graph_exec_symbol_new(graph, sqrt, inputs, 1, outputs, output_size, "sqrt");
2110}
2111
2112static ccv_cnnp_model_t* _ccv_cnnp_sqrt_copy(const ccv_cnnp_model_t* const self, void* const context);
2113
2114static const ccv_cnnp_model_vtab_t ccv_cnnp_sqrt_isa = {
2115 .build = _ccv_cnnp_sqrt_build,
2116 .copy = _ccv_cnnp_sqrt_copy,
2117};
2118
2119ccv_cnnp_model_t* ccv_cnnp_sqrt(const char* const name)
2120{
2121 ccv_cnnp_model_sqrt_t* const model_sqrt = (ccv_cnnp_model_sqrt_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_sqrt_t));
2122 model_sqrt->super.isa = &ccv_cnnp_sqrt_isa;
2123 model_sqrt->super.input_size = 1;
2124 model_sqrt->super.outputs = &model_sqrt->output;
2125 model_sqrt->super.output_size = 1;
2126 ccv_cnnp_model_copy_name(&model_sqrt->super, name);
2127 return (ccv_cnnp_model_t*)model_sqrt;
2128}
2129
2130static ccv_cnnp_model_t* _ccv_cnnp_sqrt_copy(const ccv_cnnp_model_t* const super, void* const context)
2131{
2132 const ccv_cnnp_model_sqrt_t* const self = (const ccv_cnnp_model_sqrt_t*)super;
2133 return ccv_cnnp_sqrt(self->super.name);
2134}
2135
2136// MARK - Cmul Layer
2137
2138typedef struct {
2139 ccv_cnnp_model_t super;
2140 ccv_nnc_tensor_symbol_t output;
2141} ccv_cnnp_model_cmul_t;
2142
2143static 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)
2144{
2145 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)
;
2146 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"
, 2146, __extension__ __PRETTY_FUNCTION__); }))
;
2147 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", 2147, __extension__ __PRETTY_FUNCTION__
); }))
;
2148 ccv_nnc_tensor_param_t input_params[2];
2149 int i;
2150 for (i = 0; i < 2; i++)
2151 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
2152 ccv_nnc_tensor_param_t output_params;
2153 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)
;
2154 ccv_nnc_hint_tensor_auto(mul, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
2155 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2156 ccv_nnc_graph_exec_symbol_new(graph, mul, inputs, input_size, outputs, output_size, "cmul");
2157}
2158
2159static ccv_cnnp_model_t* _ccv_cnnp_cmul_copy(const ccv_cnnp_model_t* const self, void* const context);
2160
2161static const ccv_cnnp_model_vtab_t ccv_cnnp_cmul_isa = {
2162 .build = _ccv_cnnp_cmul_build,
2163 .copy = _ccv_cnnp_cmul_copy,
2164};
2165
2166ccv_cnnp_model_t* ccv_cnnp_cmul(const char* const name)
2167{
2168 ccv_cnnp_model_cmul_t* const model_cmul = (ccv_cnnp_model_cmul_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_cmul_t));
2169 model_cmul->super.isa = &ccv_cnnp_cmul_isa;
2170 model_cmul->super.input_size = 2;
2171 model_cmul->super.outputs = &model_cmul->output;
2172 model_cmul->super.output_size = 1;
2173 ccv_cnnp_model_copy_name(&model_cmul->super, name);
2174 return (ccv_cnnp_model_t*)model_cmul;
2175}
2176
2177static ccv_cnnp_model_t* _ccv_cnnp_cmul_copy(const ccv_cnnp_model_t* const super, void* const context)
2178{
2179 return ccv_cnnp_cmul(super->name);
2180}
2181
2182// MARK - Transpose Layer
2183
2184typedef struct {
2185 ccv_cnnp_model_t super;
2186 ccv_nnc_tensor_symbol_t output;
2187 int transpose[2];
2188} ccv_cnnp_model_transpose_t;
2189
2190static 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)
2191{
2192 ccv_cnnp_model_transpose_t* const self = (ccv_cnnp_model_transpose_t*)super;
2193 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)
;
2194 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"
, 2194, __extension__ __PRETTY_FUNCTION__); }))
;
2195 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", 2195, __extension__ __PRETTY_FUNCTION__
); }))
;
2196 if (self->transpose[0] == self->transpose[1])
2197 {
2198 outputs[0] = inputs[0];
2199 return;
2200 }
2201 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2202 ccv_nnc_tensor_param_t output_params;
2203 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)
;
2204 ccv_nnc_hint_tensor_auto(transpose, (ccv_nnc_tensor_param_t []){
2205 params,
2206 }, 1, ccv_nnc_no_hint, &output_params, 1);
2207 const ccv_nnc_tensor_symbol_t transpose_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2208 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");
2209 outputs[0] = transpose_output;
2210}
2211
2212static ccv_cnnp_model_t* _ccv_cnnp_transpose_copy(const ccv_cnnp_model_t* const super, void* const context);
2213
2214static const ccv_cnnp_model_vtab_t ccv_cnnp_transpose_isa = {
2215 .build = _ccv_cnnp_transpose_build,
2216 .copy = _ccv_cnnp_transpose_copy,
2217};
2218
2219ccv_cnnp_model_t* ccv_cnnp_transpose(const int axis_a, const int axis_b, const char* const name)
2220{
2221 ccv_cnnp_model_transpose_t* const model_transpose = (ccv_cnnp_model_transpose_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_transpose_t));
2222 model_transpose->super.isa = &ccv_cnnp_transpose_isa;
2223 model_transpose->super.input_size = 1;
2224 model_transpose->super.outputs = &model_transpose->output;
2225 model_transpose->super.output_size = 1;
2226 model_transpose->transpose[0] = axis_a;
2227 model_transpose->transpose[1] = axis_b;
2228 ccv_cnnp_model_copy_name(&model_transpose->super, name);
2229 return (ccv_cnnp_model_t*)model_transpose;
2230}
2231
2232static ccv_cnnp_model_t* _ccv_cnnp_transpose_copy(const ccv_cnnp_model_t* const super, void* const context)
2233{
2234 const ccv_cnnp_model_transpose_t* const self = (const ccv_cnnp_model_transpose_t*)super;
2235 return ccv_cnnp_transpose(self->transpose[0], self->transpose[1], self->super.name);
2236}
2237
2238// MARK - Layer Norm Layer
2239
2240typedef struct {
2241 ccv_cnnp_model_t super;
2242 ccv_nnc_tensor_symbol_t output;
2243 ccv_nnc_tensor_symbol_t bias;
2244 ccv_nnc_tensor_symbol_t scale;
2245 ccv_nnc_cmd_param_t params;
2246} ccv_cnnp_model_layer_norm_t;
2247
2248static 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)
2249{
2250 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)
;
2251 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"
, 2251, __extension__ __PRETTY_FUNCTION__); }))
;
2252 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", 2252, __extension__ __PRETTY_FUNCTION__
); }))
;
2253 ccv_cnnp_model_layer_norm_t* const self = (ccv_cnnp_model_layer_norm_t*)super;
2254 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2255 ccv_nnc_tensor_param_t bias_params = params;
2256 const int nd = ccv_nnc_tensor_nd(params.dim);
2257 int i;
2258 for (i = 0; i < nd; i++)
2259 bias_params.dim[i] = 1;
2260 for (i = 0; i < self->params.lnorm.count; i++)
2261 bias_params.dim[self->params.lnorm.axis[i]] = params.dim[self->params.lnorm.axis[i]];
2262 if (self->params.lnorm.elementwise_affine)
2263 {
2264 // Both scale and bias are shared between if this model is reused.
2265 if (!self->scale.graph)
2266 self->scale = ccv_nnc_tensor_symbol_new(graph, bias_params, "scale");
2267 if (!self->bias.graph)
2268 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
2269 }
2270 const ccv_nnc_cmd_t layer_norm = ccv_nnc_cmd(CCV_NNC_LAYER_NORM_FORWARD, 0, self->params, 0);
2271 ccv_nnc_tensor_param_t output_params[3];
2272 if (self->params.lnorm.elementwise_affine)
2273 ccv_nnc_hint_tensor_auto(layer_norm, (ccv_nnc_tensor_param_t []){
2274 params,
2275 bias_params,
2276 bias_params,
2277 }, 3, ccv_nnc_no_hint, output_params, 3);
2278 else
2279 ccv_nnc_hint_tensor_auto(layer_norm, (ccv_nnc_tensor_param_t []){
2280 params,
2281 }, 1, ccv_nnc_no_hint, output_params, 3);
2282 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2283 const ccv_nnc_tensor_symbol_t saved_mean = ccv_nnc_tensor_symbol_new(graph, output_params[1], "saved_mean");
2284 const ccv_nnc_tensor_symbol_t saved_inv_std = ccv_nnc_tensor_symbol_new(graph, output_params[2], "saved_inv_std");
2285 if (self->params.lnorm.elementwise_affine)
2286 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");
2287 else
2288 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");
2289 outputs[0] = output;
2290}
2291
2292static 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)
2293{
2294 ccv_cnnp_model_layer_norm_t* const self = (ccv_cnnp_model_layer_norm_t*)super;
2295 if (self->scale.graph)
2296 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);
2297 if (self->bias.graph)
2298 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);
2299}
2300
2301static 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)
2302{
2303 ccv_cnnp_model_layer_norm_t* const self = (ccv_cnnp_model_layer_norm_t*)super;
2304 if (self->scale.graph)
2305 add_to_array(parameters, self->scale, is_trainable);
2306 if (self->bias.graph)
2307 add_to_array(parameters, self->bias, is_trainable);
2308}
2309
2310static ccv_cnnp_model_t* _ccv_cnnp_layer_norm_copy(const ccv_cnnp_model_t* const super, void* const context);
2311
2312static const ccv_cnnp_model_vtab_t ccv_cnnp_layer_norm_isa = {
2313 .build = _ccv_cnnp_layer_norm_build,
2314 .init_states = _ccv_cnnp_layer_norm_init_states,
2315 .add_to_parameter = _ccv_cnnp_layer_norm_add_to_parameter,
2316 .copy = _ccv_cnnp_layer_norm_copy,
2317};
2318
2319ccv_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)
2320{
2321 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));
2322 model_layer_norm->super.isa = &ccv_cnnp_layer_norm_isa;
2323 model_layer_norm->super.input_size = 1;
2324 model_layer_norm->super.outputs = &model_layer_norm->output;
2325 model_layer_norm->super.output_size = 1;
2326 model_layer_norm->super.is_trainable = is_trainable;
2327 ccv_cnnp_model_copy_name(&model_layer_norm->super, name);
2328 model_layer_norm->scale.d = CCV_NNC_NO_TENSOR_SYMBOL;
2329 model_layer_norm->scale.graph = 0;
2330 model_layer_norm->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
2331 model_layer_norm->bias.graph = 0;
2332 model_layer_norm->params.lnorm.epsilon = epsilon;
2333 model_layer_norm->params.lnorm.count = axis_count;
2334 model_layer_norm->params.lnorm.elementwise_affine = elementwise_affine;
2335 memcpy(model_layer_norm->params.lnorm.axis, axis, sizeof(int) * axis_count);
2336 return (ccv_cnnp_model_t*)model_layer_norm;
2337}
2338
2339static ccv_cnnp_model_t* _ccv_cnnp_layer_norm_copy(const ccv_cnnp_model_t* const super, void* const context)
2340{
2341 const ccv_cnnp_model_layer_norm_t* const self = (const ccv_cnnp_model_layer_norm_t*)super;
2342 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);
2343}
2344
2345// MARK - Group Norm Layer
2346
2347typedef struct {
2348 ccv_cnnp_model_t super;
2349 ccv_nnc_tensor_symbol_t output;
2350 ccv_nnc_tensor_symbol_t bias;
2351 ccv_nnc_tensor_symbol_t scale;
2352 ccv_nnc_cmd_param_t params;
2353} ccv_cnnp_model_group_norm_t;
2354
2355static 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)
2356{
2357 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)
;
2358 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"
, 2358, __extension__ __PRETTY_FUNCTION__); }))
;
2359 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", 2359, __extension__ __PRETTY_FUNCTION__
); }))
;
2360 ccv_cnnp_model_group_norm_t* const self = (ccv_cnnp_model_group_norm_t*)super;
2361 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2362 ccv_nnc_tensor_param_t bias_params = params;
2363 const int nd = ccv_nnc_tensor_nd(params.dim);
2364 int i;
2365 for (i = 0; i < nd; i++)
2366 bias_params.dim[i] = 1;
2367 bias_params.dim[self->params.gnorm.group_axis] = params.dim[self->params.gnorm.group_axis];
2368 if (self->params.gnorm.elementwise_affine)
2369 {
2370 // Both scale and bias are shared between if this model is reused.
2371 if (!self->scale.graph)
2372 self->scale = ccv_nnc_tensor_symbol_new(graph, bias_params, "scale");
2373 if (!self->bias.graph)
2374 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
2375 }
2376 const ccv_nnc_cmd_t group_norm = ccv_nnc_cmd(CCV_NNC_GROUP_NORM_FORWARD, 0, self->params, 0);
2377 ccv_nnc_tensor_param_t output_params[3];
2378 if (self->params.gnorm.elementwise_affine)
2379 ccv_nnc_hint_tensor_auto(group_norm, (ccv_nnc_tensor_param_t []){
2380 params,
2381 bias_params,
2382 bias_params,
2383 }, 3, ccv_nnc_no_hint, output_params, 3);
2384 else
2385 ccv_nnc_hint_tensor_auto(group_norm, (ccv_nnc_tensor_param_t []){
2386 params,
2387 }, 1, ccv_nnc_no_hint, output_params, 3);
2388 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2389 const ccv_nnc_tensor_symbol_t saved_mean = ccv_nnc_tensor_symbol_new(graph, output_params[1], "saved_mean");
2390 const ccv_nnc_tensor_symbol_t saved_inv_std = ccv_nnc_tensor_symbol_new(graph, output_params[2], "saved_inv_std");
2391 if (self->params.gnorm.elementwise_affine)
2392 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");
2393 else
2394 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");
2395 outputs[0] = output;
2396}
2397
2398static 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)
2399{
2400 ccv_cnnp_model_group_norm_t* const self = (ccv_cnnp_model_group_norm_t*)super;
2401 if (self->scale.graph)
2402 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);
2403 if (self->bias.graph)
2404 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);
2405}
2406
2407static 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)
2408{
2409 ccv_cnnp_model_group_norm_t* const self = (ccv_cnnp_model_group_norm_t*)super;
2410 if (self->scale.graph)
2411 add_to_array(parameters, self->scale, is_trainable);
2412 if (self->bias.graph)
2413 add_to_array(parameters, self->bias, is_trainable);
2414}
2415
2416static ccv_cnnp_model_t* _ccv_cnnp_group_norm_copy(const ccv_cnnp_model_t* const super, void* const context);
2417
2418static const ccv_cnnp_model_vtab_t ccv_cnnp_group_norm_isa = {
2419 .build = _ccv_cnnp_group_norm_build,
2420 .init_states = _ccv_cnnp_group_norm_init_states,
2421 .add_to_parameter = _ccv_cnnp_group_norm_add_to_parameter,
2422 .copy = _ccv_cnnp_group_norm_copy,
2423};
2424
2425ccv_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)
2426{
2427 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));
2428 model_group_norm->super.isa = &ccv_cnnp_group_norm_isa;
2429 model_group_norm->super.input_size = 1;
2430 model_group_norm->super.outputs = &model_group_norm->output;
2431 model_group_norm->super.output_size = 1;
2432 model_group_norm->super.is_trainable = is_trainable;
2433 ccv_cnnp_model_copy_name(&model_group_norm->super, name);
2434 model_group_norm->scale.d = CCV_NNC_NO_TENSOR_SYMBOL;
2435 model_group_norm->scale.graph = 0;
2436 model_group_norm->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
2437 model_group_norm->bias.graph = 0;
2438 model_group_norm->params.gnorm.group_axis = group_axis;
2439 model_group_norm->params.gnorm.groups = groups;
2440 model_group_norm->params.gnorm.epsilon = epsilon;
2441 model_group_norm->params.gnorm.reduce_count = axis_count;
2442 model_group_norm->params.gnorm.elementwise_affine = elementwise_affine;
2443 memcpy(model_group_norm->params.gnorm.reduce_axis, reduce_axis, sizeof(int) * axis_count);
2444 return (ccv_cnnp_model_t*)model_group_norm;
2445}
2446
2447static ccv_cnnp_model_t* _ccv_cnnp_group_norm_copy(const ccv_cnnp_model_t* const super, void* const context)
2448{
2449 const ccv_cnnp_model_group_norm_t* const self = (const ccv_cnnp_model_group_norm_t*)super;
2450 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);
2451}
2452
2453// MARK - RMSNorm Layer
2454
2455typedef struct {
2456 ccv_cnnp_model_t super;
2457 ccv_nnc_tensor_symbol_t output;
2458 ccv_nnc_tensor_symbol_t scale;
2459 ccv_nnc_cmd_param_t params;
2460} ccv_cnnp_model_rmsnorm_t;
2461
2462static 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)
2463{
2464 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)
;
2465 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"
, 2465, __extension__ __PRETTY_FUNCTION__); }))
;
2466 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", 2466, __extension__ __PRETTY_FUNCTION__
); }))
;
2467 ccv_cnnp_model_rmsnorm_t* const self = (ccv_cnnp_model_rmsnorm_t*)super;
2468 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2469 ccv_nnc_tensor_param_t scale_params = params;
2470 const int nd = ccv_nnc_tensor_nd(params.dim);
2471 int i;
2472 for (i = 0; i < nd; i++)
2473 scale_params.dim[i] = 1;
2474 for (i = 0; i < self->params.rmsnorm.count; i++)
2475 scale_params.dim[self->params.rmsnorm.axis[i]] = params.dim[self->params.rmsnorm.axis[i]];
2476 // Both scale and bias are shared between if this model is reused.
2477 if (!self->scale.graph)
2478 self->scale = ccv_nnc_tensor_symbol_new(graph, scale_params, "scale");
2479 const ccv_nnc_cmd_t rmsnorm = ccv_nnc_cmd(CCV_NNC_RMSNORM_FORWARD, 0, self->params, 0);
2480 ccv_nnc_tensor_param_t output_params[2];
2481 ccv_nnc_hint_tensor_auto(rmsnorm, (ccv_nnc_tensor_param_t []){
2482 params,
2483 scale_params,
2484 }, 2, ccv_nnc_no_hint, output_params, 2);
2485 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2486 const ccv_nnc_tensor_symbol_t saved_inv_std = ccv_nnc_tensor_symbol_new(graph, output_params[1], "saved_inv_std");
2487 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");
2488 outputs[0] = output;
2489}
2490
2491static 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)
2492{
2493 ccv_cnnp_model_rmsnorm_t* const self = (ccv_cnnp_model_rmsnorm_t*)super;
2494 if (self->scale.graph)
2495 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);
2496}
2497
2498static 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)
2499{
2500 ccv_cnnp_model_rmsnorm_t* const self = (ccv_cnnp_model_rmsnorm_t*)super;
2501 if (self->scale.graph)
2502 add_to_array(parameters, self->scale, is_trainable);
2503}
2504
2505static ccv_cnnp_model_t* _ccv_cnnp_rmsnorm_copy(const ccv_cnnp_model_t* const super, void* const context);
2506
2507static const ccv_cnnp_model_vtab_t ccv_cnnp_rmsnorm_isa = {
2508 .build = _ccv_cnnp_rmsnorm_build,
2509 .init_states = _ccv_cnnp_rmsnorm_init_states,
2510 .add_to_parameter = _ccv_cnnp_rmsnorm_add_to_parameter,
2511 .copy = _ccv_cnnp_rmsnorm_copy,
2512};
2513
2514ccv_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)
2515{
2516 ccv_cnnp_model_rmsnorm_t* const model_rmsnorm = (ccv_cnnp_model_rmsnorm_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_rmsnorm_t));
2517 model_rmsnorm->super.isa = &ccv_cnnp_rmsnorm_isa;
2518 model_rmsnorm->super.input_size = 1;
2519 model_rmsnorm->super.outputs = &model_rmsnorm->output;
2520 model_rmsnorm->super.output_size = 1;
2521 model_rmsnorm->super.is_trainable = is_trainable;
2522 ccv_cnnp_model_copy_name(&model_rmsnorm->super, name);
2523 model_rmsnorm->scale.d = CCV_NNC_NO_TENSOR_SYMBOL;
2524 model_rmsnorm->scale.graph = 0;
2525 model_rmsnorm->params.rmsnorm.epsilon = epsilon;
2526 model_rmsnorm->params.rmsnorm.count = axis_count;
2527 memcpy(model_rmsnorm->params.lnorm.axis, axis, sizeof(int) * axis_count);
2528 return (ccv_cnnp_model_t*)model_rmsnorm;
2529}
2530
2531static ccv_cnnp_model_t* _ccv_cnnp_rmsnorm_copy(const ccv_cnnp_model_t* const super, void* const context)
2532{
2533 const ccv_cnnp_model_rmsnorm_t* const self = (const ccv_cnnp_model_rmsnorm_t*)super;
2534 return ccv_cnnp_rmsnorm(self->params.rmsnorm.epsilon, self->params.rmsnorm.axis, self->params.rmsnorm.count, self->super.is_trainable, self->super.name);
2535}
2536
2537// MARK - Batched Matrix Mul Layer
2538
2539typedef struct {
2540 ccv_cnnp_model_t super;
2541 ccv_nnc_tensor_symbol_t output;
2542 int transpose_a[2];
2543 int transpose_b[2];
2544 int flags;
2545} ccv_cnnp_model_matmul_t;
2546
2547static 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)
2548{
2549 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)
;
2550 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"
, 2550, __extension__ __PRETTY_FUNCTION__); }))
;
2551 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", 2551, __extension__ __PRETTY_FUNCTION__
); }))
;
2552 ccv_cnnp_model_matmul_t* const self = (ccv_cnnp_model_matmul_t*)super;
2553 ccv_nnc_tensor_param_t a_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2554 ccv_nnc_tensor_param_t b_params = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
2555 ccv_nnc_tensor_param_t output_params;
2556 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)
;
2557 matmul.info.blas.flags = self->flags;
2558 ccv_nnc_hint_tensor_auto(matmul, (ccv_nnc_tensor_param_t []){
2559 a_params,
2560 b_params,
2561 }, 2, ccv_nnc_no_hint, &output_params, 1);
2562 const ccv_nnc_tensor_symbol_t matmul_output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2563 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");
2564 outputs[0] = matmul_output;
2565}
2566
2567static ccv_cnnp_model_t* _ccv_cnnp_matmul_copy(const ccv_cnnp_model_t* const super, void* const context);
2568
2569static const ccv_cnnp_model_vtab_t ccv_cnnp_matmul_isa = {
2570 .build = _ccv_cnnp_matmul_build,
2571 .copy = _ccv_cnnp_matmul_copy,
2572};
2573
2574ccv_cnnp_model_t* ccv_cnnp_matmul(const int transpose_a[2], const int transpose_b[2], const int flags, const char* const name)
2575{
2576 ccv_cnnp_model_matmul_t* const model_matmul = (ccv_cnnp_model_matmul_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_matmul_t));
2577 model_matmul->super.isa = &ccv_cnnp_matmul_isa;
2578 model_matmul->super.input_size = 2;
2579 model_matmul->super.outputs = &model_matmul->output;
2580 model_matmul->super.output_size = 1;
2581 model_matmul->transpose_a[0] = transpose_a[0];
2582 model_matmul->transpose_a[1] = transpose_a[1];
2583 model_matmul->transpose_b[0] = transpose_b[0];
2584 model_matmul->transpose_b[1] = transpose_b[1];
2585 model_matmul->flags = flags;
2586 ccv_cnnp_model_copy_name(&model_matmul->super, name);
2587 return (ccv_cnnp_model_t*)model_matmul;
2588}
2589
2590static ccv_cnnp_model_t* _ccv_cnnp_matmul_copy(const ccv_cnnp_model_t* const super, void* const context)
2591{
2592 const ccv_cnnp_model_matmul_t* const self = (const ccv_cnnp_model_matmul_t*)super;
2593 return ccv_cnnp_matmul(self->transpose_a, self->transpose_b, self->flags, self->super.name);
2594}
2595
2596// MARK - Dropout Layer
2597
2598typedef struct {
2599 ccv_cnnp_model_t super;
2600 ccv_nnc_tensor_symbol_t output;
2601 ccv_nnc_graph_exec_symbol_t dropout;
2602 float p;
2603 int entirety;
2604} ccv_cnnp_model_dropout_t;
2605
2606static 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)
2607{
2608 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)
;
2609 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"
, 2609, __extension__ __PRETTY_FUNCTION__); }))
;
2610 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", 2610, __extension__ __PRETTY_FUNCTION__
); }))
;
2611 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2612 ccv_nnc_tensor_param_t output_params[2];
2613 ccv_cnnp_model_dropout_t* const self = (ccv_cnnp_model_dropout_t*)super;
2614 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)
;
2615 ccv_nnc_hint_tensor_auto(dropout, (ccv_nnc_tensor_param_t []){
2616 params,
2617 }, 1, ccv_nnc_no_hint, output_params, 2);
2618 const ccv_nnc_tensor_symbol_t dropout_output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
2619 const ccv_nnc_tensor_symbol_t mask = ccv_nnc_tensor_symbol_new(graph, output_params[1], "mask");
2620 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");
2621 outputs[0] = dropout_output;
2622}
2623
2624static 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)
2625{
2626 ccv_cnnp_model_dropout_t* const self = (ccv_cnnp_model_dropout_t*)super;
2627 if (self->dropout.graph)
2628 {
2629 if (is_test)
2630 // During test, the dropout is not applied. Data transfer is perfect because if these are the same tensor, it will skip.
2631 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);
2632 else
2633 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);
2634 }
2635}
2636
2637static ccv_cnnp_model_t* _ccv_cnnp_dropout_copy(const ccv_cnnp_model_t* const super, void* const context);
2638
2639static const ccv_cnnp_model_vtab_t ccv_cnnp_dropout_isa = {
2640 .build = _ccv_cnnp_dropout_build,
2641 .set_is_test = _ccv_cnnp_dropout_set_is_test,
2642 .copy = _ccv_cnnp_dropout_copy,
2643};
2644
2645ccv_cnnp_model_t* ccv_cnnp_dropout(const float p, const int entirety, const char* const name)
2646{
2647 ccv_cnnp_model_dropout_t* const model_dropout = (ccv_cnnp_model_dropout_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_dropout_t));
2648 model_dropout->super.isa = &ccv_cnnp_dropout_isa;
2649 model_dropout->super.input_size = 1;
2650 model_dropout->super.outputs = &model_dropout->output;
2651 model_dropout->super.output_size = 1;
2652 model_dropout->p = p;
2653 model_dropout->entirety = entirety;
2654 ccv_cnnp_model_copy_name(&model_dropout->super, name);
2655 return (ccv_cnnp_model_t*)model_dropout;
2656}
2657
2658static ccv_cnnp_model_t* _ccv_cnnp_dropout_copy(const ccv_cnnp_model_t* const super, void* const context)
2659{
2660 const ccv_cnnp_model_dropout_t* const self = (const ccv_cnnp_model_dropout_t*)super;
2661 return ccv_cnnp_dropout(self->p, self->entirety, self->super.name);
2662}
2663
2664// MARK - Masked Fill Layer
2665
2666typedef struct {
2667 ccv_cnnp_model_t super;
2668 ccv_nnc_tensor_symbol_t output;
2669 float eq;
2670 float fill;
2671} ccv_cnnp_model_masked_fill_t;
2672
2673static 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)
2674{
2675 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)
;
2676 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"
, 2676, __extension__ __PRETTY_FUNCTION__); }))
;
2677 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", 2677, __extension__ __PRETTY_FUNCTION__
); }))
;
2678 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2679 ccv_cnnp_model_masked_fill_t* const self = (ccv_cnnp_model_masked_fill_t*)super;
2680 const ccv_nnc_tensor_symbol_t masked_fill_output = ccv_nnc_tensor_symbol_new(graph, params, 0);
2681 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");
2682 outputs[0] = masked_fill_output;
2683}
2684
2685static ccv_cnnp_model_t* _ccv_cnnp_masked_fill_copy(const ccv_cnnp_model_t* const super, void* const context);
2686
2687static const ccv_cnnp_model_vtab_t ccv_cnnp_masked_fill_isa = {
2688 .build = _ccv_cnnp_masked_fill_build,
2689 .copy = _ccv_cnnp_masked_fill_copy,
2690};
2691
2692ccv_cnnp_model_t* ccv_cnnp_masked_fill(const float eq, const float fill, const char* const name)
2693{
2694 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));
2695 model_masked_fill->super.isa = &ccv_cnnp_masked_fill_isa;
2696 model_masked_fill->super.input_size = 2;
2697 model_masked_fill->super.outputs = &model_masked_fill->output;
2698 model_masked_fill->super.output_size = 1;
2699 model_masked_fill->eq = eq;
2700 model_masked_fill->fill = fill;
2701 ccv_cnnp_model_copy_name(&model_masked_fill->super, name);
2702 return (ccv_cnnp_model_t*)model_masked_fill;
2703}
2704
2705static ccv_cnnp_model_t* _ccv_cnnp_masked_fill_copy(const ccv_cnnp_model_t* const super, void* const context)
2706{
2707 const ccv_cnnp_model_masked_fill_t* const self = (const ccv_cnnp_model_masked_fill_t*)super;
2708 return ccv_cnnp_masked_fill(self->eq, self->fill, self->super.name);
2709}
2710
2711// MARK - Index Select Layer
2712
2713typedef struct {
2714 ccv_cnnp_model_t super;
2715 ccv_nnc_tensor_symbol_t output;
2716} ccv_cnnp_model_index_select_t;
2717
2718static 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)
2719{
2720 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)
;
2721 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"
, 2721, __extension__ __PRETTY_FUNCTION__); }))
;
2722 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", 2722, __extension__ __PRETTY_FUNCTION__
); }))
;
2723 const ccv_nnc_tensor_param_t vocab_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2724 const ccv_nnc_tensor_param_t index_params = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
2725 ccv_nnc_tensor_param_t output_params;
2726 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)
;
2727 ccv_nnc_hint_tensor_auto(index_select, (ccv_nnc_tensor_param_t []){
2728 vocab_params,
2729 index_params,
2730 }, 2, ccv_nnc_no_hint, &output_params, 1);
2731 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2732 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");
2733 outputs[0] = output;
2734}
2735
2736static ccv_cnnp_model_t* _ccv_cnnp_index_select_copy(const ccv_cnnp_model_t* const super, void* const context);
2737
2738static const ccv_cnnp_model_vtab_t ccv_cnnp_index_select_isa = {
2739 .build = _ccv_cnnp_index_select_build,
2740 .copy = _ccv_cnnp_index_select_copy,
2741};
2742
2743ccv_cnnp_model_t* ccv_cnnp_index_select(const char* const name)
2744{
2745 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));
2746 model_index_select->super.isa = &ccv_cnnp_index_select_isa;
2747 model_index_select->super.input_size = 2;
2748 model_index_select->super.outputs = &model_index_select->output;
2749 model_index_select->super.output_size = 1;
2750 ccv_cnnp_model_copy_name(&model_index_select->super, name);
2751 return (ccv_cnnp_model_t*)model_index_select;
2752}
2753
2754static ccv_cnnp_model_t* _ccv_cnnp_index_select_copy(const ccv_cnnp_model_t* const super, void* const context)
2755{
2756 ccv_cnnp_model_index_select_t* const self = (ccv_cnnp_model_index_select_t*)super;
2757 return ccv_cnnp_index_select(self->super.name);
2758}
2759
2760// MARK - Embedding Layer
2761
2762typedef struct {
2763 ccv_cnnp_model_t super;
2764 ccv_nnc_tensor_symbol_t output;
2765 ccv_nnc_tensor_symbol_t vocab;
2766 int datatype;
2767 int vocab_size;
2768 int embed_size;
2769} ccv_cnnp_model_embedding_t;
2770
2771static 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)
2772{
2773 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2774 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)
;
2775 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"
, 2775, __extension__ __PRETTY_FUNCTION__); }))
;
2776 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", 2776, __extension__ __PRETTY_FUNCTION__
); }))
;
2777 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2778 ccv_nnc_tensor_param_t vocab_params = params;
2779 memset(vocab_params.dim, 0, sizeof(vocab_params.dim));
2780 vocab_params.datatype = self->datatype;
2781 vocab_params.dim[0] = self->vocab_size;
2782 vocab_params.dim[1] = self->embed_size;
2783 if (!self->vocab.graph)
2784 self->vocab = ccv_nnc_tensor_symbol_new(graph, vocab_params, "vocab");
2785 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", 2785
, __extension__ __PRETTY_FUNCTION__); }))
;
2786 ccv_nnc_tensor_param_t output_params;
2787 const ccv_nnc_cmd_t embedding = CMD_INDEX_SELECT_FORWARD()ccv_nnc_cmd(CCV_NNC_INDEX_SELECT_FORWARD, 0, ccv_nnc_cmd_auto
, 0)
;
2788 ccv_nnc_hint_tensor_auto(embedding, (ccv_nnc_tensor_param_t []){
2789 vocab_params,
2790 params,
2791 }, 2, ccv_nnc_no_hint, &output_params, 1);
2792 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2793 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");
2794 outputs[0] = output;
2795}
2796
2797static 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)
2798{
2799 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2800 const float std = sqrtf(2) / sqrtf(self->vocab_size + self->embed_size);
2801 const float bound = sqrtf(3) * std;
2802 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);
2803}
2804
2805static 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)
2806{
2807 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2808 add_to_array(parameters, self->vocab, is_trainable);
2809}
2810
2811static ccv_cnnp_model_t* _ccv_cnnp_embedding_copy(const ccv_cnnp_model_t* const super, void* const context);
2812
2813static const ccv_cnnp_model_vtab_t ccv_cnnp_embedding_isa = {
2814 .build = _ccv_cnnp_embedding_build,
2815 .init_states = _ccv_cnnp_embedding_init_states,
2816 .add_to_parameter = _ccv_cnnp_embedding_add_to_parameter,
2817 .copy = _ccv_cnnp_embedding_copy,
2818};
2819
2820ccv_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)
2821{
2822 ccv_cnnp_model_embedding_t* const model_embedding = (ccv_cnnp_model_embedding_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_embedding_t));
2823 model_embedding->super.isa = &ccv_cnnp_embedding_isa;
2824 model_embedding->super.input_size = 1;
2825 model_embedding->super.outputs = &model_embedding->output;
2826 model_embedding->super.output_size = 1;
2827 model_embedding->super.is_trainable = is_trainable;
2828 ccv_cnnp_model_copy_name(&model_embedding->super, name);
2829 model_embedding->vocab.d = CCV_NNC_NO_TENSOR_SYMBOL;
2830 model_embedding->vocab.graph = 0;
2831 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", 2831, __extension__ __PRETTY_FUNCTION__
); }))
;
2832 model_embedding->datatype = datatype;
2833 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", 2833, __extension__ __PRETTY_FUNCTION__
); }))
;
2834 model_embedding->vocab_size = vocab_size;
2835 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", 2835, __extension__ __PRETTY_FUNCTION__
); }))
;
2836 model_embedding->embed_size = embed_size;
2837 return (ccv_cnnp_model_t*)model_embedding;
2838}
2839
2840static ccv_cnnp_model_t* _ccv_cnnp_embedding_copy(const ccv_cnnp_model_t* const super, void* const context)
2841{
2842 ccv_cnnp_model_embedding_t* const self = (ccv_cnnp_model_embedding_t*)super;
2843 return ccv_cnnp_embedding(self->datatype, self->vocab_size, self->embed_size, self->super.is_trainable, self->super.name);
2844}
2845
2846// MARK - Pool Layers
2847
2848typedef struct {
2849 ccv_cnnp_model_t super;
2850 ccv_nnc_tensor_symbol_t output;
2851 int type;
2852 float width_scale;
2853 float height_scale;
2854 int align_corners;
2855} ccv_cnnp_model_upsample_t;
2856
2857static 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)
2858{
2859 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)
;
2860 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"
, 2860, __extension__ __PRETTY_FUNCTION__); }))
;
2861 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", 2861, __extension__ __PRETTY_FUNCTION__
); }))
;
2862 ccv_cnnp_model_upsample_t* const self = (ccv_cnnp_model_upsample_t*)super;
2863 const ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2864 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)
;
2865 ccv_nnc_tensor_param_t output_params;
2866 ccv_nnc_hint_tensor_auto(cmd, &params, 1, ccv_nnc_no_hint, &output_params, 1);
2867 const ccv_nnc_tensor_symbol_t output = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2868 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");
2869 outputs[0] = output;
2870}
2871
2872static ccv_cnnp_model_t* _ccv_cnnp_upsample_copy(const ccv_cnnp_model_t* const super, void* const context);
2873
2874static const ccv_cnnp_model_vtab_t ccv_cnnp_upsample_isa = {
2875 .build = _ccv_cnnp_upsample_build,
2876 .copy = _ccv_cnnp_upsample_copy,
2877};
2878
2879ccv_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)
2880{
2881 ccv_cnnp_model_upsample_t* const model_upsample = (ccv_cnnp_model_upsample_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_upsample_t));
2882 model_upsample->super.isa = &ccv_cnnp_upsample_isa;
2883 model_upsample->super.input_size = 1;
2884 model_upsample->super.outputs = &model_upsample->output;
2885 model_upsample->super.output_size = 1;
2886 ccv_cnnp_model_copy_name(&model_upsample->super, name);
2887 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", 2887, __extension__ __PRETTY_FUNCTION__
); }))
;
2888 model_upsample->type = type;
2889 model_upsample->width_scale = width_scale;
2890 model_upsample->height_scale = height_scale;
2891 model_upsample->align_corners = align_corners;
2892 return (ccv_cnnp_model_t*)model_upsample;
2893}
2894
2895static ccv_cnnp_model_t* _ccv_cnnp_upsample_copy(const ccv_cnnp_model_t* const super, void* const context)
2896{
2897 const ccv_cnnp_model_upsample_t* const self = (const ccv_cnnp_model_upsample_t*)super;
2898 return ccv_cnnp_upsample(self->type, self->width_scale, self->height_scale, self->align_corners, self->super.name);
2899}
2900
2901// MARK - Reduce Sum Layer
2902
2903typedef struct {
2904 ccv_cnnp_model_t super;
2905 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
2906 int count;
2907 ccv_nnc_tensor_symbol_t output;
2908} ccv_cnnp_model_reduce_sum_t;
2909
2910static 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)
2911{
2912 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)
;
2913 const ccv_cnnp_model_reduce_sum_t* const self = (const ccv_cnnp_model_reduce_sum_t*)super;
2914 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"
, 2914, __extension__ __PRETTY_FUNCTION__); }))
;
2915 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", 2915, __extension__ __PRETTY_FUNCTION__
); }))
;
2916 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2917 ccv_nnc_tensor_param_t output_params;
2918 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)
;
2919 int i;
2920 for (i = 0; i < self->count; i++)
2921 reduce_sum.info.reduce.axis[i] = self->axis[i];
2922 reduce_sum.info.reduce.count = self->count;
2923 ccv_nnc_hint_tensor_auto(reduce_sum, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
2924 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2925 ccv_nnc_graph_exec_symbol_new(graph, reduce_sum, inputs, input_size, outputs, output_size, "reduce_sum");
2926}
2927
2928static ccv_cnnp_model_t* _ccv_cnnp_reduce_sum_copy(const ccv_cnnp_model_t* const self, void* const context);
2929
2930static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_sum_isa = {
2931 .build = _ccv_cnnp_reduce_sum_build,
2932 .copy = _ccv_cnnp_reduce_sum_copy,
2933};
2934
2935ccv_cnnp_model_t* ccv_cnnp_reduce_sum(const int* const axis, const int axis_count, const char* const name)
2936{
2937 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));
2938 model_reduce_sum->super.isa = &ccv_cnnp_reduce_sum_isa;
2939 model_reduce_sum->super.input_size = 1;
2940 model_reduce_sum->super.outputs = &model_reduce_sum->output;
2941 model_reduce_sum->super.output_size = 1;
2942 ccv_cnnp_model_copy_name(&model_reduce_sum->super, name);
2943 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", 2943, __extension__ __PRETTY_FUNCTION__
); }))
;
2944 int i;
2945 for (i = 0; i < axis_count; i++)
2946 model_reduce_sum->axis[i] = axis[i];
2947 model_reduce_sum->count = axis_count;
2948 return (ccv_cnnp_model_t*)model_reduce_sum;
2949}
2950
2951static ccv_cnnp_model_t* _ccv_cnnp_reduce_sum_copy(const ccv_cnnp_model_t* const super, void* const context)
2952{
2953 const ccv_cnnp_model_reduce_sum_t* const self = (const ccv_cnnp_model_reduce_sum_t*)super;
2954 return ccv_cnnp_reduce_sum(self->axis, self->count, self->super.name);
2955}
2956
2957// MARK - Reduce Mean Layer
2958
2959typedef struct {
2960 ccv_cnnp_model_t super;
2961 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
2962 int count;
2963 ccv_nnc_tensor_symbol_t output;
2964} ccv_cnnp_model_reduce_mean_t;
2965
2966static 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)
2967{
2968 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)
;
2969 const ccv_cnnp_model_reduce_mean_t* const self = (const ccv_cnnp_model_reduce_mean_t*)super;
2970 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"
, 2970, __extension__ __PRETTY_FUNCTION__); }))
;
2971 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", 2971, __extension__ __PRETTY_FUNCTION__
); }))
;
2972 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
2973 ccv_nnc_tensor_param_t output_params;
2974 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)
;
2975 int i;
2976 for (i = 0; i < self->count; i++)
2977 reduce_mean.info.reduce.axis[i] = self->axis[i];
2978 reduce_mean.info.reduce.count = self->count;
2979 ccv_nnc_hint_tensor_auto(reduce_mean, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
2980 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
2981 ccv_nnc_graph_exec_symbol_new(graph, reduce_mean, inputs, input_size, outputs, output_size, "reduce_mean");
2982}
2983
2984static ccv_cnnp_model_t* _ccv_cnnp_reduce_mean_copy(const ccv_cnnp_model_t* const self, void* const context);
2985
2986static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_mean_isa = {
2987 .build = _ccv_cnnp_reduce_mean_build,
2988 .copy = _ccv_cnnp_reduce_mean_copy,
2989};
2990
2991ccv_cnnp_model_t* ccv_cnnp_reduce_mean(const int* const axis, const int axis_count, const char* const name)
2992{
2993 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));
2994 model_reduce_mean->super.isa = &ccv_cnnp_reduce_mean_isa;
2995 model_reduce_mean->super.input_size = 1;
2996 model_reduce_mean->super.outputs = &model_reduce_mean->output;
2997 model_reduce_mean->super.output_size = 1;
2998 ccv_cnnp_model_copy_name(&model_reduce_mean->super, name);
2999 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", 2999, __extension__ __PRETTY_FUNCTION__
); }))
;
3000 int i;
3001 for (i = 0; i < axis_count; i++)
3002 model_reduce_mean->axis[i] = axis[i];
3003 model_reduce_mean->count = axis_count;
3004 return (ccv_cnnp_model_t*)model_reduce_mean;
3005}
3006
3007static ccv_cnnp_model_t* _ccv_cnnp_reduce_mean_copy(const ccv_cnnp_model_t* const super, void* const context)
3008{
3009 const ccv_cnnp_model_reduce_mean_t* const self = (const ccv_cnnp_model_reduce_mean_t*)super;
3010 return ccv_cnnp_reduce_mean(self->axis, self->count, self->super.name);
3011}
3012
3013// MARK - Reduce Max Layer
3014
3015typedef struct {
3016 ccv_cnnp_model_t super;
3017 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
3018 int count;
3019 ccv_nnc_tensor_symbol_t output;
3020} ccv_cnnp_model_reduce_max_t;
3021
3022static 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)
3023{
3024 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)
;
3025 const ccv_cnnp_model_reduce_max_t* const self = (const ccv_cnnp_model_reduce_max_t*)super;
3026 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"
, 3026, __extension__ __PRETTY_FUNCTION__); }))
;
3027 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", 3027, __extension__ __PRETTY_FUNCTION__
); }))
;
3028 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3029 ccv_nnc_tensor_param_t output_params;
3030 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)
;
3031 int i;
3032 for (i = 0; i < self->count; i++)
3033 reduce_max.info.reduce.axis[i] = self->axis[i];
3034 reduce_max.info.reduce.count = self->count;
3035 ccv_nnc_hint_tensor_auto(reduce_max, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3036 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3037 ccv_nnc_graph_exec_symbol_new(graph, reduce_max, inputs, input_size, outputs, output_size, "reduce_max");
3038}
3039
3040static ccv_cnnp_model_t* _ccv_cnnp_reduce_max_copy(const ccv_cnnp_model_t* const self, void* const context);
3041
3042static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_max_isa = {
3043 .build = _ccv_cnnp_reduce_max_build,
3044 .copy = _ccv_cnnp_reduce_max_copy,
3045};
3046
3047ccv_cnnp_model_t* ccv_cnnp_reduce_max(const int* const axis, const int axis_count, const char* const name)
3048{
3049 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));
3050 model_reduce_max->super.isa = &ccv_cnnp_reduce_max_isa;
3051 model_reduce_max->super.input_size = 1;
3052 model_reduce_max->super.outputs = &model_reduce_max->output;
3053 model_reduce_max->super.output_size = 1;
3054 ccv_cnnp_model_copy_name(&model_reduce_max->super, name);
3055 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", 3055, __extension__ __PRETTY_FUNCTION__
); }))
;
3056 int i;
3057 for (i = 0; i < axis_count; i++)
3058 model_reduce_max->axis[i] = axis[i];
3059 model_reduce_max->count = axis_count;
3060 return (ccv_cnnp_model_t*)model_reduce_max;
3061}
3062
3063static ccv_cnnp_model_t* _ccv_cnnp_reduce_max_copy(const ccv_cnnp_model_t* const super, void* const context)
3064{
3065 const ccv_cnnp_model_reduce_max_t* const self = (const ccv_cnnp_model_reduce_max_t*)super;
3066 return ccv_cnnp_reduce_max(self->axis, self->count, self->super.name);
3067}
3068
3069// MARK - Reduce Min Layer
3070
3071typedef struct {
3072 ccv_cnnp_model_t super;
3073 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
3074 int count;
3075 ccv_nnc_tensor_symbol_t output;
3076} ccv_cnnp_model_reduce_min_t;
3077
3078static 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)
3079{
3080 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)
;
3081 const ccv_cnnp_model_reduce_min_t* const self = (const ccv_cnnp_model_reduce_min_t*)super;
3082 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"
, 3082, __extension__ __PRETTY_FUNCTION__); }))
;
3083 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", 3083, __extension__ __PRETTY_FUNCTION__
); }))
;
3084 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3085 ccv_nnc_tensor_param_t output_params;
3086 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)
;
3087 int i;
3088 for (i = 0; i < self->count; i++)
3089 reduce_min.info.reduce.axis[i] = self->axis[i];
3090 reduce_min.info.reduce.count = self->count;
3091 ccv_nnc_hint_tensor_auto(reduce_min, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3092 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3093 ccv_nnc_graph_exec_symbol_new(graph, reduce_min, inputs, input_size, outputs, output_size, "reduce_min");
3094}
3095
3096static ccv_cnnp_model_t* _ccv_cnnp_reduce_min_copy(const ccv_cnnp_model_t* const self, void* const context);
3097
3098static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_min_isa = {
3099 .build = _ccv_cnnp_reduce_min_build,
3100 .copy = _ccv_cnnp_reduce_min_copy,
3101};
3102
3103ccv_cnnp_model_t* ccv_cnnp_reduce_min(const int* const axis, const int axis_count, const char* const name)
3104{
3105 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));
3106 model_reduce_min->super.isa = &ccv_cnnp_reduce_min_isa;
3107 model_reduce_min->super.input_size = 1;
3108 model_reduce_min->super.outputs = &model_reduce_min->output;
3109 model_reduce_min->super.output_size = 1;
3110 ccv_cnnp_model_copy_name(&model_reduce_min->super, name);
3111 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", 3111, __extension__ __PRETTY_FUNCTION__
); }))
;
3112 int i;
3113 for (i = 0; i < axis_count; i++)
3114 model_reduce_min->axis[i] = axis[i];
3115 model_reduce_min->count = axis_count;
3116 return (ccv_cnnp_model_t*)model_reduce_min;
3117}
3118
3119static ccv_cnnp_model_t* _ccv_cnnp_reduce_min_copy(const ccv_cnnp_model_t* const super, void* const context)
3120{
3121 const ccv_cnnp_model_reduce_min_t* const self = (const ccv_cnnp_model_reduce_min_t*)super;
3122 return ccv_cnnp_reduce_min(self->axis, self->count, self->super.name);
3123}
3124
3125// MARK - Reduce Norm2 Layer
3126
3127typedef struct {
3128 ccv_cnnp_model_t super;
3129 int axis[CCV_NNC_MAX_DIM_ALLOC(12)];
3130 int count;
3131 ccv_nnc_tensor_symbol_t output;
3132} ccv_cnnp_model_reduce_norm2_t;
3133
3134static 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)
3135{
3136 const ccv_cnnp_model_reduce_norm2_t* const self = (const ccv_cnnp_model_reduce_norm2_t*)super;
3137 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)
;
3138 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"
, 3138, __extension__ __PRETTY_FUNCTION__); }))
;
3139 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", 3139, __extension__ __PRETTY_FUNCTION__
); }))
;
3140 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3141 ccv_nnc_tensor_param_t output_params;
3142 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)
;
3143 int i;
3144 for (i = 0; i < self->count; i++)
3145 reduce_norm2.info.reduce.axis[i] = self->axis[i];
3146 reduce_norm2.info.reduce.count = self->count;
3147 ccv_nnc_hint_tensor_auto(reduce_norm2, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3148 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3149 ccv_nnc_graph_exec_symbol_new(graph, reduce_norm2, inputs, input_size, outputs, output_size, "reduce_norm2");
3150}
3151
3152static ccv_cnnp_model_t* _ccv_cnnp_reduce_norm2_copy(const ccv_cnnp_model_t* const self, void* const context);
3153
3154static const ccv_cnnp_model_vtab_t ccv_cnnp_reduce_norm2_isa = {
3155 .build = _ccv_cnnp_reduce_norm2_build,
3156 .copy = _ccv_cnnp_reduce_norm2_copy,
3157};
3158
3159ccv_cnnp_model_t* ccv_cnnp_reduce_norm2(const int* const axis, const int axis_count, const char* const name)
3160{
3161 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));
3162 model_reduce_norm2->super.isa = &ccv_cnnp_reduce_norm2_isa;
3163 model_reduce_norm2->super.input_size = 1;
3164 model_reduce_norm2->super.outputs = &model_reduce_norm2->output;
3165 model_reduce_norm2->super.output_size = 1;
3166 ccv_cnnp_model_copy_name(&model_reduce_norm2->super, name);
3167 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", 3167, __extension__ __PRETTY_FUNCTION__
); }))
;
3168 int i;
3169 for (i = 0; i < axis_count; i++)
3170 model_reduce_norm2->axis[i] = axis[i];
3171 model_reduce_norm2->count = axis_count;
3172 return (ccv_cnnp_model_t*)model_reduce_norm2;
3173}
3174
3175static ccv_cnnp_model_t* _ccv_cnnp_reduce_norm2_copy(const ccv_cnnp_model_t* const super, void* const context)
3176{
3177 const ccv_cnnp_model_reduce_norm2_t* const self = (const ccv_cnnp_model_reduce_norm2_t*)super;
3178 return ccv_cnnp_reduce_norm2(self->axis, self->count, self->super.name);
3179}
3180
3181// MARK - Argmax Layer
3182
3183typedef struct {
3184 ccv_cnnp_model_t super;
3185 int axis;
3186 ccv_nnc_tensor_symbol_t output;
3187} ccv_cnnp_model_argmax_t;
3188
3189static 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)
3190{
3191 const ccv_cnnp_model_argmax_t* const self = (const ccv_cnnp_model_argmax_t*)super;
3192 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)
;
3193 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"
, 3193, __extension__ __PRETTY_FUNCTION__); }))
;
3194 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", 3194, __extension__ __PRETTY_FUNCTION__
); }))
;
3195 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3196 ccv_nnc_tensor_param_t output_params;
3197 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)
;
3198 argmax.info.reduce.axis[0] = self->axis;
3199 argmax.info.reduce.count = 1;
3200 ccv_nnc_hint_tensor_auto(argmax, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3201 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3202 ccv_nnc_graph_exec_symbol_new(graph, argmax, inputs, input_size, outputs, output_size, "argmax");
3203}
3204
3205static ccv_cnnp_model_t* _ccv_cnnp_argmax_copy(const ccv_cnnp_model_t* const self, void* const context);
3206
3207static const ccv_cnnp_model_vtab_t ccv_cnnp_argmax_isa = {
3208 .build = _ccv_cnnp_argmax_build,
3209 .copy = _ccv_cnnp_argmax_copy,
3210};
3211
3212ccv_cnnp_model_t* ccv_cnnp_argmax(const int axis, const char* const name)
3213{
3214 ccv_cnnp_model_argmax_t* const model_argmax = (ccv_cnnp_model_argmax_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_argmax_t));
3215 model_argmax->super.isa = &ccv_cnnp_argmax_isa;
3216 model_argmax->super.input_size = 1;
3217 model_argmax->super.outputs = &model_argmax->output;
3218 model_argmax->super.output_size = 1;
3219 ccv_cnnp_model_copy_name(&model_argmax->super, name);
3220 model_argmax->axis = axis;
3221 return (ccv_cnnp_model_t*)model_argmax;
3222}
3223
3224static ccv_cnnp_model_t* _ccv_cnnp_argmax_copy(const ccv_cnnp_model_t* const super, void* const context)
3225{
3226 const ccv_cnnp_model_argmax_t* const self = (const ccv_cnnp_model_argmax_t*)super;
3227 return ccv_cnnp_argmax(self->axis, self->super.name);
3228}
3229
3230// MARK - Argmin Layer
3231
3232typedef struct {
3233 ccv_cnnp_model_t super;
3234 int axis;
3235 ccv_nnc_tensor_symbol_t output;
3236} ccv_cnnp_model_argmin_t;
3237
3238static 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)
3239{
3240 const ccv_cnnp_model_argmin_t* const self = (const ccv_cnnp_model_argmin_t*)super;
3241 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)
;
3242 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"
, 3242, __extension__ __PRETTY_FUNCTION__); }))
;
3243 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", 3243, __extension__ __PRETTY_FUNCTION__
); }))
;
3244 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3245 ccv_nnc_tensor_param_t output_params;
3246 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)
;
3247 argmin.info.reduce.axis[0] = self->axis;
3248 argmin.info.reduce.count = 1;
3249 ccv_nnc_hint_tensor_auto(argmin, &input_params, 1, ccv_nnc_no_hint, &output_params, 1);
3250 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3251 ccv_nnc_graph_exec_symbol_new(graph, argmin, inputs, input_size, outputs, output_size, "argmin");
3252}
3253
3254static ccv_cnnp_model_t* _ccv_cnnp_argmin_copy(const ccv_cnnp_model_t* const self, void* const context);
3255
3256static const ccv_cnnp_model_vtab_t ccv_cnnp_argmin_isa = {
3257 .build = _ccv_cnnp_argmin_build,
3258 .copy = _ccv_cnnp_argmin_copy,
3259};
3260
3261ccv_cnnp_model_t* ccv_cnnp_argmin(const int axis, const char* const name)
3262{
3263 ccv_cnnp_model_argmin_t* const model_argmin = (ccv_cnnp_model_argmin_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_argmin_t));
3264 model_argmin->super.isa = &ccv_cnnp_argmin_isa;
3265 model_argmin->super.input_size = 1;
3266 model_argmin->super.outputs = &model_argmin->output;
3267 model_argmin->super.output_size = 1;
3268 ccv_cnnp_model_copy_name(&model_argmin->super, name);
3269 model_argmin->axis = axis;
3270 return (ccv_cnnp_model_t*)model_argmin;
3271}
3272
3273static ccv_cnnp_model_t* _ccv_cnnp_argmin_copy(const ccv_cnnp_model_t* const super, void* const context)
3274{
3275 const ccv_cnnp_model_argmin_t* const self = (const ccv_cnnp_model_argmin_t*)super;
3276 return ccv_cnnp_argmin(self->axis, self->super.name);
3277}
3278
3279// MARK - Min Layer
3280
3281typedef struct {
3282 ccv_cnnp_model_t super;
3283 ccv_nnc_tensor_symbol_t output;
3284} ccv_cnnp_model_min_t;
3285
3286static 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)
3287{
3288 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)
;
3289 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"
, 3289, __extension__ __PRETTY_FUNCTION__); }))
;
3290 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", 3290, __extension__ __PRETTY_FUNCTION__
); }))
;
3291 ccv_nnc_tensor_param_t input_params[2];
3292 int i;
3293 for (i = 0; i < 2; i++)
3294 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
3295 ccv_nnc_tensor_param_t output_params;
3296 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)
;
3297 ccv_nnc_hint_tensor_auto(min, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
3298 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3299 ccv_nnc_graph_exec_symbol_new(graph, min, inputs, input_size, outputs, output_size, "min");
3300}
3301
3302static ccv_cnnp_model_t* _ccv_cnnp_min_copy(const ccv_cnnp_model_t* const self, void* const context);
3303
3304static const ccv_cnnp_model_vtab_t ccv_cnnp_min_isa = {
3305 .build = _ccv_cnnp_min_build,
3306 .copy = _ccv_cnnp_min_copy,
3307};
3308
3309ccv_cnnp_model_t* ccv_cnnp_min(const char* const name)
3310{
3311 ccv_cnnp_model_min_t* const model_min = (ccv_cnnp_model_min_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_min_t));
3312 model_min->super.isa = &ccv_cnnp_min_isa;
3313 model_min->super.input_size = 2;
3314 model_min->super.outputs = &model_min->output;
3315 model_min->super.output_size = 1;
3316 ccv_cnnp_model_copy_name(&model_min->super, name);
3317 return (ccv_cnnp_model_t*)model_min;
3318}
3319
3320static ccv_cnnp_model_t* _ccv_cnnp_min_copy(const ccv_cnnp_model_t* const super, void* const context)
3321{
3322 const ccv_cnnp_model_min_t* const self = (const ccv_cnnp_model_min_t*)super;
3323 return ccv_cnnp_min(self->super.name);
3324}
3325
3326// MARK - Max Layer
3327
3328typedef struct {
3329 ccv_cnnp_model_t super;
3330 ccv_nnc_tensor_symbol_t output;
3331} ccv_cnnp_model_max_t;
3332
3333static 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)
3334{
3335 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)
;
3336 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"
, 3336, __extension__ __PRETTY_FUNCTION__); }))
;
3337 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", 3337, __extension__ __PRETTY_FUNCTION__
); }))
;
3338 ccv_nnc_tensor_param_t input_params[2];
3339 int i;
3340 for (i = 0; i < 2; i++)
3341 input_params[i] = ccv_nnc_tensor_symbol_params(graph, inputs[i]);
3342 ccv_nnc_tensor_param_t output_params;
3343 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)
;
3344 ccv_nnc_hint_tensor_auto(max, input_params, 2, ccv_nnc_no_hint, &output_params, 1);
3345 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params, 0);
3346 ccv_nnc_graph_exec_symbol_new(graph, max, inputs, input_size, outputs, output_size, "max");
3347}
3348
3349static ccv_cnnp_model_t* _ccv_cnnp_max_copy(const ccv_cnnp_model_t* const self, void* const context);
3350
3351static const ccv_cnnp_model_vtab_t ccv_cnnp_max_isa = {
3352 .build = _ccv_cnnp_max_build,
3353 .copy = _ccv_cnnp_max_copy,
3354};
3355
3356ccv_cnnp_model_t* ccv_cnnp_max(const char* const name)
3357{
3358 ccv_cnnp_model_max_t* const model_max = (ccv_cnnp_model_max_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_max_t));
3359 model_max->super.isa = &ccv_cnnp_max_isa;
3360 model_max->super.input_size = 2;
3361 model_max->super.outputs = &model_max->output;
3362 model_max->super.output_size = 1;
3363 ccv_cnnp_model_copy_name(&model_max->super, name);
3364 return (ccv_cnnp_model_t*)model_max;
3365}
3366
3367static ccv_cnnp_model_t* _ccv_cnnp_max_copy(const ccv_cnnp_model_t* const super, void* const context)
3368{
3369 const ccv_cnnp_model_max_t* const self = (const ccv_cnnp_model_max_t*)super;
3370 return ccv_cnnp_max(self->super.name);
3371}
3372
3373// MARK - LSTM Layer
3374
3375typedef struct {
3376 ccv_cnnp_model_t super;
3377 int masked;
3378 ccv_nnc_tensor_symbol_t output;
3379 ccv_nnc_tensor_symbol_t weights;
3380 ccv_nnc_tensor_symbol_t reserves;
3381 ccv_nnc_cmd_param_t params;
3382 ccv_nnc_graph_exec_symbol_t lstm;
3383} ccv_cnnp_model_lstm_t;
3384
3385static int _ccv_cnnp_lstm_weight_dim(int bidirectional, int num_layers, int input_size, int hidden_size, int proj_size, int bias)
3386{
3387 const int D = !!bidirectional + 1;
3388 if (hidden_size == proj_size)
3389 return (num_layers * (bias ? 8 : 0) + (num_layers - 1) * (hidden_size * 4 * D + hidden_size * 4) + input_size * 4 + hidden_size * 4) * D;
3390 else
3391 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;
3392}
3393
3394static 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)
3395{
3396 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3397 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)
;
3398 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", 3398, __extension__ __PRETTY_FUNCTION__
); }))
;
3399 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", 3399, __extension__ __PRETTY_FUNCTION__
); }))
;
3400 const int proj_size = self->params.rnn.proj_size == 0 ? self->params.rnn.hidden_size : self->params.rnn.proj_size;
3401 ccv_nnc_tensor_param_t input_params[5];
3402 input_params[0]= ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3403 if (input_size == 2)
3404 input_params[1] = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
3405 input_params[4] = input_params[0];
3406 memset(input_params[4].dim, 0, sizeof(input_params[4].dim));
3407 const int x_nd = ccv_nnc_tensor_nd(input_params[0].dim);
3408 const int feature_count = input_params[0].dim[x_nd - 1];
3409 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);
3410 input_params[4].dim[1] = self->params.rnn.hidden_size;
3411 const ccv_nnc_cmd_t lstm = ccv_nnc_cmd(CCV_NNC_LSTM_FORWARD, 0, self->params, 0);
3412 ccv_nnc_tensor_param_t output_params[4];
3413 ccv_nnc_hint_tensor_auto(lstm, input_params, 5, ccv_nnc_no_hint, output_params, 4);
3414 outputs[0] = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
3415 if (!self->weights.graph)
3416 self->weights = ccv_nnc_tensor_symbol_new(graph, input_params[4], "weights");
3417 if (!self->reserves.graph)
3418 self->reserves = ccv_nnc_tensor_symbol_new(graph, output_params[3], "reserves");
3419 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
}
;
3420 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");
3421}
3422
3423static 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)
3424{
3425 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3426 if (self->weights.graph)
3427 {
3428 const float stdv = 1.0 / sqrt(self->params.rnn.hidden_size);
3429 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);
3430 }
3431}
3432
3433static 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)
3434{
3435 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3436 if (self->weights.graph)
3437 add_to_array(parameters, self->weights, is_trainable);
3438}
3439
3440static 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)
3441{
3442 ccv_cnnp_model_lstm_t* const self = (ccv_cnnp_model_lstm_t*)super;
3443 if (self->lstm.graph)
3444 {
3445 self->params.rnn.is_test = is_test;
3446 updater(context, self->lstm, ccv_nnc_cmd(CCV_NNC_LSTM_FORWARD, 0, self->params, 0), ccv_nnc_no_hint);
3447 }
3448}
3449
3450static ccv_cnnp_model_t* _ccv_cnnp_lstm_copy(const ccv_cnnp_model_t* const self, void* const context);
3451
3452static const ccv_cnnp_model_vtab_t ccv_cnnp_lstm_isa = {
3453 .build = _ccv_cnnp_lstm_build,
3454 .init_states = _ccv_cnnp_lstm_init_states,
3455 .add_to_parameter = _ccv_cnnp_lstm_add_to_parameter,
3456 .copy = _ccv_cnnp_lstm_copy,
3457 .set_is_test = _ccv_cnnp_lstm_set_is_test,
3458};
3459
3460ccv_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)
3461{
3462 ccv_cnnp_model_lstm_t* const model_lstm = (ccv_cnnp_model_lstm_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_lstm_t));
3463 model_lstm->super.isa = &ccv_cnnp_lstm_isa;
3464 model_lstm->super.input_size = masked ? 2 : 1;
3465 model_lstm->super.outputs = &model_lstm->output;
3466 model_lstm->super.output_size = 1;
3467 model_lstm->super.is_trainable = is_trainable;
3468 ccv_cnnp_model_copy_name(&model_lstm->super, name);
3469 model_lstm->masked = masked;
3470 model_lstm->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
3471 model_lstm->weights.graph = 0;
3472 model_lstm->params.rnn.hidden_size = hidden_size;
3473 model_lstm->params.rnn.proj_size = proj_size;
3474 model_lstm->params.rnn.num_layers = num_layers;
3475 model_lstm->params.rnn.bias = bias;
3476 model_lstm->params.rnn.batch_first = batch_first;
3477 model_lstm->params.rnn.bidirectional = bidirectional;
3478 model_lstm->params.rnn.dropout = dropout;
3479 return (ccv_cnnp_model_t*)model_lstm;
3480}
3481
3482static ccv_cnnp_model_t* _ccv_cnnp_lstm_copy(const ccv_cnnp_model_t* const super, void* const context)
3483{
3484 const ccv_cnnp_model_lstm_t* const self = (const ccv_cnnp_model_lstm_t*)super;
3485 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);
3486}
3487
3488/// MARK - Datatype conversion layer.
3489
3490typedef struct {
3491 ccv_cnnp_model_t super;
3492 ccv_nnc_tensor_symbol_t output;
3493 int datatype;
3494 int ref_to_last;
3495} ccv_cnnp_model_datatype_conversion_t;
3496
3497static 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)
3498{
3499 ccv_cnnp_model_datatype_conversion_t* const self = (ccv_cnnp_model_datatype_conversion_t*)super;
3500 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)
;
3501 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3502 if (self->ref_to_last)
3503 {
3504 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", 3504, __extension__ __PRETTY_FUNCTION__
); }))
;
3505 const ccv_nnc_tensor_param_t last_params = ccv_nnc_tensor_symbol_params(graph, inputs[input_size - 1]);
3506 params.datatype = last_params.datatype;
3507 } else
3508 params.datatype = self->datatype;
3509 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", 3509, __extension__ __PRETTY_FUNCTION__
); }))
;
3510 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3511 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);
3512}
3513
3514static ccv_cnnp_model_t* _ccv_cnnp_datatype_conversion_copy(const ccv_cnnp_model_t* const self, void* const context);
3515
3516static const ccv_cnnp_model_vtab_t ccv_cnnp_datatype_conversion_isa = {
3517 .build = _ccv_cnnp_datatype_conversion_build,
3518 .copy = _ccv_cnnp_datatype_conversion_copy,
3519};
3520
3521ccv_cnnp_model_t* ccv_cnnp_datatype_conversion(const int datatype, const int ref_to_last, const char* const name)
3522{
3523 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));
3524 model_datatype_conversion->super.isa = &ccv_cnnp_datatype_conversion_isa;
3525 model_datatype_conversion->super.input_size = 0;
3526 model_datatype_conversion->super.outputs = &model_datatype_conversion->output;
3527 model_datatype_conversion->super.output_size = 1;
3528 model_datatype_conversion->datatype = datatype;
3529 model_datatype_conversion->ref_to_last = ref_to_last;
3530 ccv_cnnp_model_copy_name(&model_datatype_conversion->super, name);
3531 return (ccv_cnnp_model_t*)model_datatype_conversion;
3532}
3533
3534static ccv_cnnp_model_t* _ccv_cnnp_datatype_conversion_copy(const ccv_cnnp_model_t* const super, void* const context)
3535{
3536 ccv_cnnp_model_datatype_conversion_t* const self = (ccv_cnnp_model_datatype_conversion_t*)super;
3537 return ccv_cnnp_datatype_conversion(self->datatype, self->ref_to_last, self->super.name);
3538}
3539
3540/// MARK - Clamp layer.
3541
3542typedef struct {
3543 ccv_cnnp_model_t super;
3544 ccv_nnc_tensor_symbol_t output;
3545 float min;
3546 float max;
3547} ccv_cnnp_model_clamp_t;
3548
3549static 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)
3550{
3551 ccv_cnnp_model_clamp_t* const self = (ccv_cnnp_model_clamp_t*)super;
3552 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)
;
3553 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3554 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", 3554, __extension__ __PRETTY_FUNCTION__
); }))
;
3555 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3556 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);
3557}
3558
3559static ccv_cnnp_model_t* _ccv_cnnp_clamp_copy(const ccv_cnnp_model_t* const self, void* const context);
3560
3561static const ccv_cnnp_model_vtab_t ccv_cnnp_clamp_isa = {
3562 .build = _ccv_cnnp_clamp_build,
3563 .copy = _ccv_cnnp_clamp_copy,
3564};
3565
3566ccv_cnnp_model_t* ccv_cnnp_clamp(const float min, const float max, const char* const name)
3567{
3568 ccv_cnnp_model_clamp_t* const model_clamp = (ccv_cnnp_model_clamp_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_clamp_t));
3569 model_clamp->super.isa = &ccv_cnnp_clamp_isa;
3570 model_clamp->super.input_size = 0;
3571 model_clamp->super.outputs = &model_clamp->output;
3572 model_clamp->super.output_size = 1;
3573 model_clamp->min = min;
3574 model_clamp->max = max;
3575 ccv_cnnp_model_copy_name(&model_clamp->super, name);
3576 return (ccv_cnnp_model_t*)model_clamp;
3577}
3578
3579static ccv_cnnp_model_t* _ccv_cnnp_clamp_copy(const ccv_cnnp_model_t* const super, void* const context)
3580{
3581 ccv_cnnp_model_clamp_t* const self = (ccv_cnnp_model_clamp_t*)super;
3582 return ccv_cnnp_clamp(self->min, self->max, self->super.name);
3583}
3584
3585// MARK - Parameter Layer
3586
3587typedef struct {
3588 ccv_cnnp_model_t super;
3589 float init_bound;
3590 ccv_nnc_tensor_symbol_t weights;
3591 ccv_nnc_tensor_param_t weights_params;
3592 ccv_nnc_tensor_symbol_t output;
3593} ccv_cnnp_model_parameter_t;
3594
3595static 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)
3596{
3597 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)
;
3598 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", 3598, __extension__ __PRETTY_FUNCTION__
); }))
;
3599 ccv_cnnp_model_parameter_t* const self = (ccv_cnnp_model_parameter_t*)super;
3600 if (!self->weights.graph)
3601 self->weights = ccv_nnc_tensor_symbol_new(graph, self->weights_params, "weights");
3602 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"
, 3602, __extension__ __PRETTY_FUNCTION__); }))
;
3603 outputs[0] = self->weights;
3604}
3605
3606static 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)
3607{
3608 ccv_cnnp_model_parameter_t* const self = (ccv_cnnp_model_parameter_t*)super;
3609 if (self->init_bound > 0)
3610 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);
3611 else
3612 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);
3613}
3614
3615static 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)
3616{
3617 ccv_cnnp_model_parameter_t* const self = (ccv_cnnp_model_parameter_t*)super;
3618 add_to_array(parameters, self->weights, is_trainable);
3619}
3620
3621static ccv_cnnp_model_t* _ccv_cnnp_parameter_copy(const ccv_cnnp_model_t* const super, void* const context);
3622
3623static const ccv_cnnp_model_vtab_t ccv_cnnp_parameter_isa = {
3624 .build = _ccv_cnnp_parameter_build,
3625 .init_states = _ccv_cnnp_parameter_init_states,
3626 .add_to_parameter = _ccv_cnnp_parameter_add_to_parameter,
3627 .copy = _ccv_cnnp_parameter_copy,
3628};
3629
3630ccv_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)
3631{
3632 ccv_cnnp_model_parameter_t* const model_parameter = (ccv_cnnp_model_parameter_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_parameter_t));
3633 model_parameter->super.isa = &ccv_cnnp_parameter_isa;
3634 model_parameter->super.input_size = 0;
3635 model_parameter->super.outputs = &model_parameter->output;
3636 model_parameter->super.output_size = 1;
3637 model_parameter->super.is_trainable = is_trainable;
3638 ccv_cnnp_model_copy_name(&model_parameter->super, name);
3639 model_parameter->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
3640 model_parameter->weights.graph = 0;
3641 model_parameter->weights_params = params;
3642 return (ccv_cnnp_model_t*)model_parameter;
3643}
3644
3645static ccv_cnnp_model_t* _ccv_cnnp_parameter_copy(const ccv_cnnp_model_t* const super, void* const context)
3646{
3647 const ccv_cnnp_model_parameter_t* const self = (const ccv_cnnp_model_parameter_t*)super;
3648 return ccv_cnnp_parameter(self->weights_params, self->init_bound, self->super.is_trainable, self->super.name);
3649}
3650
3651// MARK - Scalar Layer
3652
3653typedef struct {
3654 ccv_cnnp_model_t super;
3655 int type;
3656 int format;
3657 int datatype;
3658 float value;
3659 ccv_nnc_tensor_symbol_t output;
3660} ccv_cnnp_model_scalar_t;
3661
3662static 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)
3663{
3664 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)
;
3665 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", 3665, __extension__ __PRETTY_FUNCTION__
); }))
;
3666 ccv_cnnp_model_scalar_t* const self = (ccv_cnnp_model_scalar_t*)super;
3667 ccv_nnc_tensor_param_t params = {
3668 .type = self->type,
3669 .format = self->format,
3670 .datatype = self->datatype,
3671 .dim = {
3672 1
3673 }
3674 };
3675 if (input_size > 0)
3676 {
3677 ccv_nnc_tensor_param_t input_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3678 params.type = input_params.type;
3679 params.format = input_params.format;
3680 params.datatype = input_params.datatype;
3681 }
3682 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3683 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);
3684}
3685
3686static ccv_cnnp_model_t* _ccv_cnnp_scalar_copy(const ccv_cnnp_model_t* const super, void* const context);
3687
3688static const ccv_cnnp_model_vtab_t ccv_cnnp_scalar_isa = {
3689 .build = _ccv_cnnp_scalar_build,
3690 .copy = _ccv_cnnp_scalar_copy,
3691};
3692
3693ccv_cnnp_model_t* ccv_cnnp_scalar(const int type, const int format, const int datatype, const float value, const char* const name)
3694{
3695 ccv_cnnp_model_scalar_t* const model_scalar = (ccv_cnnp_model_scalar_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_scalar_t));
3696 model_scalar->super.isa = &ccv_cnnp_scalar_isa;
3697 model_scalar->super.input_size = 0;
3698 model_scalar->super.outputs = &model_scalar->output;
3699 model_scalar->super.output_size = 1;
3700 ccv_cnnp_model_copy_name(&model_scalar->super, name);
3701 model_scalar->type = type;
3702 model_scalar->format = format;
3703 model_scalar->datatype = datatype;
3704 model_scalar->value = value;
3705 return (ccv_cnnp_model_t*)model_scalar;
3706}
3707
3708static ccv_cnnp_model_t* _ccv_cnnp_scalar_copy(const ccv_cnnp_model_t* const super, void* const context)
3709{
3710 const ccv_cnnp_model_scalar_t* const self = (const ccv_cnnp_model_scalar_t*)super;
3711 return ccv_cnnp_scalar(self->type, self->format, self->datatype, self->value, self->super.name);
3712}
3713
3714// MARK - Variable Layer
3715
3716typedef struct {
3717 ccv_cnnp_model_t super;
3718 ccv_nnc_tensor_param_t params;
3719 ccv_nnc_tensor_symbol_t output;
3720} ccv_cnnp_model_variable_t;
3721
3722static 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)
3723{
3724 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)
;
3725 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"
, 3725, __extension__ __PRETTY_FUNCTION__); }))
;
3726 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", 3726, __extension__ __PRETTY_FUNCTION__
); }))
;
3727 ccv_cnnp_model_variable_t* const self = (ccv_cnnp_model_variable_t*)super;
3728 outputs[0] = ccv_nnc_tensor_symbol_new(graph, self->params, 0);
3729}
3730
3731static ccv_cnnp_model_t* _ccv_cnnp_variable_copy(const ccv_cnnp_model_t* const super, void* const context);
3732
3733static const ccv_cnnp_model_vtab_t ccv_cnnp_variable_isa = {
3734 .build = _ccv_cnnp_variable_build,
3735 .copy = _ccv_cnnp_variable_copy,
3736};
3737
3738ccv_cnnp_model_t* ccv_cnnp_variable(const ccv_nnc_tensor_param_t params, const char* const name)
3739{
3740 ccv_cnnp_model_variable_t* const model_variable = (ccv_cnnp_model_variable_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_variable_t));
3741 model_variable->super.isa = &ccv_cnnp_variable_isa;
3742 model_variable->super.input_size = 0;
3743 model_variable->super.outputs = &model_variable->output;
3744 model_variable->super.output_size = 1;
3745 ccv_cnnp_model_copy_name(&model_variable->super, name);
3746 model_variable->params = params;
3747 return (ccv_cnnp_model_t*)model_variable;
3748}
3749
3750static ccv_cnnp_model_t* _ccv_cnnp_variable_copy(const ccv_cnnp_model_t* const super, void* const context)
3751{
3752 const ccv_cnnp_model_variable_t* const self = (const ccv_cnnp_model_variable_t*)super;
3753 return ccv_cnnp_variable(self->params, self->super.name);
3754}
3755
3756// MARK - Move Layer
3757
3758typedef struct {
3759 ccv_cnnp_model_t super;
3760 ccv_nnc_tensor_symbol_t output;
3761} ccv_cnnp_model_move_t;
3762
3763static 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)
3764{
3765 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)
;
3766 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"
, 3766, __extension__ __PRETTY_FUNCTION__); }))
;
3767 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", 3767, __extension__ __PRETTY_FUNCTION__
); }))
;
3768 outputs[0] = inputs[1];
3769 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");
3770}
3771
3772static ccv_cnnp_model_t* _ccv_cnnp_move_copy(const ccv_cnnp_model_t* const super, void* const context);
3773
3774static const ccv_cnnp_model_vtab_t ccv_cnnp_move_isa = {
3775 .build = _ccv_cnnp_move_build,
3776 .copy = _ccv_cnnp_move_copy,
3777};
3778
3779ccv_cnnp_model_t* ccv_cnnp_move(const char* const name)
3780{
3781 ccv_cnnp_model_move_t* const model_move = (ccv_cnnp_model_move_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_move_t));
3782 model_move->super.isa = &ccv_cnnp_move_isa;
3783 model_move->super.input_size = 2;
3784 model_move->super.outputs = &model_move->output;
3785 model_move->super.output_size = 1;
3786 ccv_cnnp_model_copy_name(&model_move->super, name);
3787 return (ccv_cnnp_model_t*)model_move;
3788}
3789
3790static ccv_cnnp_model_t* _ccv_cnnp_move_copy(const ccv_cnnp_model_t* const super, void* const context)
3791{
3792 const ccv_cnnp_model_move_t* const self = (const ccv_cnnp_model_move_t*)super;
3793 return ccv_cnnp_move(self->super.name);
3794}
3795
3796// MARK - "Making" Contiguous Layer
3797
3798typedef struct {
3799 ccv_cnnp_model_t super;
3800 ccv_nnc_tensor_symbol_t output;
3801} ccv_cnnp_model_contiguous_t;
3802
3803static 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)
3804{
3805 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)
;
3806 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"
, 3806, __extension__ __PRETTY_FUNCTION__); }))
;
3807 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", 3807, __extension__ __PRETTY_FUNCTION__
); }))
;
3808 ccv_nnc_tensor_param_t params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3809 ccv_nnc_tensor_symbol_t to = ccv_nnc_tensor_symbol_alias_to(graph, inputs[0]);
3810 if (to.d == CCV_NNC_NO_TENSOR_SYMBOL) // If we are not reshape an alias, it is straightforward.
3811 {
3812 outputs[0] = inputs[0];
3813 return;
3814 }
3815 // Otherwise, we need to check its stride to know if it is contiguous.
3816 int old_stride[CCV_NNC_MAX_DIM_ALLOC(12)];
3817 ccv_nnc_tensor_symbol_alias_params(graph, inputs[0], 0, old_stride);
3818 // We identify permute by checking if the stride is not in descending order.
3819 // This also covered "permute" through reshape, rather than using ccv_cnnp_permute directly.
3820 if (ccv_nnc_is_tensor_stride_packed(old_stride, params.dim))
3821 {
3822 outputs[0] = inputs[0];
3823 return;
3824 }
3825 outputs[0] = ccv_nnc_tensor_symbol_new(graph, params, 0);
3826 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");
3827 ccv_nnc_graph_exec_symbol_set_flags(graph, make_contiguous, CCV_NNC_GRAPH_EXEC_DISABLE_OPT);
3828}
3829
3830static ccv_cnnp_model_t* _ccv_cnnp_contiguous_copy(const ccv_cnnp_model_t* const super, void* const context);
3831
3832static const ccv_cnnp_model_vtab_t ccv_cnnp_contiguous_isa = {
3833 .build = _ccv_cnnp_contiguous_build,
3834 .copy = _ccv_cnnp_contiguous_copy,
3835};
3836
3837ccv_cnnp_model_t* ccv_cnnp_contiguous(const char* const name)
3838{
3839 ccv_cnnp_model_contiguous_t* const model_contiguous = (ccv_cnnp_model_contiguous_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_contiguous_t));
3840 model_contiguous->super.isa = &ccv_cnnp_contiguous_isa;
3841 model_contiguous->super.input_size = 1;
3842 model_contiguous->super.outputs = &model_contiguous->output;
3843 model_contiguous->super.output_size = 1;
3844 ccv_cnnp_model_copy_name(&model_contiguous->super, name);
3845 return (ccv_cnnp_model_t*)model_contiguous;
3846}
3847
3848static ccv_cnnp_model_t* _ccv_cnnp_contiguous_copy(const ccv_cnnp_model_t* const super, void* const context)
3849{
3850 const ccv_cnnp_model_contiguous_t* const self = (const ccv_cnnp_model_contiguous_t*)super;
3851 return ccv_cnnp_contiguous(self->super.name);
3852}
3853
3854// MARK - Scaled-Dot Product Attention Layer
3855
3856typedef struct {
3857 ccv_cnnp_model_t super;
3858 ccv_nnc_tensor_symbol_t output;
3859 ccv_nnc_tensor_symbol_t weights;
3860 ccv_nnc_tensor_symbol_t bias;
3861 float scale;
3862 int is_causal;
3863 int has_attn_mask;
3864 int flags;
3865 int fused_unify_head_weights;
3866 int no_bias;
3867} ccv_cnnp_model_scaled_dot_product_attention_t;
3868
3869static 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)
3870{
3871 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)
;
3872 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"
, 3872, __extension__ __PRETTY_FUNCTION__); }))
;
3873 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", 3873, __extension__ __PRETTY_FUNCTION__
); }))
;
3874 ccv_cnnp_model_scaled_dot_product_attention_t* const self = (ccv_cnnp_model_scaled_dot_product_attention_t*)super;
3875 const ccv_nnc_tensor_param_t q_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
3876 const ccv_nnc_tensor_param_t k_params = ccv_nnc_tensor_symbol_params(graph, inputs[1]);
3877 const ccv_nnc_tensor_param_t v_params = ccv_nnc_tensor_symbol_params(graph, inputs[2]);
3878 const int v_nd = ccv_nnc_tensor_nd(v_params.dim);
3879 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", 3879, __extension__ __PRETTY_FUNCTION__
); }))
;
3880 const int hEv = (v_nd == 3 ? 1 : v_params.dim[2]) * v_params.dim[v_nd - 1];
3881 ccv_nnc_tensor_param_t weights_params = q_params;
3882 memset(weights_params.dim, 0, sizeof(weights_params.dim));
3883 weights_params.dim[0] = hEv;
3884 weights_params.dim[1] = hEv;
3885 ccv_nnc_tensor_param_t bias_params = q_params;
3886 memset(bias_params.dim, 0, sizeof(bias_params.dim));
3887 bias_params.dim[0] = hEv;
3888 ccv_nnc_cmd_t cmd = {0};
3889 cmd.cmd = CCV_NNC_SCALED_DOT_PRODUCT_ATTENTION_FORWARD;
3890 cmd.info.scaled_dot_product_attention.scale = self->scale;
3891 cmd.info.scaled_dot_product_attention.is_causal = self->is_causal;
3892 cmd.info.scaled_dot_product_attention.flags = self->flags;
3893 ccv_nnc_tensor_param_t output_params[3];
3894 ccv_nnc_tensor_symbol_t output;
3895 ccv_nnc_tensor_symbol_t saved_softmax_lse;
3896 ccv_nnc_tensor_symbol_t saved_v_proj = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3897 ccv_nnc_tensor_symbol_t attn_mask = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3898 ccv_nnc_tensor_symbol_t weights = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3899 ccv_nnc_tensor_symbol_t bias = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
3900 if (self->has_attn_mask)
3901 attn_mask = inputs[3];
3902 if (self->fused_unify_head_weights)
3903 {
3904 if (!self->weights.graph)
3905 self->weights = ccv_nnc_tensor_symbol_new(graph, weights_params, "weights");
3906 weights = self->weights;
3907 if (!self->no_bias)
3908 {
3909 if (!self->bias.graph)
3910 self->bias = ccv_nnc_tensor_symbol_new(graph, bias_params, "bias");
3911 bias = self->bias;
3912 }
3913 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
3914 q_params,
3915 k_params,
3916 v_params,
3917 (ccv_nnc_tensor_param_t){},
3918 weights_params,
3919 bias_params,
3920 }, 6, ccv_nnc_no_hint, output_params, 3);
3921 output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
3922 saved_softmax_lse = ccv_nnc_tensor_symbol_new(graph, output_params[1], 0);
3923 saved_v_proj = ccv_nnc_tensor_symbol_new(graph, output_params[2], 0);
3924 } else {
3925 ccv_nnc_hint_tensor_auto(cmd, (ccv_nnc_tensor_param_t []){
3926 q_params,
3927 k_params,
3928 v_params,
3929 }, 3, ccv_nnc_no_hint, output_params, 2);
3930 output = ccv_nnc_tensor_symbol_new(graph, output_params[0], 0);
3931 saved_softmax_lse = ccv_nnc_tensor_symbol_new(graph, output_params[1], 0);
3932 }
3933 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_lse, saved_v_proj)(const ccv_nnc_tensor_symbol_t []){output, saved_softmax_lse,
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");
3934 outputs[0] = output;
3935}
3936
3937static 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)
3938{
3939 ccv_cnnp_model_scaled_dot_product_attention_t* const self = (ccv_cnnp_model_scaled_dot_product_attention_t*)super;
3940 if (self->weights.graph)
3941 {
3942 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"
, 3942, __extension__ __PRETTY_FUNCTION__); }))
;
3943 const ccv_nnc_tensor_param_t weight_params = ccv_nnc_tensor_symbol_params(graph, self->weights);
3944 const int c = weight_params.dim[1];
3945 const float std = sqrtf(2) / sqrtf(c);
3946 const float bound = sqrtf(3) * std;
3947 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);
3948 if (self->bias.graph)
3949 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);
3950 }
3951}
3952
3953static 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)
3954{
3955 ccv_cnnp_model_scaled_dot_product_attention_t* const self = (ccv_cnnp_model_scaled_dot_product_attention_t*)super;
3956 if (self->weights.graph)
3957 {
3958 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"
, 3958, __extension__ __PRETTY_FUNCTION__); }))
;
3959 add_to_array(parameters, self->weights, is_trainable);
3960 if (self->bias.graph)
3961 add_to_array(parameters, self->bias, is_trainable);
3962 }
3963}
3964
3965static ccv_cnnp_model_t* _ccv_cnnp_scaled_dot_product_attention_copy(const ccv_cnnp_model_t* const super, void* const context);
3966
3967static const ccv_cnnp_model_vtab_t ccv_cnnp_scaled_dot_product_attention_isa = {
3968 .build = _ccv_cnnp_scaled_dot_product_attention_build,
3969 .copy = _ccv_cnnp_scaled_dot_product_attention_copy,
3970};
3971
3972static const ccv_cnnp_model_vtab_t ccv_cnnp_scaled_dot_product_attention_fused_isa = {
3973 .build = _ccv_cnnp_scaled_dot_product_attention_build,
3974 .init_states = _ccv_cnnp_scaled_dot_product_attention_init_states,
3975 .add_to_parameter = _ccv_cnnp_scaled_dot_product_attention_add_to_parameter,
3976 .copy = _ccv_cnnp_scaled_dot_product_attention_copy,
3977};
3978
3979ccv_cnnp_model_t* ccv_cnnp_scaled_dot_product_attention(const float scale, const int is_causal, const int has_attn_mask, const int flags, const int fused_unify_head_weights, const int no_bias, const int is_trainable, const char* const name)
3980{
3981 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));
3982 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;
3983 model_scaled_dot_product_attention->super.input_size = has_attn_mask ? 4 : 3;
3984 model_scaled_dot_product_attention->super.outputs = &model_scaled_dot_product_attention->output;
3985 model_scaled_dot_product_attention->super.output_size = 1;
3986 model_scaled_dot_product_attention->super.is_trainable = is_trainable;
3987 ccv_cnnp_model_copy_name(&model_scaled_dot_product_attention->super, name);
3988 model_scaled_dot_product_attention->weights.d = CCV_NNC_NO_TENSOR_SYMBOL;
3989 model_scaled_dot_product_attention->weights.graph = 0;
3990 model_scaled_dot_product_attention->bias.d = CCV_NNC_NO_TENSOR_SYMBOL;
3991 model_scaled_dot_product_attention->bias.graph = 0;
3992 model_scaled_dot_product_attention->scale = scale;
3993 model_scaled_dot_product_attention->is_causal = is_causal;
3994 model_scaled_dot_product_attention->has_attn_mask = has_attn_mask;
3995 model_scaled_dot_product_attention->flags = flags;
3996 model_scaled_dot_product_attention->fused_unify_head_weights = fused_unify_head_weights;
3997 model_scaled_dot_product_attention->no_bias = no_bias;
3998 return (ccv_cnnp_model_t*)model_scaled_dot_product_attention;
3999}
4000
4001static ccv_cnnp_model_t* _ccv_cnnp_scaled_dot_product_attention_copy(const ccv_cnnp_model_t* const super, void* const context)
4002{
4003 const ccv_cnnp_model_scaled_dot_product_attention_t* const self = (const ccv_cnnp_model_scaled_dot_product_attention_t*)super;
4004 return ccv_cnnp_scaled_dot_product_attention(self->scale, self->is_causal, self->has_attn_mask, self->flags, self->fused_unify_head_weights, self->no_bias, self->super.is_trainable, self->super.name);
4005}
4006
4007// MARK - Debug Layer
4008
4009typedef struct {
4010 ccv_cnnp_model_t super;
4011 ccv_nnc_tensor_symbol_t output;
4012 ccv_cnnp_model_debug_f debugger;
4013 ccv_cnnp_model_debug_context_deinit_f debug_deinit;
4014 ccv_cnnp_model_debug_context_copy_f debug_copy;
4015 void* debug_context;
4016} ccv_cnnp_model_debug_t;
4017
4018static int _ccv_cnnp_debug_exec(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)
4019{
4020 if (cmd.cmd == CCV_NNC_CUSTOM_BACKWARD)
4021 {
4022 assert(0 && "don't support debug backward pass yet")((void) sizeof ((0 && "don't support debug backward pass yet"
) ? 1 : 0), __extension__ ({ if (0 && "don't support debug backward pass yet"
) ; else __assert_fail ("0 && \"don't support debug backward pass yet\""
, "ccv_cnnp_model_addons.c", 4022, __extension__ __PRETTY_FUNCTION__
); }))
;
4023 }
4024 ccv_cnnp_model_debug_t* const self = (ccv_cnnp_model_debug_t*)cmd.data;
4025 self->debugger(inputs, input_size, stream_context, self->debug_context);
4026 return CCV_NNC_EXEC_SUCCESS;
4027}
4028
4029static ccv_nnc_cmd_vtab_t ccv_cnnp_debug_exec_isa = {
4030 .exec = _ccv_cnnp_debug_exec
4031};
4032
4033static void _ccv_cnnp_debug_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)
4034{
4035 PRINT(CCV_CLI_VERBOSE, "[cnnp_debug_build] -\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("[cnnp_debug_build] -\n"); fflush(stdout); } } while
(0)
;
4036 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", 4036, __extension__ __PRETTY_FUNCTION__
); }))
;
4037 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", 4037, __extension__ __PRETTY_FUNCTION__
); }))
;
4038 ccv_nnc_tensor_symbol_t to = ccv_nnc_tensor_symbol_alias_to(graph, inputs[0]);
4039 ccv_nnc_tensor_param_t output_params = ccv_nnc_tensor_symbol_params(graph, inputs[0]);
4040 if (to.d == CCV_NNC_NO_TENSOR_SYMBOL) // If we are not reshape an alias, it is straightforward.
4041 {
4042 int ofs[CCV_NNC_MAX_DIM_ALLOC(12)] = {0};
4043 int stride[CCV_NNC_MAX_DIM_ALLOC(12)];
4044 ccv_nnc_tensor_get_stride(output_params.dim, stride);
4045 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, inputs[0], ofs, stride, output_params, 0);
4046 } else {
4047 int old_ofs[CCV_NNC_MAX_DIM_ALLOC(12)];
4048 int old_stride[CCV_NNC_MAX_DIM_ALLOC(12)];
4049 ccv_nnc_tensor_symbol_alias_params(graph, inputs[0], old_ofs, old_stride);
4050 outputs[0] = ccv_nnc_tensor_symbol_alias_new(graph, to, old_ofs, old_stride, output_params, 0);
4051 }
4052 ccv_nnc_cmd_t cmd = ccv_nnc_cmd(CCV_NNC_CUSTOM_FORWARD, (ccv_nnc_cmd_vtab_t*)&ccv_cnnp_debug_exec_isa, (ccv_nnc_cmd_param_t){}, 0);
4053 cmd.data = self;
4054 ccv_nnc_graph_exec_symbol_t make_debug = ccv_nnc_graph_exec_symbol_new(graph, cmd, inputs, input_size, outputs, 1, "debug");
4055 // Disable any optimizations.
4056 ccv_nnc_graph_exec_symbol_set_flags(graph, make_debug, CCV_NNC_GRAPH_EXEC_DISABLE_OPT);
4057}
4058
4059static void _ccv_cnnp_debug_deinit(ccv_cnnp_model_t* const super)
4060{
4061 const ccv_cnnp_model_debug_t* const self = (const ccv_cnnp_model_debug_t*)super;
4062 if (self->debug_deinit && self->debug_context)
4063 self->debug_deinit(self->debug_context);
4064}
4065
4066static ccv_cnnp_model_t* _ccv_cnnp_debug_copy(const ccv_cnnp_model_t* const super, void* const context);
4067
4068static const ccv_cnnp_model_vtab_t ccv_cnnp_debug_isa = {
4069 .build = _ccv_cnnp_debug_build,
4070 .deinit = _ccv_cnnp_debug_deinit,
4071 .copy = _ccv_cnnp_debug_copy,
4072};
4073
4074ccv_cnnp_model_t* ccv_cnnp_debug(ccv_cnnp_model_debug_f func, void* const context, ccv_cnnp_model_debug_context_deinit_f deinit, ccv_cnnp_model_debug_context_copy_f copy, const char* const name)
4075{
4076 ccv_cnnp_model_debug_t* const model_debug = (ccv_cnnp_model_debug_t*)cccalloccalloc(1, sizeof(ccv_cnnp_model_debug_t));
4077 model_debug->super.isa = &ccv_cnnp_debug_isa;
4078 model_debug->super.input_size = 0;
4079 model_debug->super.outputs = &model_debug->output;
4080 model_debug->super.output_size = 1;
4081 model_debug->debugger = func;
4082 model_debug->debug_context = context;
4083 model_debug->debug_deinit = deinit;
4084 model_debug->debug_copy = copy;
4085 ccv_cnnp_model_copy_name(&model_debug->super, name);
4086 return (ccv_cnnp_model_t*)model_debug;
4087}
4088
4089static ccv_cnnp_model_t* _ccv_cnnp_debug_copy(const ccv_cnnp_model_t* const super, void* const context)
4090{
4091 const ccv_cnnp_model_debug_t* const self = (const ccv_cnnp_model_debug_t*)super;
4092 void* debug_context = self->debug_context;
4093 if (self->debug_copy && self->debug_context)
4094 debug_context = self->debug_copy(self->debug_context);
4095 return ccv_cnnp_debug(self->debugger, debug_context, self->debug_deinit, self->debug_copy, self->super.name);
4096}