Coverage Report

Created: 2021-04-14 19:30

/home/liu/buildslave/linux-x64-runtests/build/test/int/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
{
16
  GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_SET_FORWARD, CCV_NNC_BACKEND_GPU_CUDNN) &&
17
    ccv_nnc_cmd_ok(CCV_NNC_DATA_TRANSFER_FORWARD, CCV_NNC_BACKEND_GPU_REF) &&
18
    ccv_nnc_cmd_ok(CCV_NNC_GEMM_FORWARD, CCV_NNC_BACKEND_GPU_CUBLAS) &&
19
    ccv_nnc_cmd_ok(CCV_NNC_FORMAT_TRANSFORM_FORWARD, CCV_NNC_BACKEND_GPU_CUDNN));
20
  ccv_cnnp_model_t* const concat = ccv_cnnp_concat(0, "concat");
21
  ccv_cnnp_model_t* const dense = ccv_cnnp_dense(1, 1, "linear");
22
  ccv_cnnp_model_t* const full = ccv_cnnp_sequential_new(MODEL_LIST(concat, dense), "full");
23
  ccv_nnc_tensor_param_t a_params = GPU_TENSOR_NCHW(000, 32F, 1);
24
  ccv_nnc_tensor_param_t b_params = GPU_TENSOR_NCHW(000, 32F, 2);
25
  ccv_cnnp_model_compile(full, TENSOR_PARAM_LIST(a_params, b_params), CMD_NOOP(), CMD_NOOP());
26
  CNNP_MODEL_GEN(full, CCV_NNC_LONG_DOT_GRAPH);
27
  ccv_nnc_tensor_t* const ha = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 1), 0);
28
  ha->data.f32[0] = -0.5;
29
  ccv_nnc_tensor_t* const hb = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 2), 0);
30
  hb->data.f32[0] = 0.3;
31
  hb->data.f32[1] = 2;
32
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, a_params, 0);
33
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, b_params, 0);
34
  ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(ha, hb), TENSOR_LIST(a, b), 0);
35
  ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, GPU_TENSOR_NCHW(000, 32F, 1), 0);
36
  ccv_nnc_tensor_t* const hc = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 1), 0);
37
  ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0);
38
  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);
39
  ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0);
40
  ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(c), TENSOR_LIST(hc), 0);
41
  REQUIRE_EQ_WITH_TOLERANCE(hc->data.f32[0], -0.5 + 0.3 + 2, 1e-5, "should be equal");
42
  ccv_nnc_tensor_free(a);
43
  ccv_nnc_tensor_free(b);
44
  ccv_nnc_tensor_free(c);
45
  ccv_nnc_tensor_free(ha);
46
  ccv_nnc_tensor_free(hb);
47
  ccv_nnc_tensor_free(hc);
48
  ccv_cnnp_model_free(full);
49
}
50
51
TEST_CASE("concatenate several tensors together and make sure they are simplified away")
52
1
{
53
1
  GUARD_ELSE_RETURN(ccv_nnc_cmd_ok(CCV_NNC_SET_FORWARD, CCV_NNC_BACKEND_GPU_CUDNN) &&
54
1
    ccv_nnc_cmd_ok(CCV_NNC_DATA_TRANSFER_FORWARD, CCV_NNC_BACKEND_GPU_REF) &&
55
1
    ccv_nnc_cmd_ok(CCV_NNC_GEMM_FORWARD, CCV_NNC_BACKEND_GPU_CUBLAS) &&
56
1
    ccv_nnc_cmd_ok(CCV_NNC_FORMAT_TRANSFORM_FORWARD, CCV_NNC_BACKEND_GPU_CUDNN));
57
1
  ccv_cnnp_model_t* const x_dense = ccv_cnnp_dense(1, 1, "linear");
58
1
  ccv_cnnp_model_t* const y_dense = ccv_cnnp_dense(2, 1, "linear");
59
1
  ccv_cnnp_model_t* const concat = ccv_cnnp_concat(0, "concat");
60
1
  ccv_cnnp_model_t* const dense = ccv_cnnp_dense(1, 1, "linear");
61
1
  ccv_cnnp_model_io_t const x = ccv_cnnp_input();
62
1
  ccv_cnnp_model_io_t const y = ccv_cnnp_input();
63
1
  ccv_cnnp_model_io_t xz = ccv_cnnp_model_apply(x_dense, MODEL_IO_LIST(x));
64
1
  ccv_cnnp_model_io_t yz = ccv_cnnp_model_apply(y_dense, MODEL_IO_LIST(y));
65
1
  ccv_cnnp_model_io_t z = ccv_cnnp_model_apply(concat, MODEL_IO_LIST(xz, yz));
66
1
  z = ccv_cnnp_model_apply(dense, MODEL_IO_LIST(z));
67
1
  ccv_cnnp_model_t* const full = ccv_cnnp_model_new(MODEL_IO_LIST(x, y), MODEL_IO_LIST(z), "full");
68
1
  ccv_nnc_tensor_param_t a_params = GPU_TENSOR_NCHW(000, 32F, 1);
69
1
  ccv_nnc_tensor_param_t b_params = GPU_TENSOR_NCHW(000, 32F, 2);
70
1
  ccv_cnnp_model_compile(full, TENSOR_PARAM_LIST(a_params, b_params), CMD_NOOP(), CMD_NOOP());
71
1
  CNNP_MODEL_GEN(full, CCV_NNC_LONG_DOT_GRAPH);
72
1
  ccv_nnc_tensor_t* const ha = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 1), 0);
73
1
  ha->data.f32[0] = -0.5;
74
1
  ccv_nnc_tensor_t* const hb = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 2), 0);
75
1
  hb->data.f32[0] = 0.3;
76
1
  hb->data.f32[1] = 2;
77
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, a_params, 0);
78
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, b_params, 0);
79
1
  ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(ha, hb), TENSOR_LIST(a, b), 0);
80
1
  ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, GPU_TENSOR_NCHW(000, 32F, 1), 0);
81
1
  ccv_nnc_tensor_t* const hc = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 1), 0);
82
1
  ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0);
83
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);
84
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);
85
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);
86
1
  ccv_cnnp_model_evaluate(full, (ccv_cnnp_evaluate_param_t){}, TENSOR_LIST(a, b), TENSOR_LIST(c), 0, 0);
87
1
  ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(c), TENSOR_LIST(hc), 0);
88
1
  REQUIRE_EQ_WITH_TOLERANCE(hc->data.f32[0], -0.5 * 0.5 + (0.3 + 2) * -0.5 * 2, 1e-5, "should be equal");
89
1
  ccv_nnc_tensor_free(a);
90
1
  ccv_nnc_tensor_free(b);
91
1
  ccv_nnc_tensor_free(c);
92
1
  ccv_nnc_tensor_free(ha);
93
1
  ccv_nnc_tensor_free(hb);
94
1
  ccv_nnc_tensor_free(hc);
95
1
  ccv_cnnp_model_free(full);
96
1
}
97
98
#include "case_main.h"