Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Pad.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "kernels/Pad.h"
18 #include "kernels/TestUtils.h"
19
20 namespace luci_interpreter
21 {
22 namespace kernels
23 {
24 namespace
25 {
26
27 using namespace testing;
28
29 float GetTolerance(float min, float max) { return (max - min) / 255.0; }
30
31 TEST(Pad, Uint8)
32 {
33   float kQuantizedTolerance = GetTolerance(-1.0, 1.0);
34   std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(-1.0f, 1.0f);
35   std::vector<float> input_data{-0.8, 0.2, 0.9, 0.7, 0.1, -0.3};
36   std::vector<int32_t> paddings_data{0, 0, 0, 2, 1, 3, 0, 0};
37   Tensor input_tensor{DataType::U8, {1, 2, 3, 1}, {{quant_param.first}, {quant_param.second}}, ""};
38   Tensor paddings_tensor = makeInputTensor<DataType::S32>({4, 2}, paddings_data);
39   Tensor output_tensor = makeOutputTensor(DataType::U8, quant_param.first, quant_param.second);
40   std::vector<uint8_t> quantize_input =
41       quantize<uint8_t>(input_data, quant_param.first, quant_param.second);
42   input_tensor.writeData(quantize_input.data(), quantize_input.size() * sizeof(uint8_t));
43
44   Pad kernel(&input_tensor, &paddings_tensor, &output_tensor);
45   kernel.configure();
46   kernel.execute();
47
48   std::vector<float> ref_output_data{0, -0.8, 0.2, 0.9, 0, 0, 0, 0, 0.7, 0.1, -0.3, 0, 0, 0,
49                                      0, 0,    0,   0,   0, 0, 0, 0, 0,   0,   0,    0, 0, 0};
50   EXPECT_THAT(dequantize(extractTensorData<uint8_t>(output_tensor), output_tensor.scale(),
51                          output_tensor.zero_point()),
52               ElementsAreArray(ArrayFloatNear(ref_output_data, kQuantizedTolerance)));
53   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 4, 7, 1}));
54 }
55
56 TEST(Pad, Float)
57 {
58   std::vector<float> input_data{1, 2, 3, 4, 5, 6};
59   std::vector<int32_t> paddings_data{1, 0, 0, 2, 0, 3, 0, 0};
60   Tensor input_tensor = makeInputTensor<DataType::FLOAT32>({1, 2, 3, 1}, input_data);
61   Tensor paddings_tensor = makeInputTensor<DataType::S32>({4, 2}, paddings_data);
62   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
63
64   Pad kernel(&input_tensor, &paddings_tensor, &output_tensor);
65   kernel.configure();
66   kernel.execute();
67
68   std::vector<float> ref_output_data{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69                                      0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 4, 5,
70                                      6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
71   std::initializer_list<int32_t> ref_output_shape{2, 4, 6, 1};
72   EXPECT_THAT(extractTensorData<float>(output_tensor),
73               ElementsAreArray(ArrayFloatNear(ref_output_data)));
74   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
75 }
76
77 } // namespace
78 } // namespace kernels
79 } // namespace luci_interpreter