summaryrefslogtreecommitdiff
path: root/compiler/luci-interpreter/src/kernels/Concatenation.test.cpp
blob: f893b38fd86f4ed91f8e3cdd2f5918409aaf9e23 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (c) 2020 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 "kernels/Concatenation.h"
#include "kernels/TestUtils.h"
#include "luci_interpreter/TestMemoryManager.h"

namespace luci_interpreter
{
namespace kernels
{
namespace
{

using namespace testing;

class ConcatenationTest : public ::testing::Test
{
protected:
  void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }

  std::unique_ptr<IMemoryManager> _memory_manager;
};

TEST_F(ConcatenationTest, Float)
{
  std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
  std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
  Tensor input1_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
  Tensor input2_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
  ConcatenationParams params{};

  // Try different 'axis' and expect different results.
  {
    params.axis = 0;
    params.activation = luci::FusedActFunc::NONE;

    Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
    kernel.configure();
    for (auto t : kernel.getOutputTensors())
    {
      _memory_manager->allocate_memory(*t);
    }
    kernel.execute();

    EXPECT_THAT(extractTensorData<float>(output_tensor),
                FloatArrayNear({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
  }
  {
    params.axis = -2; // Same as '0'.
    params.activation = luci::FusedActFunc::NONE;

    Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
    kernel.configure();
    _memory_manager->allocate_memory(output_tensor);
    kernel.execute();

    EXPECT_THAT(extractTensorData<float>(output_tensor),
                FloatArrayNear({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
  }
  {
    params.axis = 1;
    params.activation = luci::FusedActFunc::NONE;

    Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
    kernel.configure();
    _memory_manager->allocate_memory(output_tensor);
    kernel.execute();

    EXPECT_THAT(extractTensorData<float>(output_tensor),
                FloatArrayNear({1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12}));
  }
  {
    params.axis = -1; // Same as '1'.
    params.activation = luci::FusedActFunc::NONE;

    Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
    kernel.configure();
    _memory_manager->allocate_memory(output_tensor);
    kernel.execute();

    EXPECT_THAT(extractTensorData<float>(output_tensor),
                FloatArrayNear({1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12}));
  }
}

TEST_F(ConcatenationTest, Input_Number_Check_NEG)
{
  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
  ConcatenationParams params{};

  params.axis = -1;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

TEST_F(ConcatenationTest, Invalid_Axis_NEG)
{
  std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
  std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
  Tensor input1_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
  Tensor input2_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
  ConcatenationParams params{};

  params.axis = -3;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

TEST_F(ConcatenationTest, Mismatching_Input_Type_NEG)
{
  std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
  std::vector<uint8_t> input2_data{7, 8, 9, 10, 11, 12};
  Tensor input1_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
  Tensor input2_tensor = makeInputTensor<DataType::U8>({2, 3}, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
  ConcatenationParams params{};

  params.axis = -1;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

TEST_F(ConcatenationTest, Mismatching_Input_Dimension_Num_NEG)
{
  std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
  std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
  Tensor input1_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
  Tensor input2_tensor =
    makeInputTensor<DataType::FLOAT32>({1, 2, 3}, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
  ConcatenationParams params{};

  params.axis = -1;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

TEST_F(ConcatenationTest, Mismatching_Input_Dimension_NEG)
{
  std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
  std::vector<float> input2_data{7, 8, 9, 10, 11, 12, 13, 14, 15};
  Tensor input1_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
  Tensor input2_tensor =
    makeInputTensor<DataType::FLOAT32>({3, 3}, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
  ConcatenationParams params{};

  params.axis = -1;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

TEST_F(ConcatenationTest, Int8_Mismatching_Input_Type_NEG)
{
  std::vector<uint8_t> input1_data{1, 2, 3, 4};
  std::vector<int8_t> input2_data{5, 6, 7, 8};
  Tensor input1_tensor = makeInputTensor<DataType::U8>({2, 2}, input1_data, _memory_manager.get());
  Tensor input2_tensor = makeInputTensor<DataType::S8>({2, 2}, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::S8);
  ConcatenationParams params{};

  params.axis = -1;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

TEST_F(ConcatenationTest, Int8_Mismatching_Input_Output_Quant_Params_NEG)
{
  std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
  std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
  int quantized_dimension = 3;
  std::vector<float> scales{0.1, 0.2, 0.3};
  std::vector<int32_t> zero_points{1, -1, 1};

  Tensor input1_tensor = makeInputTensor<DataType::S8>(
    {1, 1, 2, 3}, scales, zero_points, quantized_dimension, input1_data, _memory_manager.get());
  Tensor input2_tensor = makeInputTensor<DataType::S8>(
    {1, 1, 2, 3}, scales, zero_points, quantized_dimension, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::S8, scales.at(0), zero_points.at(0));
  ConcatenationParams params{};

  params.axis = -1;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

TEST_F(ConcatenationTest, Int8_Mismatching_Zero_Point_NEG)
{
  std::vector<float> input1_data{1, 2, 3, 4};
  std::vector<float> input2_data{5, 6, 7, 8};
  float scale = 0.1;
  int32_t zero_point_1 = 1;
  int32_t zero_point_2 = -1;

  Tensor input1_tensor =
    makeInputTensor<DataType::S8>({2, 2}, scale, zero_point_1, input1_data, _memory_manager.get());
  Tensor input2_tensor =
    makeInputTensor<DataType::S8>({2, 2}, scale, zero_point_2, input2_data, _memory_manager.get());

  Tensor output_tensor = makeOutputTensor(DataType::S8, scale, zero_point_1);
  ConcatenationParams params{};

  params.axis = -1;
  params.activation = luci::FusedActFunc::NONE;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

// TODO: Remove this test when concat w/ fused_activation is supported
TEST_F(ConcatenationTest, With_Fused_Activation_NEG)
{
  std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
  std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
  Tensor input1_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
  Tensor input2_tensor =
    makeInputTensor<DataType::FLOAT32>({2, 3}, input2_data, _memory_manager.get());
  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
  ConcatenationParams params{};

  params.axis = 1;
  params.activation = luci::FusedActFunc::RELU;

  Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
  EXPECT_ANY_THROW(kernel.configure());
}

} // namespace
} // namespace kernels
} // namespace luci_interpreter