Imported Upstream version 1.18.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 #include "luci_interpreter/TestMemoryManager.h"
20
21 namespace luci_interpreter
22 {
23 namespace kernels
24 {
25 namespace
26 {
27
28 using namespace testing;
29
30 float GetTolerance(float min, float max) { return (max - min) / 255.0; }
31
32 TEST(Pad, Uint8)
33 {
34   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
35   float kQuantizedTolerance = GetTolerance(-1.0, 1.0);
36   std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(-1.0f, 1.0f);
37   std::vector<float> input_data{-0.8, 0.2, 0.9, 0.7, 0.1, -0.3};
38   std::vector<int32_t> paddings_data{0, 0, 0, 2, 1, 3, 0, 0};
39   Tensor input_tensor = makeInputTensor<DataType::U8>(
40     {1, 2, 3, 1}, quant_param.first, quant_param.second, input_data, memory_manager.get());
41   Tensor paddings_tensor =
42     makeInputTensor<DataType::S32>({4, 2}, paddings_data, memory_manager.get());
43   Tensor output_tensor = makeOutputTensor(DataType::U8, quant_param.first, quant_param.second);
44
45   Pad kernel(&input_tensor, &paddings_tensor, &output_tensor);
46   kernel.configure();
47   memory_manager->allocate_memory(output_tensor);
48   kernel.execute();
49
50   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,
51                                      0, 0,    0,   0,   0, 0, 0, 0, 0,   0,   0,    0, 0, 0};
52   EXPECT_THAT(dequantizeTensorData(output_tensor),
53               FloatArrayNear(ref_output_data, kQuantizedTolerance));
54   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 4, 7, 1}));
55 }
56
57 TEST(Pad, Float)
58 {
59   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
60   std::vector<float> input_data{1, 2, 3, 4, 5, 6};
61   std::vector<int32_t> paddings_data{1, 0, 0, 2, 0, 3, 0, 0};
62   Tensor input_tensor =
63     makeInputTensor<DataType::FLOAT32>({1, 2, 3, 1}, input_data, memory_manager.get());
64   Tensor paddings_tensor =
65     makeInputTensor<DataType::S32>({4, 2}, paddings_data, memory_manager.get());
66   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
67
68   Pad kernel(&input_tensor, &paddings_tensor, &output_tensor);
69   kernel.configure();
70   memory_manager->allocate_memory(output_tensor);
71   kernel.execute();
72
73   std::vector<float> ref_output_data{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74                                      0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 4, 5,
75                                      6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
76   std::initializer_list<int32_t> ref_output_shape{2, 4, 6, 1};
77   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
78   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
79 }
80
81 } // namespace
82 } // namespace kernels
83 } // namespace luci_interpreter