1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2016-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/gpu/cl/kernels/ClMulKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/CLHelpers.h"
27*c217d954SCole Faust #include "arm_compute/core/CL/CLKernelLibrary.h"
28*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
29*c217d954SCole Faust #include "arm_compute/core/CL/OpenCL.h"
30*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
31*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
32*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
33*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
34*c217d954SCole Faust #include "support/Cast.h"
35*c217d954SCole Faust #include "support/StringSupport.h"
36*c217d954SCole Faust
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace opencl
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace kernels
42*c217d954SCole Faust {
43*c217d954SCole Faust namespace
44*c217d954SCole Faust {
validate_arguments(const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)45*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale,
46*c217d954SCole Faust ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
47*c217d954SCole Faust {
48*c217d954SCole Faust ARM_COMPUTE_UNUSED(overflow_policy);
49*c217d954SCole Faust ARM_COMPUTE_UNUSED(rounding_policy);
50*c217d954SCole Faust
51*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
52*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src1);
53*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1,
54*c217d954SCole Faust 1,
55*c217d954SCole Faust DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
56*c217d954SCole Faust DataType::S16, DataType::QSYMM16, DataType::F16, DataType::S32,
57*c217d954SCole Faust DataType::F32);
58*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2,
59*c217d954SCole Faust 1,
60*c217d954SCole Faust DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
61*c217d954SCole Faust DataType::S16, DataType::QSYMM16, DataType::F16, DataType::S32,
62*c217d954SCole Faust DataType::F32);
63*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale < 0, "Scale cannot be negative.");
64*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
65*c217d954SCole Faust
66*c217d954SCole Faust // Check whether it is in_place calculation
67*c217d954SCole Faust const bool in_place = (src1 == dst) || (src2 == dst);
68*c217d954SCole Faust const bool src1_in_place = in_place && (src1 == dst);
69*c217d954SCole Faust
70*c217d954SCole Faust const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
71*c217d954SCole Faust
72*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
73*c217d954SCole Faust
74*c217d954SCole Faust // Validate in case of configured dst
75*c217d954SCole Faust if(dst->total_size() > 0)
76*c217d954SCole Faust {
77*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst,
78*c217d954SCole Faust 1,
79*c217d954SCole Faust DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
80*c217d954SCole Faust DataType::S16, DataType::QSYMM16, DataType::F16,
81*c217d954SCole Faust DataType::S32, DataType::F32);
82*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::U8 && (src1->data_type() != DataType::U8 || src2->data_type() != DataType::U8),
83*c217d954SCole Faust "Dst can only be U8 if both src are U8");
84*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QASYMM8 && (src1->data_type() != DataType::QASYMM8 || src2->data_type() != DataType::QASYMM8),
85*c217d954SCole Faust "Dst can only be QASYMM8 if both src are QASYMM8");
86*c217d954SCole Faust 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*c217d954SCole Faust "Dst can only be QASYMM8_SIGNED if both src are QASYMM8_SIGNED");
88*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QSYMM16 && (src1->data_type() != DataType::QSYMM16 || src2->data_type() != DataType::QSYMM16),
89*c217d954SCole Faust "Dst can only be QSYMM16 if both src are QSYMM16");
90*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((src1->data_type() == DataType::S32 || src2->data_type() == DataType::S32) && (dst->data_type() != DataType::S32),
91*c217d954SCole Faust "Dst must be S32 if source tensors are S32");
92*c217d954SCole Faust if(in_place)
93*c217d954SCole Faust {
94*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, src1_in_place ? src1->tensor_shape() : src2->tensor_shape(), 0),
95*c217d954SCole Faust "Wrong shape for dst, cannot do in_place calculation");
96*c217d954SCole Faust }
97*c217d954SCole Faust else
98*c217d954SCole Faust {
99*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0),
100*c217d954SCole Faust "Wrong shape for dst");
101*c217d954SCole Faust }
102*c217d954SCole Faust }
103*c217d954SCole Faust
104*c217d954SCole Faust return Status{};
105*c217d954SCole Faust }
106*c217d954SCole Faust } // namespace
107*c217d954SCole Faust
ClMulKernel()108*c217d954SCole Faust ClMulKernel::ClMulKernel()
109*c217d954SCole Faust {
110*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
111*c217d954SCole Faust }
112*c217d954SCole Faust
configure(const CLCompileContext & compile_context,ITensorInfo * src1,ITensorInfo * src2,ITensorInfo * dst,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)113*c217d954SCole Faust void ClMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, float scale,
114*c217d954SCole Faust ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
115*c217d954SCole Faust {
116*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
117*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src1, src2, dst,
118*c217d954SCole Faust scale, overflow_policy, rounding_policy, act_info));
119*c217d954SCole Faust
120*c217d954SCole Faust auto padding_info = get_padding_info({ src1, src2, dst });
121*c217d954SCole Faust
122*c217d954SCole Faust const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
123*c217d954SCole Faust auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
124*c217d954SCole Faust
125*c217d954SCole Faust int scale_int = -1;
126*c217d954SCole Faust // Extract sign, exponent and mantissa
127*c217d954SCole Faust int exponent = 0;
128*c217d954SCole Faust float normalized_mantissa = std::frexp(scale, &exponent);
129*c217d954SCole Faust // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
130*c217d954SCole Faust // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
131*c217d954SCole Faust // Moreover, it will be negative as we deal with 1/2^n
132*c217d954SCole Faust if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
133*c217d954SCole Faust {
134*c217d954SCole Faust // Store the positive exponent. We know that we compute 1/2^n
135*c217d954SCole Faust // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
136*c217d954SCole Faust scale_int = std::abs(exponent - 1);
137*c217d954SCole Faust }
138*c217d954SCole Faust
139*c217d954SCole Faust std::string acc_type;
140*c217d954SCole Faust // Check if it has float src and dst
141*c217d954SCole Faust if(is_data_type_float(src1->data_type()) || is_data_type_float(src2->data_type()))
142*c217d954SCole Faust {
143*c217d954SCole Faust scale_int = -1;
144*c217d954SCole Faust acc_type = (src1->data_type() == DataType::F32 || src2->data_type() == DataType::F32) ? "float" : "half";
145*c217d954SCole Faust }
146*c217d954SCole Faust else
147*c217d954SCole Faust {
148*c217d954SCole Faust if(src1->element_size() == 4 || src2->element_size() == 4)
149*c217d954SCole Faust {
150*c217d954SCole Faust // use 64 bit accumulator for 32-bit input
151*c217d954SCole Faust acc_type = "long";
152*c217d954SCole Faust }
153*c217d954SCole Faust else if(src1->element_size() == 2 || src2->element_size() == 2)
154*c217d954SCole Faust {
155*c217d954SCole Faust // Use 32-bit accumulator for 16-bit input
156*c217d954SCole Faust acc_type = "int";
157*c217d954SCole Faust }
158*c217d954SCole Faust else
159*c217d954SCole Faust {
160*c217d954SCole Faust // Use 16-bit accumulator for 8-bit input
161*c217d954SCole Faust acc_type = "ushort";
162*c217d954SCole Faust }
163*c217d954SCole Faust }
164*c217d954SCole Faust
165*c217d954SCole Faust const bool is_quantized = is_data_type_quantized(src1->data_type());
166*c217d954SCole Faust const unsigned int vec_size = adjust_vec_size(16 / dst->element_size(), dst->dimension(0));
167*c217d954SCole Faust const unsigned int vec_size_leftover = dst->dimension(0) % vec_size;
168*c217d954SCole Faust
169*c217d954SCole Faust // Set kernel build options
170*c217d954SCole Faust std::string kernel_name = "pixelwise_mul";
171*c217d954SCole Faust CLBuildOptions build_opts;
172*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(src1->data_type()));
173*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(src2->data_type()));
174*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(dst->data_type()));
175*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_IN1=" + ((dst->dimension(0) != 1 && src1->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
176*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_IN2=" + ((dst->dimension(0) != 1 && src2->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size)));
177*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(vec_size));
178*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover));
179*c217d954SCole Faust if(is_quantized && (dst->data_type() != DataType::S32))
180*c217d954SCole Faust {
181*c217d954SCole Faust const UniformQuantizationInfo iq1_info = src1->quantization_info().uniform();
182*c217d954SCole Faust const UniformQuantizationInfo iq2_info = src2->quantization_info().uniform();
183*c217d954SCole Faust const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
184*c217d954SCole Faust
185*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric(src1->data_type()),
186*c217d954SCole Faust "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset));
187*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric(src2->data_type()),
188*c217d954SCole Faust "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset));
189*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric(dst->data_type()),
190*c217d954SCole Faust "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset));
191*c217d954SCole Faust build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale));
192*c217d954SCole Faust build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale));
193*c217d954SCole Faust build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
194*c217d954SCole Faust kernel_name += "_quantized";
195*c217d954SCole Faust }
196*c217d954SCole Faust else
197*c217d954SCole Faust {
198*c217d954SCole Faust kernel_name += (scale_int >= 0) ? "_int" : "_float";
199*c217d954SCole Faust build_opts.add_option_if_else(overflow_policy == ConvertPolicy::WRAP || is_data_type_float(dst->data_type()), "-DWRAP", "-DSATURATE");
200*c217d954SCole Faust build_opts.add_option_if_else(rounding_policy == RoundingPolicy::TO_ZERO, "-DROUND=_rtz", "-DROUND=_rte");
201*c217d954SCole Faust build_opts.add_option("-DACC_DATA_TYPE=" + acc_type);
202*c217d954SCole Faust if(act_info.enabled())
203*c217d954SCole Faust {
204*c217d954SCole Faust build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
205*c217d954SCole Faust build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
206*c217d954SCole Faust build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
207*c217d954SCole Faust }
208*c217d954SCole Faust }
209*c217d954SCole Faust
210*c217d954SCole Faust // Check whether it is in_place calculation
211*c217d954SCole Faust const bool in_place = (src1 == dst) || (src2 == dst);
212*c217d954SCole Faust const bool src1_in_place = in_place && (src1 == dst);
213*c217d954SCole Faust build_opts.add_option_if(in_place, "-DIN_PLACE");
214*c217d954SCole Faust build_opts.add_option_if(src1_in_place, "-DSRC1_IN_PLACE");
215*c217d954SCole Faust
216*c217d954SCole Faust // Create kernel
217*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
218*c217d954SCole Faust
219*c217d954SCole Faust // Set scale argument
220*c217d954SCole Faust unsigned int idx = (in_place ? 2 : 3) * num_arguments_per_3D_tensor(); // Skip the src and dst parameters
221*c217d954SCole Faust
222*c217d954SCole Faust if(scale_int >= 0 && !is_quantized)
223*c217d954SCole Faust {
224*c217d954SCole Faust _kernel.setArg(idx++, scale_int);
225*c217d954SCole Faust }
226*c217d954SCole Faust else
227*c217d954SCole Faust {
228*c217d954SCole Faust _kernel.setArg(idx++, scale);
229*c217d954SCole Faust }
230*c217d954SCole Faust
231*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps(vec_size));
232*c217d954SCole Faust ICLKernel::configure_internal(win);
233*c217d954SCole Faust
234*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
235*c217d954SCole Faust
236*c217d954SCole Faust // Set config_id for enabling LWS tuning
237*c217d954SCole Faust _config_id = kernel_name;
238*c217d954SCole Faust _config_id += "_";
239*c217d954SCole Faust _config_id += lower_string(string_from_data_type(dst->data_type()));
240*c217d954SCole Faust _config_id += "_";
241*c217d954SCole Faust _config_id += support::cpp11::to_string(src1->dimension(0));
242*c217d954SCole Faust _config_id += "_";
243*c217d954SCole Faust _config_id += support::cpp11::to_string(src1->dimension(1));
244*c217d954SCole Faust _config_id += "_";
245*c217d954SCole Faust _config_id += support::cpp11::to_string(src1->dimension(2));
246*c217d954SCole Faust _config_id += "_";
247*c217d954SCole Faust _config_id += support::cpp11::to_string(src2->dimension(0));
248*c217d954SCole Faust _config_id += "_";
249*c217d954SCole Faust _config_id += support::cpp11::to_string(src2->dimension(1));
250*c217d954SCole Faust _config_id += "_";
251*c217d954SCole Faust _config_id += support::cpp11::to_string(src2->dimension(2));
252*c217d954SCole Faust _config_id += "_";
253*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(0));
254*c217d954SCole Faust _config_id += "_";
255*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(1));
256*c217d954SCole Faust _config_id += "_";
257*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(2));
258*c217d954SCole Faust }
259*c217d954SCole Faust
validate(const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)260*c217d954SCole Faust Status ClMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale,
261*c217d954SCole Faust ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
262*c217d954SCole Faust {
263*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
264*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src1, src2, dst, scale, overflow_policy, rounding_policy, act_info));
265*c217d954SCole Faust
266*c217d954SCole Faust return Status{};
267*c217d954SCole Faust }
268*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)269*c217d954SCole Faust void ClMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
270*c217d954SCole Faust {
271*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
272*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
273*c217d954SCole Faust
274*c217d954SCole Faust const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
275*c217d954SCole Faust const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
276*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
277*c217d954SCole Faust
278*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src_0, src_1, dst);
279*c217d954SCole Faust
280*c217d954SCole Faust const TensorShape &in_shape1 = src_0->info()->tensor_shape();
281*c217d954SCole Faust const TensorShape &in_shape2 = src_1->info()->tensor_shape();
282*c217d954SCole Faust const TensorShape &out_shape = dst->info()->tensor_shape();
283*c217d954SCole Faust
284*c217d954SCole Faust bool can_collapse = true;
285*c217d954SCole Faust if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
286*c217d954SCole Faust {
287*c217d954SCole Faust can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
288*c217d954SCole Faust for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
289*c217d954SCole Faust {
290*c217d954SCole Faust can_collapse = (in_shape1[d] == in_shape2[d]);
291*c217d954SCole Faust }
292*c217d954SCole Faust }
293*c217d954SCole Faust
294*c217d954SCole Faust bool has_collapsed = false;
295*c217d954SCole Faust Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
296*c217d954SCole Faust
297*c217d954SCole Faust const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
298*c217d954SCole Faust const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
299*c217d954SCole Faust
300*c217d954SCole Faust Window slice = collapsed.first_slice_window_3D();
301*c217d954SCole Faust Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
302*c217d954SCole Faust Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
303*c217d954SCole Faust
304*c217d954SCole Faust // Check whether it is in_place calculation
305*c217d954SCole Faust const bool in_place = (src_0 == dst) || (src_1 == dst);
306*c217d954SCole Faust do
307*c217d954SCole Faust {
308*c217d954SCole Faust unsigned int idx = 0;
309*c217d954SCole Faust add_3D_tensor_argument(idx, src_0, slice_input1);
310*c217d954SCole Faust add_3D_tensor_argument(idx, src_1, slice_input2);
311*c217d954SCole Faust if(!in_place)
312*c217d954SCole Faust {
313*c217d954SCole Faust add_3D_tensor_argument(idx, dst, slice);
314*c217d954SCole Faust }
315*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
316*c217d954SCole Faust
317*c217d954SCole Faust ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
318*c217d954SCole Faust ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
319*c217d954SCole Faust }
320*c217d954SCole Faust while(collapsed.slide_window_slice_3D(slice));
321*c217d954SCole Faust }
322*c217d954SCole Faust
323*c217d954SCole Faust namespace
324*c217d954SCole Faust {
325*c217d954SCole Faust constexpr unsigned int vec_size_complex = 1;
326*c217d954SCole Faust
validate_arguments_complex(const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,const ActivationLayerInfo & act_info)327*c217d954SCole Faust Status validate_arguments_complex(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
328*c217d954SCole Faust {
329*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 2, DataType::F16, DataType::F32);
330*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 2, DataType::F16, DataType::F32);
331*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
332*c217d954SCole Faust
333*c217d954SCole Faust const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
334*c217d954SCole Faust
335*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
336*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
337*c217d954SCole Faust
338*c217d954SCole Faust // Validate in case of configured dst
339*c217d954SCole Faust if(dst->total_size() > 0)
340*c217d954SCole Faust {
341*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 2, DataType::F16, DataType::F32);
342*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst);
343*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), "Wrong shape for dst");
344*c217d954SCole Faust }
345*c217d954SCole Faust
346*c217d954SCole Faust return Status{};
347*c217d954SCole Faust }
348*c217d954SCole Faust } // namespace
349*c217d954SCole Faust
ClComplexMulKernel()350*c217d954SCole Faust ClComplexMulKernel::ClComplexMulKernel()
351*c217d954SCole Faust {
352*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
353*c217d954SCole Faust }
354*c217d954SCole Faust
configure(const CLCompileContext & compile_context,ITensorInfo * src1,ITensorInfo * src2,ITensorInfo * dst,const ActivationLayerInfo & act_info)355*c217d954SCole Faust void ClComplexMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const ActivationLayerInfo &act_info)
356*c217d954SCole Faust {
357*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
358*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(src1, src2, dst, act_info));
359*c217d954SCole Faust
360*c217d954SCole Faust auto padding_info = get_padding_info({ src1, src2, dst });
361*c217d954SCole Faust
362*c217d954SCole Faust const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
363*c217d954SCole Faust auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape));
364*c217d954SCole Faust
365*c217d954SCole Faust CLBuildOptions build_opts;
366*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
367*c217d954SCole Faust if(act_info.enabled())
368*c217d954SCole Faust {
369*c217d954SCole Faust build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
370*c217d954SCole Faust build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
371*c217d954SCole Faust build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
372*c217d954SCole Faust }
373*c217d954SCole Faust
374*c217d954SCole Faust // Create kernel
375*c217d954SCole Faust _kernel = create_kernel(compile_context, "pixelwise_mul_complex", build_opts.options());
376*c217d954SCole Faust
377*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps(vec_size_complex));
378*c217d954SCole Faust ICLKernel::configure_internal(win);
379*c217d954SCole Faust
380*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
381*c217d954SCole Faust }
382*c217d954SCole Faust
validate(const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,const ActivationLayerInfo & act_info)383*c217d954SCole Faust Status ClComplexMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
384*c217d954SCole Faust {
385*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
386*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(src1, src2, dst, act_info));
387*c217d954SCole Faust
388*c217d954SCole Faust return Status{};
389*c217d954SCole Faust }
390*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)391*c217d954SCole Faust void ClComplexMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
392*c217d954SCole Faust {
393*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
394*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
395*c217d954SCole Faust
396*c217d954SCole Faust const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
397*c217d954SCole Faust const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
398*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
399*c217d954SCole Faust
400*c217d954SCole Faust const TensorShape &in_shape1 = src_0->info()->tensor_shape();
401*c217d954SCole Faust const TensorShape &in_shape2 = src_1->info()->tensor_shape();
402*c217d954SCole Faust const TensorShape &out_shape = dst->info()->tensor_shape();
403*c217d954SCole Faust
404*c217d954SCole Faust bool can_collapse = true;
405*c217d954SCole Faust if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
406*c217d954SCole Faust {
407*c217d954SCole Faust can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
408*c217d954SCole Faust for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
409*c217d954SCole Faust {
410*c217d954SCole Faust can_collapse = (in_shape1[d] == in_shape2[d]);
411*c217d954SCole Faust }
412*c217d954SCole Faust }
413*c217d954SCole Faust
414*c217d954SCole Faust bool has_collapsed = false;
415*c217d954SCole Faust Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
416*c217d954SCole Faust
417*c217d954SCole Faust const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
418*c217d954SCole Faust const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
419*c217d954SCole Faust
420*c217d954SCole Faust Window slice = collapsed.first_slice_window_3D();
421*c217d954SCole Faust Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
422*c217d954SCole Faust Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
423*c217d954SCole Faust
424*c217d954SCole Faust do
425*c217d954SCole Faust {
426*c217d954SCole Faust unsigned int idx = 0;
427*c217d954SCole Faust add_3D_tensor_argument(idx, src_0, slice_input1);
428*c217d954SCole Faust add_3D_tensor_argument(idx, src_1, slice_input2);
429*c217d954SCole Faust add_3D_tensor_argument(idx, dst, slice);
430*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
431*c217d954SCole Faust
432*c217d954SCole Faust ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
433*c217d954SCole Faust ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
434*c217d954SCole Faust }
435*c217d954SCole Faust while(collapsed.slide_window_slice_3D(slice));
436*c217d954SCole Faust }
437*c217d954SCole Faust } // namespace kernels
438*c217d954SCole Faust } // namespace opencl
439*c217d954SCole Faust } // namespace arm_compute
440