Compute Library
 22.02
NEROIAlignLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022 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  */
25 
28 #include "arm_compute/core/Utils.h"
32 #include "src/core/CPP/Validate.h"
37 
38 #include <arm_neon.h>
39 
41 
42 namespace arm_compute
43 {
44 namespace
45 {
46 struct ROIAlignSelectorData
47 {
49 };
50 
53 
54 struct ROIAlignKernel
55 {
56  const char *name;
57  const ROIAlignSelctorPtr is_selected;
58  ROIAlignUKernelPtr ukernel;
59 };
60 
61 static const ROIAlignKernel available_kernels[] =
62 {
63  {
64  "fp32_neon_roialign",
65  [](const ROIAlignSelectorData & data) { return data.dt == DataType::F32; },
67  },
68 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
69  {
70  "fp16_neon_roialign",
71  [](const ROIAlignSelectorData & data) { return data.dt == DataType::F16; },
73  },
74 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
75 #if defined(ARM_COMPUTE_ENABLE_NEON)
76  {
77  "qu8_neon_roialign",
78  [](const ROIAlignSelectorData & data) { return data.dt == DataType::QASYMM8; },
80  },
81  {
82  "qs8_neon_roialign",
83  [](const ROIAlignSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
85  },
86 #endif //defined(ARM_COMPUTE_ENABLE_NEON)
87 };
88 
89 /** Micro-kernel selector
90  *
91  * @param[in] data Selection data passed to help pick the appropriate micro-kernel
92  *
93  * @return A matching micro-kernel else nullptr
94  */
95 const ROIAlignKernel *get_implementation(const ROIAlignSelectorData &data)
96 {
97  for(const auto &uk : available_kernels)
98  {
99  if(uk.is_selected(data))
100  {
101  return &uk;
102  }
103  }
104  return nullptr;
105 }
106 
107 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
108 {
109  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
110  ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
111  ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
114  ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
116 
117  if(output->total_size() != 0)
118  {
121  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape());
122  }
123 
124  if(input->data_type() == DataType::QASYMM8 || input->data_type() == DataType::QASYMM8_SIGNED)
125  {
127 
128  const UniformQuantizationInfo rois_qinfo = rois->quantization_info().uniform();
129  ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.scale != 0.125f);
130  ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.offset != 0);
131  }
132  else
133  {
135  }
136 
137  return Status{};
138 }
139 } // namespace
140 
142  : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
143 {
144 }
145 
146 void NEROIAlignLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
147 {
148  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
149  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
150  // Output auto inizialitation if not yet initialized
151  const TensorShape output_shape = compute_roi_align_shape(*input->info(), *rois->info(), pool_info);
152  auto_init_if_empty((*output->info()), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
153  output->info()->set_data_layout(input->info()->data_layout());
154 
155  // Configure kernel window
156  const unsigned int num_rois = rois->info()->dimension(1);
157  Window window;
158  window.set(Window::DimX, Window::Dimension(0, num_rois));
159  window.set(Window::DimY, Window::Dimension(0, 1));
160 
161  // Set instance variables
162  _input = input;
163  _rois = rois;
164  _output = output;
165  _pool_info = pool_info;
166 
167  INEKernel::configure(window);
168 }
169 
171 {
172  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
173  return Status{};
174 }
175 
177 {
178  const DataLayout data_layout = _input->info()->data_layout();
179  if(data_layout == DataLayout::NCHW || data_layout == DataLayout::NHWC)
180  {
181  const auto *uk = get_implementation(ROIAlignSelectorData{ _input->info()->data_type() });
182  ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
183 
184  uk->ukernel(_input, _output, _rois, _pool_info, window, info);
185  }
186  else
187  {
188  ARM_COMPUTE_ERROR("Invalid layout");
189  }
190 }
191 } // namespace arm_compute
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
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:742
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:490
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
TensorShape compute_roi_align_shape(const ITensorInfo &input, const ITensorInfo &rois, ROIPoolingLayerInfo pool_info)
Calculate the output roi align shape of a tensor.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
#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
void neon_qs8_roialign(const ITensor *input, ITensor *output, const ITensor *rois, ROIPoolingLayerInfo pool_info, const Window &window, const ThreadInfo &info)
#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
#define REGISTER_QASYMM8_SIGNED_NEON(func_name)
Definition: Registrars.h:96
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#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
quantized, asymmetric fixed-point 16-bit number
void neon_qu8_roialign(const ITensor *input, ITensor *output, const ITensor *rois, ROIPoolingLayerInfo pool_info, const Window &window, const ThreadInfo &info)
Definition: qasymm8.cpp:29
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
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
ROIAlignUKernelPtr ukernel
const char * name
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:117
void neon_fp16_roialign(const ITensor *input, ITensor *output, const ITensor *rois, ROIPoolingLayerInfo pool_info, const Window &window, const ThreadInfo &info)
virtual ITensorInfo & set_data_layout(const DataLayout &data_layout)=0
Set the data layout of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
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.
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.
Num samples, channels, height, width.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void neon_fp32_roialign(const ITensor *input, ITensor *output, const ITensor *rois, ROIPoolingLayerInfo pool_info, const Window &window, const ThreadInfo &info)
Definition: fp32.cpp:29
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Information about executing thread and CPU.
Definition: CPPTypes.h:169
ROI Pooling Layer Information class.
Definition: Types.h:1268
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
Num samples, height, width, channels.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
const ROIAlignSelctorPtr is_selected
quantized, asymmetric fixed-point 8-bit number signed
DataType
Available data types.
Definition: Types.h:79
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
void configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
Set the input and output tensors.
Describe a multidimensional execution window.
Definition: Window.h:39
DataType dt
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
static Status validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
Static function to check if given info will lead to a valid configuration of NEROIAlignLayerKernel.