blob: b8081bbacf6837a6d1d7137676bfa040a0090a36 [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
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +010095void ClMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, float scale,
96 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +000098 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
99 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src1, src2, dst,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000100 scale, overflow_policy, rounding_policy, act_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100102 auto padding_info = get_padding_info({ src1, src2, dst });
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000103
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100104 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
105 auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
107 int scale_int = -1;
108 // Extract sign, exponent and mantissa
109 int exponent = 0;
110 float normalized_mantissa = std::frexp(scale, &exponent);
111 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
112 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
113 // Moreover, it will be negative as we deal with 1/2^n
114 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
115 {
116 // Store the positive exponent. We know that we compute 1/2^n
117 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
118 scale_int = std::abs(exponent - 1);
119 }
120
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100121 std::string acc_type;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000122 // Check if it has float src and dst
123 if(is_data_type_float(src1->data_type()) || is_data_type_float(src2->data_type()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100125 scale_int = -1;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000126 acc_type = (src1->data_type() == DataType::F32 || src2->data_type() == DataType::F32) ? "float" : "half";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 }
128 else
129 {
Suhail Munshi448cb452021-04-23 16:23:25 +0100130 if(src1->element_size() == 4 || src2->element_size() == 4)
131 {
132 // use 64 bit accumulator for 32-bit input
133 acc_type = "long";
134 }
135 else if(src1->element_size() == 2 || src2->element_size() == 2)
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100136 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100137 // Use 32-bit accumulator for 16-bit input
138 acc_type = "int";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100139 }
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100140 else
141 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100142 // Use 16-bit accumulator for 8-bit input
143 acc_type = "ushort";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100144 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 }
146
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100147 const bool is_quantized = is_data_type_quantized(src1->data_type());
148 const unsigned int vec_size = adjust_vec_size(16 / dst->element_size(), dst->dimension(0));
149 const unsigned int vec_size_leftover = dst->dimension(0) % vec_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
151 // Set kernel build options
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100152 std::string kernel_name = "pixelwise_mul";
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100153 CLBuildOptions build_opts;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000154 build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(src1->data_type()));
155 build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(src2->data_type()));
156 build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(dst->data_type()));
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100157 build_opts.add_option("-DVEC_SIZE_IN1=" + ((dst->dimension(0) != 1 && src1->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
158 build_opts.add_option("-DVEC_SIZE_IN2=" + ((dst->dimension(0) != 1 && src2->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
159 build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(vec_size));
160 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000161 if(is_quantized && (dst->data_type() != DataType::S32))
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100162 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000163 const UniformQuantizationInfo iq1_info = src1->quantization_info().uniform();
164 const UniformQuantizationInfo iq2_info = src2->quantization_info().uniform();
165 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100166
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000167 build_opts.add_option_if(is_data_type_quantized_asymmetric(src1->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100168 "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000169 build_opts.add_option_if(is_data_type_quantized_asymmetric(src2->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100170 "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset));
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000171 build_opts.add_option_if(is_data_type_quantized_asymmetric(dst->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100172 "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset));
173 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale));
174 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale));
175 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100176 kernel_name += "_quantized";
177 }
178 else
179 {
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100180 kernel_name += (scale_int >= 0) ? "_int" : "_float";
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000181 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 +0100182 build_opts.add_option_if_else(rounding_policy == RoundingPolicy::TO_ZERO, "-DROUND=_rtz", "-DROUND=_rte");
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100183 build_opts.add_option("-DACC_DATA_TYPE=" + acc_type);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000184 if(act_info.enabled())
185 {
186 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
187 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
188 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
189 }
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100190 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191
192 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100193 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194
195 // Set scale argument
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000196 unsigned int idx = 3 * num_arguments_per_3D_tensor(); // Skip the src and dst parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100198 if(scale_int >= 0 && !is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 {
200 _kernel.setArg(idx++, scale_int);
201 }
202 else
203 {
204 _kernel.setArg(idx++, scale);
205 }
206
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100207 Window win = calculate_max_window(*dst, Steps(vec_size));
208 ICLKernel::configure_internal(win);
209
210 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodice8ed3ac12021-04-29 13:32:37 +0100211
212 // Set config_id for enabling LWS tuning
213 _config_id = kernel_name;
214 _config_id += "_";
215 _config_id += lower_string(string_from_data_type(dst->data_type()));
216 _config_id += "_";
217 _config_id += support::cpp11::to_string(src1->dimension(0));
218 _config_id += "_";
219 _config_id += support::cpp11::to_string(src1->dimension(1));
220 _config_id += "_";
221 _config_id += support::cpp11::to_string(src1->dimension(2));
222 _config_id += "_";
223 _config_id += support::cpp11::to_string(src2->dimension(0));
224 _config_id += "_";
225 _config_id += support::cpp11::to_string(src2->dimension(1));
226 _config_id += "_";
227 _config_id += support::cpp11::to_string(src2->dimension(2));
228 _config_id += "_";
229 _config_id += support::cpp11::to_string(dst->dimension(0));
230 _config_id += "_";
231 _config_id += support::cpp11::to_string(dst->dimension(1));
232 _config_id += "_";
233 _config_id += support::cpp11::to_string(dst->dimension(2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234}
235
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100236Status ClMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale,
237 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000238{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000239 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
240 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src1, src2, dst, scale, overflow_policy, rounding_policy, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000241
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000242 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000243}
244
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100245void ClMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246{
247 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
248 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
249
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100250 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
251 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
252 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100253
254 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
255 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
256 const TensorShape &out_shape = dst->info()->tensor_shape();
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000257
258 bool can_collapse = true;
259 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
260 {
261 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
262 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
263 {
264 can_collapse = (in_shape1[d] == in_shape2[d]);
265 }
266 }
267
268 bool has_collapsed = false;
269 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
270
271 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
272 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
273
274 Window slice = collapsed.first_slice_window_3D();
275 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
276 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277
278 do
279 {
280 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100281 add_3D_tensor_argument(idx, src_0, slice_input1);
282 add_3D_tensor_argument(idx, src_1, slice_input2);
283 add_3D_tensor_argument(idx, dst, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100284 enqueue(queue, *this, slice, lws_hint());
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000285
Michalis Spyrouebdde652019-07-08 11:52:46 +0100286 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
287 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000289 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290}
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000291
Georgios Pinitas8be91482019-03-26 17:23:28 +0000292namespace
293{
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100294constexpr unsigned int vec_size_complex = 1;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000295
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000296Status validate_arguments_complex(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000297{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000298 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 2, DataType::F16, DataType::F32);
299 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 2, DataType::F16, DataType::F32);
300 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000301
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000302 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000303
304 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000305 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000306
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000307 // Validate in case of configured dst
308 if(dst->total_size() > 0)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000309 {
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000310 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 2, DataType::F16, DataType::F32);
311 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst);
312 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 +0000313 }
314
315 return Status{};
316}
Georgios Pinitas8be91482019-03-26 17:23:28 +0000317} // namespace
318
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100319void ClComplexMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000320{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000321 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
322 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(src1, src2, dst, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000323
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100324 auto padding_info = get_padding_info({ src1, src2, dst });
Georgios Pinitas8be91482019-03-26 17:23:28 +0000325
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100326 const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
327 auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000328
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000329 CLBuildOptions build_opts;
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000330 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000331 if(act_info.enabled())
332 {
333 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
334 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
335 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
336 }
337
Georgios Pinitas8be91482019-03-26 17:23:28 +0000338 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100339 _kernel = create_kernel(compile_context, "pixelwise_mul_complex", build_opts.options());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000340
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100341 Window win = calculate_max_window(*dst, Steps(vec_size_complex));
342 ICLKernel::configure_internal(win);
343
344 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000345}
346
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100347Status ClComplexMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000348{
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000349 ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
350 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(src1, src2, dst, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000351
352 return Status{};
353}
354
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100355void ClComplexMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000356{
357 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
358 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
359
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100360 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
361 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
362 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100363
364 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
365 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
366 const TensorShape &out_shape = dst->info()->tensor_shape();
Georgios Pinitas8be91482019-03-26 17:23:28 +0000367
368 bool can_collapse = true;
369 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
370 {
371 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
372 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
373 {
374 can_collapse = (in_shape1[d] == in_shape2[d]);
375 }
376 }
377
378 bool has_collapsed = false;
379 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
380
381 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
382 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
383
384 Window slice = collapsed.first_slice_window_3D();
385 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
386 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
387
388 do
389 {
390 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100391 add_3D_tensor_argument(idx, src_0, slice_input1);
392 add_3D_tensor_argument(idx, src_1, slice_input2);
393 add_3D_tensor_argument(idx, dst, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100394 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000395
Michalis Spyrouebdde652019-07-08 11:52:46 +0100396 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
397 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000398 }
399 while(collapsed.slide_window_slice_3D(slice));
400}
Sheri Zhangf9ab9f92021-03-16 12:09:15 +0000401} // namespace kernels
402} // namespace opencl
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100403} // namespace arm_compute