2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef __NNFW_CKER_AVERAGE_POOL_H__
19 #define __NNFW_CKER_AVERAGE_POOL_H__
21 #include "cker/neon/neon_check.h"
22 #include "cker/eigen/Utils.h"
23 #include "cker/Shape.h"
24 #include "cker/Types.h"
25 #include "cker/Utils.h"
34 // TODO Change to apply neon for this function if it is faster
36 void AveragePool(const PoolParams &, const Shape &, const T *, const Shape &, T *)
38 static_assert(std::is_integral<T>::value || std::is_floating_point<T>::value,
39 "cker::MaxPool : This function supports only integer or floating point");
40 throw std::runtime_error("cker::AveragePool : Unsupported data type");
44 void AveragePool<float>(const PoolParams ¶ms, const Shape &input_shape, const float *input_data,
45 const Shape &output_shape, float *output_data)
47 assert(input_shape.DimensionsCount() == 4);
48 assert(output_shape.DimensionsCount() == 4);
49 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
50 const int input_height = input_shape.Dims(1);
51 const int input_width = input_shape.Dims(2);
52 const int output_height = output_shape.Dims(1);
53 const int output_width = output_shape.Dims(2);
54 const int stride_height = params.stride_height;
55 const int stride_width = params.stride_width;
57 // TODO(benoitjacob) make this a proper reference impl without Eigen!
58 const auto in_mat = MapAsMatrixWithLastDimAsRows(input_data, input_shape);
59 auto out_mat = MapAsMatrixWithLastDimAsRows(output_data, output_shape);
60 // TODO(benoitjacob) get rid of the dynamic memory allocation here!
61 Eigen::VectorXf out_count(out_mat.cols());
63 // Prefill the output to 0.
65 for (int b = 0; b < batches; ++b)
67 for (int h = 0; h < input_height; ++h)
69 for (int w = 0; w < input_width; ++w)
71 // (h_start, h_end) * (w_start, w_end) is the range that the input
72 // vector projects to.
73 int hpad = h + params.padding_values.height;
74 int wpad = w + params.padding_values.width;
76 (hpad < params.filter_height) ? 0 : (hpad - params.filter_height) / stride_height + 1;
77 int h_end = std::min(hpad / stride_height + 1, output_height);
79 (wpad < params.filter_width) ? 0 : (wpad - params.filter_width) / stride_width + 1;
80 int w_end = std::min(wpad / stride_width + 1, output_width);
81 // compute elementwise sum
82 for (int ph = h_start; ph < h_end; ++ph)
84 for (int pw = w_start; pw < w_end; ++pw)
86 int out_offset = NodeOffset(b, ph, pw, output_height, output_width);
87 out_mat.col(out_offset) += in_mat.col(NodeOffset(b, h, w, input_height, input_width));
88 out_count(out_offset)++;
94 // Divide the output by the actual number of elements being averaged over
95 assert(out_count.minCoeff() > 0);
96 out_mat.array().rowwise() /= out_count.transpose().array();
98 const int flat_size = output_shape.FlatSize();
99 for (int i = 0; i < flat_size; ++i)
101 output_data[i] = ActivationFunctionWithMinMax(output_data[i], params.float_activation_min,
102 params.float_activation_max);
106 inline void AveragePool16(const PoolParams ¶ms, const Shape &input_shape,
107 const uint8_t *input_data, const Shape &output_shape,
108 uint8_t *output_data)
110 // Here, and in other pooling ops, in order to maintain locality of reference,
111 // to minimize some recalculations, and to load into NEON vector registers, we
112 // use an inner loop down the depth. Since depths can be large and hence we
113 // would need arbitrarily large temporary storage, we divide the work up into
114 // depth tranches just within the batch loop.
115 static constexpr int kPoolingAccTrancheSize = 256;
117 assert(params.quantized_activation_min <= params.quantized_activation_max);
118 assert(input_shape.DimensionsCount() == 4);
119 assert(output_shape.DimensionsCount() == 4);
120 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
121 const int depth = MatchingDim(input_shape, 3, output_shape, 3);
122 const int input_height = input_shape.Dims(1);
123 const int input_width = input_shape.Dims(2);
124 const int output_height = output_shape.Dims(1);
125 const int output_width = output_shape.Dims(2);
126 const int stride_height = params.stride_height;
127 const int stride_width = params.stride_width;
129 uint16_t acc[kPoolingAccTrancheSize];
130 for (int batch = 0; batch < batches; ++batch)
132 // We proceed through the depth in tranches (see comment above). The
133 // depth_base is the depth at the beginning of the tranche. The
134 // tranche_depth is the depth dimension of the tranche.
135 for (int depth_base = 0; depth_base < depth; depth_base += kPoolingAccTrancheSize)
137 const int tranche_depth = std::min(depth - depth_base, kPoolingAccTrancheSize);
138 for (int out_y = 0; out_y < output_height; ++out_y)
140 for (int out_x = 0; out_x < output_width; ++out_x)
142 const int in_x_origin = (out_x * stride_width) - params.padding_values.width;
143 const int in_y_origin = (out_y * stride_height) - params.padding_values.height;
144 const int filter_x_start = std::max(0, -in_x_origin);
145 const int filter_x_end = std::min(params.filter_width, input_width - in_x_origin);
146 const int filter_y_start = std::max(0, -in_y_origin);
147 const int filter_y_end = std::min(params.filter_height, input_height - in_y_origin);
148 const int filter_count =
149 (filter_x_end - filter_x_start) * (filter_y_end - filter_y_start);
150 memset(acc, 0, tranche_depth * sizeof(acc[0]));
151 const uint8_t *input_ptr =
152 input_data + depth_base +
153 depth * (in_x_origin + input_width * (in_y_origin + input_height * batch));
154 for (int fy = filter_y_start; fy < filter_y_end; fy++)
156 const uint8_t *input_row_ptr = input_ptr + depth * (fy * input_width + filter_x_start);
157 for (int fx = filter_x_start; fx < filter_x_end; fx++)
159 const uint8_t *input_channel_ptr = input_row_ptr;
162 for (; channel <= tranche_depth - 16; channel += 16)
164 uint16x8_t acc_reg[2];
165 for (int i = 0; i < 2; i++)
167 acc_reg[i] = vld1q_u16(acc + channel + 8 * i);
169 uint8x16_t input_reg = vld1q_u8(input_channel_ptr);
170 input_channel_ptr += 16;
171 acc_reg[0] = vaddw_u8(acc_reg[0], vget_low_u8(input_reg));
172 acc_reg[1] = vaddw_u8(acc_reg[1], vget_high_u8(input_reg));
173 for (int i = 0; i < 2; i++)
175 vst1q_u16(acc + channel + 8 * i, acc_reg[i]);
178 for (; channel <= tranche_depth - 8; channel += 8)
180 uint16x8_t acc_reg = vld1q_u16(acc + channel);
181 uint8x8_t input_reg = vld1_u8(input_channel_ptr);
182 input_channel_ptr += 8;
183 acc_reg = vaddw_u8(acc_reg, input_reg);
184 vst1q_u16(acc + channel, acc_reg);
187 for (; channel < tranche_depth; ++channel)
189 acc[channel] += *input_channel_ptr++;
191 input_row_ptr += depth;
194 uint8_t *output_ptr = output_data + Offset(output_shape, batch, out_y, out_x, depth_base);
197 #define AVGPOOL_DIVIDING_BY(FILTER_COUNT) \
198 if (filter_count == FILTER_COUNT) \
200 for (; channel <= tranche_depth - 8; channel += 8) \
203 for (int i = 0; i < 8; i++) \
205 buf[i] = (acc[channel + i] + FILTER_COUNT / 2) / FILTER_COUNT; \
207 uint8x8_t buf8 = vqmovn_u16(vld1q_u16(buf)); \
208 buf8 = vmin_u8(buf8, vdup_n_u8(params.quantized_activation_max)); \
209 buf8 = vmax_u8(buf8, vdup_n_u8(params.quantized_activation_min)); \
210 vst1_u8(output_ptr + channel, buf8); \
213 AVGPOOL_DIVIDING_BY(9)
214 AVGPOOL_DIVIDING_BY(15)
215 #undef AVGPOOL_DIVIDING_BY
216 for (; channel <= tranche_depth - 8; channel += 8)
219 for (int i = 0; i < 8; i++)
221 buf[i] = (acc[channel + i] + filter_count / 2) / filter_count;
223 uint8x8_t buf8 = vqmovn_u16(vld1q_u16(buf));
224 buf8 = vmin_u8(buf8, vdup_n_u8(params.quantized_activation_max));
225 buf8 = vmax_u8(buf8, vdup_n_u8(params.quantized_activation_min));
226 vst1_u8(output_ptr + channel, buf8);
229 for (; channel < tranche_depth; ++channel)
231 uint8_t a = (acc[channel] + filter_count / 2) / filter_count;
232 a = std::max<uint16_t>(a, params.quantized_activation_min);
233 a = std::min<uint16_t>(a, params.quantized_activation_max);
234 output_ptr[channel] = static_cast<uint8_t>(a);
242 inline void AveragePool32(const PoolParams ¶ms, const Shape &input_shape,
243 const uint8_t *input_data, const Shape &output_shape,
244 uint8_t *output_data)
247 // Here, and in other pooling ops, in order to maintain locality of reference,
248 // to minimize some recalculations, and to load into NEON vector registers, we
249 // use an inner loop down the depth. Since depths can be large and hence we
250 // would need arbitrarily large temporary storage, we divide the work up into
251 // depth tranches just within the batch loop.
252 static constexpr int kPoolingAccTrancheSize = 256;
254 assert(params.quantized_activation_min <= params.quantized_activation_max);
255 assert(input_shape.DimensionsCount() == 4);
256 assert(output_shape.DimensionsCount() == 4);
257 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
258 const int depth = MatchingDim(input_shape, 3, output_shape, 3);
259 const int input_height = input_shape.Dims(1);
260 const int input_width = input_shape.Dims(2);
261 const int output_height = output_shape.Dims(1);
262 const int output_width = output_shape.Dims(2);
263 const int stride_height = params.stride_height;
264 const int stride_width = params.stride_width;
266 uint32_t acc[kPoolingAccTrancheSize];
267 for (int batch = 0; batch < batches; ++batch)
269 // We proceed through the depth in tranches (see comment above). The
270 // depth_base is the depth at the beginning of the tranche. The
271 // tranche_depth is the depth dimension of the tranche.
272 for (int depth_base = 0; depth_base < depth; depth_base += kPoolingAccTrancheSize)
274 const int tranche_depth = std::min(depth - depth_base, kPoolingAccTrancheSize);
275 for (int out_y = 0; out_y < output_height; ++out_y)
277 for (int out_x = 0; out_x < output_width; ++out_x)
279 const int in_x_origin = (out_x * stride_width) - params.padding_values.width;
280 const int in_y_origin = (out_y * stride_height) - params.padding_values.height;
281 const int filter_x_start = std::max(0, -in_x_origin);
282 const int filter_x_end = std::min(params.filter_width, input_width - in_x_origin);
283 const int filter_y_start = std::max(0, -in_y_origin);
284 const int filter_y_end = std::min(params.filter_height, input_height - in_y_origin);
285 const int filter_count =
286 (filter_x_end - filter_x_start) * (filter_y_end - filter_y_start);
287 memset(acc, 0, tranche_depth * sizeof(acc[0]));
288 const uint8_t *input_ptr =
289 input_data + depth_base +
290 depth * (in_x_origin + input_width * (in_y_origin + input_height * batch));
291 for (int fy = filter_y_start; fy < filter_y_end; fy++)
293 const uint8_t *input_row_ptr = input_ptr + depth * (fy * input_width + filter_x_start);
294 for (int fx = filter_x_start; fx < filter_x_end; fx++)
296 const uint8_t *input_channel_ptr = input_row_ptr;
299 for (; channel <= tranche_depth - 16; channel += 16)
301 uint16x4_t acc_reg[4];
302 uint8x16_t input_reg = vld1q_u8(input_channel_ptr);
303 input_channel_ptr += 16;
304 acc_reg[0] = vget_low_u16(vmovl_u8(vget_low_u8(input_reg)));
305 acc_reg[1] = vget_high_u16(vmovl_u8(vget_low_u8(input_reg)));
306 acc_reg[2] = vget_low_u16(vmovl_u8(vget_high_u8(input_reg)));
307 acc_reg[3] = vget_high_u16(vmovl_u8(vget_high_u8(input_reg)));
308 for (int i = 0; i < 4; i++)
310 vst1q_u32(acc + channel + 4 * i,
311 vaddw_u16(vld1q_u32(acc + channel + 4 * i), acc_reg[i]));
314 for (; channel <= tranche_depth - 8; channel += 8)
316 uint16x4_t acc_reg[2];
317 uint16x8_t input_reg = vmovl_u8(vld1_u8(input_channel_ptr));
318 input_channel_ptr += 8;
319 acc_reg[0] = vget_low_u16(input_reg);
320 acc_reg[1] = vget_high_u16(input_reg);
321 for (int i = 0; i < 2; i++)
323 vst1q_u32(acc + channel + 4 * i,
324 vaddw_u16(vld1q_u32(acc + channel + 4 * i), acc_reg[i]));
328 for (; channel < tranche_depth; ++channel)
330 acc[channel] += *input_channel_ptr++;
332 input_row_ptr += depth;
335 uint8_t *output_ptr = output_data + Offset(output_shape, batch, out_y, out_x, depth_base);
338 #define AVGPOOL_DIVIDING_BY(FILTER_COUNT) \
339 if (filter_count == FILTER_COUNT) \
341 for (; channel <= tranche_depth - 8; channel += 8) \
344 for (int i = 0; i < 8; i++) \
346 buf[i] = (acc[channel + i] + FILTER_COUNT / 2) / FILTER_COUNT; \
348 uint8x8_t buf8 = vqmovn_u16(vld1q_u16(buf)); \
349 buf8 = vmin_u8(buf8, vdup_n_u8(params.quantized_activation_max)); \
350 buf8 = vmax_u8(buf8, vdup_n_u8(params.quantized_activation_min)); \
351 vst1_u8(output_ptr + channel, buf8); \
354 AVGPOOL_DIVIDING_BY(9)
355 AVGPOOL_DIVIDING_BY(15)
356 #undef AVGPOOL_DIVIDING_BY
357 for (; channel <= tranche_depth - 8; channel += 8)
360 for (int i = 0; i < 8; i++)
362 buf[i] = (acc[channel + i] + filter_count / 2) / filter_count;
364 uint8x8_t buf8 = vqmovn_u16(vld1q_u16(buf));
365 buf8 = vmin_u8(buf8, vdup_n_u8(params.quantized_activation_max));
366 buf8 = vmax_u8(buf8, vdup_n_u8(params.quantized_activation_min));
367 vst1_u8(output_ptr + channel, buf8);
370 for (; channel < tranche_depth; ++channel)
372 uint16_t a = (acc[channel] + filter_count / 2) / filter_count;
373 a = std::max<uint16_t>(a, params.quantized_activation_min);
374 a = std::min<uint16_t>(a, params.quantized_activation_max);
375 output_ptr[channel] = static_cast<uint8_t>(a);
384 void AveragePool<uint8_t>(const PoolParams ¶ms, const Shape &input_shape,
385 const uint8_t *input_data, const Shape &output_shape,
386 uint8_t *output_data)
388 if (params.filter_height * params.filter_width > 16 * 16)
390 AveragePool32(params, input_shape, input_data, output_shape, output_data);
394 AveragePool16(params, input_shape, input_data, output_shape, output_data);
401 #endif // __NNFW_CKER_AVERAGE_POOL_H__