Coverage Report

Created: 2021-04-12 03:25

/home/liu/buildslave/linux-x64-runtests/build/test/unit/nnc/index.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
8
TEST_SETUP()
9
{
10
  ccv_nnc_init();
11
}
12
13
TEST_CASE("index select a tensor")
14
1
{
15
1
  float ap[] = {
16
1
    1, 2,
17
1
    2, 3,
18
1
    3, 4,
19
1
  };
20
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(ap, CPU_TENSOR_NHWC(32F, 3, 2), 0);
21
1
  int ip[] = {1, 1};
22
1
  ccv_nnc_tensor_t* const indices = ccv_nnc_tensor_new(ip, CPU_TENSOR_NHWC(32S, 2), 0);
23
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2, 2), 0);
24
1
  ccv_nnc_cmd_exec(CMD_INDEX_SELECT_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(a, indices), TENSOR_LIST(b), 0);
25
1
  float btp[] = {
26
1
    2, 3,
27
1
    2, 3,
28
1
  };
29
1
  ccv_nnc_tensor_t const bt = ccv_nnc_tensor(btp, CPU_TENSOR_NHWC(32F, 2, 2), 0);
30
1
  REQUIRE_TENSOR_EQ(b, &bt, "should be equal");
31
1
  ccv_nnc_tensor_free(a);
32
1
  ccv_nnc_tensor_free(indices);
33
1
  ccv_nnc_tensor_free(b);
34
1
}
35
36
TEST_CASE("index select a 1d tensor")
37
1
{
38
1
  float ap[] = {
39
1
    1, 2, 3, 4, 5
40
1
  };
41
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(ap, CPU_TENSOR_NHWC(32F, 5), 0);
42
1
  int ip[] = {3, 2, 4};
43
1
  ccv_nnc_tensor_t* const indices = ccv_nnc_tensor_new(ip, CPU_TENSOR_NHWC(32S, 3), 0);
44
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 3), 0);
45
1
  ccv_nnc_cmd_exec(CMD_INDEX_SELECT_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(a, indices), TENSOR_LIST(b), 0);
46
1
  float btp[] = {
47
1
    4, 3, 5
48
1
  };
49
1
  ccv_nnc_tensor_t const bt = ccv_nnc_tensor(btp, CPU_TENSOR_NHWC(32F, 3), 0);
50
1
  REQUIRE_TENSOR_EQ(b, &bt, "should be equal");
51
1
  ccv_nnc_tensor_free(a);
52
1
  ccv_nnc_tensor_free(indices);
53
1
  ccv_nnc_tensor_free(b);
54
1
}
55
56
TEST_CASE("index select a tensor view")
57
1
{
58
1
  float ap[] = {
59
1
    1, 2, 3, 4,
60
1
    2, 3, 4, 5,
61
1
    3, 4, 5, 6,
62
1
  };
63
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(ap, CPU_TENSOR_NHWC(32F, 3, 4), 0);
64
1
  ccv_nnc_tensor_view_t* const av = ccv_nnc_tensor_view_new(a, CPU_TENSOR_NHWC(32F, 3, 2), DIM_ALLOC(0, 1), DIM_ALLOC(3, 4));
65
1
  int ip[] = {1, 1};
66
1
  ccv_nnc_tensor_t* const indices = ccv_nnc_tensor_new(ip, CPU_TENSOR_NHWC(32S, 2), 0);
67
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2, 4), 0);
68
1
  memset(b->data.f32, 0, 2 * 4 * sizeof(float));
69
1
  ccv_nnc_tensor_view_t* const bv = ccv_nnc_tensor_view_new(b, CPU_TENSOR_NHWC(32F, 2, 2), DIM_ALLOC(0, 1), DIM_ALLOC(2, 4));
70
1
  ccv_nnc_cmd_exec(CMD_INDEX_SELECT_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST((ccv_nnc_tensor_t*)av, indices), TENSOR_LIST((ccv_nnc_tensor_t*)bv), 0);
71
1
  float btp[] = {
72
1
    0, 3, 4, 0,
73
1
    0, 3, 4, 0,
74
1
  };
75
1
  ccv_nnc_tensor_t const bt = ccv_nnc_tensor(btp, CPU_TENSOR_NHWC(32F, 2, 4), 0);
76
1
  REQUIRE_TENSOR_EQ(b, &bt, "should be equal");
77
1
  ccv_nnc_tensor_free(a);
78
1
  ccv_nnc_tensor_view_free(av);
79
1
  ccv_nnc_tensor_free(indices);
80
1
  ccv_nnc_tensor_free(b);
81
1
  ccv_nnc_tensor_view_free(bv);
