• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2020 Arm Limited.
3  *
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 "src/core/NEON/kernels/NEROIAlignLayerKernel.h"
25 
26 #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"
32 #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"
36 
37 #include <arm_neon.h>
38 
39 using namespace arm_compute::misc::shape_calculator;
40 
41 namespace arm_compute
42 {
43 namespace
44 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * rois,ITensorInfo * output,const ROIPoolingLayerInfo & pool_info)45 Status 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);
48     ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
49     ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
50     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F32, DataType::F16);
51     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     }
61 
62     if(input->data_type() == DataType::QASYMM8 || input->data_type() == DataType::QASYMM8_SIGNED)
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 
75     return Status{};
76 }
77 } // namespace
78 
NEROIAlignLayerKernel()79 NEROIAlignLayerKernel::NEROIAlignLayerKernel()
80     : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
81 {
82 }
83 
configure(const ITensor * input,const ITensor * rois,ITensor * output,const ROIPoolingLayerInfo & pool_info)84 void 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));
88     // 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 
93     // Configure kernel window
94     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()));
102 
103     // Set instance variables
104     _input     = input;
105     _rois      = rois;
106     _output    = output;
107     _pool_info = pool_info;
108 
109     INEKernel::configure(window);
110 }
111 
validate(const ITensorInfo * input,const ITensorInfo * rois,ITensorInfo * output,const ROIPoolingLayerInfo & pool_info)112 Status 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 */
119 template <typename input_data_type>
roi_align_1x1(const ITensor * input,unsigned int roi_batch,float region_start_x,float bin_size_x,int grid_size_x,float region_end_x,float region_start_y,float bin_size_y,int grid_size_y,float region_end_y,int pz)120 inline 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)
131 {
132     if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
133     {
134         return input_data_type(0);
135     }
136     else
137     {
138         const DataLayout data_layout = input->info()->data_layout();
139         float            avg         = 0;
140         // Iterate through the aligned pooling region
141         for(int iy = 0; iy < grid_size_y; ++iy)
142         {
143             for(int ix = 0; ix < grid_size_x; ++ix)
144             {
145                 // Align the window in the middle of every bin
146                 float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y);
147                 float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x);
148 
149                 // Interpolation in the [0,0] [0,1] [1,0] [1,1] square
150                 const int y_low  = y;
151                 const int x_low  = x;
152                 const int y_high = y_low + 1;
153                 const int x_high = x_low + 1;
154 
155                 const float ly = y - y_low;
156                 const float lx = x - x_low;
157                 const float hy = 1. - ly;
158                 const float hx = 1. - lx;
159 
160                 const float w1 = hy * hx;
161                 const float w2 = hy * lx;
162                 const float w3 = ly * hx;
163                 const float w4 = ly * lx;
164                 if(data_layout == DataLayout::NCHW)
165                 {
166                     const auto data1 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch)));
167                     const auto data2 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch)));
168                     const auto data3 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch)));
169                     const auto data4 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_high, pz, roi_batch)));
170                     avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
171                 }
172                 else
173                 {
174                     const auto data1 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_low, roi_batch)));
175                     const auto data2 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_low, roi_batch)));
176                     const auto data3 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_high, roi_batch)));
177                     const auto data4 = *reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_high, roi_batch)));
178                     avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
179                 }
180             }
181         }
182 
183         avg /= grid_size_x * grid_size_y;
184         return input_data_type(avg);
185     }
186 }
187 
188 /** Average pooling over an aligned window */
189 template <typename input_data_type>
roi_align_1x1_qasymm8(const ITensor * input,unsigned int roi_batch,float region_start_x,float bin_size_x,int grid_size_x,float region_end_x,float region_start_y,float bin_size_y,int grid_size_y,float region_end_y,int pz,const QuantizationInfo & out_qinfo)190 inline input_data_type roi_align_1x1_qasymm8(const ITensor          *input,
191                                              unsigned int            roi_batch,
192                                              float                   region_start_x,
193                                              float                   bin_size_x,
194                                              int                     grid_size_x,
195                                              float                   region_end_x,
196                                              float                   region_start_y,
197                                              float                   bin_size_y,
198                                              int                     grid_size_y,
199                                              float                   region_end_y,
200                                              int                     pz,
201                                              const QuantizationInfo &out_qinfo)
202 {
203     if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
204     {
205         return input_data_type(out_qinfo.uniform().offset);
206     }
207     else
208     {
209         float                         avg              = 0;
210         const UniformQuantizationInfo input_qinfo      = input->info()->quantization_info().uniform();
211         const bool                    is_qasymm_signed = is_data_type_quantized_asymmetric_signed(input->info()->data_type());
212         const DataLayout              data_layout      = input->info()->data_layout();
213 
214         // Iterate through the aligned pooling region
215         for(int iy = 0; iy < grid_size_y; ++iy)
216         {
217             for(int ix = 0; ix < grid_size_x; ++ix)
218             {
219                 // Align the window in the middle of every bin
220                 float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y);
221                 float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x);
222 
223                 // Interpolation in the [0,0] [0,1] [1,0] [1,1] square
224                 const int y_low  = y;
225                 const int x_low  = x;
226                 const int y_high = y_low + 1;
227                 const int x_high = x_low + 1;
228 
229                 const float ly = y - y_low;
230                 const float lx = x - x_low;
231                 const float hy = 1. - ly;
232                 const float hx = 1. - lx;
233 
234                 const float w1 = hy * hx;
235                 const float w2 = hy * lx;
236                 const float w3 = ly * hx;
237                 const float w4 = ly * lx;
238 
239                 if(data_layout == DataLayout::NCHW)
240                 {
241                     if(is_qasymm_signed)
242                     {
243                         float data1 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch))), input_qinfo);
244                         float data2 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch))), input_qinfo);
245                         float data3 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch))), input_qinfo);
246                         float data4 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_high, pz, roi_batch))), input_qinfo);
247                         avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
248                     }
249                     else
250                     {
251                         float data1 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch))), input_qinfo);
252                         float data2 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch))), input_qinfo);
253                         float data3 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch))), input_qinfo);
254                         float data4 = dequantize_qasymm8(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(x_high, y_high, pz, roi_batch))), input_qinfo);
255                         avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
256                     }
257                 }
258                 else
259                 {
260                     if(is_qasymm_signed)
261                     {
262                         const auto data1 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_low, roi_batch))), input_qinfo);
263                         const auto data2 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_low, roi_batch))), input_qinfo);
264                         const auto data3 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_low, y_high, roi_batch))), input_qinfo);
265                         const auto data4 = dequantize_qasymm8_signed(*reinterpret_cast<const input_data_type *>(input->ptr_to_element(Coordinates(pz, x_high, y_high, roi_batch))), input_qinfo);
266                         avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
267                     }
268                     else
269                     {
270                         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);
271                         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);
272                         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);
273                         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);
274                         avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
275                     }
276                 }
277             }
278         }
279 
280         avg /= grid_size_x * grid_size_y;
281 
282         input_data_type res = 0;
283         if(is_qasymm_signed)
284         {
285             res = quantize_qasymm8_signed(avg, out_qinfo);
286         }
287         else
288         {
289             res = quantize_qasymm8(avg, out_qinfo);
290         }
291         return res;
292     }
293 }
294 
compute_region_coordinate(int p,float bin_size,float roi_anchor,float max_value)295 inline float compute_region_coordinate(int p, float bin_size, float roi_anchor, float max_value)
296 {
297     const float region_start = p * bin_size + roi_anchor;
298     return utility::clamp(region_start, 0.0f, max_value);
299 }
300 
run(const Window & window,const ThreadInfo & info)301 void NEROIAlignLayerKernel::run(const Window &window, const ThreadInfo &info)
302 {
303     const DataLayout data_layout = _input->info()->data_layout();
304     if(data_layout == DataLayout::NCHW || data_layout == DataLayout::NHWC)
305     {
306         switch(_input->info()->data_type())
307         {
308             case DataType::QASYMM8:
309             {
310                 NEROIAlignLayerKernel::internal_run<uint8_t, uint16_t>(window, info);
311                 break;
312             }
313             case DataType::QASYMM8_SIGNED:
314             {
315                 NEROIAlignLayerKernel::internal_run<int8_t, uint16_t>(window, info);
316                 break;
317             }
318             case DataType::F32:
319             {
320                 NEROIAlignLayerKernel::internal_run<float>(window, info);
321                 break;
322             }
323 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
324             case DataType::F16:
325             {
326                 NEROIAlignLayerKernel::internal_run<float16_t>(window, info);
327                 break;
328             }
329 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
330             default:
331             {
332                 ARM_COMPUTE_ERROR("DataType not supported");
333                 break;
334             }
335         }
336     }
337     else
338     {
339         ARM_COMPUTE_ERROR("Invalid layout");
340     }
341 }
342 
343 template <typename input_data_type, typename roi_data_type>
internal_run(const Window & window,const ThreadInfo & info)344 void NEROIAlignLayerKernel::internal_run(const Window &window, const ThreadInfo &info)
345 {
346     ARM_COMPUTE_UNUSED(info);
347     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
348     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
349 
350     const DataLayout data_layout    = _input->info()->data_layout();
351     const size_t     values_per_roi = _rois->info()->dimension(0);
352 
353     const int roi_list_start = window.x().start();
354     const int roi_list_end   = window.x().end();
355 
356     const unsigned int idx_width  = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
357     const unsigned int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
358     const unsigned int idx_depth  = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
359 
360     const int input_width   = _input->info()->dimension(idx_width);
361     const int input_height  = _input->info()->dimension(idx_height);
362     const int input_chanels = _input->info()->dimension(idx_depth);
363     const int pooled_w      = _pool_info.pooled_width();
364     const int pooled_h      = _pool_info.pooled_height();
365 
366     const DataType data_type = _input->info()->data_type();
367     const bool     is_qasymm = is_data_type_quantized_asymmetric(data_type);
368 
369     const auto             *rois_ptr   = reinterpret_cast<const roi_data_type *>(_rois->buffer());
370     const QuantizationInfo &rois_qinfo = _rois->info()->quantization_info();
371     for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx)
372     {
373         const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx];
374 
375         roi_data_type qx1 = rois_ptr[values_per_roi * roi_indx + 1];
376         roi_data_type qy1 = rois_ptr[values_per_roi * roi_indx + 2];
377         roi_data_type qx2 = rois_ptr[values_per_roi * roi_indx + 3];
378         roi_data_type qy2 = rois_ptr[values_per_roi * roi_indx + 4];
379         float         x1(qx1);
380         float         x2(qx2);
381         float         y1(qy1);
382         float         y2(qy2);
383         if(is_qasymm)
384         {
385             x1 = dequantize_qasymm16(qx1, rois_qinfo);
386             x2 = dequantize_qasymm16(qx2, rois_qinfo);
387             y1 = dequantize_qasymm16(qy1, rois_qinfo);
388             y2 = dequantize_qasymm16(qy2, rois_qinfo);
389         }
390         const float roi_anchor_x = x1 * _pool_info.spatial_scale();
391         const float roi_anchor_y = y1 * _pool_info.spatial_scale();
392         const float roi_dims_x   = std::max((x2 - x1) * _pool_info.spatial_scale(), 1.0f);
393         const float roi_dims_y   = std::max((y2 - y1) * _pool_info.spatial_scale(), 1.0f);
394         float       bin_size_x   = roi_dims_x / _pool_info.pooled_width();
395         float       bin_size_y   = roi_dims_y / _pool_info.pooled_height();
396 
397         // Iterate through all feature maps
398         for(int ch = 0; ch < input_chanels; ++ch)
399         {
400             // Iterate through all output pixels
401             for(int py = 0; py < pooled_h; ++py)
402             {
403                 for(int px = 0; px < pooled_w; ++px)
404                 {
405                     const float     region_start_x = compute_region_coordinate(px, bin_size_x, roi_anchor_x, input_width);
406                     const float     region_start_y = compute_region_coordinate(py, bin_size_y, roi_anchor_y, input_height);
407                     const float     region_end_x   = compute_region_coordinate(px + 1, bin_size_x, roi_anchor_x, input_width);
408                     const float     region_end_y   = compute_region_coordinate(py + 1, bin_size_y, roi_anchor_y, input_height);
409                     const int       roi_bin_grid_x = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_x));
410                     const int       roi_bin_grid_y = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_y));
411                     input_data_type out_val(0);
412                     if(is_qasymm)
413                     {
414                         out_val = roi_align_1x1_qasymm8<input_data_type>(
415                                       _input, roi_batch, region_start_x, bin_size_x,
416                                       roi_bin_grid_x, region_end_x, region_start_y, bin_size_y,
417                                       roi_bin_grid_y, region_end_y, ch, _output->info()->quantization_info());
418                     }
419                     else
420                     {
421                         out_val = roi_align_1x1<input_data_type>(
422                                       _input, roi_batch, region_start_x, bin_size_x,
423                                       roi_bin_grid_x, region_end_x, region_start_y, bin_size_y,
424                                       roi_bin_grid_y, region_end_y, ch);
425                     }
426 
427                     if(data_layout == DataLayout::NCHW)
428                     {
429                         auto out_ptr = reinterpret_cast<input_data_type *>(_output->ptr_to_element(Coordinates(px, py, ch, roi_indx)));
430                         *out_ptr     = out_val;
431                     }
432                     else
433                     {
434                         auto out_ptr = reinterpret_cast<input_data_type *>(_output->ptr_to_element(Coordinates(ch, px, py, roi_indx)));
435                         *out_ptr     = out_val;
436                     }
437                 }
438             }
439         }
440     }
441 }
442 } // namespace arm_compute
443