/home/liu/actions-runner/_work/ccv/ccv/test/unit/nnc/swish.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("compare swish gradient with fine-grained symbolic graph") |
15 | 1 | { |
16 | 1 | ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new(); |
17 | 1 | const ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 10), "x"); |
18 | 1 | const ccv_nnc_tensor_symbol_t sigmoid = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "sigmoid"); |
19 | 1 | const ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "y"); |
20 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_SIGMOID_FORWARD(), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(sigmoid), "sigmoid"); |
21 | 1 | ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_MUL_FORWARD(1), TENSOR_SYMBOL_LIST(x, sigmoid), TENSOR_SYMBOL_LIST(y), "y"); |
22 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
23 | 1 | SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
24 | 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)); |
25 | 1 | ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
26 | 1 | const ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x); |
27 | 1 | const ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y); |
28 | 1 | ccv_nnc_graph_t* graph = 0; |
29 | 1 | ccv_nnc_tensor_arena_t* tensor_arena = 0; |
30 | 1 | ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0; |
31 | 1 | ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, |
32 | 1 | 0, 0, TENSOR_SYMBOL_LIST(y, dx), |
33 | 1 | SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), |
34 | 1 | &graph, &tensor_arena, &graph_exec_arena); |
35 | 1 | GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH); |
36 | 1 | ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x); |
37 | 1 | dsfmt_t dsfmt; |
38 | 1 | int i; |
39 | 1 | dsfmt_init_gen_rand(&dsfmt, 1); |
40 | 11 | for (i = 0; i < 10; i++10 ) |
41 | 10 | x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt); |
42 | 1 | ccv_nnc_symbolic_graph_t* const swish_symbolic_graph = ccv_nnc_symbolic_graph_new(); |
43 | 1 | ccv_nnc_tensor_symbol_t sx = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, CPU_TENSOR_NHWC(32F, 10), "x"); |
44 | 1 | ccv_nnc_tensor_symbol_t sy = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, ccv_nnc_tensor_auto, "y"); |
45 | 1 | ccv_nnc_graph_exec_symbol_new(swish_symbolic_graph, CMD_SWISH_FORWARD(), TENSOR_SYMBOL_LIST(sx), TENSOR_SYMBOL_LIST(sy), "swish"); |
46 | 1 | ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
47 | 1 | SYMBOLIC_GRAPH_GEN(swish_symbolic_graph, CCV_NNC_LONG_DOT_GRAPH); |
48 | 1 | ccv_nnc_symbolic_graph_backward(swish_symbolic_graph, TENSOR_SYMBOL_LIST(sy), TENSOR_SYMBOL_LIST(sx), SYMBOLIC_GRAPH_SOURCES(swish_symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(swish_symbolic_graph)); |
49 | 1 | ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS); |
50 | 1 | const ccv_nnc_tensor_symbol_t sdx = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sx); |
51 | 1 | const ccv_nnc_tensor_symbol_t sdy = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sy); |
52 | 1 | ccv_nnc_graph_t* swish_graph = 0; |
53 | 1 | ccv_nnc_tensor_arena_t* swish_tensor_arena = 0; |
54 | 1 | ccv_nnc_graph_exec_arena_t* swish_graph_exec_arena = 0; |
55 | 1 | ccv_nnc_symbolic_graph_compile(swish_symbolic_graph, ccv_nnc_default_compile_params, |
56 | 1 | 0, 0, TENSOR_SYMBOL_LIST(sy, sdx), |
57 | 1 | SYMBOLIC_GRAPH_SOURCES(swish_symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(swish_symbolic_graph), |
58 | 1 | &swish_graph, &swish_tensor_arena, &swish_graph_exec_arena); |
59 | 1 | ccv_nnc_tensor_t* const sx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sx); |
60 | 1 | memcpy(sx_tensor->data.f32, x_tensor->data.f32, sizeof(float) * 10); |
61 | 1 | ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dy); |
62 | 1 | ccv_nnc_tensor_t* const sdy_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdy); |
63 | 11 | for (i = 0; i < 10; i++10 ) |
64 | 10 | sdy_tensor->data.f32[i] = dy_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt) * 2 - 1; |
65 | 1 | ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0); |
66 | 1 | ccv_nnc_graph_run(swish_graph, 0, TRAVERSE_FULL, 0, 0); |
67 | 1 | ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, y); |
68 | 1 | ccv_nnc_tensor_t* const sy_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sy); |
69 | 1 | REQUIRE_TENSOR_EQ(y_tensor, sy_tensor, "graph computed result should match swish op result"); |
70 | 1 | ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dx); |
71 | 1 | ccv_nnc_tensor_t* const sdx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdx); |
72 | 1 | REQUIRE_TENSOR_EQ(dx_tensor, sdx_tensor, "gradient computed result should match swish op result"); |
73 | 1 | ccv_nnc_symbolic_graph_free(symbolic_graph); |
74 | 1 | ccv_nnc_tensor_arena_free(tensor_arena); |
75 | 1 | ccv_nnc_graph_exec_arena_free(graph_exec_arena); |
76 | 1 | ccv_nnc_graph_free(graph); |
77 | 1 | ccv_nnc_symbolic_graph_free(swish_symbolic_graph); |
78 | 1 | ccv_nnc_tensor_arena_free(swish_tensor_arena); |
79 | 1 | ccv_nnc_graph_exec_arena_free(swish_graph_exec_arena); |
80 | 1 | ccv_nnc_graph_free(swish_graph); |
81 | 1 | } |
82 | | |
83 | | #include "case_main.h" |