/home/liu/actions-runner/_work/ccv/ccv/test/int/nnc/gelu.tests.c
Line | Count | Source (jump to first uncovered line) |
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("gelu in float") |
15 | | { |
16 | | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) || ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_MPS)); |
17 | | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
18 | | ccv_nnc_tensor_symbol_t a = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 32F, 20, 10), "a"); |
19 | | ccv_nnc_tensor_symbol_t b = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 32F, 20, 10), "b"); |
20 | | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(0), TENSOR_SYMBOL_LIST(a), TENSOR_SYMBOL_LIST(b), "gelu"); |
21 | | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
22 | | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
23 | | ccv_nnc_graph_t* graph = 0; |
24 | | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
25 | | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
26 | | 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 | | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
28 | | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
29 | | dsfmt_t dsfmt; |
30 | | dsfmt_init_gen_rand(&dsfmt, 0); |
31 | | int i; |
32 | | for (i = 0; i < 20 * 10; i++) |
33 | | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
34 | | ccv_nnc_tensor_t* const a_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, a); |
35 | | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(a_tensor), 0); |
36 | | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
37 | | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
38 | | ccv_nnc_tensor_t* const b_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, b); |
39 | | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(b_tensor), TENSOR_LIST(y_tensor), 0); |
40 | | ccv_nnc_tensor_t* const ty = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
41 | | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty), 0); |
42 | | REQUIRE_TENSOR_EQ(ty, y_tensor, "gelu from cudnn should match from CPU"); |
43 | | ccv_nnc_tensor_free(x_tensor); |
44 | | ccv_nnc_tensor_free(y_tensor); |
45 | | ccv_nnc_tensor_free(ty); |
46 | | ccv_nnc_graph_free(graph); |
47 | | ccv_nnc_tensor_arena_free(tensor_arena); |
48 | | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
49 | | ccv_nnc_symbolic_graph_free(symbolic_graph); |
50 | | } |
51 | | |
52 | | TEST_CASE("gelu in half precision") |
53 | 1 | { |
54 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) || ccv_nnc_cmd_ok(CCV_NNC_GELU_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_GELU_FORWARD(0), TENSOR_SYMBOL_LIST(a), TENSOR_SYMBOL_LIST(b), "gelu"); |
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_GELU_FORWARD(0), 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, "gelu from cudnn 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("gelu gradient in float") |
97 | 1 | { |
98 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) && |
99 | 1 | ccv_nnc_cmd_ok(CCV_NNC_GELU_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_GELU_FORWARD(0), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "gelu"); |
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_GELU_FORWARD(0), 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_GELU_BACKWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, x_tensor, 0), 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("mps gelu gradient in float") |
157 | 1 | { |
158 | 1 | GUARD_ELSE_RETURN((ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_MPS) && |
159 | 1 | ccv_nnc_cmd_ok(CCV_NNC_GELU_BACKWARD, CCV_NNC_BACKEND_MPS))); |
160 | 0 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
161 | 0 | ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 10, 100), "x"); |
162 | 0 | ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 10, 100), "y"); |
163 | 0 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(0), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "gelu"); |
164 | 0 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
165 | 0 | 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 | 0 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
167 | 0 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
168 | 0 | ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
169 | 0 | ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
170 | 0 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
171 | 0 | dsfmt_t dsfmt; |
172 | 0 | dsfmt_init_gen_rand(&dsfmt, 0); |
173 | 0 | int i; |
174 | 0 | for (i = 0; i < 10 * 100; i++) |
175 | 0 | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
176 | 0 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
177 | 0 | for (i = 0; i < 10 * 100; i++) |
178 | 0 | dy_tensor->data.f32[i] = 0; |
179 | 0 | for (i = 0; i < 10; i++) |
180 | 0 | dy_tensor->data.f32[i * 100 + i] = 1; |
181 | 0 | ccv_nnc_tensor_t* const dyt = ccv_nnc_tensor_new(0, GPU_TENSOR_NHWC(000, 32F, 10, 100), 0); |
182 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor), TENSOR_LIST(dyt), 0); |
183 | 0 | ccv_nnc_graph_t* graph = 0; |
184 | 0 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
185 | 0 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
186 | 0 | 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); |
187 | 0 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
188 | 0 | ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
189 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(xt), 0); |
190 | 0 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
191 | 0 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
192 | 0 | ccv_nnc_tensor_t* const dxt = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
193 | 0 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
194 | 0 | ccv_nnc_tensor_t* const yt = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
195 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dxt), TENSOR_LIST(dx_tensor), 0); |
196 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(yt), TENSOR_LIST(y_tensor), 0); |
197 | 0 | ccv_nnc_tensor_t* const ty_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
198 | 0 | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty_tensor), 0); |
199 | 0 | REQUIRE_TENSOR_EQ(ty_tensor, y_tensor, "forward pass should match"); |
200 | 0 | ccv_nnc_tensor_t* const tdx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
201 | 0 | ccv_nnc_cmd_exec(CMD_GELU_BACKWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, x_tensor, 0), TENSOR_LIST(tdx_tensor), 0); |
202 | 0 | REQUIRE_TENSOR_EQ(tdx_tensor, dx_tensor, "backward pass should match"); |
203 | 0 | ccv_nnc_tensor_free(x_tensor); |
204 | 0 | ccv_nnc_tensor_free(y_tensor); |
205 | 0 | ccv_nnc_tensor_free(dx_tensor); |
206 | 0 | ccv_nnc_tensor_free(dy_tensor); |
207 | 0 | ccv_nnc_tensor_free(ty_tensor); |
208 | 0 | ccv_nnc_tensor_free(tdx_tensor); |
209 | 0 | ccv_nnc_tensor_free(dyt); |
210 | 0 | ccv_nnc_graph_free(graph); |
211 | 0 | ccv_nnc_tensor_arena_free(tensor_arena); |
212 | 0 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
213 | 0 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
214 | 0 | } |
215 | | |
216 | | TEST_CASE("gelu gradient in half precision") |
217 | 1 | { |
218 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) && |
219 | 1 | ccv_nnc_cmd_ok(CCV_NNC_GELU_BACKWARD, CCV_NNC_BACKEND_GPU_REF)); |
220 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
221 | 1 | ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "x"); |
222 | 1 | ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "y"); |
223 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(0), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "gelu"); |
224 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
225 | 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)); |
226 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
227 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
228 | 1 | ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
229 | 1 | ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
230 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
231 | 1 | dsfmt_t dsfmt; |
232 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
233 | 1 | int i; |
234 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
235 | 1.00k | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
236 | 1 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
237 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
238 | 1.00k | dy_tensor->data.f32[i] = 0; |
239 | 11 | for (i = 0; i < 10; i++10 ) |
240 | 10 | dy_tensor->data.f32[i * 100 + i] = 1; |
241 | 1 | ccv_nnc_tensor_t* const dy16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
242 | 1 | ccv_nnc_tensor_t* const dyt = ccv_nnc_tensor_new(0, GPU_TENSOR_NHWC(000, 16F, 10, 100), 0); |
243 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor), TENSOR_LIST(dy16_tensor), 0); |
244 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy16_tensor), TENSOR_LIST(dyt), 0); |
245 | 1 | ccv_nnc_graph_t* graph = 0; |
246 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
247 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
248 | 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); |
249 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
250 | 1 | ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
251 | 1 | ccv_nnc_tensor_t* const x16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
252 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(x16_tensor), 0); |
253 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x16_tensor), TENSOR_LIST(xt), 0); |
254 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
255 | 1 | ccv_nnc_tensor_t* const dx16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
256 | 1 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
257 | 1 | ccv_nnc_tensor_t* const dxt = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
258 | 1 | ccv_nnc_tensor_t* const y16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
259 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
260 | 1 | ccv_nnc_tensor_t* const yt = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
261 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dxt), TENSOR_LIST(dx16_tensor), 0); |
262 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dx16_tensor), TENSOR_LIST(dx_tensor), 0); |
263 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(yt), TENSOR_LIST(y16_tensor), 0); |
264 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(y16_tensor), TENSOR_LIST(y_tensor), 0); |
265 | 1 | ccv_nnc_tensor_t* const ty_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
266 | 1 | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty_tensor), 0); |
267 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, ty_tensor->data.f32, y_tensor->data.f32, 10 * 100, 1e-3, "forward pass should match"); |
268 | 1 | ccv_nnc_tensor_t* const tdx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
269 | 1 | ccv_nnc_cmd_exec(CMD_GELU_BACKWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, x_tensor, 0), TENSOR_LIST(tdx_tensor), 0); |
270 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, tdx_tensor->data.f32, dx_tensor->data.f32, 10 * 100, 1e-3, "backward pass should match"); |
271 | 1 | ccv_nnc_tensor_free(x_tensor); |
272 | 1 | ccv_nnc_tensor_free(x16_tensor); |
273 | 1 | ccv_nnc_tensor_free(y_tensor); |
274 | 1 | ccv_nnc_tensor_free(y16_tensor); |
275 | 1 | ccv_nnc_tensor_free(dx_tensor); |
276 | 1 | ccv_nnc_tensor_free(dx16_tensor); |
277 | 1 | ccv_nnc_tensor_free(dy_tensor); |
278 | 1 | ccv_nnc_tensor_free(dy16_tensor); |
279 | 1 | ccv_nnc_tensor_free(ty_tensor); |
280 | 1 | ccv_nnc_tensor_free(tdx_tensor); |
281 | 1 | ccv_nnc_tensor_free(dyt); |
282 | 1 | ccv_nnc_graph_free(graph); |
283 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
284 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
285 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
286 | 1 | } |
287 | | |
288 | | TEST_CASE("mps gelu gradient in half precision") |
289 | 1 | { |
290 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_MPS) && |
291 | 1 | ccv_nnc_cmd_ok(CCV_NNC_GELU_BACKWARD, CCV_NNC_BACKEND_MPS)); |
292 | 0 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
293 | 0 | ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "x"); |
294 | 0 | ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "y"); |
295 | 0 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(0), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "gelu"); |
296 | 0 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
297 | 0 | 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)); |
298 | 0 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
299 | 0 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
300 | 0 | ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
301 | 0 | ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
302 | 0 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
303 | 0 | dsfmt_t dsfmt; |
304 | 0 | dsfmt_init_gen_rand(&dsfmt, 0); |
305 | 0 | int i; |
306 | 0 | for (i = 0; i < 10 * 100; i++) |
307 | 0 | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
308 | 0 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
309 | 0 | for (i = 0; i < 10 * 100; i++) |
310 | 0 | dy_tensor->data.f32[i] = 0; |
311 | 0 | for (i = 0; i < 10; i++) |
312 | 0 | dy_tensor->data.f32[i * 100 + i] = 1; |
313 | 0 | ccv_nnc_tensor_t* const dy16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
314 | 0 | ccv_nnc_tensor_t* const dyt = ccv_nnc_tensor_new(0, GPU_TENSOR_NHWC(000, 16F, 10, 100), 0); |
315 | 0 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor), TENSOR_LIST(dy16_tensor), 0); |
316 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy16_tensor), TENSOR_LIST(dyt), 0); |
317 | 0 | ccv_nnc_graph_t* graph = 0; |
318 | 0 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
319 | 0 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
320 | 0 | 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); |
321 | 0 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
322 | 0 | ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
323 | 0 | ccv_nnc_tensor_t* const x16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
324 | 0 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(x16_tensor), 0); |
325 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x16_tensor), TENSOR_LIST(xt), 0); |
326 | 0 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
327 | 0 | ccv_nnc_tensor_t* const dx16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
328 | 0 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
329 | 0 | ccv_nnc_tensor_t* const dxt = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
330 | 0 | ccv_nnc_tensor_t* const y16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
331 | 0 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
332 | 0 | ccv_nnc_tensor_t* const yt = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
333 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dxt), TENSOR_LIST(dx16_tensor), 0); |
334 | 0 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dx16_tensor), TENSOR_LIST(dx_tensor), 0); |
335 | 0 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(yt), TENSOR_LIST(y16_tensor), 0); |
336 | 0 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(y16_tensor), TENSOR_LIST(y_tensor), 0); |
337 | 0 | ccv_nnc_tensor_t* const ty_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
338 | 0 | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty_tensor), 0); |
339 | 0 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, ty_tensor->data.f32, y_tensor->data.f32, 10 * 100, 1e-3, "forward pass should match"); |
340 | 0 | ccv_nnc_tensor_t* const tdx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
341 | 0 | ccv_nnc_cmd_exec(CMD_GELU_BACKWARD(0), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, x_tensor, 0), TENSOR_LIST(tdx_tensor), 0); |
342 | 0 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, tdx_tensor->data.f32, dx_tensor->data.f32, 10 * 100, 1e-3, "backward pass should match"); |
343 | 0 | ccv_nnc_tensor_free(x_tensor); |
344 | 0 | ccv_nnc_tensor_free(x16_tensor); |
345 | 0 | ccv_nnc_tensor_free(y_tensor); |
346 | 0 | ccv_nnc_tensor_free(y16_tensor); |
347 | 0 | ccv_nnc_tensor_free(dx_tensor); |
348 | 0 | ccv_nnc_tensor_free(dx16_tensor); |
349 | 0 | ccv_nnc_tensor_free(dy_tensor); |
350 | 0 | ccv_nnc_tensor_free(dy16_tensor); |
351 | 0 | ccv_nnc_tensor_free(ty_tensor); |
352 | 0 | ccv_nnc_tensor_free(tdx_tensor); |
353 | 0 | ccv_nnc_tensor_free(dyt); |
354 | 0 | ccv_nnc_graph_free(graph); |
355 | 0 | ccv_nnc_tensor_arena_free(tensor_arena); |
356 | 0 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
357 | 0 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
358 | 0 | } |
359 | | |
360 | | TEST_CASE("fast gelu in float") |
361 | 1 | { |
362 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) || ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_MPS)); |
363 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
364 | 1 | ccv_nnc_tensor_symbol_t a = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 32F, 20, 10), "a"); |
365 | 1 | ccv_nnc_tensor_symbol_t b = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 32F, 20, 10), "b"); |
366 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(1), TENSOR_SYMBOL_LIST(a), TENSOR_SYMBOL_LIST(b), "gelu"); |
367 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
368 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
369 | 1 | ccv_nnc_graph_t* graph = 0; |
370 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
371 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
372 | 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); |
373 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
374 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
375 | 1 | dsfmt_t dsfmt; |
376 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
377 | 1 | int i; |
378 | 201 | for (i = 0; i < 20 * 10; i++200 ) |
379 | 200 | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
380 | 1 | ccv_nnc_tensor_t* const a_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, a); |
381 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(a_tensor), 0); |
382 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
383 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
384 | 1 | ccv_nnc_tensor_t* const b_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, b); |
385 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(b_tensor), TENSOR_LIST(y_tensor), 0); |
386 | 1 | ccv_nnc_tensor_t* const ty = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
387 | 1 | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty), 0); |
388 | 1 | REQUIRE_TENSOR_EQ(ty, y_tensor, "gelu from cudnn should match from CPU"); |
389 | 1 | ccv_nnc_tensor_free(x_tensor); |
390 | 1 | ccv_nnc_tensor_free(y_tensor); |
391 | 1 | ccv_nnc_tensor_free(ty); |
392 | 1 | ccv_nnc_graph_free(graph); |
393 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
394 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
395 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
396 | 1 | } |
397 | | |
398 | | TEST_CASE("fast gelu in half precision") |
399 | 1 | { |
400 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) || ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_MPS)); |
401 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
402 | 1 | ccv_nnc_tensor_symbol_t a = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 16F, 20, 10), "a"); |
403 | 1 | ccv_nnc_tensor_symbol_t b = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NCHW(000, 16F, 20, 10), "b"); |
404 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(1), TENSOR_SYMBOL_LIST(a), TENSOR_SYMBOL_LIST(b), "gelu"); |
405 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
406 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
407 | 1 | ccv_nnc_graph_t* graph = 0; |
408 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
409 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
410 | 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); |
411 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
412 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
413 | 1 | dsfmt_t dsfmt; |
414 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
415 | 1 | int i; |
416 | 201 | for (i = 0; i < 20 * 10; i++200 ) |
417 | 200 | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
418 | 1 | ccv_nnc_tensor_t* const a_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, a); |
419 | 1 | ccv_nnc_tensor_t* const x16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(16F, 20, 10), 0); |
420 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(x16_tensor), 0); |
421 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x16_tensor), TENSOR_LIST(a_tensor), 0); |
422 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
423 | 1 | ccv_nnc_tensor_t* const y16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(16F, 20, 10), 0); |
424 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
425 | 1 | ccv_nnc_tensor_t* const b_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, b); |
426 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(b_tensor), TENSOR_LIST(y16_tensor), 0); |
427 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(y16_tensor), TENSOR_LIST(y_tensor), 0); |
428 | 1 | ccv_nnc_tensor_t* const ty = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 20, 10), 0); |
429 | 1 | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty), 0); |
430 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, ty->data.f32, y_tensor->data.f32, 20 * 10, 1e-3, "gelu from cudnn should match from CPU"); |
431 | 1 | ccv_nnc_tensor_free(x_tensor); |
432 | 1 | ccv_nnc_tensor_free(x16_tensor); |
433 | 1 | ccv_nnc_tensor_free(y16_tensor); |
434 | 1 | ccv_nnc_tensor_free(y_tensor); |
435 | 1 | ccv_nnc_tensor_free(ty); |
436 | 1 | ccv_nnc_graph_free(graph); |
437 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
438 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
439 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
440 | 1 | } |
441 | | |
442 | | TEST_CASE("fast gelu gradient in float") |
443 | 1 | { |
444 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) && |
445 | 1 | ccv_nnc_cmd_ok(CCV_NNC_GELU_BACKWARD, CCV_NNC_BACKEND_GPU_REF)); |
446 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
447 | 1 | ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 10, 100), "x"); |
448 | 1 | ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 10, 100), "y"); |
449 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(1), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "gelu"); |
450 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
451 | 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)); |
452 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
453 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
454 | 1 | ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
455 | 1 | ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
456 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
457 | 1 | dsfmt_t dsfmt; |
458 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
459 | 1 | int i; |
460 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
461 | 1.00k | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
462 | 1 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
463 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
464 | 1.00k | dy_tensor->data.f32[i] = 0; |
465 | 11 | for (i = 0; i < 10; i++10 ) |
466 | 10 | dy_tensor->data.f32[i * 100 + i] = 1; |
467 | 1 | ccv_nnc_tensor_t* const dyt = ccv_nnc_tensor_new(0, GPU_TENSOR_NHWC(000, 32F, 10, 100), 0); |
468 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor), TENSOR_LIST(dyt), 0); |
469 | 1 | ccv_nnc_graph_t* graph = 0; |
470 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
471 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
472 | 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); |
473 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
474 | 1 | ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
475 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(xt), 0); |
476 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
477 | 1 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
478 | 1 | ccv_nnc_tensor_t* const dxt = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
479 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
480 | 1 | ccv_nnc_tensor_t* const yt = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
481 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dxt), TENSOR_LIST(dx_tensor), 0); |
482 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(yt), TENSOR_LIST(y_tensor), 0); |
483 | 1 | ccv_nnc_tensor_t* const ty_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
484 | 1 | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty_tensor), 0); |
485 | 1 | REQUIRE_TENSOR_EQ(ty_tensor, y_tensor, "forward pass should match"); |
486 | 1 | ccv_nnc_tensor_t* const tdx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
487 | 1 | ccv_nnc_cmd_exec(CMD_GELU_BACKWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, x_tensor, 0), TENSOR_LIST(tdx_tensor), 0); |
488 | 1 | REQUIRE_TENSOR_EQ(tdx_tensor, dx_tensor, "backward pass should match"); |
489 | 1 | ccv_nnc_tensor_free(x_tensor); |
490 | 1 | ccv_nnc_tensor_free(y_tensor); |
491 | 1 | ccv_nnc_tensor_free(dx_tensor); |
492 | 1 | ccv_nnc_tensor_free(dy_tensor); |
493 | 1 | ccv_nnc_tensor_free(ty_tensor); |
494 | 1 | ccv_nnc_tensor_free(tdx_tensor); |
495 | 1 | ccv_nnc_tensor_free(dyt); |
496 | 1 | ccv_nnc_graph_free(graph); |
497 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
498 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
499 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
500 | 1 | } |
501 | | |
502 | | TEST_CASE("fast gelu gradient in half precision") |
503 | 1 | { |
504 | 1 | GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_GELU_FORWARD, CCV_NNC_BACKEND_GPU_REF) && |
505 | 1 | ccv_nnc_cmd_ok(CCV_NNC_GELU_BACKWARD, CCV_NNC_BACKEND_GPU_REF)); |
506 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
507 | 1 | ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "x"); |
508 | 1 | ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 10, 100), "y"); |
509 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GELU_FORWARD(1), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(y), "gelu"); |
510 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
511 | 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)); |
512 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
513 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
514 | 1 | ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
515 | 1 | ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
516 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
517 | 1 | dsfmt_t dsfmt; |
518 | 1 | dsfmt_init_gen_rand(&dsfmt, 0); |
519 | 1 | int i; |
520 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
521 | 1.00k | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
522 | 1 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
523 | 1.00k | for (i = 0; i < 10 * 100; i++1.00k ) |
524 | 1.00k | dy_tensor->data.f32[i] = 0; |
525 | 11 | for (i = 0; i < 10; i++10 ) |
526 | 10 | dy_tensor->data.f32[i * 100 + i] = 1; |
527 | 1 | ccv_nnc_tensor_t* const dy16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
528 | 1 | ccv_nnc_tensor_t* const dyt = ccv_nnc_tensor_new(0, GPU_TENSOR_NHWC(000, 16F, 10, 100), 0); |
529 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor), TENSOR_LIST(dy16_tensor), 0); |
530 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dy16_tensor), TENSOR_LIST(dyt), 0); |
531 | 1 | ccv_nnc_graph_t* graph = 0; |
532 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
533 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
534 | 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); |
535 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
536 | 1 | ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
537 | 1 | ccv_nnc_tensor_t* const x16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
538 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(x16_tensor), 0); |
539 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x16_tensor), TENSOR_LIST(xt), 0); |
540 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
541 | 1 | ccv_nnc_tensor_t* const dx16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
542 | 1 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
543 | 1 | ccv_nnc_tensor_t* const dxt = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
544 | 1 | ccv_nnc_tensor_t* const y16_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 10, 100), 0); |
545 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
546 | 1 | ccv_nnc_tensor_t* const yt = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
547 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dxt), TENSOR_LIST(dx16_tensor), 0); |
548 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(dx16_tensor), TENSOR_LIST(dx_tensor), 0); |
549 | 1 | ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(yt), TENSOR_LIST(y16_tensor), 0); |
550 | 1 | ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(y16_tensor), TENSOR_LIST(y_tensor), 0); |
551 | 1 | ccv_nnc_tensor_t* const ty_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
552 | 1 | ccv_nnc_cmd_exec(CMD_GELU_FORWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(ty_tensor), 0); |
553 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, ty_tensor->data.f32, y_tensor->data.f32, 10 * 100, 1e-3, "forward pass should match"); |
554 | 1 | ccv_nnc_tensor_t* const tdx_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 10, 100), 0); |
555 | 1 | ccv_nnc_cmd_exec(CMD_GELU_BACKWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(dy_tensor, x_tensor, 0), TENSOR_LIST(tdx_tensor), 0); |
556 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, tdx_tensor->data.f32, dx_tensor->data.f32, 10 * 100, 1e-3, "backward pass should match"); |
557 | 1 | ccv_nnc_tensor_free(x_tensor); |
558 | 1 | ccv_nnc_tensor_free(x16_tensor); |
559 | 1 | ccv_nnc_tensor_free(y_tensor); |
560 | 1 | ccv_nnc_tensor_free(y16_tensor); |
561 | 1 | ccv_nnc_tensor_free(dx_tensor); |
562 | 1 | ccv_nnc_tensor_free(dx16_tensor); |
563 | 1 | ccv_nnc_tensor_free(dy_tensor); |
564 | 1 | ccv_nnc_tensor_free(dy16_tensor); |
565 | 1 | ccv_nnc_tensor_free(ty_tensor); |
566 | 1 | ccv_nnc_tensor_free(tdx_tensor); |
567 | 1 | ccv_nnc_tensor_free(dyt); |
568 | 1 | ccv_nnc_graph_free(graph); |
569 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
570 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
571 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
572 | 1 | } |
573 | | |
574 | | #include "case_main.h" |