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