Imported Upstream version 1.21.0
[platform/core/ml/nnfw.git] / compiler / luci-micro / luci-interpreter / src / kernels / MaxPool2D.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/MaxPool2D.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 class MaxPool2DTest : public ::testing::Test
31 {
32 protected:
33   void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
34
35   std::unique_ptr<IMemoryManager> _memory_manager;
36 };
37
38 TEST_F(MaxPool2DTest, Float)
39 {
40   Shape input_shape{1, 3, 5, 1};
41   std::vector<float> input_data{
42     1,  -1, 0,  -2, 2,  //
43     -7, -6, -5, -4, -3, //
44     5,  4,  3,  6,  7,  //
45   };
46   Tensor input_tensor =
47     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
48   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
49
50   Pool2DParams params{};
51   params.padding = Padding::VALID;
52   params.filter_height = 2;
53   params.filter_width = 3;
54   params.stride_height = 1;
55   params.stride_width = 2;
56   params.activation = Activation::RELU6;
57
58   MaxPool2D kernel(&input_tensor, &output_tensor, params);
59   kernel.configure();
60   _memory_manager->allocate_memory(output_tensor);
61   kernel.execute();
62
63   std::vector<float> ref_output_data{
64     1, 2, //
65     5, 6, //
66   };
67   std::initializer_list<int32_t> ref_output_shape{1, 2, 2, 1};
68   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
69   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
70 }
71
72 TEST_F(MaxPool2DTest, Uint8)
73 {
74   std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(-15.9375, 15.9375);
75   std::vector<float> input_data{
76     0,  -6, 12, 4, //
77     -3, -2, 10, 7, //
78   };
79   Tensor input_tensor = makeInputTensor<DataType::U8>(
80     {1, 2, 4, 1}, quant_param.first, quant_param.second, input_data, _memory_manager.get());
81   Tensor output_tensor = makeOutputTensor(DataType::U8, quant_param.first, quant_param.second);
82
83   Pool2DParams params{};
84   params.padding = Padding::VALID;
85   params.filter_height = 2;
86   params.filter_width = 2;
87   params.stride_height = 2;
88   params.stride_width = 2;
89   params.activation = Activation::RELU6;
90
91   MaxPool2D kernel(&input_tensor, &output_tensor, params);
92   kernel.configure();
93   _memory_manager->allocate_memory(output_tensor);
94   kernel.execute();
95
96   std::vector<float> ref_output_data{0.0, 6.0};
97   std::initializer_list<int32_t> ref_output_shape{1, 1, 2, 1};
98   EXPECT_THAT(dequantizeTensorData(output_tensor), FloatArrayNear(ref_output_data));
99   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
100 }
101
102 TEST_F(MaxPool2DTest, SInt16)
103 {
104   Shape input_shape{1, 3, 5, 1};
105   std::vector<int32_t> ref_output_shape{1, 2, 2, 1};
106   std::vector<float> input_data{
107     1,  -1, 0,  -2, 2,  //
108     -7, -6, -5, -4, -3, //
109     5,  4,  3,  6,  7,  //
110   };
111   std::vector<float> ref_output_data{
112     1, 2, //
113     5, 6, //
114   };
115
116   Tensor input_tensor =
117     makeInputTensor<DataType::S16>(input_shape, 0.2, 0, input_data, _memory_manager.get());
118   Tensor output_tensor = makeOutputTensor(DataType::S16, 0.2, 0);
119
120   Pool2DParams params{};
121   params.padding = Padding::VALID;
122   params.filter_height = 2;
123   params.filter_width = 3;
124   params.stride_height = 1;
125   params.stride_width = 2;
126   params.activation = Activation::RELU6;
127
128   MaxPool2D kernel(&input_tensor, &output_tensor, params);
129   kernel.configure();
130   _memory_manager->allocate_memory(output_tensor);
131   kernel.execute();
132
133   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
134   EXPECT_THAT(dequantizeTensorData(output_tensor), FloatArrayNear(ref_output_data));
135 }
136
137 } // namespace
138 } // namespace kernels
139 } // namespace luci_interpreter