blob: b645353dd67a9383e97c31969f102b2bc1aaabc3 [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
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100101Status validate_arguments_divide_operation(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100102{
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
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100274ClElementwiseKernel::ClElementwiseKernel()
275{
276 _type = CLKernelType::ELEMENTWISE;
277}
278
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000279void 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
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000285 std::string kernel_name = "elementwise_operation_" + name();
286 if(is_data_type_quantized(src1->data_type()))
287 {
288 kernel_name += "_quantized";
289 }
290
291 // Set kernel build options
292 CLBuildOptions build_opts = generate_build_options(*src1, *src2, *dst);
293 if(_act_info.enabled())
294 {
295 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(_act_info.activation())));
296 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(_act_info.a()));
297 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(_act_info.b()));
298 }
299
300 // Create kernel
301 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
302
303 ICLKernel::configure_internal(win_config.second);
304
305 _config_id = generate_id_for_tuning(kernel_name, *src1, *dst);
306}
307
308void ClElementwiseKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
309{
310 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
311 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
312
313 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
314 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
315 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
316
317 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
318 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
319 const TensorShape &out_shape = dst->info()->tensor_shape();
320
321 bool can_collapse = true;
322 const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1;
323 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector)
324 {
325 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
326 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
327 {
328 can_collapse = (in_shape1[d] == in_shape2[d]);
329 }
330 }
331
332 bool has_collapsed = false;
333 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
334
335 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
336 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
337
338 Window slice = collapsed.first_slice_window_3D();
339 Window slice_src1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
340 Window slice_src2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
341 do
342 {
343 unsigned int idx = 0;
344 add_3D_tensor_argument(idx, src_0, slice_src1);
345 add_3D_tensor_argument(idx, src_1, slice_src2);
346 add_3D_tensor_argument(idx, dst, slice);
347
348 enqueue(queue, *this, slice, lws_hint());
349 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src1));
350 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src2));
351 }
352 while(collapsed.slide_window_slice_3D(slice));
353}
354
355/** Logical binary */
356
357void ClLogicalBinaryKernel::configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
358{
359 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
360 ARM_COMPUTE_ERROR_THROW_ON(ClLogicalBinaryKernel::validate(op, src1, src2, dst));
361 _op = op;
362 configure_common(compile_context, src1, src2, dst);
363}
364
365Status ClLogicalBinaryKernel::validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
366{
367 ARM_COMPUTE_UNUSED(op);
368 ARM_COMPUTE_ASSERT(op != LogicalOperation::Unknown && op != LogicalOperation::Not);
369 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
370
371 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::U8);
372 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
373
374 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
375 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_logical_binary_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
376
377 return Status{};
378}
379
380std::string ClLogicalBinaryKernel::name()
381{
382 switch(_op)
383 {
384 case LogicalOperation::And:
385 return "AND";
386 case LogicalOperation::Or:
387 return "OR";
388 case LogicalOperation::Not:
389 /* fall through */
390 default:
391 ARM_COMPUTE_ASSERT(true);
392 }
393 return "";
394}
395
396std::pair<Status, Window> ClLogicalBinaryKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
397{
398 return validate_and_configure_window_for_logical_binary_operators(src1, src2, dst);
399}
400
401CLBuildOptions ClLogicalBinaryKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
402{
403 // The arithmetic utility functions can be share
404 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
405}
406
407std::string ClLogicalBinaryKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
408{
409 return generate_id_for_tuning_common(kernel_name, src1, dst);
410}
411
412/** Arithmetic operations with saturation*/
413void ClSaturatedArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output,
414 const ConvertPolicy &policy,
415 const ActivationLayerInfo &act_info)
416{
417 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
418 ARM_COMPUTE_ERROR_THROW_ON(ClSaturatedArithmeticKernel::validate(op, input1, input2, output, policy, act_info));
419 auto padding_info = get_padding_info({ input1, input2, output });
420
421 _policy = policy;
422 _op = op;
423 _act_info = act_info;
424 configure_common(compile_context, input1, input2, output);
425 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
426}
427
428Status ClSaturatedArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
429 const ActivationLayerInfo &act_info)
430{
431 ARM_COMPUTE_UNUSED(op, policy);
432 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
433 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output));
434 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone()).first);
435 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
436
437 return Status{};
438}
439
440std::pair<Status, Window> ClSaturatedArithmeticKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
441{
442 return validate_and_configure_window_for_arithmetic_operators(input1, input2, output);
443}
444
445CLBuildOptions ClSaturatedArithmeticKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output)
446{
447 const bool has_float_out = is_data_type_float(output.data_type());
448 auto build_options = generate_build_options_with_arithmetic_rules(input1, input2, output, name());
449 build_options.add_option((_policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE");
450 return build_options;
451}
452
453std::string ClSaturatedArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output)
454{
455 auto config_id = generate_id_for_tuning_common(kernel_name, input1, output);
456 config_id += (_policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_";
457 config_id += lower_string(string_from_data_layout(input1.data_layout()));
458 return config_id;
459}
460
461std::string ClSaturatedArithmeticKernel::name()
462{
463 return supported_sat_arithmetic_ops[_op];
464}
465
466/** Arithmetic operations*/
467void ClArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
468 const ActivationLayerInfo &act_info)
469{
470 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
471 ARM_COMPUTE_ERROR_THROW_ON(ClArithmeticKernel::validate(op, src1, src2, dst, act_info));
472 auto padding_info = get_padding_info({ src1, src2, dst });
473
474 _op = op;
475 _act_info = act_info;
476 configure_common(compile_context, src1, src2, dst);
477 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
478}
479
480Status ClArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
481{
482 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100483 if(op == ArithmeticOperation::DIV)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000484 {
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100485 // Partial integer support S32/F32/F16
486 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_divide_operation(src1, src2, dst));
487 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
488 }
489 else if(op == ArithmeticOperation::POWER)
490 {
491 // Power operators doesn't support integer arithmetic
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000492 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_float_only_supported_rules(*src1, *src2, *dst));
493 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
494 }
495 else
496 {
497 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
498 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
499 }
500 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
501
502 return Status{};
503}
504std::pair<Status, Window> ClArithmeticKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
505{
506 if(_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER)
507 {
508 // Division and Power operators don't support integer arithmetic
509 return validate_and_configure_window_for_division(src1, src2, dst);
510 }
511 else
512 {
513 return validate_and_configure_window_for_arithmetic_operators(src1, src2, dst);
514 }
515}
516
517CLBuildOptions ClArithmeticKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
518{
519 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
520}
521std::string ClArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
522{
523 return generate_id_for_tuning_common(kernel_name, src1, dst);
524}
525
526std::string ClArithmeticKernel::name()
527{
528 return supported_arithmetic_ops[_op];
529}
530} // namespace kernels
531} // namespace opencl
532} // namespace arm_compute