blob: 955cdc2074038062be24bfd5fe085b24aa0d8fad [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 */
24#include "arm_compute/core/NEON/kernels/NEROIAlignLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CPP/Validate.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Window.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
33#include "arm_compute/core/utils/misc/Utility.h"
34
35#include <arm_neon.h>
36
37using namespace arm_compute::misc::shape_calculator;
38
39namespace arm_compute
40{
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
Pablo Tellod75f9e92019-08-23 16:26:26 +010046 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
47 ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
Pablo Telloebe2e8c2019-08-23 16:26:26 +010048 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 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW);
50 ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
51 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
52
53 if(output->total_size() != 0)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape());
58 }
Pablo Telloebe2e8c2019-08-23 16:26:26 +010059
60 if(input->data_type() == DataType::QASYMM8)
61 {
62 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::QASYMM16);
63
64 const UniformQuantizationInfo rois_qinfo = rois->quantization_info().uniform();
65 ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.scale != 0.125f);
66 ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.offset != 0);
67 }
68 else
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois);
71 }
72
Pablo Tellod75f9e92019-08-23 16:26:26 +010073 return Status{};
74}
Pablo Tellod75f9e92019-08-23 16:26:26 +010075} // namespace
76
77NEROIAlignLayerKernel::NEROIAlignLayerKernel()
78 : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
79{
80}
81
82void NEROIAlignLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
83{
84 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
85 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
Manuel Bottini16bd6dd2020-06-01 14:40:54 +010086 // Output auto inizialitation if not yet initialized
87 const TensorShape output_shape = compute_roi_align_shape(*input->info(), *rois->info(), pool_info);
88 auto_init_if_empty((*output->info()), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
89 output->info()->set_data_layout(input->info()->data_layout());
90
Pablo Tellod75f9e92019-08-23 16:26:26 +010091 // Configure kernel window
Manuel Bottini16bd6dd2020-06-01 14:40:54 +010092 const unsigned int num_rois = rois->info()->dimension(1);
93 Window window;
94 window.set(Window::DimX, Window::Dimension(0, num_rois));
95 window.set(Window::DimY, Window::Dimension(0, 1));
96
97 Coordinates coord;
98 coord.set_num_dimensions(output->info()->num_dimensions());
99 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
Pablo Tellod75f9e92019-08-23 16:26:26 +0100100
101 // Set instance variables
102 _input = input;
103 _rois = rois;
104 _output = output;
105 _pool_info = pool_info;
106
Manuel Bottini16bd6dd2020-06-01 14:40:54 +0100107 INEKernel::configure(window);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100108}
109
110Status NEROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
111{
112 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
113 return Status{};
114}
115
116/** Average pooling over an aligned window */
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100117template <typename input_data_type, DataLayout data_layout>
118inline input_data_type roi_align_1x1(const ITensor *input,
119 unsigned int roi_batch,
120 float region_start_x,
121 float bin_size_x,
122 int grid_size_x,
123 float region_end_x,
124 float region_start_y,
125 float bin_size_y,
126 int grid_size_y,
127 float region_end_y,
128 int pz)
Pablo Tellod75f9e92019-08-23 16:26:26 +0100129{
130 if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
131 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100132 return input_data_type(0);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100133 }
134 else
135 {
136 float avg = 0;
137 // Iterate through the aligned pooling region
138 for(int iy = 0; iy < grid_size_y; ++iy)
139 {
140 for(int ix = 0; ix < grid_size_x; ++ix)
141 {
142 // Align the window in the middle of every bin
143 float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y);
144 float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x);
145
146 // Interpolation in the [0,0] [0,1] [1,0] [1,1] square
147 const int y_low = y;
148 const int x_low = x;
149 const int y_high = y_low + 1;
150 const int x_high = x_low + 1;
151
152 const float ly = y - y_low;
153 const float lx = x - x_low;
154 const float hy = 1. - ly;
155 const float hx = 1. - lx;
156
157 const float w1 = hy * hx;
158 const float w2 = hy * lx;
159 const float w3 = ly * hx;
160 const float w4 = ly * lx;
161 if(data_layout == DataLayout::NCHW)
162 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100163 const auto data1 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch)));
164 const auto data2 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch)));
165 const auto data3 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch)));
166 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 +0100167 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
168 }
169 else
170 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100171 const auto data1 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_low, roi_batch)));
172 const auto data2 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_low, roi_batch)));
173 const auto data3 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_high, roi_batch)));
174 const auto data4 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_high, roi_batch)));
175 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
176 }
177 }
178 }
179
180 avg /= grid_size_x * grid_size_y;
181 return input_data_type(avg);
182 }
183}
184
185/** Average pooling over an aligned window */
186template <typename input_data_type, DataLayout data_layout>
187inline input_data_type roi_align_1x1_qasymm8(const ITensor *input,
188 unsigned int roi_batch,
189 float region_start_x,
190 float bin_size_x,
191 int grid_size_x,
192 float region_end_x,
193 float region_start_y,
194 float bin_size_y,
195 int grid_size_y,
196 float region_end_y,
197 int pz,
198 const QuantizationInfo &out_qinfo)
199{
200 if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
201 {
202 return input_data_type(out_qinfo.uniform().offset);
203 }
204 else
205 {
206 float avg = 0;
207 const UniformQuantizationInfo input_qinfo = input->info()->quantization_info().uniform();
208 // Iterate through the aligned pooling region
209 for(int iy = 0; iy < grid_size_y; ++iy)
210 {
211 for(int ix = 0; ix < grid_size_x; ++ix)
212 {
213 // Align the window in the middle of every bin
214 float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y);
215 float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x);
216
217 // Interpolation in the [0,0] [0,1] [1,0] [1,1] square
218 const int y_low = y;
219 const int x_low = x;
220 const int y_high = y_low + 1;
221 const int x_high = x_low + 1;
222
223 const float ly = y - y_low;
224 const float lx = x - x_low;
225 const float hy = 1. - ly;
226 const float hx = 1. - lx;
227
228 const float w1 = hy * hx;
229 const float w2 = hy * lx;
230 const float w3 = ly * hx;
231 const float w4 = ly * lx;
232
233 if(data_layout == DataLayout::NCHW)
234 {
235 float data1 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch))), input_qinfo);
236 float data2 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch))), input_qinfo);
237 float data3 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch))), input_qinfo);
238 float data4 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_high, pz, roi_batch))), input_qinfo);
239 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
240 }
241 else
242 {
243 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);
244 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);
245 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);
246 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 +0100247 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
248 }
249 }
250 }
251
252 avg /= grid_size_x * grid_size_y;
253
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100254 return quantize_qasymm8(avg, out_qinfo);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100255 }
256}
257
258inline float compute_region_coordinate(int p, float bin_size, float roi_anchor, float max_value)
259{
260 const float region_start = p * bin_size + roi_anchor;
261 return utility::clamp(region_start, 0.0f, max_value);
262}
263
264void NEROIAlignLayerKernel::run(const Window &window, const ThreadInfo &info)
265{
266 if(_input->info()->data_layout() == DataLayout::NCHW)
267 {
268 switch(_input->info()->data_type())
269 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100270 case DataType::QASYMM8:
271 {
272 NEROIAlignLayerKernel::internal_run<DataLayout::NCHW, uint8_t, uint16_t>(window, info);
273 break;
274 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100275 case DataType::F32:
276 {
277 NEROIAlignLayerKernel::internal_run<DataLayout::NCHW, float>(window, info);
278 break;
279 }
280#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
281 case DataType::F16:
282 {
283 NEROIAlignLayerKernel::internal_run<DataLayout::NCHW, float16_t>(window, info);
284 break;
285 }
286#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
287 default:
288 {
289 ARM_COMPUTE_ERROR("DataType not supported");
290 break;
291 }
292 }
293 }
294 else if(_input->info()->data_layout() == DataLayout::NHWC)
295 {
296 switch(_input->info()->data_type())
297 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100298 case DataType::QASYMM8:
299 {
300 NEROIAlignLayerKernel::internal_run<DataLayout::NHWC, uint8_t, uint16_t>(window, info);
301 break;
302 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100303 case DataType::F32:
304 {
305 NEROIAlignLayerKernel::internal_run<DataLayout::NHWC, float>(window, info);
306 break;
307 }
308#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
309 case DataType::F16:
310 {
311 NEROIAlignLayerKernel::internal_run<DataLayout::NHWC, float16_t>(window, info);
312 break;
313 }
314#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
315 default:
316 {
317 ARM_COMPUTE_ERROR("DataType not supported");
318 break;
319 }
320 }
321 }
322 else
323 {
324 ARM_COMPUTE_ERROR("Invalid layout");
325 }
326}
327
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100328template <DataLayout data_layout, typename input_data_type, typename roi_data_type>
Pablo Tellod75f9e92019-08-23 16:26:26 +0100329void NEROIAlignLayerKernel::internal_run(const Window &window, const ThreadInfo &info)
330{
331 ARM_COMPUTE_UNUSED(info);
332 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
333 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
334
335 const size_t values_per_roi = _rois->info()->dimension(0);
336
337 const int roi_list_start = window.x().start();
338 const int roi_list_end = window.x().end();
339
340 const unsigned int idx_width = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::WIDTH);
341 const unsigned int idx_height = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::HEIGHT);
342 const unsigned int idx_depth = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL);
343
344 const int input_width = _input->info()->dimension(idx_width);
345 const int input_height = _input->info()->dimension(idx_height);
346 const int input_chanels = _input->info()->dimension(idx_depth);
347 const int pooled_w = _pool_info.pooled_width();
348 const int pooled_h = _pool_info.pooled_height();
349
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100350 const DataType data_type = _input->info()->data_type();
351 const bool is_qasymm = is_data_type_quantized_asymmetric(data_type);
Pablo Tellod75f9e92019-08-23 16:26:26 +0100352
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100353 const auto *rois_ptr = reinterpret_cast<const roi_data_type *>(_rois->buffer());
354 const QuantizationInfo &rois_qinfo = _rois->info()->quantization_info();
Pablo Tellod75f9e92019-08-23 16:26:26 +0100355 for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx)
356 {
357 const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx];
Pablo Tellod75f9e92019-08-23 16:26:26 +0100358
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100359 roi_data_type qx1 = rois_ptr[values_per_roi * roi_indx + 1];
360 roi_data_type qy1 = rois_ptr[values_per_roi * roi_indx + 2];
361 roi_data_type qx2 = rois_ptr[values_per_roi * roi_indx + 3];
362 roi_data_type qy2 = rois_ptr[values_per_roi * roi_indx + 4];
363 float x1(qx1);
364 float x2(qx2);
365 float y1(qy1);
366 float y2(qy2);
367 if(is_qasymm)
368 {
369 x1 = dequantize_qasymm16(qx1, rois_qinfo);
370 x2 = dequantize_qasymm16(qx2, rois_qinfo);
371 y1 = dequantize_qasymm16(qy1, rois_qinfo);
372 y2 = dequantize_qasymm16(qy2, rois_qinfo);
373 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100374 const float roi_anchor_x = x1 * _pool_info.spatial_scale();
375 const float roi_anchor_y = y1 * _pool_info.spatial_scale();
376 const float roi_dims_x = std::max((x2 - x1) * _pool_info.spatial_scale(), 1.0f);
377 const float roi_dims_y = std::max((y2 - y1) * _pool_info.spatial_scale(), 1.0f);
378 float bin_size_x = roi_dims_x / _pool_info.pooled_width();
379 float bin_size_y = roi_dims_y / _pool_info.pooled_height();
380
381 // Iterate through all feature maps
382 for(int ch = 0; ch < input_chanels; ++ch)
383 {
384 // Iterate through all output pixels
385 for(int py = 0; py < pooled_h; ++py)
386 {
387 for(int px = 0; px < pooled_w; ++px)
388 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100389 const float region_start_x = compute_region_coordinate(px, bin_size_x, roi_anchor_x, input_width);
390 const float region_start_y = compute_region_coordinate(py, bin_size_y, roi_anchor_y, input_height);
391 const float region_end_x = compute_region_coordinate(px + 1, bin_size_x, roi_anchor_x, input_width);
392 const float region_end_y = compute_region_coordinate(py + 1, bin_size_y, roi_anchor_y, input_height);
393 const int roi_bin_grid_x = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_x));
394 const int roi_bin_grid_y = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_y));
395 input_data_type out_val(0);
396 if(is_qasymm)
397 {
398 out_val = roi_align_1x1_qasymm8<input_data_type, data_layout>(
399 _input, roi_batch, region_start_x, bin_size_x,
400 roi_bin_grid_x, region_end_x, region_start_y, bin_size_y,
401 roi_bin_grid_y, region_end_y, ch, _output->info()->quantization_info());
402 }
403 else
404 {
405 out_val = roi_align_1x1<input_data_type, data_layout>(
406 _input, roi_batch, region_start_x, bin_size_x,
407 roi_bin_grid_x, region_end_x, region_start_y, bin_size_y,
408 roi_bin_grid_y, region_end_y, ch);
409 }
Pablo Tellod75f9e92019-08-23 16:26:26 +0100410
411 if(data_layout == DataLayout::NCHW)
412 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100413 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 +0100414 *out_ptr = out_val;
415 }
416 else
417 {
Pablo Telloebe2e8c2019-08-23 16:26:26 +0100418 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 +0100419 *out_ptr = out_val;
420 }
421 }
422 }
423 }
424 }
425}
426} // namespace arm_compute