blob: 6beee576b5d21cdd3fc583f71c4156ed27489290 [file] [log] [blame]
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClElementwiseKernel.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000025
Matthew Bentham314d3e22023-06-23 10:53:52 +000026#include "arm_compute/core/utils/ActivationFunctionUtils.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000027#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/ICLTensor.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000029#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
31#include "arm_compute/core/utils/StringUtils.h"
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000032#include "src/common/utils/Validate.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000033#include "src/core/CL/CLValidate.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
36#include "support/Cast.h"
37#include "support/StringSupport.h"
38#include <map>
39
40namespace arm_compute
41{
42namespace opencl
43{
44namespace kernels
45{
46namespace
47{
48constexpr unsigned int vector_size_byte_opencl = 16;
49
50std::map<ArithmeticOperation, std::string> supported_arithmetic_ops =
51{
52 { ArithmeticOperation::ADD, "ADD" },
53 { ArithmeticOperation::SUB, "SUB" },
54 { ArithmeticOperation::DIV, "DIV" },
55 { ArithmeticOperation::SQUARED_DIFF, "SQUARED_DIFF" },
56 { ArithmeticOperation::MIN, "MIN" },
57 { ArithmeticOperation::MAX, "MAX" },
58 { ArithmeticOperation::POWER, "POWER" },
59 { ArithmeticOperation::PRELU, "PRELU" },
60};
61
62std::map<ArithmeticOperation, std::string> supported_sat_arithmetic_ops =
63{
64 { ArithmeticOperation::ADD, "ADD" },
65 { ArithmeticOperation::SUB, "SUB" },
66};
67
68std::string generate_id_for_tuning_common(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
69{
70 std::string config_id;
71 // Set config_id for enabling LWS tuning
72 config_id = kernel_name;
73 config_id += "_";
74 config_id += lower_string(string_from_data_type(src1.data_type()));
75 config_id += "_";
76 config_id += support::cpp11::to_string(dst.dimension(0));
77 config_id += "_";
78 config_id += support::cpp11::to_string(dst.dimension(1));
79 return config_id;
80}
81
Sheri Zhanga387e272021-06-29 17:34:06 +010082Status validate_in_place_output_shape(const bool in_place, const bool src1_in_place, const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst, const TensorShape &out_shape)
83{
84 if(in_place)
85 {
86 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, src1_in_place ? src1.tensor_shape() : src2.tensor_shape(), 0),
87 "Wrong shape for dst, cannot do in_place calculation");
88 }
89 else
90 {
91 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
92 "Wrong shape for dst");
93 }
94 return Status{};
95}
96
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000097Status validate_arguments_with_float_only_supported_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
98{
99 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(&src1, &src2, &dst);
100 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
101 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::F16, DataType::F32);
102 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
103
Sheri Zhanga387e272021-06-29 17:34:06 +0100104 // Check whether it is in_place calculation
105 const bool in_place = (&src1 == &dst) || (&src2 == &dst);
106 const bool src1_in_place = in_place && (&src1 == &dst);
107
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000108 const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
109
110 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
111
112 // Validate in case of configured dst
113 if(dst.total_size() > 0)
114 {
115 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::F16, DataType::F32);
116 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
Sheri Zhanga387e272021-06-29 17:34:06 +0100117 ARM_COMPUTE_RETURN_ON_ERROR(validate_in_place_output_shape(in_place, src1_in_place, src1, src2, dst, out_shape));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000118 }
119
120 return Status{};
121}
122
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100123Status validate_arguments_divide_operation(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100124{
125 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
126 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src1);
127 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::F16, DataType::F32, DataType::S32);
128 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
129
Sheri Zhanga387e272021-06-29 17:34:06 +0100130 // Check whether it is in_place calculation
131 const bool in_place = (src1 == dst) || (src2 == dst);
132 const bool src1_in_place = in_place && (src1 == dst);
133
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100134 const TensorShape out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
135
136 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
137
138 // Validate in case of configured dst
139 if(dst->total_size() > 0)
140 {
141 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::F16, DataType::F32, DataType::S32);
142 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst);
Sheri Zhanga387e272021-06-29 17:34:06 +0100143 ARM_COMPUTE_RETURN_ON_ERROR(validate_in_place_output_shape(in_place, src1_in_place, *src1, *src2, *dst, out_shape));
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100144 }
145
146 return Status{};
147}
148
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000149Status validate_arguments_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
150{
151 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
152 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
153 DataType::S16, DataType::QSYMM16, DataType::F16,
154 DataType::S32, DataType::F32);
Georgios Pinitasda816752021-07-02 09:22:14 +0100155 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000156
Georgios Pinitasda816752021-07-02 09:22:14 +0100157 if(is_data_type_quantized_symmetric(src1.data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000158 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100159 const int32_t in1_offset = src1.quantization_info().uniform().offset;
160 const int32_t in2_offset = src2.quantization_info().uniform().offset;
161 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in1_offset != 0, "For quantized symmetric, offset must be zero");
162 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in2_offset != 0, "For quantized symmetric, offset must be zero");
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000163 }
164
Sheri Zhanga387e272021-06-29 17:34:06 +0100165 // Check whether it is in_place calculation
166 const bool in_place = (&src1 == &dst) || (&src2 == &dst);
167 const bool src1_in_place = in_place && (&src1 == &dst);
168
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000169 const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000170 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
171
172 // Validate in case of configured dst
173 if(dst.total_size() > 0)
174 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100175 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
176 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0), "Wrong shape for dst");
Sheri Zhanga387e272021-06-29 17:34:06 +0100177 ARM_COMPUTE_RETURN_ON_ERROR(validate_in_place_output_shape(in_place, src1_in_place, src1, src2, dst, out_shape));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000178
Georgios Pinitasda816752021-07-02 09:22:14 +0100179 if(is_data_type_quantized_symmetric(dst.data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000180 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100181 const int32_t offset = dst.quantization_info().uniform().offset;
182 ARM_COMPUTE_RETURN_ERROR_ON_MSG(offset != 0, "For quantized symmetric, offset must be zero");
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000183 }
184 }
185 return Status{};
186}
187
188CLBuildOptions generate_build_options_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst, const std::string &operation_string)
189{
190 CLBuildOptions build_opts;
191
192 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
193
Georgios Pinitasda816752021-07-02 09:22:14 +0100194 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src1.data_type()));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000195 build_opts.add_option("-DVEC_SIZE_IN1=" + support::cpp11::to_string(src1.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
196 build_opts.add_option("-DVEC_SIZE_IN2=" + support::cpp11::to_string(src2.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
197 build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration));
198 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(dst.dimension(0) % num_elems_processed_per_iteration));
199 build_opts.add_option("-DOP=" + operation_string);
200 if(is_data_type_quantized(src1.data_type()))
201 {
202 const UniformQuantizationInfo iq1info = src1.quantization_info().uniform();
203 const UniformQuantizationInfo iq2info = src2.quantization_info().uniform();
204 const UniformQuantizationInfo oqinfo = dst.quantization_info().uniform();
205
206 build_opts.add_option("-DOFFSET_IN1=" + support::cpp11::to_string(iq1info.offset));
207 build_opts.add_option("-DOFFSET_IN2=" + support::cpp11::to_string(iq2info.offset));
208 build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(oqinfo.offset));
209 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1info.scale));
210 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2info.scale));
211 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oqinfo.scale));
212 }
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100213 build_opts.add_option_if(src1.data_type() == DataType::S32, "-DS32");
214
Sheri Zhanga387e272021-06-29 17:34:06 +0100215 // Check whether it is in_place calculation
216 const bool in_place = (&src1 == &dst) || (&src2 == &dst);
217 const bool src1_in_place = in_place && (&src1 == &dst);
218 build_opts.add_option_if(in_place, "-DIN_PLACE");
219 build_opts.add_option_if(src1_in_place, "-DSRC1_IN_PLACE");
220
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000221 return build_opts;
222}
223
224std::pair<Status, Window> configure_window_arithmetic_common(ITensorInfo &dst)
225{
226 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
227 Window win = calculate_max_window(dst, Steps(num_elems_processed_per_iteration));
228 return std::make_pair(Status{}, win);
229}
230
231std::pair<Status, Window> validate_and_configure_window_for_arithmetic_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
232{
233 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
234 const TensorShape &out_shape = broadcast_pair.first;
235
Georgios Pinitasda816752021-07-02 09:22:14 +0100236 auto_init_if_empty(dst, out_shape, 1, src1.data_type());
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000237
238 return configure_window_arithmetic_common(dst);
239}
240
241std::pair<Status, Window> validate_and_configure_window_for_logical_binary_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
242{
243 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
244 const TensorShape &out_shape = broadcast_pair.first;
245
246 set_shape_if_empty(dst, out_shape);
247 set_data_type_if_unknown(dst, DataType::U8);
248
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000249 return configure_window_arithmetic_common(dst);
250}
251
252std::pair<Status, Window> validate_and_configure_window_for_division(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
253{
254 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
255 const TensorShape &out_shape = broadcast_pair.first;
Georgios Pinitasda816752021-07-02 09:22:14 +0100256
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000257 auto_init_if_empty(dst, out_shape, 1, src1.data_type());
Georgios Pinitasda816752021-07-02 09:22:14 +0100258
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000259 return configure_window_arithmetic_common(dst);
260}
261} // namespace
262
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100263ClElementwiseKernel::ClElementwiseKernel()
264{
265 _type = CLKernelType::ELEMENTWISE;
266}
267
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000268void ClElementwiseKernel::configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
269{
270 // Configure kernel window
271 auto win_config = validate_and_configure_window(*src1, *src2, *dst);
272 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
273
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000274 std::string kernel_name = "elementwise_operation_" + name();
275 if(is_data_type_quantized(src1->data_type()))
276 {
277 kernel_name += "_quantized";
278 }
279
280 // Set kernel build options
281 CLBuildOptions build_opts = generate_build_options(*src1, *src2, *dst);
282 if(_act_info.enabled())
283 {
284 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(_act_info.activation())));
285 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(_act_info.a()));
286 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(_act_info.b()));
287 }
288
289 // Create kernel
290 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
291
292 ICLKernel::configure_internal(win_config.second);
293
294 _config_id = generate_id_for_tuning(kernel_name, *src1, *dst);
295}
296
297void ClElementwiseKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
298{
299 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
300 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
301
302 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
303 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
304 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
305
Sheri Zhanga387e272021-06-29 17:34:06 +0100306 ARM_COMPUTE_ERROR_ON_NULLPTR(src_0, src_1, dst);
307
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000308 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
309 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
310 const TensorShape &out_shape = dst->info()->tensor_shape();
311
312 bool can_collapse = true;
313 const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1;
314 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector)
315 {
316 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
317 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
318 {
319 can_collapse = (in_shape1[d] == in_shape2[d]);
320 }
321 }
322
323 bool has_collapsed = false;
324 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
325
326 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
327 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
328
329 Window slice = collapsed.first_slice_window_3D();
330 Window slice_src1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
331 Window slice_src2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Sheri Zhanga387e272021-06-29 17:34:06 +0100332
333 // Check whether it is in_place calculation
334 const bool in_place = (src_0 == dst) || (src_1 == dst);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000335 do
336 {
337 unsigned int idx = 0;
338 add_3D_tensor_argument(idx, src_0, slice_src1);
339 add_3D_tensor_argument(idx, src_1, slice_src2);
Sheri Zhanga387e272021-06-29 17:34:06 +0100340 if(!in_place)
341 {
342 add_3D_tensor_argument(idx, dst, slice);
343 }
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000344
345 enqueue(queue, *this, slice, lws_hint());
346 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src1));
347 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src2));
348 }
349 while(collapsed.slide_window_slice_3D(slice));
350}
351
352/** Logical binary */
353
354void ClLogicalBinaryKernel::configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
355{
356 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
357 ARM_COMPUTE_ERROR_THROW_ON(ClLogicalBinaryKernel::validate(op, src1, src2, dst));
358 _op = op;
359 configure_common(compile_context, src1, src2, dst);
360}
361
362Status ClLogicalBinaryKernel::validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
363{
364 ARM_COMPUTE_UNUSED(op);
365 ARM_COMPUTE_ASSERT(op != LogicalOperation::Unknown && op != LogicalOperation::Not);
366 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
367
368 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::U8);
369 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
370
371 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
372 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_logical_binary_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
373
374 return Status{};
375}
376
377std::string ClLogicalBinaryKernel::name()
378{
379 switch(_op)
380 {
381 case LogicalOperation::And:
382 return "AND";
383 case LogicalOperation::Or:
384 return "OR";
385 case LogicalOperation::Not:
386 /* fall through */
387 default:
388 ARM_COMPUTE_ASSERT(true);
389 }
390 return "";
391}
392
393std::pair<Status, Window> ClLogicalBinaryKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
394{
395 return validate_and_configure_window_for_logical_binary_operators(src1, src2, dst);
396}
397
398CLBuildOptions ClLogicalBinaryKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
399{
400 // The arithmetic utility functions can be share
401 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
402}
403
404std::string ClLogicalBinaryKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
405{
406 return generate_id_for_tuning_common(kernel_name, src1, dst);
407}
408
409/** Arithmetic operations with saturation*/
410void ClSaturatedArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output,
411 const ConvertPolicy &policy,
412 const ActivationLayerInfo &act_info)
413{
414 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
415 ARM_COMPUTE_ERROR_THROW_ON(ClSaturatedArithmeticKernel::validate(op, input1, input2, output, policy, act_info));
416 auto padding_info = get_padding_info({ input1, input2, output });
417
418 _policy = policy;
419 _op = op;
420 _act_info = act_info;
421 configure_common(compile_context, input1, input2, output);
422 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
423}
424
425Status ClSaturatedArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
426 const ActivationLayerInfo &act_info)
427{
428 ARM_COMPUTE_UNUSED(op, policy);
429 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
430 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output));
431 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone()).first);
432 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
433
434 return Status{};
435}
436
437std::pair<Status, Window> ClSaturatedArithmeticKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
438{
439 return validate_and_configure_window_for_arithmetic_operators(input1, input2, output);
440}
441
442CLBuildOptions ClSaturatedArithmeticKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output)
443{
444 const bool has_float_out = is_data_type_float(output.data_type());
445 auto build_options = generate_build_options_with_arithmetic_rules(input1, input2, output, name());
446 build_options.add_option((_policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE");
447 return build_options;
448}
449
450std::string ClSaturatedArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output)
451{
452 auto config_id = generate_id_for_tuning_common(kernel_name, input1, output);
453 config_id += (_policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_";
454 config_id += lower_string(string_from_data_layout(input1.data_layout()));
455 return config_id;
456}
457
458std::string ClSaturatedArithmeticKernel::name()
459{
460 return supported_sat_arithmetic_ops[_op];
461}
462
463/** Arithmetic operations*/
464void ClArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
465 const ActivationLayerInfo &act_info)
466{
467 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
468 ARM_COMPUTE_ERROR_THROW_ON(ClArithmeticKernel::validate(op, src1, src2, dst, act_info));
469 auto padding_info = get_padding_info({ src1, src2, dst });
470
471 _op = op;
472 _act_info = act_info;
473 configure_common(compile_context, src1, src2, dst);
474 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
475}
476
477Status ClArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
478{
479 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100480 if(op == ArithmeticOperation::DIV)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000481 {
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100482 // Partial integer support S32/F32/F16
483 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_divide_operation(src1, src2, dst));
484 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
485 }
486 else if(op == ArithmeticOperation::POWER)
487 {
488 // Power operators doesn't support integer arithmetic
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000489 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_float_only_supported_rules(*src1, *src2, *dst));
490 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
491 }
492 else
493 {
494 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
495 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
496 }
497 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
498
499 return Status{};
500}
501std::pair<Status, Window> ClArithmeticKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
502{
503 if(_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER)
504 {
505 // Division and Power operators don't support integer arithmetic
506 return validate_and_configure_window_for_division(src1, src2, dst);
507 }
508 else
509 {
510 return validate_and_configure_window_for_arithmetic_operators(src1, src2, dst);
511 }
512}
513
514CLBuildOptions ClArithmeticKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
515{
516 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
517}
518std::string ClArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
519{
520 return generate_id_for_tuning_common(kernel_name, src1, dst);
521}
522
523std::string ClArithmeticKernel::name()
524{
525 return supported_arithmetic_ops[_op];
526}
527} // namespace kernels
528} // namespace opencl
529} // namespace arm_compute