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