Compute Library
 21.11
NEROIPoolingLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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  */
28 #include "src/core/CPP/Validate.h"
32 
33 #include <cfloat>
34 
35 namespace arm_compute
36 {
37 namespace
38 {
39 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
40 {
41  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, rois);
42 
43  //Validate arguments
45  ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
46  ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
48  ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
49 
50  if(output->total_size() != 0)
51  {
53  ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) != pool_info.pooled_width()) || (output->dimension(1) != pool_info.pooled_height()));
54  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(2));
55  ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(1) != output->dimension(3));
56  }
57 
58  return Status{};
59 }
60 
61 /** Evaluate number needing to be stored in output tensor as quantized format.
62  *
63  * @param[in] input Source tensor. Data types supported: QASYMM8
64  * @param[out] output Destination tensor. Where output value will be stored, same datatype as input
65  * @param[in] region_start_x Beginning region of x coordinate of pooling region
66  * @param[in] region_start_y Beginning region of y coordinate of pooling region
67  * @param[in] region_end_x End of pooling region, x coordinate
68  * @param[in] region_end_y End of pooling region, y coordinate
69  * @param[in] fm Channel index of coordinate in output Tensor to store value
70  * @param[in] px Width index of coodinate in output Tensor to store value
71  * @param[in] py Height index of coordinate in output Tensor to store value
72  * @param[in] roi_batch Index of image to perform Pooling on in input Tensor
73  * @param[in] roi_indx Index of image of coordinate in output Tensor to store value
74  */
75 template <typename T>
76 void template_eval(const ITensor *input, const ITensor *output, int region_start_x, int region_start_y,
77  int region_end_x, int region_end_y, int fm, int px, int py, int roi_batch, int roi_indx)
78 {
79  if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
80  {
81  *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = 0;
82  }
83  else
84  {
85  T curr_max = std::numeric_limits<T>::lowest(); // Min value of typename T
86  for(int j = region_start_y; j < region_end_y; ++j)
87  {
88  for(int i = region_start_x; i < region_end_x; ++i)
89  {
90  const auto val = *reinterpret_cast<const T *>(input->ptr_to_element(Coordinates(i, j, fm, roi_batch)));
91  curr_max = std::max(val, curr_max);
92  }
93  }
94 
95  // if quantized datatype, requantize then store in output tensor
96  if(is_data_type_quantized(input->info()->data_type()))
97  {
98  // covert qasymm to new output quantization scale and offset
99  UniformQuantizationInfo uqinfo = compute_requantization_scale_offset(input->info()->quantization_info().uniform(), output->info()->quantization_info().uniform());
100  *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = quantize_qasymm8(curr_max, uqinfo);
101  }
102  else
103  {
104  *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = curr_max;
105  }
106  }
107 }
108 } // namespace
109 
111  : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
112 {
113 }
114 
115 Status NEROIPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
116 {
117  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
118  return Status{};
119 }
120 
121 void NEROIPoolingLayerKernel::configure(const ITensor *input, const ITensor *rois, const ITensor *output, const ROIPoolingLayerInfo &pool_info)
122 {
123  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
124 
125  //Validate arguments
126  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
127 
128  // Output auto initialization if not yet initialized
129  TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->info()->dimension(1));
130 
131  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), output->info()->quantization_info());
132 
134  ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
135 
136  // Set instance variables
137  _input = input;
138  _rois = rois;
139  _output = output;
140  _pool_info = pool_info;
141 
142  // Configure kernel window
143  Window window;
144  window.set(Window::DimX, Window::Dimension(0, rois->info()->dimension(1)));
145  window.set(Window::DimY, Window::Dimension(0, 1));
146 
147  INEKernel::configure(window);
148 }
149 
151 {
152  ARM_COMPUTE_UNUSED(info);
155 
156  const size_t values_per_roi = _rois->info()->dimension(0);
157 
158  const int roi_list_start = window.x().start();
159  const int roi_list_end = window.x().end();
160  const int width = _input->info()->dimension(Window::DimX);
161  const int height = _input->info()->dimension(Window::DimY);
162  const int fms = _input->info()->dimension(Window::DimZ);
163  const int pooled_w = _pool_info.pooled_width();
164  const int pooled_h = _pool_info.pooled_height();
165  const float spatial_scale = _pool_info.spatial_scale();
166 
167  const auto *rois_ptr = reinterpret_cast<const uint16_t *>(_rois->buffer());
168  const auto data_type = _input->info()->data_type();
169 
170  for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx)
171  {
172  const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx];
173  const auto x1 = rois_ptr[values_per_roi * roi_indx + 1];
174  const auto y1 = rois_ptr[values_per_roi * roi_indx + 2];
175  const auto x2 = rois_ptr[values_per_roi * roi_indx + 3];
176  const auto y2 = rois_ptr[values_per_roi * roi_indx + 4];
177 
178  // Scale ROI
179  const int roi_anchor_x = support::cpp11::round(x1 * spatial_scale);
180  const int roi_anchor_y = support::cpp11::round(y1 * spatial_scale);
181  const int roi_width = std::max(support::cpp11::round((x2 - x1) * spatial_scale), 1.f);
182  const int roi_height = std::max(support::cpp11::round((y2 - y1) * spatial_scale), 1.f);
183 
184  // Iterate through all feature maps
185  for(int fm = 0; fm < fms; ++fm)
186  {
187  // Iterate through all output pixels
188  for(int py = 0; py < pooled_h; ++py)
189  {
190  for(int px = 0; px < pooled_w; ++px)
191  {
192  auto region_start_x = static_cast<int>(std::floor((static_cast<float>(px) / pooled_w) * roi_width));
193  auto region_end_x = static_cast<int>(std::floor((static_cast<float>(px + 1) / pooled_w) * roi_width));
194  auto region_start_y = static_cast<int>(std::floor((static_cast<float>(py) / pooled_h) * roi_height));
195  auto region_end_y = static_cast<int>(std::floor((static_cast<float>(py + 1) / pooled_h) * roi_height));
196 
197  region_start_x = std::min(std::max(region_start_x + roi_anchor_x, 0), width);
198  region_end_x = std::min(std::max(region_end_x + roi_anchor_x, 0), width);
199  region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height);
200  region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height);
201 
202  switch(data_type)
203  {
204  case DataType::F32:
205  template_eval<float>(_input, _output, region_start_x, region_start_y, region_end_x, region_end_y, fm, px, py, roi_batch, roi_indx);
206  break;
207  case DataType::QASYMM8:
208  template_eval<qasymm8_t>(_input, _output, region_start_x, region_start_y, region_end_x, region_end_y, fm, px, py, roi_batch, roi_indx);
209  break;
210  default:
211  ARM_COMPUTE_ERROR("DataType not Supported");
212  break;
213  }
214  }
215  }
216  }
217  }
218 }
219 } // namespace arm_compute
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:981
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
uint8_t quantize_qasymm8(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given an unsigned 8-bit asymmetric quantization scheme.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void configure(const ITensor *input, const ITensor *rois, const ITensor *output, const ROIPoolingLayerInfo &pool_info)
Set the input and output tensors.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
static Status validate(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
Static function to check if given info will lead to a valid configuration of NEROIPoolingLayerKernel...
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
1 channel, 1 U16 per channel
unsigned int pooled_width() const
Get the pooled width of the layer.
Definition: Types.h:1283
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
const DataType data_type
Definition: Im2Col.cpp:150
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:539
quantized, asymmetric fixed-point 8-bit number unsigned
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory. ...
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
UniformQuantizationInfo compute_requantization_scale_offset(const UniformQuantizationInfo &uqinfo_in, const UniformQuantizationInfo &uqinfo_out)
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:158
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
unsigned int pooled_height() const
Get the pooled height of the layer.
Definition: Types.h:1288
ROI Pooling Layer Information class.
Definition: Types.h:1268
T round(T value)
Round floating-point value with half value rounding away from zero.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
float spatial_scale() const
Get the spatial scale.
Definition: Types.h:1293
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145