9c22c1c867b28296add796092aacfc3a2886be1a
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / AvgPoolLayer.cc
1 /*
2  * Copyright (c) 2018 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 "AvgPoolLayer.h"
18
19 #include <cker/operation/AveragePool.h>
20
21 namespace onert
22 {
23 namespace backend
24 {
25 namespace cpu
26 {
27 namespace ops
28 {
29
30 #define AVGPOOLING_PARAMETERS                            \
31   nnfw::cker::PoolParams op_params;                      \
32   op_params.stride_height = _strideHeight;               \
33   op_params.stride_width = _strideWidth;                 \
34   op_params.filter_height = _kernelHeight;               \
35   op_params.filter_width = _kernelWidth;                 \
36   op_params.padding_values.height = (int8_t)_paddingTop; \
37   op_params.padding_values.width = (int8_t)_paddingLeft;
38
39 AvgPoolLayer::AvgPoolLayer()
40     : _input(nullptr), _output(nullptr), _paddingLeft(0), _paddingTop(0), _paddingRight(0),
41       _paddingBottom(0), _strideWidth(0), _strideHeight(0), _kernelWidth(0), _kernelHeight(0),
42       _activation(ir::Activation::NONE)
43 {
44   // DO NOTHING
45 }
46
47 void AvgPoolLayer::averagePoolFloat32()
48 {
49   AVGPOOLING_PARAMETERS
50   float output_activation_min = 0, output_activation_max = 0;
51   CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
52   op_params.float_activation_min = output_activation_min;
53   op_params.float_activation_max = output_activation_max;
54
55   nnfw::cker::AveragePool(op_params, getTensorShape(_input),
56                           reinterpret_cast<const float *>(_input->buffer()),
57                           getTensorShape(_output), reinterpret_cast<float *>(_output->buffer()));
58 }
59 void AvgPoolLayer::averagePoolQuant8()
60 {
61   AVGPOOLING_PARAMETERS
62   int32_t output_activation_min = 0;
63   int32_t output_activation_max = 0;
64   CalculateActivationRangeUint8(_activation, _output, &output_activation_min,
65                                 &output_activation_max);
66   op_params.quantized_activation_min = output_activation_min;
67   op_params.quantized_activation_max = output_activation_max;
68
69   nnfw::cker::AveragePool(op_params, getTensorShape(_input),
70                           reinterpret_cast<const uint8_t *>(_input->buffer()),
71                           getTensorShape(_output), reinterpret_cast<uint8_t *>(_output->buffer()));
72 }
73
74 void AvgPoolLayer::configure(const IPortableTensor *input, const uint32_t paddingLeft,
75                              const uint32_t paddingRight, const uint32_t paddingTop,
76                              const uint32_t paddingBottom, const uint32_t strideWidth,
77                              const uint32_t strideHeight, const uint32_t kernelWidth,
78                              const uint32_t kernelHeight, const ir::Activation activation,
79                              IPortableTensor *output)
80 {
81   assert(input != nullptr);
82   assert(output != nullptr);
83
84   _input = input;
85   _paddingLeft = paddingLeft;
86   _paddingRight = paddingRight;
87   _paddingTop = paddingTop;
88   _paddingBottom = paddingBottom;
89   _strideWidth = strideWidth;
90   _strideHeight = strideHeight;
91   _kernelWidth = kernelWidth;
92   _kernelHeight = kernelHeight;
93   _activation = activation;
94   _output = output;
95 }
96
97 void AvgPoolLayer::run()
98 {
99   if (_input->data_type() == OperandType::FLOAT32)
100   {
101     averagePoolFloat32();
102   }
103   else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
104   {
105     averagePoolQuant8();
106   }
107   else
108   {
109     throw std::runtime_error{"AvgPool: unsupported data type"};
110   }
111 }
112
113 #undef AVGPOOLING_PARAMETERS
114
115 } // namespace ops
116 } // namespace cpu
117 } // namespace backend
118 } // namespace onert