ONE - On-device Neural Engine
Loading...
Searching...
No Matches
MaxPool2D.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
17#include "kernels/MaxPool2D.h"
18
19#include "kernels/Utils.h"
20
21#include <tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h>
22#include <tensorflow/lite/kernels/internal/reference/pooling.h>
23
24#include <stdexcept>
25
26namespace luci_interpreter
27{
28
29namespace kernels
30{
31
32MaxPool2D::MaxPool2D(const Tensor *input, Tensor *output, const Pool2DParams &params)
33 : KernelWithParams<Pool2DParams>({input}, {output}, params)
34{
35}
36
38{
39 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
40 assert(input()->shape().num_dims() == 4);
41 const Shape &input_shape = input()->shape();
42 const int32_t batches = input_shape.dim(0);
43 const int32_t input_height = input_shape.dim(1);
44 const int32_t input_width = input_shape.dim(2);
45 const int32_t depth = input_shape.dim(3);
46
47 const int32_t output_height =
49 const int32_t output_width =
51
52 _padding_height =
53 computePadding(_params.stride_height, 1, input_height, _params.filter_height, output_height);
54 _padding_width =
55 computePadding(_params.stride_width, 1, input_width, _params.filter_width, output_width);
56
57 output()->resize({batches, output_height, output_width, depth});
58 if (input()->element_type() == DataType::U8)
59 {
60 LUCI_INTERPRETER_CHECK(std::abs(output()->scale() - input()->scale()) <= 1.0e-6);
61 LUCI_INTERPRETER_CHECK(output()->zero_point() == input()->zero_point());
62 }
63 else if (input()->element_type() == DataType::S16)
64 {
65 LUCI_INTERPRETER_CHECK(std::abs(output()->scale() - input()->scale()) <= 1.0e-6);
66 LUCI_INTERPRETER_CHECK(input()->zero_point() == 0 && output()->zero_point() == 0);
67 }
68}
69
71{
72 switch (input()->element_type())
73 {
74 case DataType::FLOAT32:
75 evalFloat();
76 break;
77 case DataType::U8:
78 evalQuantized();
79 break;
80 case DataType::S16:
81 evalSInt16();
82 break;
83 default:
84 throw std::runtime_error("luci-intp MaxPool2D Unsupported type.");
85 }
86}
87
88void MaxPool2D::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;
101 params.float_activation_min = activation_min;
102 params.float_activation_max = activation_max;
103
104 tflite::reference_ops::MaxPool(params, getTensorShape(input()), getTensorData<float>(input()),
105 getTensorShape(output()), getTensorData<float>(output()));
106}
107
108void MaxPool2D::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;
121 params.quantized_activation_min = activation_min;
122 params.quantized_activation_max = activation_max;
123
124 tflite::reference_ops::MaxPool(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
125 getTensorShape(output()), getTensorData<uint8_t>(output()));
126}
127
128void MaxPool2D::evalSInt16() const
129{
130 int32_t activation_min{};
131 int32_t activation_max{};
132 calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);
133
134 tflite::PoolParams params{};
135 params.padding_values.height = _padding_height;
136 params.padding_values.width = _padding_width;
141 params.quantized_activation_min = activation_min;
142 params.quantized_activation_max = activation_max;
143
144 tflite::reference_integer_ops::MaxPool(
145 params, getTensorShape(input()), getTensorData<int16_t>(input()), //
146 getTensorShape(output()), getTensorData<int16_t>(output()));
147}
148
149} // namespace kernels
150} // namespace luci_interpreter
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
const Tensor * input() const
Definition MaxPool2D.h:33
MaxPool2D(const Tensor *input, Tensor *output, const Pool2DParams &params)
Definition MaxPool2D.cpp:32
#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