Coverage Report

Created: 2019-07-03 22:50

/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], [4, 5, 6]] + [7, 8, 9]")
14
1
{
15
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2, 3), 0);
16
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 3), 0);
17
1
  ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2, 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
  a->data.f32[3] = 4;
22
1
  a->data.f32[4] = 5;
23
1
  a->data.f32[5] = 6;
24
1
  b->data.f32[0] = 7;
25
1
  b->data.f32[1] = 8;
26
1
  b->data.f32[2] = 9;
27
1
  ccv_nnc_cmd_exec(CMD_ADD_FORWARD(1, 1), ccv_nnc_no_hint, 0, TENSOR_LIST(a, b), TENSOR_LIST(c), 0);
28
1
  float ctp[] = {
29
1
    8, 10, 12,
30
1
    11, 13, 15
31
1
  };
32
1
  ccv_nnc_tensor_t ct = ccv_nnc_tensor(ctp, CPU_TENSOR_NHWC(32F, 2, 3), 0);
33
1
  REQUIRE_TENSOR_EQ(c, &ct, "result should be equal");
34
1
  ccv_nnc_tensor_free(a);
35
1
  ccv_nnc_tensor_free(b);
36
1
  ccv_nnc_tensor_free(c);
37
1
}
38
39
TEST_CASE("broadcasting semantics for add [[1], [2], [3], [4]] + [5, 6]")
40
1
{
41
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 1), 0);
42
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2), 0);
43
1
  ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 2), 0);
44
1
  a->data.f32[0] = 1;
45
1
  a->data.f32[1] = 2;
46
1
  a->data.f32[2] = 3;
47
1
  a->data.f32[3] = 4;
48
1
  b->data.f32[0] = 5;
49
1
  b->data.f32[1] = 6;
50
1
  ccv_nnc_cmd_exec(CMD_ADD_FORWARD(1, 1), ccv_nnc_no_hint, 0, TENSOR_LIST(a, b), TENSOR_LIST(c), 0);
51
1
  float ctp[] = {
52
1
    6, 7,
53
1
    7, 8,
54
1
    8, 9,
55
1
    9, 10
56
1
  };
57
1
  ccv_nnc_tensor_t ct = ccv_nnc_tensor(ctp, CPU_TENSOR_NHWC(32F, 4, 2), 0);
58
1
  REQUIRE_TENSOR_EQ(c, &ct, "result should be equal");
59
1
  ccv_nnc_tensor_free(a);
60
1
  ccv_nnc_tensor_free(b);
61
1
  ccv_nnc_tensor_free(c);
62
1
}
63
64
TEST_CASE("broadcasting semantics for mul [[1, 2, 3], [4, 5, 6]] * [7, 8, 9]")
65
1
{
66
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2, 3), 0);
67
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 3), 0);
68
1
  ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2, 3), 0);
69
1
  a->data.f32[0] = 1;
70
1
  a->data.f32[1] = 2;
71
1
  a->data.f32[2] = 3;
72
1
  a->data.f32[3] = 4;
73
1
  a->data.f32[4] = 5;
74
1
  a->data.f32[5] = 6;
75
1
  b->data.f32[0] = 7;
76
1
  b->data.f32[1] = 8;
77
1
  b->data.f32[2] = 9;
78
1
  ccv_nnc_cmd_exec(CMD_MUL_FORWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(a, b), TENSOR_LIST(c), 0);
79
1
  float ctp[] = {
80
1
    7, 16, 27,
81
1
    28, 40, 54
82
1
  };
83
1
  ccv_nnc_tensor_t ct = ccv_nnc_tensor(ctp, CPU_TENSOR_NHWC(32F, 2, 3), 0);
84
1
  REQUIRE_TENSOR_EQ(c, &ct, "result should be equal");
85
1
  ccv_nnc_tensor_free(a);
86
1
  ccv_nnc_tensor_free(b);
87
1
  ccv_nnc_tensor_free(c);
88
1
}
89
90
TEST_CASE("broadcasting semantics for mul [[1], [2], [3], [4]] * [5, 6]")
91
1
{
92
1
  ccv_nnc_tensor_t* const a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 1), 0);
93
1
  ccv_nnc_tensor_t* const b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 2), 0);
94
1
  ccv_nnc_tensor_t* const c = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 2), 0);
95
1
  a->data.f32[0] = 1;
96
1
  a->data.f32[1] = 2;
97
1
  a->data.f32[2] = 3;
98
1
  a->data.f32[3] = 4;
99
1
  b->data.f32[0] = 5;
100
1
  b->data.f32[1] = 6;
101
1
  ccv_nnc_cmd_exec(CMD_MUL_FORWARD(1), ccv_nnc_no_hint, 0, TENSOR_LIST(a, b), TENSOR_LIST(c), 0);
102
1
  float ctp[] = {
103
1
    5, 6,
104
1
    10, 12,
105
1
    15, 18,
106
1
    20, 24
107
1
  };
108
1
  ccv_nnc_tensor_t ct = ccv_nnc_tensor(ctp, CPU_TENSOR_NHWC(32F, 4, 2), 0);
109
1
  REQUIRE_TENSOR_EQ(c, &ct, "result should be equal");
110
1
  ccv_nnc_tensor_free(a);
111
1
  ccv_nnc_tensor_free(b);
112
1
  ccv_nnc_tensor_free(c);
113
1
}
114
115
#include "case_main.h"