Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / AveragePool2D.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/AveragePool2D.h"
18
19 #include "kernels/Utils.h"
20
21 #include <tensorflow/lite/kernels/internal/reference/pooling.h>
22
23 #include <stdexcept>
24
25 namespace luci_interpreter
26 {
27
28 namespace kernels
29 {
30
31 AveragePool2D::AveragePool2D(const Tensor *input, Tensor *output, const Pool2DParams &params)
32     : KernelWithParams<Pool2DParams>({input}, {output}, params)
33 {
34 }
35
36 void AveragePool2D::configure()
37 {
38   if (input()->element_type() != output()->element_type())
39   {
40     throw std::runtime_error("Input Tensor and Output Tensor Type must be same");
41   }
42   if (input()->shape().num_dims() != 4)
43   {
44     throw std::runtime_error("Input Tensor Shape must be 4-D");
45   }
46   const Shape &input_shape = input()->shape();
47
48   const int32_t batches = input_shape.dim(0);
49   const int32_t input_height = input_shape.dim(1);
50   const int32_t input_width = input_shape.dim(2);
51   const int32_t depth = input_shape.dim(3);
52
53   const int32_t output_height = computeOutputSize(_params.padding, input_height,
54                                                   _params.filter_height, _params.stride_height);
55   const int32_t output_width =
56       computeOutputSize(_params.padding, input_width, _params.filter_width, _params.stride_width);
57
58   _padding_height =
59       computePadding(_params.stride_height, 1, input_height, _params.filter_height, output_height);
60   _padding_width =
61       computePadding(_params.stride_width, 1, input_width, _params.filter_width, output_width);
62   if (input()->element_type() == DataType::U8)
63   {
64     if (input()->scale() != output()->scale() || input()->zero_point() != output()->zero_point())
65     {
66       throw std::runtime_error(
67           "Quantization param for Input and output must be same(scale or zero-point)");
68     }
69   }
70   output()->resize({batches, output_height, output_width, depth});
71 }
72
73 void AveragePool2D::execute() const
74 {
75   switch (input()->element_type())
76   {
77     case DataType::FLOAT32:
78       evalFloat();
79       break;
80     case DataType::U8:
81       evalQuantized();
82       break;
83     default:
84       throw std::runtime_error("Unsupported type.");
85   }
86 }
87
88 void AveragePool2D::evalFloat() const
89 {
90   float activation_min{};
91   float activation_max{};
92   calculateActivationRange(_params.activation, &activation_min, &activation_max);
93
94   tflite::PoolParams params{};
95   params.padding_values.height = _padding_height;
96   params.padding_values.width = _padding_width;
97   params.stride_height = _params.stride_height;
98   params.stride_width = _params.stride_width;
99   params.filter_height = _params.filter_height;
100   params.filter_width = _params.filter_width;
101   params.float_activation_min = activation_min;
102   params.float_activation_max = activation_max;
103
104   tflite::reference_ops::AveragePool(params, getTensorShape(input()), getTensorData<float>(input()),
105                                      getTensorShape(output()), getTensorData<float>(output()));
106 }
107
108 void AveragePool2D::evalQuantized() const
109 {
110   int32_t activation_min{};
111   int32_t activation_max{};
112   calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);
113
114   tflite::PoolParams params{};
115   params.padding_values.height = _padding_height;
116   params.padding_values.width = _padding_width;
117   params.stride_height = _params.stride_height;
118   params.stride_width = _params.stride_width;
119   params.filter_height = _params.filter_height;
120   params.filter_width = _params.filter_width;
121   params.quantized_activation_min = activation_min;
122   params.quantized_activation_max = activation_max;
123
124   tflite::reference_ops::AveragePool(params, getTensorShape(input()),
125                                      getTensorData<uint8_t>(input()), getTensorShape(output()),
126                                      getTensorData<uint8_t>(output()));
127 }
128
129 } // namespace kernels
130 } // namespace luci_interpreter