/home/liu/actions-runner/_work/ccv/ccv/test/unit/nnc/concat.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("concatenate several tensors together") |
15 | 1 | { |
16 | 1 | ccv_cnnp_model_t* const concat = ccv_cnnp_concat(0, "concat"); |
17 | 1 | ccv_cnnp_model_t* const dense = ccv_cnnp_dense(1, 1, 0, 1, "linear"); |
18 | 1 | ccv_cnnp_model_t* const full = ccv_cnnp_sequential_new(MODEL_LIST(concat, dense), 1, "full"); |
19 | 1 | ccv_nnc_tensor_param_t a_params = CPU_TENSOR_NCHW(32F, 1); |
20 | 1 | ccv_nnc_tensor_param_t b_params = CPU_TENSOR_NCHW(32F, 2); |
21 | 1 | ccv_cnnp_model_compile(full, TENSOR_PARAM_LIST(a_params, b_params), CMD_NOOP(), CMD_NOOP()); |
22 | 1 | CNNP_MODEL_GEN(full, CCV_NNC_LONG_DOT_GRAPH); |
23 | 1 | ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, a_params, 0); |
24 | 1 | a->data.f32[0] = -0.5; |
25 | 1 | ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, b_params, 0); |
26 | 1 | b->data.f32[0] = 0.3; |
27 | 1 | b->data.f32[1] = 2; |
28 | 1 | ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 1), 0); |
29 | 1 | ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0); |
30 | 1 | ccv_cnnp_model_parameters_map(full, ccv_cnnp_model_parameters(full, ALL_PARAMETERS, ALL_PARAMETERS), CMD_SET_FORWARD(1), ccv_nnc_no_hint, 0, 0, 0, 0, 0, 0); |
31 | 1 | ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0); |
32 | 1 | REQUIRE_EQ_WITH_TOLERANCE(c->data.f32[0], -0.5 + 0.3 + 2, 1e-5, "should be equal"); |
33 | 1 | ccv_nnc_tensor_free(a); |
34 | 1 | ccv_nnc_tensor_free(b); |
35 | 1 | ccv_nnc_tensor_free(c); |
36 | 1 | ccv_cnnp_model_free(full); |
37 | 1 | } |
38 | | |
39 | | TEST_CASE("concatenate several tensors together and make sure they are simplified away") |
40 | 1 | { |
41 | 1 | ccv_cnnp_model_t* const x_dense = ccv_cnnp_dense(1, 1, 0, 1, "linear"); |
42 | 1 | ccv_cnnp_model_t* const y_dense = ccv_cnnp_dense(2, 1, 0, 1, "linear"); |
43 | 1 | ccv_cnnp_model_t* const concat = ccv_cnnp_concat(0, "concat"); |
44 | 1 | ccv_cnnp_model_t* const dense = ccv_cnnp_dense(1, 1, 0, 1, "linear"); |
45 | 1 | ccv_cnnp_model_io_t const x = ccv_cnnp_input(); |
46 | 1 | ccv_cnnp_model_io_t const y = ccv_cnnp_input(); |
47 | 1 | ccv_cnnp_model_io_t xz = ccv_cnnp_model_apply(x_dense, MODEL_IO_LIST(x)); |
48 | 1 | ccv_cnnp_model_io_t yz = ccv_cnnp_model_apply(y_dense, MODEL_IO_LIST(y)); |
49 | 1 | ccv_cnnp_model_io_t z = ccv_cnnp_model_apply(concat, MODEL_IO_LIST(xz, yz)); |
50 | 1 | z = ccv_cnnp_model_apply(dense, MODEL_IO_LIST(z)); |
51 | 1 | ccv_cnnp_model_t* const full = ccv_cnnp_model_new(MODEL_IO_LIST(x, y), MODEL_IO_LIST(z), 1, "full"); |
52 | 1 | ccv_nnc_tensor_param_t a_params = CPU_TENSOR_NCHW(32F, 1); |
53 | 1 | ccv_nnc_tensor_param_t b_params = CPU_TENSOR_NCHW(32F, 2); |
54 | 1 | ccv_cnnp_model_compile(full, TENSOR_PARAM_LIST(a_params, b_params), CMD_NOOP(), CMD_NOOP()); |
55 | 1 | CNNP_MODEL_GEN(full, CCV_NNC_LONG_DOT_GRAPH); |
56 | 1 | ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, a_params, 0); |
57 | 1 | a->data.f32[0] = -0.5; |
58 | 1 | ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, b_params, 0); |
59 | 1 | b->data.f32[0] = 0.3; |
60 | 1 | b->data.f32[1] = 2; |
61 | 1 | ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 1), 0); |
62 | 1 | ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0); |
63 | 1 | ccv_cnnp_model_parameters_map(full, ccv_cnnp_model_parameters(x_dense, ALL_PARAMETERS, ALL_PARAMETERS), CMD_SET_FORWARD(0.5), ccv_nnc_no_hint, 0, 0, 0, 0, 0, 0); |
64 | 1 | ccv_cnnp_model_parameters_map(full, ccv_cnnp_model_parameters(y_dense, ALL_PARAMETERS, ALL_PARAMETERS), CMD_SET_FORWARD(-0.5), ccv_nnc_no_hint, 0, 0, 0, 0, 0, 0); |
65 | 1 | ccv_cnnp_model_parameters_map(full, ccv_cnnp_model_parameters(dense, ALL_PARAMETERS, ALL_PARAMETERS), CMD_SET_FORWARD(1), ccv_nnc_no_hint, 0, 0, 0, 0, 0, 0); |
66 | 1 | ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0); |
67 | 1 | REQUIRE_EQ_WITH_TOLERANCE(c->data.f32[0], -0.5 * 0.5 + (0.3 + 2) * -0.5 * 2, 1e-5, "should be equal"); |
68 | 1 | ccv_nnc_tensor_free(a); |
69 | 1 | ccv_nnc_tensor_free(b); |
70 | 1 | ccv_nnc_tensor_free(c); |
71 | 1 | ccv_cnnp_model_free(full); |
72 | 1 | } |
73 | | |
74 | | #include "case_main.h" |