ONE - On-device Neural Engine
Loading...
Searching...
No Matches
AveragePool2D.cpp
Go to the documentation of this file.
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
18
19#include "kernels/Utils.h"
20
21#include "PALAveragePool2d.h"
22
23#include <stdexcept>
24
25namespace luci_interpreter
26{
27
28namespace kernels
29{
30
31AveragePool2D::AveragePool2D(const Tensor *input, Tensor *output, Tensor *scratchpad,
32 const Pool2DParams &params)
33 : KernelWithParams<Pool2DParams>({input}, {output, scratchpad}, params)
34{
35}
36
38{
39 if (input()->element_type() != output()->element_type())
40 {
41 throw std::runtime_error("Input Tensor and Output Tensor Type must be same");
42 }
43 if (input()->shape().num_dims() != 4)
44 {
45 throw std::runtime_error("Input Tensor Shape must be 4-D");
46 }
47 const Shape &input_shape = input()->shape();
48
49 const int32_t batches = input_shape.dim(0);
50 const int32_t input_height = input_shape.dim(1);
51 const int32_t input_width = input_shape.dim(2);
52 const int32_t depth = input_shape.dim(3);
53
54 const int32_t output_height =
56 const int32_t output_width =
58
59 _padding_height =
60 computePadding(_params.stride_height, 1, input_height, _params.filter_height, output_height);
61 _padding_width =
62 computePadding(_params.stride_width, 1, input_width, _params.filter_width, output_width);
63 if (input()->element_type() == DataType::U8)
64 {
65 LUCI_INTERPRETER_CHECK(std::abs(output()->scale() - input()->scale()) <= 1.0e-6);
66 LUCI_INTERPRETER_CHECK(output()->zero_point() == input()->zero_point());
67 }
68 else if (input()->element_type() == DataType::S16)
69 {
70 LUCI_INTERPRETER_CHECK(std::abs(output()->scale() - input()->scale()) <= 1.0e-6);
71 LUCI_INTERPRETER_CHECK(input()->zero_point() == 0 && output()->zero_point() == 0);
72 }
73 else if (input()->element_type() == DataType::S8)
74 {
75 LUCI_INTERPRETER_CHECK(std::abs(output()->scale() - input()->scale()) <= 1.0e-6);
76 LUCI_INTERPRETER_CHECK(output()->zero_point() == input()->zero_point());
77 }
78 output()->resize({batches, output_height, output_width, depth});
79
80 auto scratchpad = getOutputTensors()[1];
81 luci_interpreter_pal::SetupScratchpadTensor(scratchpad, input()->element_type(),
83}
84
86{
87 switch (input()->element_type())
88 {
89 case DataType::FLOAT32:
90 evalFloat();
91 break;
92 case DataType::U8:
93 evalQuantized();
94 break;
95 case DataType::S16:
96 evalSInt16();
97 break;
98 case DataType::S8:
99 evalSInt8();
100 break;
101 default:
102 throw std::runtime_error("luci-intp AveragePool2D Unsupported type.");
103 }
104}
105
106void AveragePool2D::evalFloat() const
107{
108 float activation_min{};
109 float activation_max{};
110 calculateActivationRange(_params.activation, &activation_min, &activation_max);
111
112 tflite::PoolParams params{};
113 params.padding_values.height = _padding_height;
114 params.padding_values.width = _padding_width;
119 params.float_activation_min = activation_min;
120 params.float_activation_max = activation_max;
121
122 tflite::reference_ops::AveragePool(params, getTensorShape(input()), getTensorData<float>(input()),
123 getTensorShape(output()), getTensorData<float>(output()));
124}
125
126void AveragePool2D::evalQuantized() const
127{
128 int32_t activation_min{};
129 int32_t activation_max{};
130 calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);
131
132 tflite::PoolParams params{};
133 params.padding_values.height = _padding_height;
134 params.padding_values.width = _padding_width;
139 params.quantized_activation_min = activation_min;
140 params.quantized_activation_max = activation_max;
141
142 tflite::reference_ops::AveragePool(params, getTensorShape(input()),
143 getTensorData<uint8_t>(input()), getTensorShape(output()),
144 getTensorData<uint8_t>(output()));
145}
146
147void AveragePool2D::evalSInt8() const
148{
149 int32_t activation_min{};
150 int32_t activation_max{};
151 calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);
152 tflite::PoolParams params{};
153 params.padding_values.height = _padding_height;
154 params.padding_values.width = _padding_width;
159 params.quantized_activation_min = activation_min;
160 params.quantized_activation_max = activation_max;
161
162 auto scratchpad = getOutputTensors()[1];
163 int8_t *scratchpad_data = nullptr;
164 if (scratchpad->is_allocatable())
165 scratchpad_data = scratchpad->data<int8_t>();
166
168 params, getTensorShape(input()), getTensorData<int8_t>(input()), getTensorShape(output()),
169 getTensorData<int8_t>(output()), getTensorShape(scratchpad), scratchpad_data);
170}
171
172void AveragePool2D::evalSInt16() const
173{
174 int32_t activation_min{};
175 int32_t activation_max{};
176 calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);
177
178 tflite::PoolParams params{};
179 params.padding_values.height = _padding_height;
180 params.padding_values.width = _padding_width;
185 params.quantized_activation_min = activation_min;
186 params.quantized_activation_max = activation_max;
187
188 tflite::reference_integer_ops::AveragePool(
189 params, getTensorShape(input()), getTensorData<int16_t>(input()), //
190 getTensorShape(output()), getTensorData<int16_t>(output()));
191}
192
193} // namespace kernels
194} // namespace luci_interpreter
const std::vector< Tensor * > & getOutputTensors() const
Definition Kernel.h:40
const Pool2DParams & params() const
Definition Kernel.h:67
int32_t dim(int i) const
Definition Tensor.h:41
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
AveragePool2D(const Tensor *input, Tensor *output, Tensor *scratchpad, const Pool2DParams &params)
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
int32_t computePadding(int32_t stride, int32_t dilation_rate, int32_t in_size, int32_t filter_size, int32_t out_size)
Definition Utils.h:41
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
void calculateActivationRange(Activation activation, T *activation_min, T *activation_max)
Definition Utils.cpp:52
void calculateActivationRangeQuantized(Activation activation, const Tensor *output, int32_t *activation_min, int32_t *activation_max)
Definition Utils.cpp:119
int32_t computeOutputSize(Padding padding, int32_t image_size, int32_t filter_size, int32_t stride, int32_t dilation_rate=1)
Definition Utils.h:59
void AveragePool< int8_t >(const tflite::PoolParams &params, const tflite::RuntimeShape &input_shape, const int8_t *input_data, const tflite::RuntimeShape &output_shape, int8_t *output_data, const tflite::RuntimeShape &scratchpad_shape, int8_t *scratchpad_data)