Compute Library
 20.02.1
CLROIAlignLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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  */
25 
35 #include "arm_compute/core/Utils.h"
38 
40 
41 namespace arm_compute
42 {
43 namespace
44 {
45 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
46 {
48  ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
49  ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
53  ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
54 
55  if(output->total_size() != 0)
56  {
59  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape());
60  }
61 
63  {
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  {
73  }
74  return Status{};
75 }
76 
77 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
78 {
80 
81  // Output auto inizialitation if not yet initialized
82  const TensorShape output_shape = compute_roi_align_shape(*input, *rois, pool_info);
83  auto_init_if_empty((*output), output_shape, 1, input->data_type());
84  output->set_data_layout(input->data_layout());
85 
86  // Configure kernel window
87  constexpr unsigned int num_elems_processed_per_iteration = 1;
88  Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
89 
90  AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
91  AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration);
92 
93  bool window_changed = update_window_and_padding(win, input_access, output_access);
94  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
95  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
96  return std::make_pair(err, win);
97 }
98 } // namespace
99 
101  : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
102 {
103 }
104 
105 void CLROIAlignLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
106 {
107  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
108  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
109 
110  // Configure kernel window
111  auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info);
112  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
113 
114  _input = input;
115  _output = output;
116  _rois = rois;
117  _pool_info = pool_info;
118 
119  const DataType data_type = input->info()->data_type();
120  const bool is_qasymm = is_data_type_quantized_asymmetric(data_type);
121 
122  // Set build options
123  CLBuildOptions build_opts;
124  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
125  build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type()));
126  build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH))));
127  build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT))));
128  build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL))));
129  build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width()));
130  build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height()));
131  build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale()));
132  build_opts.add_option_if(input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
133  build_opts.add_option_if(pool_info.sampling_ratio() > 0, "-DSAMPLING_RATIO=" + support::cpp11::to_string(pool_info.sampling_ratio()));
134 
135  if(is_qasymm)
136  {
137  const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
138  const UniformQuantizationInfo roisq_info = rois->info()->quantization_info().uniform();
139  const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
140 
141  build_opts.add_option("-DOFFSET_IN=" + float_to_string_with_full_precision(iq_info.offset));
142  build_opts.add_option("-DSCALE_IN=" + float_to_string_with_full_precision(iq_info.scale));
143  build_opts.add_option("-DOFFSET_ROIS=" + float_to_string_with_full_precision(roisq_info.offset));
144  build_opts.add_option("-DSCALE_ROIS=" + float_to_string_with_full_precision(roisq_info.scale));
145  build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
146  build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
147  }
148 
149  // Create kernel
150  const std::string kernel_name = (is_qasymm) ? "roi_align_layer_quantized" : "roi_align_layer";
151  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
152 
153  ICLKernel::configure_internal(win_config.second);
154 }
155 
157 {
158  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
159  return Status{};
160 }
161 
162 void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue)
163 {
166 
168  Window slice_rois = slice;
169  // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
170  slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
172 
173  // Set arguments
174  unsigned int idx = 0;
175  add_3D_tensor_argument(idx, _input, slice);
176  add_2D_tensor_argument(idx, _rois, slice_rois);
177  add_3D_tensor_argument(idx, _output, slice);
178  add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
179  add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
180 
181  enqueue(queue, *this, slice, lws_hint());
182 }
183 } // namespace arm_compute
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:34
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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.
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
const StringSet & options() const
Gets the current options list set.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:247
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::string to_string(T &&value)
Convert integer and float values to string.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
1 channel, 1 F32 per channel
void run(const Window &window, cl::CommandQueue &queue)
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Quantization info when assuming per layer quantization.
quantized, asymmetric fixed-point 16-bit number
unsigned int pooled_width() const
Get the pooled width of the layer.
Definition: Types.h:1389
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
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:158
Copyright (c) 2017-2020 ARM Limited.
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...
Definition: Helpers.inl:202
1 channel, 1 F16 per channel
void add_option(std::string option)
Adds option to the existing build option list.
void configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
Set the input and output tensors.
std::string get_data_size_from_data_type(const DataType &dt)
Get the size of a data type in number of bits.
Definition: CLHelpers.cpp:191
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: Helpers.h:402
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1211
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
quantized, asymmetric fixed-point 8-bit number unsigned
std::string kernel_name
UniformQuantizationInfo uniform() const
Return per layer quantization info.
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:37
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
unsigned int sampling_ratio() const
Get sampling ratio.
Definition: Types.h:1404
Num samples, channels, height, width.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1139
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
Definition: Window.inl:167
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:134
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
unsigned int pooled_height() const
Get the pooled height of the layer.
Definition: Types.h:1394
ROI Pooling Layer Information class.
Definition: Types.h:1374
Num samples, height, width, channels.
float spatial_scale() const
Get the spatial scale.
Definition: Types.h:1399
unsigned int num_elems_processed_per_iteration
quantized, asymmetric fixed-point 8-bit number signed
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:327
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:746
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:289
DataType
Available data types.
Definition: Types.h:75
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
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 CLROIAlignLayerKernel.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.