blob: f005e9226efb790df982666443a64374b0923a05 [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);
Georgios Pinitasda816752021-07-02 09:22:14 +0100130 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000131
Georgios Pinitasda816752021-07-02 09:22:14 +0100132 if(is_data_type_quantized_symmetric(src1.data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000133 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100134 const int32_t in1_offset = src1.quantization_info().uniform().offset;
135 const int32_t in2_offset = src2.quantization_info().uniform().offset;
136 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in1_offset != 0, "For quantized symmetric, offset must be zero");
137 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in2_offset != 0, "For quantized symmetric, offset must be zero");
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000138 }
139
140 const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000141 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
142
143 // Validate in case of configured dst
144 if(dst.total_size() > 0)
145 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100146 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
147 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0), "Wrong shape for dst");
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000148
Georgios Pinitasda816752021-07-02 09:22:14 +0100149 if(is_data_type_quantized_symmetric(dst.data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000150 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100151 const int32_t offset = dst.quantization_info().uniform().offset;
152 ARM_COMPUTE_RETURN_ERROR_ON_MSG(offset != 0, "For quantized symmetric, offset must be zero");
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000153 }
154 }
155 return Status{};
156}
157
158CLBuildOptions generate_build_options_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst, const std::string &operation_string)
159{
160 CLBuildOptions build_opts;
161
162 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
163
Georgios Pinitasda816752021-07-02 09:22:14 +0100164 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src1.data_type()));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000165 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 }
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100183 build_opts.add_option_if(src1.data_type() == DataType::S32, "-DS32");
184
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000185 return build_opts;
186}
187
188std::pair<Status, Window> configure_window_arithmetic_common(ITensorInfo &dst)
189{
190 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
191 Window win = calculate_max_window(dst, Steps(num_elems_processed_per_iteration));
192 return std::make_pair(Status{}, win);
193}
194
195std::pair<Status, Window> validate_and_configure_window_for_arithmetic_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
196{
197 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
198 const TensorShape &out_shape = broadcast_pair.first;
199
Georgios Pinitasda816752021-07-02 09:22:14 +0100200 auto_init_if_empty(dst, out_shape, 1, src1.data_type());
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000201
202 return configure_window_arithmetic_common(dst);
203}
204
205std::pair<Status, Window> validate_and_configure_window_for_logical_binary_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
206{
207 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
208 const TensorShape &out_shape = broadcast_pair.first;
209
210 set_shape_if_empty(dst, out_shape);
211 set_data_type_if_unknown(dst, DataType::U8);
212
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000213 return configure_window_arithmetic_common(dst);
214}
215
216std::pair<Status, Window> validate_and_configure_window_for_division(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
217{
218 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
219 const TensorShape &out_shape = broadcast_pair.first;
Georgios Pinitasda816752021-07-02 09:22:14 +0100220
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000221 auto_init_if_empty(dst, out_shape, 1, src1.data_type());
Georgios Pinitasda816752021-07-02 09:22:14 +0100222
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000223 return configure_window_arithmetic_common(dst);
224}
225} // namespace
226
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100227ClElementwiseKernel::ClElementwiseKernel()
228{
229 _type = CLKernelType::ELEMENTWISE;
230}
231
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000232void ClElementwiseKernel::configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
233{
234 // Configure kernel window
235 auto win_config = validate_and_configure_window(*src1, *src2, *dst);
236 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
237
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000238 std::string kernel_name = "elementwise_operation_" + name();
239 if(is_data_type_quantized(src1->data_type()))
240 {
241 kernel_name += "_quantized";
242 }
243
244 // Set kernel build options
245 CLBuildOptions build_opts = generate_build_options(*src1, *src2, *dst);
246 if(_act_info.enabled())
247 {
248 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(_act_info.activation())));
249 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(_act_info.a()));
250 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(_act_info.b()));
251 }
252
253 // Create kernel
254 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
255
256 ICLKernel::configure_internal(win_config.second);
257
258 _config_id = generate_id_for_tuning(kernel_name, *src1, *dst);
259}
260
261void ClElementwiseKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
262{
263 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
264 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
265
266 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
267 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
268 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
269
270 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
271 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
272 const TensorShape &out_shape = dst->info()->tensor_shape();
273
274 bool can_collapse = true;
275 const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1;
276 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector)
277 {
278 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
279 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
280 {
281 can_collapse = (in_shape1[d] == in_shape2[d]);
282 }
283 }
284
285 bool has_collapsed = false;
286 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
287
288 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
289 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
290
291 Window slice = collapsed.first_slice_window_3D();
292 Window slice_src1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
293 Window slice_src2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
294 do
295 {
296 unsigned int idx = 0;
297 add_3D_tensor_argument(idx, src_0, slice_src1);
298 add_3D_tensor_argument(idx, src_1, slice_src2);
299 add_3D_tensor_argument(idx, dst, slice);
300
301 enqueue(queue, *this, slice, lws_hint());
302 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src1));
303 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src2));
304 }
305 while(collapsed.slide_window_slice_3D(slice));
306}
307
308/** Logical binary */
309
310void ClLogicalBinaryKernel::configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
311{
312 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
313 ARM_COMPUTE_ERROR_THROW_ON(ClLogicalBinaryKernel::validate(op, src1, src2, dst));
314 _op = op;
315 configure_common(compile_context, src1, src2, dst);
316}
317
318Status ClLogicalBinaryKernel::validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
319{
320 ARM_COMPUTE_UNUSED(op);
321 ARM_COMPUTE_ASSERT(op != LogicalOperation::Unknown && op != LogicalOperation::Not);
322 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
323
324 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::U8);
325 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
326
327 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
328 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_logical_binary_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
329
330 return Status{};
331}
332
333std::string ClLogicalBinaryKernel::name()
334{
335 switch(_op)
336 {
337 case LogicalOperation::And:
338 return "AND";
339 case LogicalOperation::Or:
340 return "OR";
341 case LogicalOperation::Not:
342 /* fall through */
343 default:
344 ARM_COMPUTE_ASSERT(true);
345 }
346 return "";
347}
348
349std::pair<Status, Window> ClLogicalBinaryKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
350{
351 return validate_and_configure_window_for_logical_binary_operators(src1, src2, dst);
352}
353
354CLBuildOptions ClLogicalBinaryKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
355{
356 // The arithmetic utility functions can be share
357 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
358}
359
360std::string ClLogicalBinaryKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
361{
362 return generate_id_for_tuning_common(kernel_name, src1, dst);
363}
364
365/** Arithmetic operations with saturation*/
366void ClSaturatedArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output,
367 const ConvertPolicy &policy,
368 const ActivationLayerInfo &act_info)
369{
370 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
371 ARM_COMPUTE_ERROR_THROW_ON(ClSaturatedArithmeticKernel::validate(op, input1, input2, output, policy, act_info));
372 auto padding_info = get_padding_info({ input1, input2, output });
373
374 _policy = policy;
375 _op = op;
376 _act_info = act_info;
377 configure_common(compile_context, input1, input2, output);
378 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
379}
380
381Status ClSaturatedArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
382 const ActivationLayerInfo &act_info)
383{
384 ARM_COMPUTE_UNUSED(op, policy);
385 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
386 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output));
387 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone()).first);
388 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
389
390 return Status{};
391}
392
393std::pair<Status, Window> ClSaturatedArithmeticKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
394{
395 return validate_and_configure_window_for_arithmetic_operators(input1, input2, output);
396}
397
398CLBuildOptions ClSaturatedArithmeticKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output)
399{
400 const bool has_float_out = is_data_type_float(output.data_type());
401 auto build_options = generate_build_options_with_arithmetic_rules(input1, input2, output, name());
402 build_options.add_option((_policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE");
403 return build_options;
404}
405
406std::string ClSaturatedArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output)
407{
408 auto config_id = generate_id_for_tuning_common(kernel_name, input1, output);
409 config_id += (_policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_";
410 config_id += lower_string(string_from_data_layout(input1.data_layout()));
411 return config_id;
412}
413
414std::string ClSaturatedArithmeticKernel::name()
415{
416 return supported_sat_arithmetic_ops[_op];
417}
418
419/** Arithmetic operations*/
420void ClArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
421 const ActivationLayerInfo &act_info)
422{
423 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
424 ARM_COMPUTE_ERROR_THROW_ON(ClArithmeticKernel::validate(op, src1, src2, dst, act_info));
425 auto padding_info = get_padding_info({ src1, src2, dst });
426
427 _op = op;
428 _act_info = act_info;
429 configure_common(compile_context, src1, src2, dst);
430 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
431}
432
433Status ClArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
434{
435 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100436 if(op == ArithmeticOperation::DIV)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000437 {
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100438 // Partial integer support S32/F32/F16
439 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_divide_operation(src1, src2, dst));
440 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
441 }
442 else if(op == ArithmeticOperation::POWER)
443 {
444 // Power operators doesn't support integer arithmetic
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000445 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_float_only_supported_rules(*src1, *src2, *dst));
446 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
447 }
448 else
449 {
450 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
451 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
452 }
453 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
454
455 return Status{};
456}
457std::pair<Status, Window> ClArithmeticKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
458{
459 if(_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER)
460 {
461 // Division and Power operators don't support integer arithmetic
462 return validate_and_configure_window_for_division(src1, src2, dst);
463 }
464 else
465 {
466 return validate_and_configure_window_for_arithmetic_operators(src1, src2, dst);
467 }
468}
469
470CLBuildOptions ClArithmeticKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
471{
472 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
473}
474std::string ClArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
475{
476 return generate_id_for_tuning_common(kernel_name, src1, dst);
477}
478
479std::string ClArithmeticKernel::name()
480{
481 return supported_arithmetic_ops[_op];
482}
483} // namespace kernels
484} // namespace opencl
485} // namespace arm_compute