Coverage Report

Created: 2021-04-05 01:08

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