blob: 0bf1932085f5a453c65d0856ab9d058fa029e5fd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhangf9ab9f92021-03-16 12:09:15 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/ClMulKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/TensorInfo.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CL/CLValidate.h"
32#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
34#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Georgios Pinitas8be91482019-03-26 17:23:28 +000037namespace arm_compute
38{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000039namespace opencl
40{
41namespace kernels
42{
Giorgio Arena70623822017-11-27 15:50:10 +000043namespace
44{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000045Status validate_arguments(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000046 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Giorgio Arena70623822017-11-27 15:50:10 +000047{
48 ARM_COMPUTE_UNUSED(overflow_policy);
49 ARM_COMPUTE_UNUSED(rounding_policy);
50
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000051 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
52 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src1);
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1,
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000054 1,
55 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
Suhail Munshi448cb452021-04-23 16:23:25 +010056 DataType::S16, DataType::QSYMM16, DataType::F16, DataType::S32,
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000057 DataType::F32);
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000058 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2,
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000059 1,
60 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
Suhail Munshi448cb452021-04-23 16:23:25 +010061 DataType::S16, DataType::QSYMM16, DataType::F16, DataType::S32,
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000062 DataType::F32);
Giorgio Arena70623822017-11-27 15:50:10 +000063 ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale < 0, "Scale cannot be negative.");
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000064 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
Giorgio Arena70623822017-11-27 15:50:10 +000065
Sheri Zhanga387e272021-06-29 17:34:06 +010066 // Check whether it is in_place calculation
67 const bool in_place = (src1 == dst) || (src2 == dst);
68 const bool src1_in_place = in_place && (src1 == dst);
69
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000070 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000071
72 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Giorgio Arena70623822017-11-27 15:50:10 +000073
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000074 // Validate in case of configured dst
75 if(dst->total_size() > 0)
Giorgio Arena70623822017-11-27 15:50:10 +000076 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000077 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst,
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000078 1,
79 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
80 DataType::S16, DataType::QSYMM16, DataType::F16,
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +010081 DataType::S32, DataType::F32);
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000082 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::U8 && (src1->data_type() != DataType::U8 || src2->data_type() != DataType::U8),
83 "Dst can only be U8 if both src are U8");
84 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QASYMM8 && (src1->data_type() != DataType::QASYMM8 || src2->data_type() != DataType::QASYMM8),
85 "Dst can only be QASYMM8 if both src are QASYMM8");
86 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QASYMM8_SIGNED && (src1->data_type() != DataType::QASYMM8_SIGNED || src2->data_type() != DataType::QASYMM8_SIGNED),
87 "Dst can only be QASYMM8_SIGNED if both src are QASYMM8_SIGNED");
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QSYMM16 && (src1->data_type() != DataType::QSYMM16 || src2->data_type() != DataType::QSYMM16),
89 "Dst can only be QSYMM16 if both src are QSYMM16");
Suhail Munshi448cb452021-04-23 16:23:25 +010090 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src1->data_type() == DataType::S32 || src2->data_type() == DataType::S32) && (dst->data_type() != DataType::S32),
91 "Dst must be S32 if source tensors are S32");
Sheri Zhanga387e272021-06-29 17:34:06 +010092 if(in_place)
93 {
94 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, src1_in_place ? src1->tensor_shape() : src2->tensor_shape(), 0),
95 "Wrong shape for dst, cannot do in_place calculation");
96 }
97 else
98 {
99 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0),
100 "Wrong shape for dst");
101 }
Giorgio Arena70623822017-11-27 15:50:10 +0000102 }
103
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000104 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000105}
Giorgio Arena70623822017-11-27 15:50:10 +0000106} // namespace
107
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100108ClMulKernel::ClMulKernel()
109{
110 _type = CLKernelType::ELEMENTWISE;
111}
112
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100113void ClMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, float scale,
114 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000116 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
117 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src1, src2, dst,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000118 scale, overflow_policy, rounding_policy, act_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100120 auto padding_info = get_padding_info({ src1, src2, dst });
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000121
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100122 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
123 auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
125 int scale_int = -1;
126 // Extract sign, exponent and mantissa
127 int exponent = 0;
128 float normalized_mantissa = std::frexp(scale, &exponent);
129 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
130 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
131 // Moreover, it will be negative as we deal with 1/2^n
132 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
133 {
134 // Store the positive exponent. We know that we compute 1/2^n
135 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
136 scale_int = std::abs(exponent - 1);
137 }
138
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100139 std::string acc_type;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000140 // Check if it has float src and dst
141 if(is_data_type_float(src1->data_type()) || is_data_type_float(src2->data_type()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100143 scale_int = -1;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000144 acc_type = (src1->data_type() == DataType::F32 || src2->data_type() == DataType::F32) ? "float" : "half";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 }
146 else
147 {
Suhail Munshi448cb452021-04-23 16:23:25 +0100148 if(src1->element_size() == 4 || src2->element_size() == 4)
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100149 {
Suhail Munshi448cb452021-04-23 16:23:25 +0100150 // use 64 bit accumulator for 32-bit input
151 acc_type = "long";
152 }
153 else if(src1->element_size() == 2 || src2->element_size() == 2)
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100154 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100155 // Use 32-bit accumulator for 16-bit input
156 acc_type = "int";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100157 }
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100158 else
159 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100160 // Use 16-bit accumulator for 8-bit input
161 acc_type = "ushort";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100162 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 }
164
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100165 const bool is_quantized = is_data_type_quantized(src1->data_type());
166 const unsigned int vec_size = adjust_vec_size(16 / dst->element_size(), dst->dimension(0));
167 const unsigned int vec_size_leftover = dst->dimension(0) % vec_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168
169 // Set kernel build options
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100170 std::string kernel_name = "pixelwise_mul";
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100171 CLBuildOptions build_opts;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000172 build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(src1->data_type()));
173 build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(src2->data_type()));
174 build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(dst->data_type()));
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100175 build_opts.add_option("-DVEC_SIZE_IN1=" + ((dst->dimension(0) != 1 && src1->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
176 build_opts.add_option("-DVEC_SIZE_IN2=" + ((dst->dimension(0) != 1 && src2->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
177 build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(vec_size));
178 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000179 if(is_quantized && (dst->data_type() != DataType::S32))
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100180 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000181 const UniformQuantizationInfo iq1_info = src1->quantization_info().uniform();
182 const UniformQuantizationInfo iq2_info = src2->quantization_info().uniform();
183 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100184
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000185 build_opts.add_option_if(is_data_type_quantized_asymmetric(src1->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100186 "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000187 build_opts.add_option_if(is_data_type_quantized_asymmetric(src2->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100188 "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000189 build_opts.add_option_if(is_data_type_quantized_asymmetric(dst->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100190 "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset));
191 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale));
192 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale));
193 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100194 kernel_name += "_quantized";
195 }
196 else
197 {
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100198 kernel_name += (scale_int >= 0) ? "_int" : "_float";
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000199 build_opts.add_option_if_else(overflow_policy == ConvertPolicy::WRAP || is_data_type_float(dst->data_type()), "-DWRAP", "-DSATURATE");
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100200 build_opts.add_option_if_else(rounding_policy == RoundingPolicy::TO_ZERO, "-DROUND=_rtz", "-DROUND=_rte");
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100201 build_opts.add_option("-DACC_DATA_TYPE=" + acc_type);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000202 if(act_info.enabled())
203 {
204 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
205 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
206 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
207 }
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100208 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209
Sheri Zhanga387e272021-06-29 17:34:06 +0100210 // Check whether it is in_place calculation
211 const bool in_place = (src1 == dst) || (src2 == dst);
212 const bool src1_in_place = in_place && (src1 == dst);
213 build_opts.add_option_if(in_place, "-DIN_PLACE");
214 build_opts.add_option_if(src1_in_place, "-DSRC1_IN_PLACE");
215
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100217 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218
219 // Set scale argument
Sheri Zhanga387e272021-06-29 17:34:06 +0100220 unsigned int idx = (in_place ? 2 : 3) * num_arguments_per_3D_tensor(); // Skip the src and dst parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100222 if(scale_int >= 0 && !is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 {
224 _kernel.setArg(idx++, scale_int);
225 }
226 else
227 {
228 _kernel.setArg(idx++, scale);
229 }
230
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100231 Window win = calculate_max_window(*dst, Steps(vec_size));
232 ICLKernel::configure_internal(win);
233
234 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodice8ed3ac12021-04-29 13:32:37 +0100235
236 // Set config_id for enabling LWS tuning
237 _config_id = kernel_name;
238 _config_id += "_";
239 _config_id += lower_string(string_from_data_type(dst->data_type()));
240 _config_id += "_";
241 _config_id += support::cpp11::to_string(src1->dimension(0));
242 _config_id += "_";
243 _config_id += support::cpp11::to_string(src1->dimension(1));
244 _config_id += "_";
245 _config_id += support::cpp11::to_string(src1->dimension(2));
246 _config_id += "_";
247 _config_id += support::cpp11::to_string(src2->dimension(0));
248 _config_id += "_";
249 _config_id += support::cpp11::to_string(src2->dimension(1));
250 _config_id += "_";
251 _config_id += support::cpp11::to_string(src2->dimension(2));
252 _config_id += "_";
253 _config_id += support::cpp11::to_string(dst->dimension(0));
254 _config_id += "_";
255 _config_id += support::cpp11::to_string(dst->dimension(1));
256 _config_id += "_";
257 _config_id += support::cpp11::to_string(dst->dimension(2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258}
259
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100260Status ClMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale,
261 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000262{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000263 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
264 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src1, src2, dst, scale, overflow_policy, rounding_policy, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000265
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000266 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000267}
268
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100269void ClMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270{
271 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
272 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
273
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100274 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
275 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
276 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100277
Sheri Zhanga387e272021-06-29 17:34:06 +0100278 ARM_COMPUTE_ERROR_ON_NULLPTR(src_0, src_1, dst);
279
Michalis Spyrou1009e872020-07-27 12:48:34 +0100280 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
281 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
282 const TensorShape &out_shape = dst->info()->tensor_shape();
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000283
284 bool can_collapse = true;
285 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
286 {
287 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
288 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
289 {
290 can_collapse = (in_shape1[d] == in_shape2[d]);
291 }
292 }
293
294 bool has_collapsed = false;
295 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
296
297 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
298 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
299
300 Window slice = collapsed.first_slice_window_3D();
301 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
302 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303
Sheri Zhanga387e272021-06-29 17:34:06 +0100304 // Check whether it is in_place calculation
305 const bool in_place = (src_0 == dst) || (src_1 == dst);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 do
307 {
308 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100309 add_3D_tensor_argument(idx, src_0, slice_input1);
310 add_3D_tensor_argument(idx, src_1, slice_input2);
Sheri Zhanga387e272021-06-29 17:34:06 +0100311 if(!in_place)
312 {
313 add_3D_tensor_argument(idx, dst, slice);
314 }
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100315 enqueue(queue, *this, slice, lws_hint());
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000316
Michalis Spyrouebdde652019-07-08 11:52:46 +0100317 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
318 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000320 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321}
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000322
Georgios Pinitas8be91482019-03-26 17:23:28 +0000323namespace
324{
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100325constexpr unsigned int vec_size_complex = 1;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000326
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000327Status validate_arguments_complex(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000328{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000329 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 2, DataType::F16, DataType::F32);
330 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 2, DataType::F16, DataType::F32);
331 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000332
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000333 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000334
335 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000336 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000337
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000338 // Validate in case of configured dst
339 if(dst->total_size() > 0)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000340 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000341 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 2, DataType::F16, DataType::F32);
342 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst);
343 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), "Wrong shape for dst");
Georgios Pinitas8be91482019-03-26 17:23:28 +0000344 }
345
346 return Status{};
347}
Georgios Pinitas8be91482019-03-26 17:23:28 +0000348} // namespace
349
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100350ClComplexMulKernel::ClComplexMulKernel()
351{
352 _type = CLKernelType::ELEMENTWISE;
353}
354
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100355void ClComplexMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000356{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000357 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
358 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(src1, src2, dst, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000359
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100360 auto padding_info = get_padding_info({ src1, src2, dst });
Georgios Pinitas8be91482019-03-26 17:23:28 +0000361
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100362 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
363 auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000364
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000365 CLBuildOptions build_opts;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000366 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000367 if(act_info.enabled())
368 {
369 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
370 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
371 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
372 }
373
Georgios Pinitas8be91482019-03-26 17:23:28 +0000374 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100375 _kernel = create_kernel(compile_context, "pixelwise_mul_complex", build_opts.options());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000376
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100377 Window win = calculate_max_window(*dst, Steps(vec_size_complex));
378 ICLKernel::configure_internal(win);
379
380 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000381}
382
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100383Status ClComplexMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000384{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000385 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
386 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(src1, src2, dst, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000387
388 return Status{};
389}
390
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100391void ClComplexMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000392{
393 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
394 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
395
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100396 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
397 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
398 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100399
400 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
401 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
402 const TensorShape &out_shape = dst->info()->tensor_shape();
Georgios Pinitas8be91482019-03-26 17:23:28 +0000403
404 bool can_collapse = true;
405 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
406 {
407 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
408 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
409 {
410 can_collapse = (in_shape1[d] == in_shape2[d]);
411 }
412 }
413
414 bool has_collapsed = false;
415 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
416
417 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
418 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
419
420 Window slice = collapsed.first_slice_window_3D();
421 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
422 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
423
424 do
425 {
426 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100427 add_3D_tensor_argument(idx, src_0, slice_input1);
428 add_3D_tensor_argument(idx, src_1, slice_input2);
429 add_3D_tensor_argument(idx, dst, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100430 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000431
Michalis Spyrouebdde652019-07-08 11:52:46 +0100432 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
433 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000434 }
435 while(collapsed.slide_window_slice_3D(slice));
436}
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000437} // namespace kernels
438} // namespace opencl
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100439} // namespace arm_compute