/home/liu/actions-runner/_work/ccv/ccv/test/int/nnc/leaky_relu.tests.c
Line | Count | Source |
1 | | #include "case.h" |
2 | | #include "ccv_case.h" |
3 | | #include "ccv_nnc_case.h" |
4 | | #include <ccv.h> |
5 | | #include <nnc/ccv_nnc.h> |
6 | | #include <nnc/ccv_nnc_easy.h> |
7 | | #include <3rdparty/dsfmt/dSFMT.h> |
8 | | |
9 | | TEST_SETUP() |
10 | | { |
11 | | ccv_nnc_init(); |
12 | | } |
13 | | |
14 | | TEST_CASE("leaky relu in float") |
15 | 1 | { |
16 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) || ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_FORWARD, CCV_NNC_BACKEND_MPS)); |
17 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
18 | 1 | ccv_nnc_tensor_symbol_t a = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 32F, 20, 10), "a"); |
19 | 1 | ccv_nnc_tensor_symbol_t b = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 32F, 20, 10), "b"); |
20 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_LEAKY_RELU_FORWARD(0.2), TENSOR_SYMBOL_LIST(a), TENSOR_SYMBOL_LIST(b), "leaky relu"); |
21 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
22 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
23 | 1 | ccv_nnc_graph_t* graph = 0; |
24 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
25 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
26 | 1 | ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, 0, 0, 0, 0, SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena); |
27 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
28 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
29 | 1 | dsfmt_t dsfmt; |
30 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
31 | 1 | int i; |
32 | 201 | for (i = 0; i < 20 * 10; i++200 ) |
33 | 200 | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
34 | 1 | ccv_nnc_tensor_t* const a_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, a); |
35 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(a_tensor), 0); |
36 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
37 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
38 | 1 | ccv_nnc_tensor_t* const b_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, b); |
39 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(b_tensor), TENSOR_LIST(y_tensor), 0); |
40 | 1 | ccv_nnc_tensor_t* const ty = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
41 | 1 | ccv_nnc_cmd_exec(CMD_LEAKY_RELU_FORWARD(0.2), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty), 0); |
42 | 1 | REQUIRE_TENSOR_EQ(ty, y_tensor, "leaky relu from cuda should match from CPU"); |
43 | 1 | ccv_nnc_tensor_free(x_tensor); |
44 | 1 | ccv_nnc_tensor_free(y_tensor); |
45 | 1 | ccv_nnc_tensor_free(ty); |
46 | 1 | ccv_nnc_graph_free(graph); |
47 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
48 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
49 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
50 | 1 | } |
51 | | |
52 | | TEST_CASE("leaky relu in half precision") |
53 | 1 | { |
54 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) || ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_FORWARD, CCV_NNC_BACKEND_MPS)); |
55 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
56 | 1 | ccv_nnc_tensor_symbol_t a = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 16F, 20, 10), "a"); |
57 | 1 | ccv_nnc_tensor_symbol_t b = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 16F, 20, 10), "b"); |
58 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_LEAKY_RELU_FORWARD(0.2), TENSOR_SYMBOL_LIST(a), TENSOR_SYMBOL_LIST(b), "leaky relu"); |
59 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
60 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
61 | 1 | ccv_nnc_graph_t* graph = 0; |
62 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
63 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
64 | 1 | ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, 0, 0, 0, 0, SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena); |
65 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
66 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
67 | 1 | dsfmt_t dsfmt; |
68 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
69 | 1 | int i; |
70 | 201 | for (i = 0; i < 20 * 10; i++200 ) |
71 | 200 | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
72 | 1 | ccv_nnc_tensor_t* const a_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, a); |
73 | 1 | ccv_nnc_tensor_t* const x16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(16F, 20, 10), 0); |
74 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(x16_tensor), 0); |
75 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x16_tensor), TENSOR_LIST(a_tensor), 0); |
76 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
77 | 1 | ccv_nnc_tensor_t* const y16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(16F, 20, 10), 0); |
78 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
79 | 1 | ccv_nnc_tensor_t* const b_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, b); |
80 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(b_tensor), TENSOR_LIST(y16_tensor), 0); |
81 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(y16_tensor), TENSOR_LIST(y_tensor), 0); |
82 | 1 | ccv_nnc_tensor_t* const ty = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
83 | 1 | ccv_nnc_cmd_exec(CMD_LEAKY_RELU_FORWARD(0.2), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty), 0); |
84 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, ty->data.f32, y_tensor->data.f32, 20 * 10, 1e-3, "leaky relu from cuda should match from CPU"); |
85 | 1 | ccv_nnc_tensor_free(x_tensor); |
86 | 1 | ccv_nnc_tensor_free(x16_tensor); |
87 | 1 | ccv_nnc_tensor_free(y16_tensor); |
88 | 1 | ccv_nnc_tensor_free(y_tensor); |
89 | 1 | ccv_nnc_tensor_free(ty); |
90 | 1 | ccv_nnc_graph_free(graph); |
91 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
92 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
93 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
94 | 1 | } |
95 | | |
96 | | TEST_CASE("leaky relu gradient in float") |
97 | 1 | { |
98 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) && |
99 | 1 | ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_BACKWARD, CCV_NNC_BACKEND_GPU_REF)); |
100 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
101 | 1 | ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 10, 100), "x"); |
102 | 1 | ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 10, 100), "y"); |
103 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_LEAKY_RELU_FORWARD(0.2), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "leaky relu"); |
104 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
105 | 1 | ccv_nnc_symbolic_graph_backward(symbolic_graph, TENSOR_SYMBOL_LIST(y), TENSOR_SYMBOL_LIST(x), SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph)); |
106 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
107 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
108 | 1 | ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
109 | 1 | ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
110 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
111 | 1 | dsfmt_t dsfmt; |
112 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
113 | 1 | int i; |
114 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
115 | 1.00k | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
116 | 1 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
117 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
118 | 1.00k | dy_tensor->data.f32[i] = 0; |
119 | 11 | for (i = 0; i < 10; i++10 ) |
120 | 10 | dy_tensor->data.f32[i * 100 + i] = 1; |
121 | 1 | ccv_nnc_tensor_t* const dyt = ccv_nnc_tensor_new(0, GPU_TENSOR_NHWC(000, 32F, 10, 100), 0); |
122 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor), TENSOR_LIST(dyt), 0); |
123 | 1 | ccv_nnc_graph_t* graph = 0; |
124 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
125 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
126 | 1 | ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, TENSOR_BIND_MAP(KV(dy, dyt)), TENSOR_SYMBOL_LIST(y), SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena); |
127 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
128 | 1 | ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
129 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(xt), 0); |
130 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
131 | 1 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
132 | 1 | ccv_nnc_tensor_t* const dxt = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
133 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
134 | 1 | ccv_nnc_tensor_t* const yt = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
135 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dxt), TENSOR_LIST(dx_tensor), 0); |
136 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(yt), TENSOR_LIST(y_tensor), 0); |
137 | 1 | ccv_nnc_tensor_t* const ty_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
138 | 1 | ccv_nnc_cmd_exec(CMD_LEAKY_RELU_FORWARD(0.2), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty_tensor), 0); |
139 | 1 | REQUIRE_TENSOR_EQ(ty_tensor, y_tensor, "forward pass should match"); |
140 | 1 | ccv_nnc_tensor_t* const tdx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
141 | 1 | ccv_nnc_cmd_exec(CMD_LEAKY_RELU_BACKWARD(0.2), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, 0, y_tensor), TENSOR_LIST(tdx_tensor), 0); |
142 | 1 | REQUIRE_TENSOR_EQ(tdx_tensor, dx_tensor, "backward pass should match"); |
143 | 1 | ccv_nnc_tensor_free(x_tensor); |
144 | 1 | ccv_nnc_tensor_free(y_tensor); |
145 | 1 | ccv_nnc_tensor_free(dx_tensor); |
146 | 1 | ccv_nnc_tensor_free(dy_tensor); |
147 | 1 | ccv_nnc_tensor_free(ty_tensor); |
148 | 1 | ccv_nnc_tensor_free(tdx_tensor); |
149 | 1 | ccv_nnc_tensor_free(dyt); |
150 | 1 | ccv_nnc_graph_free(graph); |
151 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
152 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
153 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
154 | 1 | } |
155 | | |
156 | | TEST_CASE("leaky relu gradient in half precision") |
157 | 1 | { |
158 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) && |
159 | 1 | ccv_nnc_cmd_ok(CCV_NNC_LEAKY_RELU_BACKWARD, CCV_NNC_BACKEND_GPU_REF)); |
160 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
161 | 1 | ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "x"); |
162 | 1 | ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "y"); |
163 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_LEAKY_RELU_FORWARD(0.2), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "leaky relu"); |
164 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
165 | 1 | ccv_nnc_symbolic_graph_backward(symbolic_graph, TENSOR_SYMBOL_LIST(y), TENSOR_SYMBOL_LIST(x), SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph)); |
166 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
167 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
168 | 1 | ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
169 | 1 | ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
170 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
171 | 1 | dsfmt_t dsfmt; |
172 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
173 | 1 | int i; |
174 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
175 | 1.00k | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
176 | 1 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
177 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
178 | 1.00k | dy_tensor->data.f32[i] = 0; |
179 | 11 | for (i = 0; i < 10; i++10 ) |
180 | 10 | dy_tensor->data.f32[i * 100 + i] = 1; |
181 | 1 | ccv_nnc_tensor_t* const dy16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
182 | 1 | ccv_nnc_tensor_t* const dyt = ccv_nnc_tensor_new(0, GPU_TENSOR_NHWC(000, 16F, 10, 100), 0); |
183 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor), TENSOR_LIST(dy16_tensor), 0); |
184 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy16_tensor), TENSOR_LIST(dyt), 0); |
185 | 1 | ccv_nnc_graph_t* graph = 0; |
186 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
187 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
188 | 1 | ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, TENSOR_BIND_MAP(KV(dy, dyt)), TENSOR_SYMBOL_LIST(y), SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena); |
189 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
190 | 1 | ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
191 | 1 | ccv_nnc_tensor_t* const x16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
192 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(x16_tensor), 0); |
193 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x16_tensor), TENSOR_LIST(xt), 0); |
194 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
195 | 1 | ccv_nnc_tensor_t* const dx16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
196 | 1 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
197 | 1 | ccv_nnc_tensor_t* const dxt = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
198 | 1 | ccv_nnc_tensor_t* const y16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
199 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
200 | 1 | ccv_nnc_tensor_t* const yt = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
201 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dxt), TENSOR_LIST(dx16_tensor), 0); |
202 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dx16_tensor), TENSOR_LIST(dx_tensor), 0); |
203 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(yt), TENSOR_LIST(y16_tensor), 0); |
204 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(y16_tensor), TENSOR_LIST(y_tensor), 0); |
205 | 1 | ccv_nnc_tensor_t* const ty_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
206 | 1 | ccv_nnc_cmd_exec(CMD_LEAKY_RELU_FORWARD(0.2), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty_tensor), 0); |
207 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, ty_tensor->data.f32, y_tensor->data.f32, 10 * 100, 1e-3, "forward pass should match"); |
208 | 1 | ccv_nnc_tensor_t* const tdx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
209 | 1 | ccv_nnc_cmd_exec(CMD_LEAKY_RELU_BACKWARD(0.2), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, 0, y_tensor), TENSOR_LIST(tdx_tensor), 0); |
210 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, tdx_tensor->data.f32, dx_tensor->data.f32, 10 * 100, 1e-3, "backward pass should match"); |
211 | 1 | ccv_nnc_tensor_free(x_tensor); |
212 | 1 | ccv_nnc_tensor_free(x16_tensor); |
213 | 1 | ccv_nnc_tensor_free(y_tensor); |
214 | 1 | ccv_nnc_tensor_free(y16_tensor); |
215 | 1 | ccv_nnc_tensor_free(dx_tensor); |
216 | 1 | ccv_nnc_tensor_free(dx16_tensor); |
217 | 1 | ccv_nnc_tensor_free(dy_tensor); |
218 | 1 | ccv_nnc_tensor_free(dy16_tensor); |
219 | 1 | ccv_nnc_tensor_free(ty_tensor); |
220 | 1 | ccv_nnc_tensor_free(tdx_tensor); |
221 | 1 | ccv_nnc_tensor_free(dyt); |
222 | 1 | ccv_nnc_graph_free(graph); |
223 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
224 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
225 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
226 | 1 | } |
227 | | |
228 | | #include "case_main.h" |