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