blob: c48cda8b8e3e9b8642cee0b56d2b1f445b6881e5 [file] [log] [blame]
Pablo Tellod75f9e92019-08-23 16:26:26 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Pablo Tellod75f9e92019-08-23 16:26:26 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEROIAlignLayerKernel.h"
Pablo Tellod75f9e92019-08-23 16:26:26 +010025
Pablo Tellod75f9e92019-08-23 16:26:26 +010026#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Window.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31#include "arm_compute/core/utils/misc/Utility.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/AccessWindowStatic.h"
33#include "src/core/CPP/Validate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Pablo Tellod75f9e92019-08-23 16:26:26 +010036
37#include <arm_neon.h>
38
39using namespace arm_compute::misc::shape_calculator;
40
41namespace arm_compute
42{
43namespace
44{
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
Pablo Tellod75f9e92019-08-23 16:26:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
49 ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
Pablo Telloebe2e8c2019-08-23 16:26:26 +010050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32, DataType::F16);
Pablo Tellod75f9e92019-08-23 16:26:26 +010051 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW);
52 ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
53 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
54
55 if(output->total_size() != 0)
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape());
60 }
Pablo Telloebe2e8c2019-08-23 16:26:26 +010061
62 if(input->data_type() == DataType::QASYMM8)
63 {
64 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::QASYMM16);
65
66 const UniformQuantizationInfo rois_qinfo = rois->quantization_info().uniform();
67 ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.scale != 0.125f);
68 ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.offset != 0);
69 }
70 else
71 {
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois);
73 }
74
Pablo Tellod75f9e92019-08-23 16:26:26 +010075 return Status{};
76}
Pablo Tellod75f9e92019-08-23 16:26:26 +010077} // namespace
78
79NEROIAlignLayerKernel::NEROIAlignLayerKernel()
80 : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
81{
82}
83
84void NEROIAlignLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
85{
86 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
87 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
Manuel Bottini16bd6dd2020-06-01 14:40:54 +010088 // Output auto inizialitation if not yet initialized
89 const TensorShape output_shape = compute_roi_align_shape(*input->info(), *rois->info(), pool_info);
90 auto_init_if_empty((*output->info()), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
91 output->info()->set_data_layout(input->info()->data_layout());
92
Pablo Tellod75f9e92019-08-23 16:26:26 +010093 // Configure kernel window
Manuel Bottini16bd6dd2020-06-01 14:40:54 +010094 const unsigned int num_rois = rois->info()->dimension(1);
95 Window window;
96 window.set(Window::DimX, Window::Dimension(0, num_rois));
97 window.set(Window::DimY, Window::Dimension(0, 1));
98
99 Coordinates coord;
100 coord.set_num_dimensions(output->info()->num_dimensions());
101 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
Pablo Tellod75f9e92019-08-23 16:26:26 +0100102
103 // Set instance variables
104 _input = input;
105 _rois = rois;
106 _output = output;
107 _pool_info = pool_info;
108
Manuel Bottini16bd6dd2020-06-01 14:40:54 +0100109 INEKernel::configure(window);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100110}
111
112Status NEROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
113{
114 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
115 return Status{};
116}
117
118/** Average pooling over an aligned window */
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100119template <typename input_data_type, DataLayout data_layout>
120inline input_data_type roi_align_1x1(const ITensor *input,
121 unsigned int roi_batch,
122 float region_start_x,
123 float bin_size_x,
124 int grid_size_x,
125 float region_end_x,
126 float region_start_y,
127 float bin_size_y,
128 int grid_size_y,
129 float region_end_y,
130 int pz)
Pablo Tellod75f9e92019-08-23 16:26:26 +0100131{
132 if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
133 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100134 return input_data_type(0);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100135 }
136 else
137 {
138 float avg = 0;
139 // Iterate through the aligned pooling region
140 for(int iy = 0; iy < grid_size_y; ++iy)
141 {
142 for(int ix = 0; ix < grid_size_x; ++ix)
143 {
144 // Align the window in the middle of every bin
145 float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y);
146 float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x);
147
148 // Interpolation in the [0,0] [0,1] [1,0] [1,1] square
149 const int y_low = y;
150 const int x_low = x;
151 const int y_high = y_low + 1;
152 const int x_high = x_low + 1;
153
154 const float ly = y - y_low;
155 const float lx = x - x_low;
156 const float hy = 1. - ly;
157 const float hx = 1. - lx;
158
159 const float w1 = hy * hx;
160 const float w2 = hy * lx;
161 const float w3 = ly * hx;
162 const float w4 = ly * lx;
163 if(data_layout == DataLayout::NCHW)
164 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100165 const auto data1 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch)));
166 const auto data2 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch)));
167 const auto data3 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch)));
168 const auto data4 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_high, pz, roi_batch)));
Pablo Tellod75f9e92019-08-23 16:26:26 +0100169 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
170 }
171 else
172 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100173 const auto data1 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_low, roi_batch)));
174 const auto data2 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_low, roi_batch)));
175 const auto data3 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_high, roi_batch)));
176 const auto data4 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_high, roi_batch)));
177 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
178 }
179 }
180 }
181
182 avg /= grid_size_x * grid_size_y;
183 return input_data_type(avg);
184 }
185}
186
187/** Average pooling over an aligned window */
188template <typename input_data_type, DataLayout data_layout>
189inline input_data_type roi_align_1x1_qasymm8(const ITensor *input,
190 unsigned int roi_batch,
191 float region_start_x,
192 float bin_size_x,
193 int grid_size_x,
194 float region_end_x,
195 float region_start_y,
196 float bin_size_y,
197 int grid_size_y,
198 float region_end_y,
199 int pz,
200 const QuantizationInfo &out_qinfo)
201{
202 if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
203 {
204 return input_data_type(out_qinfo.uniform().offset);
205 }
206 else
207 {
208 float avg = 0;
209 const UniformQuantizationInfo input_qinfo = input->info()->quantization_info().uniform();
210 // Iterate through the aligned pooling region
211 for(int iy = 0; iy < grid_size_y; ++iy)
212 {
213 for(int ix = 0; ix < grid_size_x; ++ix)
214 {
215 // Align the window in the middle of every bin
216 float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y);
217 float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x);
218
219 // Interpolation in the [0,0] [0,1] [1,0] [1,1] square
220 const int y_low = y;
221 const int x_low = x;
222 const int y_high = y_low + 1;
223 const int x_high = x_low + 1;
224
225 const float ly = y - y_low;
226 const float lx = x - x_low;
227 const float hy = 1. - ly;
228 const float hx = 1. - lx;
229
230 const float w1 = hy * hx;
231 const float w2 = hy * lx;
232 const float w3 = ly * hx;
233 const float w4 = ly * lx;
234
235 if(data_layout == DataLayout::NCHW)
236 {
237 float data1 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch))), input_qinfo);
238 float data2 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch))), input_qinfo);
239 float data3 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch))), input_qinfo);
240 float data4 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_high, pz, roi_batch))), input_qinfo);
241 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
242 }
243 else
244 {
245 const auto data1 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_low, roi_batch))), input_qinfo);
246 const auto data2 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_low, roi_batch))), input_qinfo);
247 const auto data3 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_high, roi_batch))), input_qinfo);
248 const auto data4 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_high, roi_batch))), input_qinfo);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100249 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
250 }
251 }
252 }
253
254 avg /= grid_size_x * grid_size_y;
255
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100256 return quantize_qasymm8(avg, out_qinfo);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100257 }
258}
259
260inline float compute_region_coordinate(int p, float bin_size, float roi_anchor, float max_value)
261{
262 const float region_start = p * bin_size + roi_anchor;
263 return utility::clamp(region_start, 0.0f, max_value);
264}
265
266void NEROIAlignLayerKernel::run(const Window &window, const ThreadInfo &info)
267{
268 if(_input->info()->data_layout() == DataLayout::NCHW)
269 {
270 switch(_input->info()->data_type())
271 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100272 case DataType::QASYMM8:
273 {
274 NEROIAlignLayerKernel::internal_run<DataLayout::NCHW, uint8_t, uint16_t>(window, info);
275 break;
276 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100277 case DataType::F32:
278 {
279 NEROIAlignLayerKernel::internal_run<DataLayout::NCHW, float>(window, info);
280 break;
281 }
282#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
283 case DataType::F16:
284 {
285 NEROIAlignLayerKernel::internal_run<DataLayout::NCHW, float16_t>(window, info);
286 break;
287 }
288#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
289 default:
290 {
291 ARM_COMPUTE_ERROR("DataType not supported");
292 break;
293 }
294 }
295 }
296 else if(_input->info()->data_layout() == DataLayout::NHWC)
297 {
298 switch(_input->info()->data_type())
299 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100300 case DataType::QASYMM8:
301 {
302 NEROIAlignLayerKernel::internal_run<DataLayout::NHWC, uint8_t, uint16_t>(window, info);
303 break;
304 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100305 case DataType::F32:
306 {
307 NEROIAlignLayerKernel::internal_run<DataLayout::NHWC, float>(window, info);
308 break;
309 }
310#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
311 case DataType::F16:
312 {
313 NEROIAlignLayerKernel::internal_run<DataLayout::NHWC, float16_t>(window, info);
314 break;
315 }
316#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
317 default:
318 {
319 ARM_COMPUTE_ERROR("DataType not supported");
320 break;
321 }
322 }
323 }
324 else
325 {
326 ARM_COMPUTE_ERROR("Invalid layout");
327 }
328}
329
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100330template <DataLayout data_layout, typename input_data_type, typename roi_data_type>
Pablo Tellod75f9e92019-08-23 16:26:26 +0100331void NEROIAlignLayerKernel::internal_run(const Window &window, const ThreadInfo &info)
332{
333 ARM_COMPUTE_UNUSED(info);
334 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
335 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
336
337 const size_t values_per_roi = _rois->info()->dimension(0);
338
339 const int roi_list_start = window.x().start();
340 const int roi_list_end = window.x().end();
341
342 const unsigned int idx_width = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::WIDTH);
343 const unsigned int idx_height = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::HEIGHT);
344 const unsigned int idx_depth = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL);
345
346 const int input_width = _input->info()->dimension(idx_width);
347 const int input_height = _input->info()->dimension(idx_height);
348 const int input_chanels = _input->info()->dimension(idx_depth);
349 const int pooled_w = _pool_info.pooled_width();
350 const int pooled_h = _pool_info.pooled_height();
351
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100352 const DataType data_type = _input->info()->data_type();
353 const bool is_qasymm = is_data_type_quantized_asymmetric(data_type);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100354
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100355 const auto *rois_ptr = reinterpret_cast<const roi_data_type *>(_rois->buffer());
356 const QuantizationInfo &rois_qinfo = _rois->info()->quantization_info();
Pablo Tellod75f9e92019-08-23 16:26:26 +0100357 for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx)
358 {
359 const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx];
Pablo Tellod75f9e92019-08-23 16:26:26 +0100360
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100361 roi_data_type qx1 = rois_ptr[values_per_roi * roi_indx + 1];
362 roi_data_type qy1 = rois_ptr[values_per_roi * roi_indx + 2];
363 roi_data_type qx2 = rois_ptr[values_per_roi * roi_indx + 3];
364 roi_data_type qy2 = rois_ptr[values_per_roi * roi_indx + 4];
365 float x1(qx1);
366 float x2(qx2);
367 float y1(qy1);
368 float y2(qy2);
369 if(is_qasymm)
370 {
371 x1 = dequantize_qasymm16(qx1, rois_qinfo);
372 x2 = dequantize_qasymm16(qx2, rois_qinfo);
373 y1 = dequantize_qasymm16(qy1, rois_qinfo);
374 y2 = dequantize_qasymm16(qy2, rois_qinfo);
375 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100376 const float roi_anchor_x = x1 * _pool_info.spatial_scale();
377 const float roi_anchor_y = y1 * _pool_info.spatial_scale();
378 const float roi_dims_x = std::max((x2 - x1) * _pool_info.spatial_scale(), 1.0f);
379 const float roi_dims_y = std::max((y2 - y1) * _pool_info.spatial_scale(), 1.0f);
380 float bin_size_x = roi_dims_x / _pool_info.pooled_width();
381 float bin_size_y = roi_dims_y / _pool_info.pooled_height();
382
383 // Iterate through all feature maps
384 for(int ch = 0; ch < input_chanels; ++ch)
385 {
386 // Iterate through all output pixels
387 for(int py = 0; py < pooled_h; ++py)
388 {
389 for(int px = 0; px < pooled_w; ++px)
390 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100391 const float region_start_x = compute_region_coordinate(px, bin_size_x, roi_anchor_x, input_width);
392 const float region_start_y = compute_region_coordinate(py, bin_size_y, roi_anchor_y, input_height);
393 const float region_end_x = compute_region_coordinate(px + 1, bin_size_x, roi_anchor_x, input_width);
394 const float region_end_y = compute_region_coordinate(py + 1, bin_size_y, roi_anchor_y, input_height);
395 const int roi_bin_grid_x = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_x));
396 const int roi_bin_grid_y = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_y));
397 input_data_type out_val(0);
398 if(is_qasymm)
399 {
400 out_val = roi_align_1x1_qasymm8<input_data_type, data_layout>(
401 _input, roi_batch, region_start_x, bin_size_x,
402 roi_bin_grid_x, region_end_x, region_start_y, bin_size_y,
403 roi_bin_grid_y, region_end_y, ch, _output->info()->quantization_info());
404 }
405 else
406 {
407 out_val = roi_align_1x1<input_data_type, data_layout>(
408 _input, roi_batch, region_start_x, bin_size_x,
409 roi_bin_grid_x, region_end_x, region_start_y, bin_size_y,
410 roi_bin_grid_y, region_end_y, ch);
411 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100412
413 if(data_layout == DataLayout::NCHW)
414 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100415 auto out_ptr = reinterpret_cast<input_data_type *>(_output->ptr_to_element(Coordinates(px, py, ch, roi_indx)));
Pablo Tellod75f9e92019-08-23 16:26:26 +0100416 *out_ptr = out_val;
417 }
418 else
419 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100420 auto out_ptr = reinterpret_cast<input_data_type *>(_output->ptr_to_element(Coordinates(ch, px, py, roi_indx)));
Pablo Tellod75f9e92019-08-23 16:26:26 +0100421 *out_ptr = out_val;
422 }
423 }
424 }
425 }
426 }
427}
428} // namespace arm_compute