blob: cdb3527a92147430454be9bc5f650d46a448fa69 [file] [log] [blame]
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +00003 *
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClElementwiseKernel.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLTensor.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000028#include "arm_compute/core/Utils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/utils/ActivationFunctionUtils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
31#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000033#include "src/common/utils/Validate.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000034#include "src/core/CL/CLValidate.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000035#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
37#include "support/Cast.h"
38#include "support/StringSupport.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000040#include <map>
41
42namespace arm_compute
43{
44namespace opencl
45{
46namespace kernels
47{
48namespace
49{
50constexpr unsigned int vector_size_byte_opencl = 16;
51
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052std::map<ArithmeticOperation, std::string> supported_arithmetic_ops = {
53 {ArithmeticOperation::ADD, "ADD"}, {ArithmeticOperation::SUB, "SUB"},
54 {ArithmeticOperation::DIV, "DIV"}, {ArithmeticOperation::SQUARED_DIFF, "SQUARED_DIFF"},
55 {ArithmeticOperation::MIN, "MIN"}, {ArithmeticOperation::MAX, "MAX"},
56 {ArithmeticOperation::POWER, "POWER"}, {ArithmeticOperation::PRELU, "PRELU"},
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000057};
58
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059std::map<ArithmeticOperation, std::string> supported_sat_arithmetic_ops = {
60 {ArithmeticOperation::ADD, "ADD"},
61 {ArithmeticOperation::SUB, "SUB"},
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000062};
63
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064std::string
65generate_id_for_tuning_common(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000066{
67 std::string config_id;
68 // Set config_id for enabling LWS tuning
69 config_id = kernel_name;
70 config_id += "_";
71 config_id += lower_string(string_from_data_type(src1.data_type()));
72 config_id += "_";
73 config_id += support::cpp11::to_string(dst.dimension(0));
74 config_id += "_";
75 config_id += support::cpp11::to_string(dst.dimension(1));
76 return config_id;
77}
78
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079Status validate_in_place_output_shape(const bool in_place,
80 const bool src1_in_place,
81 const ITensorInfo &src1,
82 const ITensorInfo &src2,
83 const ITensorInfo &dst,
84 const TensorShape &out_shape)
Sheri Zhanga387e272021-06-29 17:34:06 +010085{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 if (in_place)
Sheri Zhanga387e272021-06-29 17:34:06 +010087 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
89 detail::have_different_dimensions(out_shape, src1_in_place ? src1.tensor_shape() : src2.tensor_shape(), 0),
90 "Wrong shape for dst, cannot do in_place calculation");
Sheri Zhanga387e272021-06-29 17:34:06 +010091 }
92 else
93 {
94 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
95 "Wrong shape for dst");
96 }
97 return Status{};
98}
99
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100Status validate_arguments_with_float_only_supported_rules(const ITensorInfo &src1,
101 const ITensorInfo &src2,
102 const ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000103{
104 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(&src1, &src2, &dst);
105 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
106 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::F16, DataType::F32);
107 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
108
Sheri Zhanga387e272021-06-29 17:34:06 +0100109 // Check whether it is in_place calculation
110 const bool in_place = (&src1 == &dst) || (&src2 == &dst);
111 const bool src1_in_place = in_place && (&src1 == &dst);
112
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000113 const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
114
115 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
116
117 // Validate in case of configured dst
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 if (dst.total_size() > 0)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000119 {
120 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::F16, DataType::F32);
121 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 ARM_COMPUTE_RETURN_ON_ERROR(
123 validate_in_place_output_shape(in_place, src1_in_place, src1, src2, dst, out_shape));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000124 }
125
126 return Status{};
127}
128
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100129Status validate_arguments_divide_operation(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100130{
131 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
132 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src1);
133 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::F16, DataType::F32, DataType::S32);
134 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
135
Sheri Zhanga387e272021-06-29 17:34:06 +0100136 // Check whether it is in_place calculation
137 const bool in_place = (src1 == dst) || (src2 == dst);
138 const bool src1_in_place = in_place && (src1 == dst);
139
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100140 const TensorShape out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
141
142 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
143
144 // Validate in case of configured dst
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 if (dst->total_size() > 0)
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100146 {
147 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::F16, DataType::F32, DataType::S32);
148 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 ARM_COMPUTE_RETURN_ON_ERROR(
150 validate_in_place_output_shape(in_place, src1_in_place, *src1, *src2, *dst, out_shape));
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100151 }
152
153 return Status{};
154}
155
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156Status
157validate_arguments_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000158{
159 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::U8, DataType::QASYMM8,
161 DataType::QASYMM8_SIGNED, DataType::S16, DataType::QSYMM16,
162 DataType::F16, DataType::S32, DataType::F32);
Georgios Pinitasda816752021-07-02 09:22:14 +0100163 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000164
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100165 if (is_data_type_quantized_symmetric(src1.data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000166 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100167 const int32_t in1_offset = src1.quantization_info().uniform().offset;
168 const int32_t in2_offset = src2.quantization_info().uniform().offset;
169 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in1_offset != 0, "For quantized symmetric, offset must be zero");
170 ARM_COMPUTE_RETURN_ERROR_ON_MSG(in2_offset != 0, "For quantized symmetric, offset must be zero");
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000171 }
172
Sheri Zhanga387e272021-06-29 17:34:06 +0100173 // Check whether it is in_place calculation
174 const bool in_place = (&src1 == &dst) || (&src2 == &dst);
175 const bool src1_in_place = in_place && (&src1 == &dst);
176
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000177 const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000178 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
179
180 // Validate in case of configured dst
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100181 if (dst.total_size() > 0)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000182 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100183 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
185 "Wrong shape for dst");
186 ARM_COMPUTE_RETURN_ON_ERROR(
187 validate_in_place_output_shape(in_place, src1_in_place, src1, src2, dst, out_shape));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000188
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 if (is_data_type_quantized_symmetric(dst.data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000190 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100191 const int32_t offset = dst.quantization_info().uniform().offset;
192 ARM_COMPUTE_RETURN_ERROR_ON_MSG(offset != 0, "For quantized symmetric, offset must be zero");
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000193 }
194 }
195 return Status{};
196}
197
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198CLBuildOptions generate_build_options_with_arithmetic_rules(const ITensorInfo &src1,
199 const ITensorInfo &src2,
200 const ITensorInfo &dst,
201 const std::string &operation_string)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000202{
203 CLBuildOptions build_opts;
204
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 const unsigned int num_elems_processed_per_iteration =
206 adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000207
Georgios Pinitasda816752021-07-02 09:22:14 +0100208 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src1.data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 build_opts.add_option("-DVEC_SIZE_IN1=" +
210 support::cpp11::to_string(src1.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
211 build_opts.add_option("-DVEC_SIZE_IN2=" +
212 support::cpp11::to_string(src2.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000213 build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" +
215 support::cpp11::to_string(dst.dimension(0) % num_elems_processed_per_iteration));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000216 build_opts.add_option("-DOP=" + operation_string);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100217 if (is_data_type_quantized(src1.data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000218 {
219 const UniformQuantizationInfo iq1info = src1.quantization_info().uniform();
220 const UniformQuantizationInfo iq2info = src2.quantization_info().uniform();
221 const UniformQuantizationInfo oqinfo = dst.quantization_info().uniform();
222
223 build_opts.add_option("-DOFFSET_IN1=" + support::cpp11::to_string(iq1info.offset));
224 build_opts.add_option("-DOFFSET_IN2=" + support::cpp11::to_string(iq2info.offset));
225 build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(oqinfo.offset));
226 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1info.scale));
227 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2info.scale));
228 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oqinfo.scale));
229 }
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100230 build_opts.add_option_if(src1.data_type() == DataType::S32, "-DS32");
231
Sheri Zhanga387e272021-06-29 17:34:06 +0100232 // Check whether it is in_place calculation
233 const bool in_place = (&src1 == &dst) || (&src2 == &dst);
234 const bool src1_in_place = in_place && (&src1 == &dst);
235 build_opts.add_option_if(in_place, "-DIN_PLACE");
236 build_opts.add_option_if(src1_in_place, "-DSRC1_IN_PLACE");
237
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000238 return build_opts;
239}
240
241std::pair<Status, Window> configure_window_arithmetic_common(ITensorInfo &dst)
242{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100243 const unsigned int num_elems_processed_per_iteration =
244 adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
245 Window win = calculate_max_window(dst, Steps(num_elems_processed_per_iteration));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000246 return std::make_pair(Status{}, win);
247}
248
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100249std::pair<Status, Window>
250validate_and_configure_window_for_arithmetic_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000251{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100252 const std::pair<TensorShape, ValidRegion> broadcast_pair =
253 ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000254 const TensorShape &out_shape = broadcast_pair.first;
255
Georgios Pinitasda816752021-07-02 09:22:14 +0100256 auto_init_if_empty(dst, out_shape, 1, src1.data_type());
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000257
258 return configure_window_arithmetic_common(dst);
259}
260
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100261std::pair<Status, Window>
262validate_and_configure_window_for_logical_binary_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000263{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100264 const std::pair<TensorShape, ValidRegion> broadcast_pair =
265 ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000266 const TensorShape &out_shape = broadcast_pair.first;
267
268 set_shape_if_empty(dst, out_shape);
269 set_data_type_if_unknown(dst, DataType::U8);
270
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000271 return configure_window_arithmetic_common(dst);
272}
273
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100274std::pair<Status, Window>
275validate_and_configure_window_for_division(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000276{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100277 const std::pair<TensorShape, ValidRegion> broadcast_pair =
278 ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000279 const TensorShape &out_shape = broadcast_pair.first;
Georgios Pinitasda816752021-07-02 09:22:14 +0100280
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000281 auto_init_if_empty(dst, out_shape, 1, src1.data_type());
Georgios Pinitasda816752021-07-02 09:22:14 +0100282
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000283 return configure_window_arithmetic_common(dst);
284}
285} // namespace
286
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100287ClElementwiseKernel::ClElementwiseKernel()
288{
289 _type = CLKernelType::ELEMENTWISE;
290}
291
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292void ClElementwiseKernel::configure_common(const ClCompileContext &compile_context,
293 ITensorInfo *src1,
294 ITensorInfo *src2,
295 ITensorInfo *dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000296{
297 // Configure kernel window
298 auto win_config = validate_and_configure_window(*src1, *src2, *dst);
299 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
300
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000301 std::string kernel_name = "elementwise_operation_" + name();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100302 if (is_data_type_quantized(src1->data_type()))
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000303 {
304 kernel_name += "_quantized";
305 }
306
307 // Set kernel build options
308 CLBuildOptions build_opts = generate_build_options(*src1, *src2, *dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100309 if (_act_info.enabled())
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000310 {
311 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(_act_info.activation())));
312 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(_act_info.a()));
313 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(_act_info.b()));
314 }
315
316 // Create kernel
317 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
318
319 ICLKernel::configure_internal(win_config.second);
320
321 _config_id = generate_id_for_tuning(kernel_name, *src1, *dst);
322}
323
324void ClElementwiseKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
325{
326 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
327 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
328
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100329 const auto src_0 =
330 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
331 const auto src_1 =
332 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
333 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000334
Sheri Zhanga387e272021-06-29 17:34:06 +0100335 ARM_COMPUTE_ERROR_ON_NULLPTR(src_0, src_1, dst);
336
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000337 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
338 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
339 const TensorShape &out_shape = dst->info()->tensor_shape();
340
341 bool can_collapse = true;
342 const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100343 if (std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000344 {
345 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100346 for (size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000347 {
348 can_collapse = (in_shape1[d] == in_shape2[d]);
349 }
350 }
351
352 bool has_collapsed = false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100353 Window collapsed =
354 can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000355
356 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
357 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
358
359 Window slice = collapsed.first_slice_window_3D();
360 Window slice_src1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
361 Window slice_src2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Sheri Zhanga387e272021-06-29 17:34:06 +0100362
363 // Check whether it is in_place calculation
364 const bool in_place = (src_0 == dst) || (src_1 == dst);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000365 do
366 {
367 unsigned int idx = 0;
368 add_3D_tensor_argument(idx, src_0, slice_src1);
369 add_3D_tensor_argument(idx, src_1, slice_src2);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100370 if (!in_place)
Sheri Zhanga387e272021-06-29 17:34:06 +0100371 {
372 add_3D_tensor_argument(idx, dst, slice);
373 }
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000374
375 enqueue(queue, *this, slice, lws_hint());
376 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src1));
377 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src2));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100378 } while (collapsed.slide_window_slice_3D(slice));
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000379}
380
381/** Logical binary */
382
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100383void ClLogicalBinaryKernel::configure(const ClCompileContext &compile_context,
384 LogicalOperation op,
385 ITensorInfo *src1,
386 ITensorInfo *src2,
387 ITensorInfo *dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000388{
389 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
390 ARM_COMPUTE_ERROR_THROW_ON(ClLogicalBinaryKernel::validate(op, src1, src2, dst));
391 _op = op;
392 configure_common(compile_context, src1, src2, dst);
393}
394
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100395Status ClLogicalBinaryKernel::validate(LogicalOperation op,
396 const ITensorInfo *src1,
397 const ITensorInfo *src2,
398 const ITensorInfo *dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000399{
400 ARM_COMPUTE_UNUSED(op);
401 ARM_COMPUTE_ASSERT(op != LogicalOperation::Unknown && op != LogicalOperation::Not);
402 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
403
404 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::U8);
405 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
406
407 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100408 ARM_COMPUTE_RETURN_ON_ERROR(
409 validate_and_configure_window_for_logical_binary_operators(*src1->clone(), *src2->clone(), *dst->clone())
410 .first);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000411
412 return Status{};
413}
414
415std::string ClLogicalBinaryKernel::name()
416{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100417 switch (_op)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000418 {
419 case LogicalOperation::And:
420 return "AND";
421 case LogicalOperation::Or:
422 return "OR";
423 case LogicalOperation::Not:
424 /* fall through */
425 default:
426 ARM_COMPUTE_ASSERT(true);
427 }
428 return "";
429}
430
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100431std::pair<Status, Window>
432ClLogicalBinaryKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000433{
434 return validate_and_configure_window_for_logical_binary_operators(src1, src2, dst);
435}
436
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100437CLBuildOptions
438ClLogicalBinaryKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000439{
440 // The arithmetic utility functions can be share
441 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
442}
443
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100444std::string ClLogicalBinaryKernel::generate_id_for_tuning(const std::string &kernel_name,
445 const ITensorInfo &src1,
446 const ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000447{
448 return generate_id_for_tuning_common(kernel_name, src1, dst);
449}
450
451/** Arithmetic operations with saturation*/
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100452void ClSaturatedArithmeticKernel::configure(const ClCompileContext &compile_context,
453 ArithmeticOperation op,
454 ITensorInfo *input1,
455 ITensorInfo *input2,
456 ITensorInfo *output,
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000457 const ConvertPolicy &policy,
458 const ActivationLayerInfo &act_info)
459{
460 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
461 ARM_COMPUTE_ERROR_THROW_ON(ClSaturatedArithmeticKernel::validate(op, input1, input2, output, policy, act_info));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100462 auto padding_info = get_padding_info({input1, input2, output});
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000463
464 _policy = policy;
465 _op = op;
466 _act_info = act_info;
467 configure_common(compile_context, input1, input2, output);
468 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
469}
470
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100471Status ClSaturatedArithmeticKernel::validate(ArithmeticOperation op,
472 const ITensorInfo *input1,
473 const ITensorInfo *input2,
474 const ITensorInfo *output,
475 const ConvertPolicy &policy,
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000476 const ActivationLayerInfo &act_info)
477{
478 ARM_COMPUTE_UNUSED(op, policy);
479 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
480 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100481 ARM_COMPUTE_RETURN_ON_ERROR(
482 validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone())
483 .first);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000484 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
485
486 return Status{};
487}
488
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100489std::pair<Status, Window> ClSaturatedArithmeticKernel::validate_and_configure_window(ITensorInfo &input1,
490 ITensorInfo &input2,
491 ITensorInfo &output)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000492{
493 return validate_and_configure_window_for_arithmetic_operators(input1, input2, output);
494}
495
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100496CLBuildOptions ClSaturatedArithmeticKernel::generate_build_options(const ITensorInfo &input1,
497 const ITensorInfo &input2,
498 const ITensorInfo &output)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000499{
500 const bool has_float_out = is_data_type_float(output.data_type());
501 auto build_options = generate_build_options_with_arithmetic_rules(input1, input2, output, name());
502 build_options.add_option((_policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE");
503 return build_options;
504}
505
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100506std::string ClSaturatedArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name,
507 const ITensorInfo &input1,
508 const ITensorInfo &output)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000509{
510 auto config_id = generate_id_for_tuning_common(kernel_name, input1, output);
511 config_id += (_policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_";
512 config_id += lower_string(string_from_data_layout(input1.data_layout()));
513 return config_id;
514}
515
516std::string ClSaturatedArithmeticKernel::name()
517{
518 return supported_sat_arithmetic_ops[_op];
519}
520
521/** Arithmetic operations*/
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100522void ClArithmeticKernel::configure(const ClCompileContext &compile_context,
523 ArithmeticOperation op,
524 ITensorInfo *src1,
525 ITensorInfo *src2,
526 ITensorInfo *dst,
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000527 const ActivationLayerInfo &act_info)
528{
529 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
530 ARM_COMPUTE_ERROR_THROW_ON(ClArithmeticKernel::validate(op, src1, src2, dst, act_info));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100531 auto padding_info = get_padding_info({src1, src2, dst});
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000532
533 _op = op;
534 _act_info = act_info;
535 configure_common(compile_context, src1, src2, dst);
536 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
537}
538
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100539Status ClArithmeticKernel::validate(ArithmeticOperation op,
540 const ITensorInfo *src1,
541 const ITensorInfo *src2,
542 const ITensorInfo *dst,
543 const ActivationLayerInfo &act_info)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000544{
545 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100546 if (op == ArithmeticOperation::DIV)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000547 {
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100548 // Partial integer support S32/F32/F16
549 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_divide_operation(src1, src2, dst));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100550 ARM_COMPUTE_RETURN_ON_ERROR(
551 validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100552 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100553 else if (op == ArithmeticOperation::POWER)
Suhail Munshiae1c9fe2021-04-14 12:16:49 +0100554 {
555 // Power operators doesn't support integer arithmetic
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000556 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_float_only_supported_rules(*src1, *src2, *dst));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100557 ARM_COMPUTE_RETURN_ON_ERROR(
558 validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000559 }
560 else
561 {
562 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100563 ARM_COMPUTE_RETURN_ON_ERROR(
564 validate_and_configure_window_for_arithmetic_operators(*src1->clone(), *src2->clone(), *dst->clone())
565 .first);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000566 }
567 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
568
569 return Status{};
570}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100571std::pair<Status, Window>
572ClArithmeticKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000573{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100574 if (_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000575 {
576 // Division and Power operators don't support integer arithmetic
577 return validate_and_configure_window_for_division(src1, src2, dst);
578 }
579 else
580 {
581 return validate_and_configure_window_for_arithmetic_operators(src1, src2, dst);
582 }
583}
584
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100585CLBuildOptions
586ClArithmeticKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000587{
588 return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
589}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100590std::string ClArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name,
591 const ITensorInfo &src1,
592 const ITensorInfo &dst)
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000593{
594 return generate_id_for_tuning_common(kernel_name, src1, dst);
595}
596
597std::string ClArithmeticKernel::name()
598{
599 return supported_arithmetic_ops[_op];
600}
601} // namespace kernels
602} // namespace opencl
603} // namespace arm_compute