blob: 65f3bec09920c867f214eafd781334949d01cb80 [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 Pinitas0dc0d8e2021-04-30 03:18:37 +010024#include "src/core/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 Zhangf9ab9f92021-03-16 12:09:15 +000066 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000067
68 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Giorgio Arena70623822017-11-27 15:50:10 +000069
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000070 // Validate in case of configured dst
71 if(dst->total_size() > 0)
Giorgio Arena70623822017-11-27 15:50:10 +000072 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000073 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst,
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000074 1,
75 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
76 DataType::S16, DataType::QSYMM16, DataType::F16,
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +010077 DataType::S32, DataType::F32);
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000078 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::U8 && (src1->data_type() != DataType::U8 || src2->data_type() != DataType::U8),
79 "Dst can only be U8 if both src are U8");
80 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QASYMM8 && (src1->data_type() != DataType::QASYMM8 || src2->data_type() != DataType::QASYMM8),
81 "Dst can only be QASYMM8 if both src are QASYMM8");
82 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QASYMM8_SIGNED && (src1->data_type() != DataType::QASYMM8_SIGNED || src2->data_type() != DataType::QASYMM8_SIGNED),
83 "Dst can only be QASYMM8_SIGNED if both src are QASYMM8_SIGNED");
84 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QSYMM16 && (src1->data_type() != DataType::QSYMM16 || src2->data_type() != DataType::QSYMM16),
85 "Dst can only be QSYMM16 if both src are QSYMM16");
Suhail Munshi448cb452021-04-23 16:23:25 +010086 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src1->data_type() == DataType::S32 || src2->data_type() == DataType::S32) && (dst->data_type() != DataType::S32),
87 "Dst must be S32 if source tensors are S32");
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000088 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), "Wrong shape for dst");
Giorgio Arena70623822017-11-27 15:50:10 +000089 }
90
Georgios Pinitas631c41a2017-12-06 11:53:03 +000091 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000092}
Giorgio Arena70623822017-11-27 15:50:10 +000093} // namespace
94
Giorgio Arena4a95bba2021-06-28 11:00:27 +010095ClMulKernel::ClMulKernel()
96{
97 _type = CLKernelType::ELEMENTWISE;
98}
99
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100100void ClMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, float scale,
101 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000103 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
104 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src1, src2, dst,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000105 scale, overflow_policy, rounding_policy, act_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100107 auto padding_info = get_padding_info({ src1, src2, dst });
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000108
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100109 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
110 auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
112 int scale_int = -1;
113 // Extract sign, exponent and mantissa
114 int exponent = 0;
115 float normalized_mantissa = std::frexp(scale, &exponent);
116 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
117 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
118 // Moreover, it will be negative as we deal with 1/2^n
119 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
120 {
121 // Store the positive exponent. We know that we compute 1/2^n
122 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
123 scale_int = std::abs(exponent - 1);
124 }
125
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100126 std::string acc_type;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000127 // Check if it has float src and dst
128 if(is_data_type_float(src1->data_type()) || is_data_type_float(src2->data_type()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100130 scale_int = -1;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000131 acc_type = (src1->data_type() == DataType::F32 || src2->data_type() == DataType::F32) ? "float" : "half";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 }
133 else
134 {
Suhail Munshi448cb452021-04-23 16:23:25 +0100135 if(src1->element_size() == 4 || src2->element_size() == 4)
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100136 {
Suhail Munshi448cb452021-04-23 16:23:25 +0100137 // use 64 bit accumulator for 32-bit input
138 acc_type = "long";
139 }
140 else if(src1->element_size() == 2 || src2->element_size() == 2)
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100141 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100142 // Use 32-bit accumulator for 16-bit input
143 acc_type = "int";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100144 }
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100145 else
146 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100147 // Use 16-bit accumulator for 8-bit input
148 acc_type = "ushort";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100149 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 }
151
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100152 const bool is_quantized = is_data_type_quantized(src1->data_type());
153 const unsigned int vec_size = adjust_vec_size(16 / dst->element_size(), dst->dimension(0));
154 const unsigned int vec_size_leftover = dst->dimension(0) % vec_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
156 // Set kernel build options
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100157 std::string kernel_name = "pixelwise_mul";
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100158 CLBuildOptions build_opts;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000159 build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(src1->data_type()));
160 build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(src2->data_type()));
161 build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(dst->data_type()));
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100162 build_opts.add_option("-DVEC_SIZE_IN1=" + ((dst->dimension(0) != 1 && src1->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
163 build_opts.add_option("-DVEC_SIZE_IN2=" + ((dst->dimension(0) != 1 && src2->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
164 build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(vec_size));
165 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000166 if(is_quantized && (dst->data_type() != DataType::S32))
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100167 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000168 const UniformQuantizationInfo iq1_info = src1->quantization_info().uniform();
169 const UniformQuantizationInfo iq2_info = src2->quantization_info().uniform();
170 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100171
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000172 build_opts.add_option_if(is_data_type_quantized_asymmetric(src1->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100173 "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000174 build_opts.add_option_if(is_data_type_quantized_asymmetric(src2->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100175 "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000176 build_opts.add_option_if(is_data_type_quantized_asymmetric(dst->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100177 "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset));
178 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale));
179 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale));
180 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100181 kernel_name += "_quantized";
182 }
183 else
184 {
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100185 kernel_name += (scale_int >= 0) ? "_int" : "_float";
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000186 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 +0100187 build_opts.add_option_if_else(rounding_policy == RoundingPolicy::TO_ZERO, "-DROUND=_rtz", "-DROUND=_rte");
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100188 build_opts.add_option("-DACC_DATA_TYPE=" + acc_type);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000189 if(act_info.enabled())
190 {
191 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
192 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
193 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
194 }
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100195 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196
197 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100198 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199
200 // Set scale argument
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000201 unsigned int idx = 3 * num_arguments_per_3D_tensor(); // Skip the src and dst parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100203 if(scale_int >= 0 && !is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 {
205 _kernel.setArg(idx++, scale_int);
206 }
207 else
208 {
209 _kernel.setArg(idx++, scale);
210 }
211
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100212 Window win = calculate_max_window(*dst, Steps(vec_size));
213 ICLKernel::configure_internal(win);
214
215 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodice8ed3ac12021-04-29 13:32:37 +0100216
217 // Set config_id for enabling LWS tuning
218 _config_id = kernel_name;
219 _config_id += "_";
220 _config_id += lower_string(string_from_data_type(dst->data_type()));
221 _config_id += "_";
222 _config_id += support::cpp11::to_string(src1->dimension(0));
223 _config_id += "_";
224 _config_id += support::cpp11::to_string(src1->dimension(1));
225 _config_id += "_";
226 _config_id += support::cpp11::to_string(src1->dimension(2));
227 _config_id += "_";
228 _config_id += support::cpp11::to_string(src2->dimension(0));
229 _config_id += "_";
230 _config_id += support::cpp11::to_string(src2->dimension(1));
231 _config_id += "_";
232 _config_id += support::cpp11::to_string(src2->dimension(2));
233 _config_id += "_";
234 _config_id += support::cpp11::to_string(dst->dimension(0));
235 _config_id += "_";
236 _config_id += support::cpp11::to_string(dst->dimension(1));
237 _config_id += "_";
238 _config_id += support::cpp11::to_string(dst->dimension(2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239}
240
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100241Status ClMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale,
242 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000243{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000244 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
245 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src1, src2, dst, scale, overflow_policy, rounding_policy, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000246
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000247 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000248}
249
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100250void ClMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251{
252 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
253 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
254
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100255 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
256 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
257 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100258
259 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
260 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
261 const TensorShape &out_shape = dst->info()->tensor_shape();
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000262
263 bool can_collapse = true;
264 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
265 {
266 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
267 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
268 {
269 can_collapse = (in_shape1[d] == in_shape2[d]);
270 }
271 }
272
273 bool has_collapsed = false;
274 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
275
276 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
277 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
278
279 Window slice = collapsed.first_slice_window_3D();
280 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
281 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282
283 do
284 {
285 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100286 add_3D_tensor_argument(idx, src_0, slice_input1);
287 add_3D_tensor_argument(idx, src_1, slice_input2);
288 add_3D_tensor_argument(idx, dst, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100289 enqueue(queue, *this, slice, lws_hint());
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000290
Michalis Spyrouebdde652019-07-08 11:52:46 +0100291 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
292 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000294 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295}
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000296
Georgios Pinitas8be91482019-03-26 17:23:28 +0000297namespace
298{
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100299constexpr unsigned int vec_size_complex = 1;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000300
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000301Status validate_arguments_complex(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000302{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000303 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 2, DataType::F16, DataType::F32);
304 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 2, DataType::F16, DataType::F32);
305 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000306
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000307 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000308
309 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000310 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000311
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000312 // Validate in case of configured dst
313 if(dst->total_size() > 0)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000314 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000315 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 2, DataType::F16, DataType::F32);
316 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst);
317 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 +0000318 }
319
320 return Status{};
321}
Georgios Pinitas8be91482019-03-26 17:23:28 +0000322} // namespace
323
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100324ClComplexMulKernel::ClComplexMulKernel()
325{
326 _type = CLKernelType::ELEMENTWISE;
327}
328
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100329void ClComplexMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000330{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000331 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
332 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(src1, src2, dst, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000333
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100334 auto padding_info = get_padding_info({ src1, src2, dst });
Georgios Pinitas8be91482019-03-26 17:23:28 +0000335
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100336 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
337 auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000338
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000339 CLBuildOptions build_opts;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000340 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000341 if(act_info.enabled())
342 {
343 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
344 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
345 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
346 }
347
Georgios Pinitas8be91482019-03-26 17:23:28 +0000348 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100349 _kernel = create_kernel(compile_context, "pixelwise_mul_complex", build_opts.options());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000350
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100351 Window win = calculate_max_window(*dst, Steps(vec_size_complex));
352 ICLKernel::configure_internal(win);
353
354 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000355}
356
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100357Status ClComplexMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000358{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000359 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
360 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(src1, src2, dst, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000361
362 return Status{};
363}
364
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100365void ClComplexMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000366{
367 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
368 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
369
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100370 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
371 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
372 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100373
374 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
375 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
376 const TensorShape &out_shape = dst->info()->tensor_shape();
Georgios Pinitas8be91482019-03-26 17:23:28 +0000377
378 bool can_collapse = true;
379 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
380 {
381 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
382 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
383 {
384 can_collapse = (in_shape1[d] == in_shape2[d]);
385 }
386 }
387
388 bool has_collapsed = false;
389 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
390
391 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
392 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
393
394 Window slice = collapsed.first_slice_window_3D();
395 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
396 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
397
398 do
399 {
400 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100401 add_3D_tensor_argument(idx, src_0, slice_input1);
402 add_3D_tensor_argument(idx, src_1, slice_input2);
403 add_3D_tensor_argument(idx, dst, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100404 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000405
Michalis Spyrouebdde652019-07-08 11:52:46 +0100406 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
407 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000408 }
409 while(collapsed.slide_window_slice_3D(slice));
410}
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000411} // namespace kernels
412} // namespace opencl
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100413} // namespace arm_compute