Compute Library
 23.11
CLSelectKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021, 2023 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 
31 #include "arm_compute/core/Utils.h"
33 
34 #include "src/core/CL/CLValidate.h"
37 #include "support/StringSupport.h"
38 
39 namespace arm_compute
40 {
41 namespace
42 {
43 Status validate_arguments(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
44 {
45  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(c, x, y, output);
51 
52  const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions());
53  ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape()));
55  ((c->tensor_shape().num_dimensions() > 1) ||
56  (c->tensor_shape().x() != x->tensor_shape()[x->tensor_shape().num_dimensions() - 1])));
57 
58  if (output->total_size() != 0)
59  {
62  }
63 
64  return Status{};
65 }
66 } // namespace
67 
68 CLSelectKernel::CLSelectKernel() : _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false)
69 {
71 }
72 
73 void CLSelectKernel::configure(const CLCompileContext &compile_context,
74  const ICLTensor *c,
75  const ICLTensor *x,
76  const ICLTensor *y,
77  ICLTensor *output)
78 {
79  ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output);
80  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(c->info(), x->info(), y->info(), output->info()));
81 
82  _c = c;
83  _x = x;
84  _y = y;
85  _output = output;
86  _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions());
87 
88  auto padding_info = get_padding_info({c, x, y, output});
89  const unsigned int vec_size_x = adjust_vec_size(16 / x->info()->element_size(), x->info()->dimension(0));
90  const int vec_size_x_leftovers = output->info()->dimension(0) % vec_size_x;
91 
92  // Set build options
93  CLBuildOptions build_opts;
94  build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(x->info()->element_size()));
95  build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
96  build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftovers));
97 
98  // Create kernel
99  std::string kernel_name = "select";
100  if (_has_same_rank)
101  {
102  kernel_name += "_same_rank";
103  }
104  else
105  {
106  const bool is_input_rank_greater_than_two = x->info()->tensor_shape().num_dimensions() > 2;
107  if (is_input_rank_greater_than_two)
108  {
109  const size_t width = x->info()->tensor_shape().x();
110  const size_t height = x->info()->tensor_shape().y();
111  const size_t outer_size = x->info()->tensor_shape()[x->info()->tensor_shape().num_dimensions() - 1];
112  const size_t depth_size = x->info()->tensor_shape().total_size() / (width * height * outer_size);
113  build_opts.add_option("-DDEPTH_SIZE=" + support::cpp11::to_string(depth_size));
114  }
115  kernel_name += "_different_rank";
116  kernel_name += is_input_rank_greater_than_two ? "_n" : "_2";
117  }
118  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
119 
120  // Configure kernel window
121  auto_init_if_empty(*output->info(), *x->info()->clone());
122  Window win = calculate_max_window(*x->info(), Steps(vec_size_x));
123  ICLKernel::configure_internal(win);
124 
125  _config_id = "select_";
126  _config_id += string_from_data_type(x->info()->data_type());
127  _config_id += "_";
128  _config_id += support::cpp11::to_string(x->info()->dimension(0));
129  _config_id += "_";
130  _config_id += support::cpp11::to_string(x->info()->dimension(1));
131  _config_id += "_";
132  _config_id += support::cpp11::to_string(x->info()->dimension(2));
134 }
135 
136 Status
137 CLSelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
138 {
140  return Status{};
141 }
142 
143 void CLSelectKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue)
144 {
147 
149  Window slice = collapsed.first_slice_window_3D();
150 
151  if (!_has_same_rank)
152  {
153  Window vector_slice = window.first_slice_window_1D();
154  vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
155  unsigned int idx = 0;
156  add_1D_tensor_argument(idx, _c, vector_slice);
157  }
158 
159  do
160  {
161  unsigned int idx = _has_same_rank ? 0 : num_arguments_per_1D_tensor();
162  if (_has_same_rank)
163  {
164  add_3D_tensor_argument(idx, _c, slice);
165  }
166  add_3D_tensor_argument(idx, _x, slice);
167  add_3D_tensor_argument(idx, _y, slice);
168  add_3D_tensor_argument(idx, _output, slice);
169 
170  enqueue(queue, *this, slice, lws_hint());
171  } while (collapsed.slide_window_slice_3D(slice));
172 }
173 } // namespace arm_compute
arm_compute::Steps
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
StringSupport.h
arm_compute::ICLKernel::add_1D_tensor_argument
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:186
AdjustVecSize.h
ICLTensor.h
arm_compute::ITensorInfo::tensor_shape
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Helpers.h
arm_compute::CLBuildOptions::options
const StringSet & options() const
Gets the current options list set.
Definition: CLCompileContext.cpp:72
arm_compute::calculate_max_window
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Definition: WindowHelpers.cpp:29
arm_compute::CLSelectKernel::CLSelectKernel
CLSelectKernel()
Default constructor.
Definition: CLSelectKernel.cpp:68
arm_compute::cpu::kernels::validate_arguments
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
Definition: CpuDirectConv2dKernel.cpp:57
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1079
arm_compute::ITensorInfo::element_size
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:574
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
arm_compute::Window::collapse_if_possible
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:72
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:41
TensorInfo.h
arm_compute::string_from_data_type
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: DataTypeUtils.cpp:31
arm_compute::Window::first_slice_window_1D
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:290
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:677
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:952
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:205
arm_compute::ITensorInfo::dimension
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
arm_compute::CLSelectKernel::run
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: CLSelectKernel.cpp:143
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
ARM_COMPUTE_ERROR_ON
#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
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
arm_compute::CLBuildOptions::add_option
void add_option(std::string option)
Adds option to the existing build option list.
Definition: CLCompileContext.cpp:41
arm_compute::CLCompileContext
CLCompileContext class.
Definition: CLCompileContext.h:204
is_same_rank
bool is_same_rank
Definition: NESelectKernel.cpp:52
arm_compute::CLSelectKernel::configure
void configure(const CLCompileContext &compile_context, const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output)
Initialise the kernel's inputs and output.
Definition: CLSelectKernel.cpp:73
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:298
arm_compute::auto_init_if_empty
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: AutoConfiguration.h:43
arm_compute::TensorShape::total_size
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:175
arm_compute::DataType::U8
@ U8
unsigned 8-bit number
arm_compute::CLSelectKernel::validate
static Status validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLSelectKernel.
Definition: CLSelectKernel.cpp:137
ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:36
arm_compute::ICLKernel::num_arguments_per_1D_tensor
constexpr static unsigned int num_arguments_per_1D_tensor()
Returns the number of arguments enqueued per 1D tensor object.
Definition: ICLKernel.h:305
arm_compute::create_kernel
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:409
arm_compute::Status
Status class.
Definition: Error.h:52
WindowHelpers.h
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:203
arm_compute::ITensorInfo::data_type
virtual DataType data_type() const =0
Data type used for each element of the tensor.
arm_compute::Dimensions::x
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:86
arm_compute::Window::slide_window_slice_3D
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:350
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
CLSelectKernel.h
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:53
AutoConfiguration.h
arm_compute::Window::first_slice_window_3D
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:306
CLValidate.h
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::ICLKernel::add_3D_tensor_argument
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:234
Utils.h
arm_compute::misc::ICloneable::clone
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
arm_compute::get_cl_unsigned_type_from_element_size
std::string get_cl_unsigned_type_from_element_size(size_t element_size)
Translates the element size to an unsigned integer data type.
Definition: CLHelpers.cpp:107
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::ELEMENTWISE
@ ELEMENTWISE
Elementwise CL kernel type.
Definition: CLTypes.h:83
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::adjust_vec_size
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input's first dimension,...
Definition: AdjustVecSize.h:38
arm_compute::has_padding_changed
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:491
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::ICLKernel::lws_hint
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:383
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::DataType::UNKNOWN
@ UNKNOWN
Unknown data type.
arm_compute::CLBuildOptions
Build options.
Definition: CLCompileContext.h:38
arm_compute::Dimensions::y
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:91
arm_compute::Dimensions::num_dimensions
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:142
arm_compute::get_padding_info
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo * > infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:476
arm_compute::test::validation::reference::slice
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
Definition: SliceOperations.cpp:38
CLHelpers.h
kernel_name
std::string kernel_name
Definition: ClIm2ColKernel.cpp:58
arm_compute::enqueue
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:33