Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / PadV2.test.cpp
1 /*
2  * Copyright (c) 2021 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/PadV2.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(PadV2, 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   std::vector<float> constant_values_data{0.5};
40   Tensor input_tensor = makeInputTensor<DataType::U8>(
41     {1, 2, 3, 1}, quant_param.first, quant_param.second, input_data, memory_manager.get());
42   Tensor paddings_tensor =
43     makeInputTensor<DataType::S32>({4, 2}, paddings_data, memory_manager.get());
44   Tensor constant_values = makeInputTensor<DataType::U8>(
45     {1}, quant_param.first, quant_param.second, constant_values_data, memory_manager.get());
46   Tensor output_tensor = makeOutputTensor(DataType::U8, quant_param.first, quant_param.second);
47
48   PadV2 kernel(&input_tensor, &paddings_tensor, &constant_values, &output_tensor);
49   kernel.configure();
50   memory_manager->allocate_memory(output_tensor);
51   kernel.execute();
52
53   std::vector<float> ref_output_data = {
54     0.5, -0.8, 0.2, 0.9, 0.5, 0.5, 0.5, 0.5, 0.7, 0.1, -0.3, 0.5, 0.5, 0.5,  //
55     0.5, 0.5,  0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,  0.5, 0.5, 0.5}; //
56   EXPECT_THAT(dequantizeTensorData(output_tensor),
57               FloatArrayNear(ref_output_data, kQuantizedTolerance));
58   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 4, 7, 1}));
59 }
60
61 TEST(PadV2, Float)
62 {
63   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
64   std::vector<float> input_data{1, 2, 3, 4, 5, 6};
65   std::vector<int32_t> paddings_data{1, 0, 0, 2, 0, 3, 0, 0};
66   std::vector<float> constant_values_data{7};
67   Tensor input_tensor =
68     makeInputTensor<DataType::FLOAT32>({1, 2, 3, 1}, input_data, memory_manager.get());
69   Tensor paddings_tensor =
70     makeInputTensor<DataType::S32>({4, 2}, paddings_data, memory_manager.get());
71   Tensor constant_values =
72     makeInputTensor<DataType::FLOAT32>({1}, constant_values_data, memory_manager.get());
73   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
74
75   PadV2 kernel(&input_tensor, &paddings_tensor, &constant_values, &output_tensor);
76   kernel.configure();
77   memory_manager->allocate_memory(output_tensor);
78   kernel.execute();
79
80   std::vector<float> ref_output_data{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
81                                      7, 7, 7, 7, 7, 7, 7, 7, 1, 2, 3, 7, 7, 7, 4, 5,
82                                      6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
83   std::initializer_list<int32_t> ref_output_shape{2, 4, 6, 1};
84   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
85   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
86 }
87
88 } // namespace
89 } // namespace kernels
90 } // namespace luci_interpreter