summaryrefslogtreecommitdiff
path: root/libs/kernel/acl/src/cl/Conv2D.test.cpp
blob: e34cdeea5dbb860834a9063c2ec9640f872a4bca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include <OperationsUtils.h>
#include <kernel/acl/nnfw_kernel_acl.h>
#include <kernel/acl/Conv2D.h>

// TODO: fix include path in CMakeFiles
#include "../util.h"

using namespace nnfw::kernel::acl;

TEST(KernelACL_TC, convFloat32_3x3to1x1)
{
  float inputData[9];
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  float filterData[9];
  const nnfw::rt::Shape filterShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  float biasData[1] = { 1.0 };
  const nnfw::rt::Shape biasShape = { OperandType::FLOAT32, {1}, 1.0, 0 };
  int32_t padding_left = 0;
  int32_t padding_right = 0;
  int32_t padding_top = 0;
  int32_t padding_bottom = 0;
  int32_t stride_width = 1;
  int32_t stride_height = 1;
  int32_t activation = static_cast<int32_t>(FusedActivationFunc::RELU);
  float outputData[1];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,1,1,1}, 1.0, 0 };
  bool bret;

  util::initData(inputData, sizeof(inputData) / sizeof(inputData[0]), 1.0);
  util::initData(filterData, sizeof(filterData) / sizeof(filterData[0]), 1.0);
  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = convFloat32(inputData, inputShape,
                     filterData, filterShape,
                     biasData, biasShape,
                     padding_left, padding_right,
                     padding_top, padding_bottom,
                     stride_width, stride_height,
                     activation,
                     outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectData[] = { 10.0f };
  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}

TEST(KernelACL_TC, convFloat32_3x3to3x3)
{
  float inputData[9];
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  float filterData[9];
  const nnfw::rt::Shape filterShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  float biasData[1] = { 1.0 };
  const nnfw::rt::Shape biasShape = { OperandType::FLOAT32, {1}, 1.0, 0 };
  int32_t padding_left = 1;
  int32_t padding_right = 1;
  int32_t padding_top = 1;
  int32_t padding_bottom = 1;
  int32_t stride_width = 1;
  int32_t stride_height = 1;
  int32_t activation = static_cast<int32_t>(FusedActivationFunc::RELU);
  float outputData[9];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  bool bret;

  util::initData(inputData, sizeof(inputData) / sizeof(inputData[0]), 1.0);
  util::initData(filterData, sizeof(filterData) / sizeof(filterData[0]), 1.0);
  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = convFloat32(inputData, inputShape,
                     filterData, filterShape,
                     biasData, biasShape,
                     padding_left, padding_right,
                     padding_top, padding_bottom,
                     stride_width, stride_height,
                     activation,
                     outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectData[] = {
    5.0f, 7.0f, 5.0f,
    7.0f, 10.0f, 7.0f,
    5.0f, 7.0f, 5.0f
  };
  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}

TEST(KernelACL_TC, convFloat32_3x3to3x3_RELU)
{
  float inputData[9];
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  float filterData[9];
  const nnfw::rt::Shape filterShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  float biasData[1] = { -5.0f };
  const nnfw::rt::Shape biasShape = { OperandType::FLOAT32, {1}, 1.0, 0 };
  int32_t padding_left = 1;
  int32_t padding_right = 1;
  int32_t padding_top = 1;
  int32_t padding_bottom = 1;
  int32_t stride_width = 1;
  int32_t stride_height = 1;
  int32_t activation = static_cast<int32_t>(FusedActivationFunc::RELU);
  float outputData[9];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,3,3,1}, 1.0, 0 };
  bool bret;

  util::initData(inputData, sizeof(inputData) / sizeof(inputData[0]), 1.0);
  util::initData(filterData, sizeof(filterData) / sizeof(filterData[0]), 1.0);
  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = convFloat32(inputData, inputShape,
                     filterData, filterShape,
                     biasData, biasShape,
                     padding_left, padding_right,
                     padding_top, padding_bottom,
                     stride_width, stride_height,
                     activation,
                     outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectData[] =
  {
    0.0f, 1.0f, 0.0f,
    1.0f, 4.0f, 1.0f,
    0.0f, 1.0f, 0.0f
  };

  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}

TEST(KernelACL_TC, convFloat32_3x5to3x3)
{
  float inputData[15] = {
    1,2,3,4,5,
    6,7,8,9,10,
    11,12,13,14,15
  };
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,3,5,1}, 1.0, 0 };
  float filterData[18] = {
    1,1,1, 1,1,1, 1,1,1,
    2,2,2, 2,2,2, 2,2,2
  };
  const nnfw::rt::Shape filterShape = { OperandType::FLOAT32, {2,3,3,1}, 1.0, 0 };
  float biasData[2] = { 1.0, 1.0 };
  const nnfw::rt::Shape biasShape = { OperandType::FLOAT32, {2}, 1.0, 0 };
  int32_t padding_left = 1;
  int32_t padding_right = 1;
  int32_t padding_top = 1;
  int32_t padding_bottom = 1;
  int32_t stride_width = 1;
  int32_t stride_height = 1;
  int32_t activation = static_cast<int32_t>(FusedActivationFunc::RELU);
  float outputData[30];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,3,5,2}, 1.0, 0 };
  bool bret;

  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = convFloat32(inputData, inputShape,
                     filterData, filterShape,
                     biasData, biasShape,
                     padding_left, padding_right,
                     padding_top, padding_bottom,
                     stride_width, stride_height,
                     activation,
                     outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectNCHW[] = {
    17.0f, 28.0f, 34.0f, 40.0f, 29.0f,
    40.0f, 64.0f, 73.0f, 82.0f, 58.0f,
    37.0f, 58.0f, 64.0f, 70.0f, 49.0f,

    33.0f, 55.0f, 67.0f, 79.0f, 57.0f,
    79.0f, 127.0f, 145.0f, 163.0f, 115.0f,
    73.0f, 115.0f, 127.0f, 139.0f, 97.0f
  };
  float expectData[30];
  util::NCHW2NHWC(expectNCHW, expectData, outputShape);
  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}