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