xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/kernels/CLSelectKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-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  */
24 #include "src/core/CL/kernels/CLSelectKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/Helpers.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Utils.h"
32 #include "src/core/CL/CLValidate.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35 
36 #include "support/StringSupport.h"
37 
38 namespace arm_compute
39 {
40 namespace
41 {
validate_arguments(const ITensorInfo * c,const ITensorInfo * x,const ITensorInfo * y,const ITensorInfo * output)42 Status validate_arguments(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
43 {
44     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(c, x, y, output);
45     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(x);
46     ARM_COMPUTE_RETURN_ERROR_ON(x->data_type() == DataType::UNKNOWN);
47     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, y);
48     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, y);
49     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::U8);
50 
51     const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions());
52     ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape()));
53     ARM_COMPUTE_RETURN_ERROR_ON(!is_same_rank && ((c->tensor_shape().num_dimensions() > 1) || (c->tensor_shape().x() != x->tensor_shape()[x->tensor_shape().num_dimensions() - 1])));
54 
55     if(output->total_size() != 0)
56     {
57         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, output);
58         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, output);
59     }
60 
61     return Status{};
62 }
63 } // namespace
64 
CLSelectKernel()65 CLSelectKernel::CLSelectKernel()
66     : _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false)
67 {
68     _type = CLKernelType::ELEMENTWISE;
69 }
70 
configure(const CLCompileContext & compile_context,const ICLTensor * c,const ICLTensor * x,const ICLTensor * y,ICLTensor * output)71 void CLSelectKernel::configure(const CLCompileContext &compile_context, const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output)
72 {
73     ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output);
74     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(c->info(), x->info(), y->info(), output->info()));
75 
76     _c             = c;
77     _x             = x;
78     _y             = y;
79     _output        = output;
80     _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions());
81 
82     auto               padding_info         = get_padding_info({ c, x, y, output });
83     const unsigned int vec_size_x           = adjust_vec_size(16 / x->info()->element_size(), x->info()->dimension(0));
84     const int          vec_size_x_leftovers = output->info()->dimension(0) % vec_size_x;
85 
86     // Set build options
87     CLBuildOptions build_opts;
88     build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(x->info()->element_size()));
89     build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
90     build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftovers));
91 
92     // Create kernel
93     std::string kernel_name = "select";
94     if(_has_same_rank)
95     {
96         kernel_name += "_same_rank";
97     }
98     else
99     {
100         const bool is_input_rank_greater_than_two = x->info()->tensor_shape().num_dimensions() > 2;
101         if(is_input_rank_greater_than_two)
102         {
103             const size_t width      = x->info()->tensor_shape().x();
104             const size_t height     = x->info()->tensor_shape().y();
105             const size_t outer_size = x->info()->tensor_shape()[x->info()->tensor_shape().num_dimensions() - 1];
106             const size_t depth_size = x->info()->tensor_shape().total_size() / (width * height * outer_size);
107             build_opts.add_option("-DDEPTH_SIZE=" + support::cpp11::to_string(depth_size));
108         }
109         kernel_name += "_different_rank";
110         kernel_name += is_input_rank_greater_than_two ? "_n" : "_2";
111     }
112     _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
113 
114     // Configure kernel window
115     auto_init_if_empty(*output->info(), *x->info()->clone());
116     Window win = calculate_max_window(*x->info(), Steps(vec_size_x));
117     ICLKernel::configure_internal(win);
118 
119     _config_id = "select_";
120     _config_id += string_from_data_type(x->info()->data_type());
121     _config_id += "_";
122     _config_id += support::cpp11::to_string(x->info()->dimension(0));
123     _config_id += "_";
124     _config_id += support::cpp11::to_string(x->info()->dimension(1));
125     _config_id += "_";
126     _config_id += support::cpp11::to_string(x->info()->dimension(2));
127     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
128 }
129 
validate(const ITensorInfo * c,const ITensorInfo * x,const ITensorInfo * y,const ITensorInfo * output)130 Status CLSelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
131 {
132     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(c, x, y, output));
133     return Status{};
134 }
135 
run(const arm_compute::Window & window,cl::CommandQueue & queue)136 void CLSelectKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue)
137 {
138     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
139     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
140 
141     Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
142     Window slice     = collapsed.first_slice_window_3D();
143 
144     if(!_has_same_rank)
145     {
146         Window vector_slice = window.first_slice_window_1D();
147         vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
148         unsigned int idx = 0;
149         add_1D_tensor_argument(idx, _c, vector_slice);
150     }
151 
152     do
153     {
154         unsigned int idx = _has_same_rank ? 0 : num_arguments_per_1D_tensor();
155         if(_has_same_rank)
156         {
157             add_3D_tensor_argument(idx, _c, slice);
158         }
159         add_3D_tensor_argument(idx, _x, slice);
160         add_3D_tensor_argument(idx, _y, slice);
161         add_3D_tensor_argument(idx, _output, slice);
162 
163         enqueue(queue, *this, slice, lws_hint());
164     }
165     while(collapsed.slide_window_slice_3D(slice));
166 }
167 } // namespace arm_compute
168