2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "kernels/AveragePool2D.h"
19 #include "kernels/Utils.h"
21 #include <tensorflow/lite/kernels/internal/reference/pooling.h>
25 namespace luci_interpreter
31 AveragePool2D::AveragePool2D(const Tensor *input, Tensor *output, const Pool2DParams ¶ms)
32 : KernelWithParams<Pool2DParams>({input}, {output}, params)
36 void AveragePool2D::configure()
38 if (input()->element_type() != output()->element_type())
40 throw std::runtime_error("Input Tensor and Output Tensor Type must be same");
42 if (input()->shape().num_dims() != 4)
44 throw std::runtime_error("Input Tensor Shape must be 4-D");
46 const Shape &input_shape = input()->shape();
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);
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);
59 computePadding(_params.stride_height, 1, input_height, _params.filter_height, output_height);
61 computePadding(_params.stride_width, 1, input_width, _params.filter_width, output_width);
62 if (input()->element_type() == DataType::U8)
64 if (input()->scale() != output()->scale() || input()->zero_point() != output()->zero_point())
66 throw std::runtime_error(
67 "Quantization param for Input and output must be same(scale or zero-point)");
70 output()->resize({batches, output_height, output_width, depth});
73 void AveragePool2D::execute() const
75 switch (input()->element_type())
77 case DataType::FLOAT32:
84 throw std::runtime_error("Unsupported type.");
88 void AveragePool2D::evalFloat() const
90 float activation_min{};
91 float activation_max{};
92 calculateActivationRange(_params.activation, &activation_min, &activation_max);
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;
104 tflite::reference_ops::AveragePool(params, getTensorShape(input()), getTensorData<float>(input()),
105 getTensorShape(output()), getTensorData<float>(output()));
108 void AveragePool2D::evalQuantized() const
110 int32_t activation_min{};
111 int32_t activation_max{};
112 calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);
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;
124 tflite::reference_ops::AveragePool(params, getTensorShape(input()),
125 getTensorData<uint8_t>(input()), getTensorShape(output()),
126 getTensorData<uint8_t>(output()));
129 } // namespace kernels
130 } // namespace luci_interpreter