blob: 7d204b1348d38898194772c39a2449bab744bd45 [file] [log] [blame]
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +00001/*
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/gpu/cl/kernels/ClElementwiseKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "src/core/CL/CLValidate.h"
29#include "src/core/common/Validate.h"
30#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
32#include "support/Cast.h"
33#include "support/StringSupport.h"
34#include <map>
35
36namespace arm_compute
37{
38namespace opencl
39{
40namespace kernels
41{
42namespace
43{
44constexpr unsigned int vector_size_byte_opencl = 16;
45
46std::map<ArithmeticOperation, std::string> supported_arithmetic_ops =
47{
48 { ArithmeticOperation::ADD, "ADD" },
49 { ArithmeticOperation::SUB, "SUB" },
50 { ArithmeticOperation::DIV, "DIV" },
51 { ArithmeticOperation::SQUARED_DIFF, "SQUARED_DIFF" },
52 { ArithmeticOperation::MIN, "MIN" },
53 { ArithmeticOperation::MAX, "MAX" },
54 { ArithmeticOperation::POWER, "POWER" },
55 { ArithmeticOperation::PRELU, "PRELU" },
56};
57
58std::map<ArithmeticOperation, std::string> supported_sat_arithmetic_ops =
59{
60 { ArithmeticOperation::ADD, "ADD" },
61 { ArithmeticOperation::SUB, "SUB" },
62};
63
64std::string generate_id_for_tuning_common(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
65{
66 std::string config_id;
67 // Set config_id for enabling LWS tuning
68 config_id = kernel_name;
69 config_id += "_";
70 config_id += lower_string(string_from_data_type(src1.data_type()));
71 config_id += "_";
72 config_id += support::cpp11::to_string(dst.dimension(0));
73 config_id += "_";
74 config_id += support::cpp11::to_string(dst.dimension(1));
75 return config_id;
76}
77
78Status validate_arguments_with_float_only_supported_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
79{
80 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(&src1, &src2, &dst);
81 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
82 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::F16, DataType::F32);
83 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
84
85 const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
86
87 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
88
89 // Validate in case of configured dst
90 if(dst.total_size() > 0)
91 {
92 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::F16, DataType::F32);
93 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
94 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
95 "Wrong shape for dst");
96 }
97
98 return Status{};
99}
100
101Status validate_arguments_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
102{
103 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
104 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
105 DataType::S16, DataType::QSYMM16, DataType::F16,
106 DataType::S32, DataType::F32);
107 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src2);
108 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src2, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
109 DataType::S16, DataType::QSYMM16, DataType::F16,
110 DataType::S32, DataType::F32);
111
112 const bool is_quantized = is_data_type_quantized(src1.data_type()) || is_data_type_quantized(src2.data_type());
113 if(is_quantized)
114 {
115 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
116
117 if(is_data_type_quantized_symmetric(src1.data_type()))
118 {
119 const int32_t in1_offset = src1.quantization_info().uniform().offset;
120 const int32_t in2_offset = src2.quantization_info().uniform().offset;
121 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in1_offset != 0, "For quantized symmetric, offset must be zero");
122 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in2_offset != 0, "For quantized symmetric, offset must be zero");
123 }
124 }
125
126 const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
127
128 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
129
130 // Validate in case of configured dst
131 if(dst.total_size() > 0)
132 {
133 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&dst);
134 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
135 DataType::S16, DataType::QSYMM16, DataType::F16,
136 DataType::S32, DataType::F32);
137 ARM_COMPUTE_RETURN_ERROR_ON_MSG((dst.data_type() == DataType::U8) && ((src1.data_type() != DataType::U8) || (src2.data_type() != DataType::U8)),
138 "dst can only be U8 if both inputs are U8");
139 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
140 "Wrong shape for dst");
141
142 if(is_quantized)
143 {
144 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
145
146 if(is_data_type_quantized_symmetric(dst.data_type()))
147 {
148 const int32_t offset = dst.quantization_info().uniform().offset;
149 ARM_COMPUTE_RETURN_ERROR_ON_MSG(offset != 0, "For quantized symmetric, offset must be zero");
150 }
151 }
152 }
153 return Status{};
154}
155
156CLBuildOptions generate_build_options_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst, const std::string &operation_string)
157{
158 CLBuildOptions build_opts;
159
160 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
161
162 build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(src1.data_type()));
163 build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(src2.data_type()));
164 build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(dst.data_type()));
165 build_opts.add_option("-DVEC_SIZE_IN1=" + support::cpp11::to_string(src1.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
166 build_opts.add_option("-DVEC_SIZE_IN2=" + support::cpp11::to_string(src2.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
167 build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration));
168 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(dst.dimension(0) % num_elems_processed_per_iteration));
169 build_opts.add_option("-DOP=" + operation_string);
170 if(is_data_type_quantized(src1.data_type()))
171 {
172 const UniformQuantizationInfo iq1info = src1.quantization_info().uniform();
173 const UniformQuantizationInfo iq2info = src2.quantization_info().uniform();
174 const UniformQuantizationInfo oqinfo = dst.quantization_info().uniform();
175
176 build_opts.add_option("-DOFFSET_IN1=" + support::cpp11::to_string(iq1info.offset));
177 build_opts.add_option("-DOFFSET_IN2=" + support::cpp11::to_string(iq2info.offset));
178 build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(oqinfo.offset));
179 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1info.scale));
180 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2info.scale));
181 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oqinfo.scale));
182 }
183 return build_opts;
184}
185
186std::pair<Status, Window> configure_window_arithmetic_common(ITensorInfo &dst)
187{
188 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
189 Window win = calculate_max_window(dst, Steps(num_elems_processed_per_iteration));
190 return std::make_pair(Status{}, win);
191}
192
193std::pair<Status, Window> validate_and_configure_window_for_arithmetic_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
194{
195 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
196 const TensorShape &out_shape = broadcast_pair.first;
197
198 set_shape_if_empty(dst, out_shape);
199
200 if(src1.data_type() == DataType::S16 || src2.data_type() == DataType::S16)
201 {
202 set_format_if_unknown(dst, Format::S16);
203 }
204 else if(src1.data_type() == DataType::F16 || src2.data_type() == DataType::F16)
205 {
206 set_format_if_unknown(dst, Format::F16);
207 }
208 else if(src1.data_type() == DataType::F32 || src2.data_type() == DataType::F32)
209 {
210 set_format_if_unknown(dst, Format::F32);
211 }
212 else if(src1.data_type() == DataType::QASYMM8 || src2.data_type() == DataType::QASYMM8)
213 {
214 set_data_type_if_unknown(dst, DataType::QASYMM8);
215 }
216 else if(src1.data_type() == DataType::QASYMM8_SIGNED || src2.data_type() == DataType::QASYMM8_SIGNED)
217 {
218 set_data_type_if_unknown(dst, DataType::QASYMM8_SIGNED);
219 }
220 else if(src1.data_type() == DataType::QSYMM16 || src2.data_type() == DataType::QSYMM16)
221 {
222 set_data_type_if_unknown(dst, DataType::QSYMM16);
223 }
224
225 return configure_window_arithmetic_common(dst);
226}
227
228std::pair<Status, Window> validate_and_configure_window_for_logical_binary_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
229{
230 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
231 const TensorShape &out_shape = broadcast_pair.first;
232
233 set_shape_if_empty(dst, out_shape);
234 set_data_type_if_unknown(dst, DataType::U8);
235
236 // The arithmetic utility functions can be share
237 return configure_window_arithmetic_common(dst);
238}
239
240std::pair<Status, Window> validate_and_configure_window_for_division(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
241{
242 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
243 const TensorShape &out_shape = broadcast_pair.first;
244 auto_init_if_empty(dst, out_shape, 1, src1.data_type());
245 return configure_window_arithmetic_common(dst);
246}
247} // namespace
248
249void ClElementwiseKernel::configure_common(ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
250{
251 configure_common(CLKernelLibrary::get().get_compile_context(), src1, src2, dst);
252}
253
254void ClElementwiseKernel::configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
255{
256 // Configure kernel window
257 auto win_config = validate_and_configure_window(*src1, *src2, *dst);
258 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
259
260 _src1 = src1;
261 _src2 = src2;
262 _dst = dst;
263
264 std::string kernel_name = "elementwise_operation_" + name();
265 if(is_data_type_quantized(src1->data_type()))
266 {
267 kernel_name += "_quantized";
268 }
269
270 // Set kernel build options
271 CLBuildOptions build_opts = generate_build_options(*src1, *src2, *dst);
272 if(_act_info.enabled())
273 {
274 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(_act_info.activation())));
275 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(_act_info.a()));
276 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(_act_info.b()));
277 }
278
279 // Create kernel
280 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
281
282 ICLKernel::configure_internal(win_config.second);
283
284 _config_id = generate_id_for_tuning(kernel_name, *src1, *dst);
285}
286
287void ClElementwiseKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
288{
289 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
290 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
291
292 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
293 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
294 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
295
296 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
297 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
298 const TensorShape &out_shape = dst->info()->tensor_shape();
299
300 bool can_collapse = true;
301 const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1;
302 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector)
303 {
304 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
305 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
306 {
307 can_collapse = (in_shape1[d] == in_shape2[d]);
308 }
309 }
310
311 bool has_collapsed = false;
312 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
313
314 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
315 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
316
317 Window slice = collapsed.first_slice_window_3D();
318 Window slice_src1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
319 Window slice_src2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
320 do
321 {
322 unsigned int idx = 0;
323 add_3D_tensor_argument(idx, src_0, slice_src1);
324 add_3D_tensor_argument(idx, src_1, slice_src2);
325 add_3D_tensor_argument(idx, dst, slice);
326
327 enqueue(queue, *this, slice, lws_hint());
328 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src1));
329 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src2));
330 }
331 while(collapsed.slide_window_slice_3D(slice));
332}
333
334/** Logical binary */
335
336void ClLogicalBinaryKernel::configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
337{
338 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
339 ARM_COMPUTE_ERROR_THROW_ON(ClLogicalBinaryKernel::validate(op, src1, src2, dst));
340 _op = op;
341 configure_common(compile_context, src1, src2, dst);
342}
343
344Status ClLogicalBinaryKernel::validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
345{
346 ARM_COMPUTE_UNUSED(op);
347 ARM_COMPUTE_ASSERT(op != LogicalOperation::Unknown && op != LogicalOperation::Not);
348 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
349
350 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::U8);
351 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
352
353 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
354 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_logical_binary_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
355
356 return Status{};
357}
358
359std::string ClLogicalBinaryKernel::name()
360{
361 switch(_op)
362 {
363 case LogicalOperation::And:
364 return "AND";
365 case LogicalOperation::Or:
366 return "OR";
367 case LogicalOperation::Not:
368 /* fall through */
369 default:
370 ARM_COMPUTE_ASSERT(true);
371 }
372 return "";
373}
374
375std::pair<Status, Window> ClLogicalBinaryKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
376{
377 return validate_and_configure_window_for_logical_binary_operators(src1, src2, dst);
378}
379
380CLBuildOptions ClLogicalBinaryKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
381{
382 // The arithmetic utility functions can be share
383 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
384}
385
386std::string ClLogicalBinaryKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
387{
388 return generate_id_for_tuning_common(kernel_name, src1, dst);
389}
390
391/** Arithmetic operations with saturation*/
392void ClSaturatedArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output,
393 const ConvertPolicy &policy,
394 const ActivationLayerInfo &act_info)
395{
396 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
397 ARM_COMPUTE_ERROR_THROW_ON(ClSaturatedArithmeticKernel::validate(op, input1, input2, output, policy, act_info));
398 auto padding_info = get_padding_info({ input1, input2, output });
399
400 _policy = policy;
401 _op = op;
402 _act_info = act_info;
403 configure_common(compile_context, input1, input2, output);
404 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
405}
406
407Status ClSaturatedArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
408 const ActivationLayerInfo &act_info)
409{
410 ARM_COMPUTE_UNUSED(op, policy);
411 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
412 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output));
413 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone()).first);
414 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
415
416 return Status{};
417}
418
419std::pair<Status, Window> ClSaturatedArithmeticKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
420{
421 return validate_and_configure_window_for_arithmetic_operators(input1, input2, output);
422}
423
424CLBuildOptions ClSaturatedArithmeticKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output)
425{
426 const bool has_float_out = is_data_type_float(output.data_type());
427 auto build_options = generate_build_options_with_arithmetic_rules(input1, input2, output, name());
428 build_options.add_option((_policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE");
429 return build_options;
430}
431
432std::string ClSaturatedArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output)
433{
434 auto config_id = generate_id_for_tuning_common(kernel_name, input1, output);
435 config_id += (_policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_";
436 config_id += lower_string(string_from_data_layout(input1.data_layout()));
437 return config_id;
438}
439
440std::string ClSaturatedArithmeticKernel::name()
441{
442 return supported_sat_arithmetic_ops[_op];
443}
444
445/** Arithmetic operations*/
446void ClArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
447 const ActivationLayerInfo &act_info)
448{
449 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
450 ARM_COMPUTE_ERROR_THROW_ON(ClArithmeticKernel::validate(op, src1, src2, dst, act_info));
451 auto padding_info = get_padding_info({ src1, src2, dst });
452
453 _op = op;
454 _act_info = act_info;
455 configure_common(compile_context, src1, src2, dst);
456 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
457}
458
459Status ClArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
460{
461 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
462 if(op == ArithmeticOperation::DIV || op == ArithmeticOperation::POWER)
463 {
464 // Division and Power operators don't support integer arithmetic
465 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_float_only_supported_rules(*src1, *src2, *dst));
466 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
467 }
468 else
469 {
470 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
471 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
472 }
473 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
474
475 return Status{};
476}
477std::pair<Status, Window> ClArithmeticKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
478{
479 if(_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER)
480 {
481 // Division and Power operators don't support integer arithmetic
482 return validate_and_configure_window_for_division(src1, src2, dst);
483 }
484 else
485 {
486 return validate_and_configure_window_for_arithmetic_operators(src1, src2, dst);
487 }
488}
489
490CLBuildOptions ClArithmeticKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
491{
492 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
493}
494std::string ClArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
495{
496 return generate_id_for_tuning_common(kernel_name, src1, dst);
497}
498
499std::string ClArithmeticKernel::name()
500{
501 return supported_arithmetic_ops[_op];
502}
503} // namespace kernels
504} // namespace opencl
505} // namespace arm_compute