blob: d9a1044e1f2794068363e954feb685f5c4c86b84 [file] [log] [blame]
Georgios Pinitasaaa27182018-11-21 16:32:15 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasaaa27182018-11-21 16:32:15 +00003 *
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 "arm_compute/core/CL/kernels/CLSelectKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Georgios Pinitasaaa27182018-11-21 16:32:15 +000028#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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitasaaa27182018-11-21 16:32:15 +000035
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Georgios Pinitasaaa27182018-11-21 16:32:15 +000037
38namespace arm_compute
39{
40namespace
41{
42Status validate_arguments(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
43{
Manuel Bottini8481d832019-12-10 15:28:40 +000044 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(c, x, y);
Georgios Pinitasaaa27182018-11-21 16:32:15 +000045 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(x);
Manuel Bottini8481d832019-12-10 15:28:40 +000046 ARM_COMPUTE_RETURN_ERROR_ON(x->data_type() == DataType::UNKNOWN);
Georgios Pinitasaaa27182018-11-21 16:32:15 +000047 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 != nullptr && 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
64std::pair<Status, Window> validate_and_configure_window(ITensorInfo *c, ITensorInfo *x, ITensorInfo *y, ITensorInfo *output)
65{
66 if(output != nullptr)
67 {
68 // Output tensor auto initialization if not yet initialized
69 auto_init_if_empty(*output, *x->clone());
70 }
71
72 const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions());
73
74 const unsigned int num_elems_processed_per_iteration = 16 / x->element_size();
75
76 // Configure kernel window
77 Window win = calculate_max_window(*x, Steps(num_elems_processed_per_iteration));
78 AccessWindowHorizontal x_access(x, 0, num_elems_processed_per_iteration);
79 AccessWindowHorizontal y_access(y, 0, num_elems_processed_per_iteration);
80 bool window_changed = update_window_and_padding(win, x_access, y_access);
81
82 // Update window for condition
83 if(is_same_rank)
84 {
85 AccessWindowHorizontal c_access(c, 0, num_elems_processed_per_iteration);
86 window_changed = window_changed || update_window_and_padding(win, c_access);
87 }
88
89 // Update window for output
90 if(output != nullptr)
91 {
92 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
93 window_changed = window_changed || update_window_and_padding(win, output_access);
94 output_access.set_valid_region(win, x->valid_region());
95 }
96
97 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
98 return std::make_pair(err, win);
99}
100} // namespace
101
102CLSelectKernel::CLSelectKernel()
103 : _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false)
104{
105}
106void CLSelectKernel::configure(const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output)
107{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100108 configure(CLKernelLibrary::get().get_compile_context(), c, x, y, output);
109}
110
Manuel Bottini2803f702020-04-21 16:20:03 +0100111void CLSelectKernel::configure(const CLCompileContext &compile_context, const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100112{
Georgios Pinitasaaa27182018-11-21 16:32:15 +0000113 ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output);
114 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(c->info(), x->info(), y->info(), output->info()));
115
116 _c = c;
117 _x = x;
118 _y = y;
119 _output = output;
120 _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions());
121
122 const unsigned int num_elems_processed_per_iteration = 16 / x->info()->element_size();
123
124 // Set build options
125 CLBuildOptions build_opts;
126 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(x->info()->data_type()));
Georgios Pinitasaaa27182018-11-21 16:32:15 +0000127 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
128
129 // Create kernel
130 std::string kernel_name = "select";
131 if(_has_same_rank)
132 {
133 kernel_name += "_same_rank";
134 }
135 else
136 {
137 const bool is_input_rank_greater_than_two = x->info()->tensor_shape().num_dimensions() > 2;
138 if(is_input_rank_greater_than_two)
139 {
140 const size_t width = x->info()->tensor_shape().x();
141 const size_t height = x->info()->tensor_shape().y();
142 const size_t outer_size = x->info()->tensor_shape()[x->info()->tensor_shape().num_dimensions() - 1];
143 const size_t depth_size = x->info()->tensor_shape().total_size() / (width * height * outer_size);
144 build_opts.add_option("-DDEPTH_SIZE=" + support::cpp11::to_string(depth_size));
145 }
146 kernel_name += "_different_rank";
147 kernel_name += is_input_rank_greater_than_two ? "_n" : "_2";
148 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100149 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Georgios Pinitasaaa27182018-11-21 16:32:15 +0000150
151 // Configure kernel window
152 auto win_config = validate_and_configure_window(c->info(), x->info(), y->info(), output->info());
153 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
154 ICLKernel::configure_internal(win_config.second);
155
156 _config_id = "select_";
157 _config_id += string_from_data_type(x->info()->data_type());
158 _config_id += "_";
159 _config_id += support::cpp11::to_string(x->info()->dimension(0));
160 _config_id += "_";
161 _config_id += support::cpp11::to_string(x->info()->dimension(1));
162 _config_id += "_";
163 _config_id += support::cpp11::to_string(x->info()->dimension(2));
164}
165
166Status CLSelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
167{
168 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(c, x, y, output));
169 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(c->clone().get(), x->clone().get(), y->clone().get(), output->clone().get()).first);
170 return Status{};
171}
172
173void CLSelectKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue)
174{
175 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
176 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
177
178 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
179 Window slice = collapsed.first_slice_window_3D();
180
181 if(!_has_same_rank)
182 {
183 Window vector_slice = window.first_slice_window_1D();
184 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
185 unsigned int idx = 0;
186 add_1D_tensor_argument(idx, _c, vector_slice);
187 }
188
189 do
190 {
191 unsigned int idx = _has_same_rank ? 0 : num_arguments_per_1D_tensor();
192 if(_has_same_rank)
193 {
194 add_3D_tensor_argument(idx, _c, slice);
195 }
196 add_3D_tensor_argument(idx, _x, slice);
197 add_3D_tensor_argument(idx, _y, slice);
198 add_3D_tensor_argument(idx, _output, slice);
199
200 enqueue(queue, *this, slice, lws_hint());
201 }
202 while(collapsed.slide_window_slice_3D(slice));
203}
204} // namespace arm_compute