82
1
}
83
84
TEST_CASE("backward index select a tensor")
85
1
{
86
1
  float bp[] = {
87
1
    1, 2,
88
1
    2, 3,
89
1
  };
90
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 3, 2), 0);
91
1
  int ip[] = {1, 1};
92
1
  ccv_nnc_tensor_t* const indices = ccv_nnc_tensor_new(ip, CPU_TENSOR_NHWC(32S, 2), 0);
93
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(bp, CPU_TENSOR_NHWC(32F, 2, 2), 0);
94
1
  ccv_nnc_cmd_exec(CMD_INDEX_SELECT_BACKWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(b, 0, indices), TENSOR_LIST(a), 0);
95
1
  float atp[] = {
96
1
    0, 0,
97
1
    3, 5,
98
1
    0, 0,
99
1
  };
100
1
  ccv_nnc_tensor_t const at = ccv_nnc_tensor(atp, CPU_TENSOR_NHWC(32F, 3, 2), 0);
101
1
  REQUIRE_TENSOR_EQ(a, &at, "should be equal");
102
1
  ccv_nnc_tensor_free(a);
103
1
  ccv_nnc_tensor_free(indices);
104
1
  ccv_nnc_tensor_free(b);
105
1
}
106
107
TEST_CASE("backward index select a 1d tensor")
108
1
{
109
1
  float bp[] = {
110
1
    4, 3, 5,
111
1
  };
112
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 5), 0);
113
1
  int ip[] = {3, 2, 4};
114
1
  ccv_nnc_tensor_t* const indices = ccv_nnc_tensor_new(ip, CPU_TENSOR_NHWC(32S, 3), 0);
115
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(bp, CPU_TENSOR_NHWC(32F, 3), 0);
116
1
  ccv_nnc_cmd_exec(CMD_INDEX_SELECT_BACKWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(b, 0, indices), TENSOR_LIST(a), 0);
117
1
  float atp[] = {
118
1
    0, 0, 3, 4, 5
119
1
  };
120
1
  ccv_nnc_tensor_t const at = ccv_nnc_tensor(atp, CPU_TENSOR_NHWC(32F, 5), 0);
121
1
  REQUIRE_TENSOR_EQ(a, &at, "should be equal");
122
1
  ccv_nnc_tensor_free(a);
123
1
  ccv_nnc_tensor_free(indices);
124
1
  ccv_nnc_tensor_free(b);
125
1
}
126
127
TEST_CASE("backward index select a tensor view")
128
1
{
129
1
  float bp[] = {
130
1
    0, 3, 4, 0,
131
1
    0, 1, 5, 0,
132
1
  };
133
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 3, 4), 0);
134
1
  int i;
135
13
  for (i = 0; i < 3 * 4; 
i++12
)
136
12
    a->data.f32[i] = i;
137
1
  ccv_nnc_tensor_view_t* const av = ccv_nnc_tensor_view_new(a, CPU_TENSOR_NHWC(32F, 3, 2), DIM_ALLOC(0, 1), DIM_ALLOC(3, 4));
138
1
  int ip[] = {1, 1};
139
1
  ccv_nnc_tensor_t* const indices = ccv_nnc_tensor_new(ip, CPU_TENSOR_NHWC(32S, 2), 0);
140
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(bp, CPU_TENSOR_NHWC(32F, 2, 4), 0);
141
1
  ccv_nnc_tensor_view_t* const bv = ccv_nnc_tensor_view_new(b, CPU_TENSOR_NHWC(32F, 2, 2), DIM_ALLOC(0, 1), DIM_ALLOC(2, 4));
142
1
  ccv_nnc_cmd_exec(CMD_INDEX_SELECT_BACKWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST((ccv_nnc_tensor_t*)bv, 0, indices), TENSOR_LIST((ccv_nnc_tensor_t*)av), 0);
143
1
  float atp[] = {
144
1
    0, 0, 0, 3,
145
1
    4, 4, 9, 7,
146
1
    8, 0, 0, 11,
147
1
  };
148
1
  ccv_nnc_tensor_t const at = ccv_nnc_tensor(atp, CPU_TENSOR_NHWC(32F, 3, 4), 0);
149
1
  REQUIRE_TENSOR_EQ(a, &at, "should be equal");
150
1
  ccv_nnc_tensor_free(a);
151
1
  ccv_nnc_tensor_view_free(av);
152
1
  ccv_nnc_tensor_free(indices);
153
1
  ccv_nnc_tensor_free(b);
154
1
  ccv_nnc_tensor_view_free(bv);
155
1
}
156
157
#include "case_main.h"