## Coverage Report

#### Created: 2026-05-04 15:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /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
    {
    16
      ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new();
    17
      const ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 10), "x");
    18
      const ccv_nnc_tensor_symbol_t sigmoid = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "sigmoid");
    19
      const ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "y");
    20
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_SIGMOID_FORWARD(), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(sigmoid), "sigmoid");
    21
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_MUL_FORWARD(1), TENSOR_SYMBOL_LIST(x, sigmoid), TENSOR_SYMBOL_LIST(y), "y");
    22
      ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    23
      SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
    24
      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
      ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    26
      const ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x);
    27
      const ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y);
    28
      ccv_nnc_graph_t* graph = 0;
    29
      ccv_nnc_tensor_arena_t* tensor_arena = 0;
    30
      ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
    31
      ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params,
    32
        0, 0, TENSOR_SYMBOL_LIST(y, dx),
    33
        SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph),
    34
        &graph, &tensor_arena, &graph_exec_arena);
    35
      GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
    36
      ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x);
    37
      dsfmt_t dsfmt;
    38
      int i;
    39
      dsfmt_init_gen_rand(&dsfmt, 1);
    40
      for (i = 0; i < 10; i++)
    41
        x_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt);
    42
      ccv_nnc_symbolic_graph_t* const swish_symbolic_graph = ccv_nnc_symbolic_graph_new();
    43
      ccv_nnc_tensor_symbol_t sx = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, CPU_TENSOR_NHWC(32F, 10), "x");
    44
      ccv_nnc_tensor_symbol_t sy = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, ccv_nnc_tensor_auto, "y");
    45
      ccv_nnc_graph_exec_symbol_new(swish_symbolic_graph, CMD_SWISH_FORWARD(1), TENSOR_SYMBOL_LIST(sx), TENSOR_SYMBOL_LIST(sy), "swish");
    46
      ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    47
      SYMBOLIC_GRAPH_GEN(swish_symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
    48
      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
      ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    50
      const ccv_nnc_tensor_symbol_t sdx = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sx);
    51
      const ccv_nnc_tensor_symbol_t sdy = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sy);
    52
      ccv_nnc_graph_t* swish_graph = 0;
    53
      ccv_nnc_tensor_arena_t* swish_tensor_arena = 0;
    54
      ccv_nnc_graph_exec_arena_t* swish_graph_exec_arena = 0;
    55
      ccv_nnc_symbolic_graph_compile(swish_symbolic_graph, ccv_nnc_default_compile_params,
    56
        0, 0, TENSOR_SYMBOL_LIST(sy, sdx),
    57
        SYMBOLIC_GRAPH_SOURCES(swish_symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(swish_symbolic_graph),
    58
        &swish_graph, &swish_tensor_arena, &swish_graph_exec_arena);
    59
      ccv_nnc_tensor_t* const sx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sx);
    60
      memcpy(sx_tensor->data.f32, x_tensor->data.f32, sizeof(float) * 10);
    61
      ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dy);
    62
      ccv_nnc_tensor_t* const sdy_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdy);
    63
      for (i = 0; i < 10; i++)
    64
        sdy_tensor->data.f32[i] = dy_tensor->data.f32[i] = dsfmt_genrand_open_close(&dsfmt) * 2 - 1;
    65
      ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0);
    66
      ccv_nnc_graph_run(swish_graph, 0, TRAVERSE_FULL, 0, 0);
    67
      ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, y);
    68
      ccv_nnc_tensor_t* const sy_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sy);
    69
      REQUIRE_TENSOR_EQ(y_tensor, sy_tensor, "graph computed result should match swish op result");
    70
      ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dx);
    71
      ccv_nnc_tensor_t* const sdx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdx);
    72
      REQUIRE_TENSOR_EQ(dx_tensor, sdx_tensor, "gradient computed result should match swish op result");
    73
      ccv_nnc_symbolic_graph_free(symbolic_graph);
    74
      ccv_nnc_tensor_arena_free(tensor_arena);
    75
      ccv_nnc_graph_exec_arena_free(graph_exec_arena);
    76
      ccv_nnc_graph_free(graph);
    77
      ccv_nnc_symbolic_graph_free(swish_symbolic_graph);
    78
      ccv_nnc_tensor_arena_free(swish_tensor_arena);
    79
      ccv_nnc_graph_exec_arena_free(swish_graph_exec_arena);
    80
      ccv_nnc_graph_free(swish_graph);
    81
    }
    82
    83
    TEST_CASE("swish with non-one beta")
    84
    1
    {
    85
    1
      const float beta = 1.75;
    86
    1
      ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new();
    87
    1
      const ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 32), "x");
    88
    1
      const ccv_nnc_tensor_symbol_t bx = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "bx");
    89
    1
      const ccv_nnc_tensor_symbol_t sigmoid = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "sigmoid");
    90
    1
      const ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "y");
    91
    1
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_SCALAR_MUL_FORWARD(beta), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(bx), "beta_x");
    92
    1
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_SIGMOID_FORWARD(), TENSOR_SYMBOL_LIST(bx), TENSOR_SYMBOL_LIST(sigmoid), "sigmoid");
    93
    1
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_MUL_FORWARD(1), TENSOR_SYMBOL_LIST(x, sigmoid), TENSOR_SYMBOL_LIST(y), "y");
    94
    1
      ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    95
    1
      SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
    96
    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));
    97
    1
      ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    98
    1
      const ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x);
    99
    1
      const ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y);
    100
    1
      ccv_nnc_graph_t* graph = 0;
    101
    1
      ccv_nnc_tensor_arena_t* tensor_arena = 0;
    102
    1
      ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
    103
    1
      ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params,
    104
    1
        0, 0, TENSOR_SYMBOL_LIST(y, dx),
    105
    1
        SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph),
    106
    1
        &graph, &tensor_arena, &graph_exec_arena);
    107
    1
      ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x);
    108
    1
      dsfmt_t dsfmt;
    109
    1
      int i;
    110
    1
      dsfmt_init_gen_rand(&dsfmt, 42);
    111
    33
      for (i = 0; i < 32; i++32)
    112
    32
        x_tensor->data.f32[i] = (float)(dsfmt_genrand_open_close(&dsfmt) * 4 - 2);
    113
    1
      ccv_nnc_symbolic_graph_t* const swish_symbolic_graph = ccv_nnc_symbolic_graph_new();
    114
    1
      ccv_nnc_tensor_symbol_t sx = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, CPU_TENSOR_NHWC(32F, 32), "x");
    115
    1
      ccv_nnc_tensor_symbol_t sy = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, ccv_nnc_tensor_auto, "y");
    116
    1
      ccv_nnc_graph_exec_symbol_new(swish_symbolic_graph, CMD_SWISH_FORWARD(beta), TENSOR_SYMBOL_LIST(sx), TENSOR_SYMBOL_LIST(sy), "swish");
    117
    1
      ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    118
    1
      SYMBOLIC_GRAPH_GEN(swish_symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
    119
    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));
    120
    1
      ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    121
    1
      const ccv_nnc_tensor_symbol_t sdx = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sx);
    122
    1
      const ccv_nnc_tensor_symbol_t sdy = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sy);
    123
    1
      ccv_nnc_graph_t* swish_graph = 0;
    124
    1
      ccv_nnc_tensor_arena_t* swish_tensor_arena = 0;
    125
    1
      ccv_nnc_graph_exec_arena_t* swish_graph_exec_arena = 0;
    126
    1
      ccv_nnc_symbolic_graph_compile(swish_symbolic_graph, ccv_nnc_default_compile_params,
    127
    1
        0, 0, TENSOR_SYMBOL_LIST(sy, sdx),
    128
    1
        SYMBOLIC_GRAPH_SOURCES(swish_symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(swish_symbolic_graph),
    129
    1
        &swish_graph, &swish_tensor_arena, &swish_graph_exec_arena);
    130
    1
      ccv_nnc_tensor_t* const sx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sx);
    131
    1
      memcpy(sx_tensor->data.f32, x_tensor->data.f32, sizeof(float) * 32);
    132
    1
      ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dy);
    133
    1
      ccv_nnc_tensor_t* const sdy_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdy);
    134
    33
      for (i = 0; i < 32; i++32)
    135
    32
        sdy_tensor->data.f32[i] = dy_tensor->data.f32[i] = (float)(dsfmt_genrand_open_close(&dsfmt) * 2 - 1);
    136
    1
      ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0);
    137
    1
      ccv_nnc_graph_run(swish_graph, 0, TRAVERSE_FULL, 0, 0);
    138
    1
      ccv_nnc_tensor_t* const y_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, y);
    139
    1
      ccv_nnc_tensor_t* const sy_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sy);
    140
    1
      REQUIRE_TENSOR_EQ(y_tensor, sy_tensor, "graph computed result should match swish op result");
    141
    1
      ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dx);
    142
    1
      ccv_nnc_tensor_t* const sdx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdx);
    143
    1
      REQUIRE_TENSOR_EQ(dx_tensor, sdx_tensor, "gradient computed result should match swish op result");
    144
    1
      ccv_nnc_symbolic_graph_free(symbolic_graph);
    145
    1
      ccv_nnc_tensor_arena_free(tensor_arena);
    146
    1
      ccv_nnc_graph_exec_arena_free(graph_exec_arena);
    147
    1
      ccv_nnc_graph_free(graph);
    148
    1
      ccv_nnc_symbolic_graph_free(swish_symbolic_graph);
    149
    1
      ccv_nnc_tensor_arena_free(swish_tensor_arena);
    150
    1
      ccv_nnc_graph_exec_arena_free(swish_graph_exec_arena);
    151
    1
      ccv_nnc_graph_free(swish_graph);
    152
    1
    }
    153
    154
    TEST_CASE("swish gradient with non-one beta")
    155
    1
    {
    156
    1
      const float beta = 0.5;
    157
    1
      ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new();
    158
    1
      const ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 64), "x");
    159
    1
      const ccv_nnc_tensor_symbol_t bx = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "bx");
    160
    1
      const ccv_nnc_tensor_symbol_t sigmoid = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "sigmoid");
    161
    1
      const ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "y");
    162
    1
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_SCALAR_MUL_FORWARD(beta), TENSOR_SYMBOL_LIST(x), TENSOR_SYMBOL_LIST(bx), "beta_x");
    163
    1
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_SIGMOID_FORWARD(), TENSOR_SYMBOL_LIST(bx), TENSOR_SYMBOL_LIST(sigmoid), "sigmoid");
    164
    1
      ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_MUL_FORWARD(1), TENSOR_SYMBOL_LIST(x, sigmoid), TENSOR_SYMBOL_LIST(y), "y");
    165
    1
      ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    166
    1
      SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
    167
    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));
    168
    1
      ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    169
    1
      const ccv_nnc_tensor_symbol_t dx = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, x);
    170
    1
      const ccv_nnc_tensor_symbol_t dy = ccv_nnc_tensor_symbol_for_backward(symbolic_graph, y);
    171
    1
      ccv_nnc_graph_t* graph = 0;
    172
    1
      ccv_nnc_tensor_arena_t* tensor_arena = 0;
    173
    1
      ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
    174
    1
      ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params,
    175
    1
        0, 0, TENSOR_SYMBOL_LIST(dx),
    176
    1
        SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph),
    177
    1
        &graph, &tensor_arena, &graph_exec_arena);
    178
    1
      ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x);
    179
    1
      dsfmt_t dsfmt;
    180
    1
      int i;
    181
    1
      dsfmt_init_gen_rand(&dsfmt, 97);
    182
    65
      for (i = 0; i < 64; i++64)
    183
    64
        x_tensor->data.f32[i] = (float)(dsfmt_genrand_open_close(&dsfmt) * 6 - 3);
    184
    1
      ccv_nnc_symbolic_graph_t* const swish_symbolic_graph = ccv_nnc_symbolic_graph_new();
    185
    1
      ccv_nnc_tensor_symbol_t sx = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, CPU_TENSOR_NHWC(32F, 64), "x");
    186
    1
      ccv_nnc_tensor_symbol_t sy = ccv_nnc_tensor_symbol_new(swish_symbolic_graph, ccv_nnc_tensor_auto, "y");
    187
    1
      ccv_nnc_graph_exec_symbol_new(swish_symbolic_graph, CMD_SWISH_FORWARD(beta), TENSOR_SYMBOL_LIST(sx), TENSOR_SYMBOL_LIST(sy), "swish");
    188
    1
      ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    189
    1
      SYMBOLIC_GRAPH_GEN(swish_symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
    190
    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));
    191
    1
      ccv_nnc_graph_exec_symbol_autogen(swish_symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
    192
    1
      const ccv_nnc_tensor_symbol_t sdx = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sx);
    193
    1
      const ccv_nnc_tensor_symbol_t sdy = ccv_nnc_tensor_symbol_for_backward(swish_symbolic_graph, sy);
    194
    1
      ccv_nnc_graph_t* swish_graph = 0;
    195
    1
      ccv_nnc_tensor_arena_t* swish_tensor_arena = 0;
    196
    1
      ccv_nnc_graph_exec_arena_t* swish_graph_exec_arena = 0;
    197
    1
      ccv_nnc_symbolic_graph_compile(swish_symbolic_graph, ccv_nnc_default_compile_params,
    198
    1
        0, 0, TENSOR_SYMBOL_LIST(sdx),
    199
    1
        SYMBOLIC_GRAPH_SOURCES(swish_symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(swish_symbolic_graph),
    200
    1
        &swish_graph, &swish_tensor_arena, &swish_graph_exec_arena);
    201
    1
      ccv_nnc_tensor_t* const sx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sx);
    202
    1
      memcpy(sx_tensor->data.f32, x_tensor->data.f32, sizeof(float) * 64);
    203
    1
      ccv_nnc_tensor_t* const dy_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dy);
    204
    1
      ccv_nnc_tensor_t* const sdy_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdy);
    205
    65
      for (i = 0; i < 64; i++64)
    206
    64
        sdy_tensor->data.f32[i] = dy_tensor->data.f32[i] = (float)(dsfmt_genrand_open_close(&dsfmt) * 2 - 1);
    207
    1
      ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0);
    208
    1
      ccv_nnc_graph_run(swish_graph, 0, TRAVERSE_FULL, 0, 0);
    209
    1
      ccv_nnc_tensor_t* const dx_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, dx);
    210
    1
      ccv_nnc_tensor_t* const sdx_tensor = ccv_nnc_tensor_from_symbol(swish_tensor_arena, sdx);
    211
    1
      REQUIRE_TENSOR_EQ(dx_tensor, sdx_tensor, "gradient computed result should match swish op result");
    212
    1
      ccv_nnc_symbolic_graph_free(symbolic_graph);
    213
    1
      ccv_nnc_tensor_arena_free(tensor_arena);
    214
    1
      ccv_nnc_graph_exec_arena_free(graph_exec_arena);
    215
    1
      ccv_nnc_graph_free(graph);
    216
    1
      ccv_nnc_symbolic_graph_free(swish_symbolic_graph);
    217
    1
      ccv_nnc_tensor_arena_free(swish_tensor_arena);
    218
    1
      ccv_nnc_graph_exec_arena_free(swish_graph_exec_arena);
    219
    1
      ccv_nnc_graph_free(swish_graph);
    220
    1
    }
    221
    222
    #include "case_main.h"