Compute Library
 21.02
CLROIPoolingLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
33 #include "arm_compute/core/Utils.h"
35 #include "src/core/CL/CLValidate.h"
38 #include "support/StringSupport.h"
39 
40 #include <cmath>
41 #include <set>
42 #include <string>
43 
44 namespace arm_compute
45 {
46 namespace
47 {
48 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
49 {
50  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
51 
52  // Output auto initialization if not yet initialized
53  TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->dimension(2), rois->dimension(1));
54  auto_init_if_empty((*output), output_shape, 1, input->data_type());
55 
56  // Configure kernel window
57  constexpr unsigned int num_elems_processed_per_iteration = 1;
58  Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
59 
60  AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
61  AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration);
62 
63  bool window_changed = update_window_and_padding(win, input_access, output_access);
64  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
65  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
66  return std::make_pair(err, win);
67 }
68 } // namespace
69 
71  : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
72 {
73 }
74 
75 void CLROIPoolingLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
76 {
77  configure(CLKernelLibrary::get().get_compile_context(), input, rois, output, pool_info);
78 }
79 
80 void CLROIPoolingLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
81 {
82  ARM_COMPUTE_ERROR_ON_NULLPTR(input, rois, output);
83 
84  //Validate arguments
85  ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info());
87  ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5);
91  ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
92 
93  if(output->info()->total_size() != 0)
94  {
96  ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
97  ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2));
98  ARM_COMPUTE_ERROR_ON(rois->info()->dimension(1) != output->info()->dimension(3));
99  }
100 
101  // Configure kernel window
102  auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info);
103  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
104 
105  // Set instance variables
106  _input = input;
107  _rois = rois;
108  _output = output;
109  _pool_info = pool_info;
110 
111  // Set build options
112  std::set<std::string> build_opts;
113  build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
114  build_opts.emplace(("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())));
115  build_opts.emplace(("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))));
116  build_opts.emplace(("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))));
117  build_opts.emplace(("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))));
118  build_opts.emplace(("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())));
119  build_opts.emplace(("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())));
120  build_opts.emplace(("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale())));
121 
122  // Create kernel
123  std::string kernel_name = "roi_pooling_layer";
124  _kernel = create_kernel(compile_context, kernel_name, build_opts);
125 
126  // Set static kernel arguments
127  unsigned int idx = 2 * num_arguments_per_3D_tensor() + num_arguments_per_1D_array();
128  add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
129  add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
130 
131  ICLKernel::configure_internal(win_config.second);
132 }
133 
134 void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
135 {
138 
140  Window slice_rois = slice;
141  // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
142  slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
143  slice.set(Window::DimZ, window[3]);
144 
145  // Set arguments
146  unsigned int idx = 0;
147  add_3D_tensor_argument(idx, _input, slice);
148  add_2D_tensor_argument(idx, _rois, slice_rois);
149  add_3D_tensor_argument(idx, _output, slice);
150  add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
151  add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
152 
153  enqueue(queue, *this, slice, lws_hint());
154 }
155 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
static constexpr unsigned int num_arguments_per_1D_array()
Returns the number of arguments enqueued per 1D array object.
Definition: ICLKernel.h:190
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
std::string to_string(T &&value)
Convert integer and float values to string.
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
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
1 channel, 1 U16 per channel
unsigned int pooled_width() const
Get the pooled width of the layer.
Definition: Types.h:1324
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:172
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:403
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: WindowHelpers.h:46
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:214
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
std::string kernel_name
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
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
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
CLCompileContext class.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
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&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:148
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
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:1329
ROI Pooling Layer Information class.
Definition: Types.h:1309
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Wrapper to configure the Khronos OpenCL C++ header.
float spatial_scale() const
Get the spatial scale.
Definition: Types.h:1334
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
void configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
Set the input and output tensors.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
#define ARM_COMPUTE_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:32
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)