blob: 229937ef31516aa702d6ba127184ebed4709e10d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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 */
24#include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorInfo.h"
Michalis Spyrou1009e872020-07-27 12:48:34 +010032#include "arm_compute/core/utils/misc/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000033#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Georgios Pinitas8be91482019-03-26 17:23:28 +000035namespace arm_compute
36{
Giorgio Arena70623822017-11-27 15:50:10 +000037namespace
38{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000039constexpr unsigned int num_elems_processed_per_iteration = 16;
40
Georgios Pinitas631c41a2017-12-06 11:53:03 +000041Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000042 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Giorgio Arena70623822017-11-27 15:50:10 +000043{
44 ARM_COMPUTE_UNUSED(overflow_policy);
45 ARM_COMPUTE_UNUSED(rounding_policy);
46
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input1);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1,
50 1,
51 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
52 DataType::S16, DataType::QSYMM16, DataType::F16,
53 DataType::F32);
54 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2,
55 1,
56 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
57 DataType::S16, DataType::QSYMM16, DataType::F16,
58 DataType::F32);
Giorgio Arena70623822017-11-27 15:50:10 +000059 ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale < 0, "Scale cannot be negative.");
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000060 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
Giorgio Arena70623822017-11-27 15:50:10 +000061
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000062 const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
63
64 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Giorgio Arena70623822017-11-27 15:50:10 +000065
66 // Validate in case of configured output
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000067 if(output->total_size() > 0)
Giorgio Arena70623822017-11-27 15:50:10 +000068 {
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000069 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output,
70 1,
71 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
72 DataType::S16, DataType::QSYMM16, DataType::F16,
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +010073 DataType::S32, DataType::F32);
Giorgio Arena70623822017-11-27 15:50:10 +000074 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8),
75 "Output can only be U8 if both inputs are U8");
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +010076 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QASYMM8 && (input1->data_type() != DataType::QASYMM8 || input2->data_type() != DataType::QASYMM8),
77 "Output can only be QASYMM8 if both inputs are QASYMM8");
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000078 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QASYMM8_SIGNED && (input1->data_type() != DataType::QASYMM8_SIGNED || input2->data_type() != DataType::QASYMM8_SIGNED),
79 "Output can only be QASYMM8_SIGNED if both inputs are QASYMM8_SIGNED");
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +010080 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QSYMM16 && (input1->data_type() != DataType::QSYMM16 || input2->data_type() != DataType::QSYMM16),
81 "Output can only be QSYMM16 if both inputs are QSYMM16");
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +010082 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::S32 && (input1->data_type() != DataType::QSYMM16 || input2->data_type() != DataType::QSYMM16),
83 "Output can only be S32 if both inputs are QSYMM16");
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000084 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output");
Giorgio Arena70623822017-11-27 15:50:10 +000085 }
86
Georgios Pinitas631c41a2017-12-06 11:53:03 +000087 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000088}
89
Georgios Pinitas631c41a2017-12-06 11:53:03 +000090std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
Giorgio Arena70623822017-11-27 15:50:10 +000091{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000092 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2);
93 const TensorShape &out_shape = broadcast_pair.first;
94 const ValidRegion &valid_region = broadcast_pair.second;
Giorgio Arena70623822017-11-27 15:50:10 +000095
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000096 // Auto initialize output if not initialized
97 {
98 set_shape_if_empty(*output, out_shape);
99
100 if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16)
101 {
102 set_format_if_unknown(*output, Format::S16);
103 }
104 else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32)
105 {
106 set_format_if_unknown(*output, Format::F32);
107 }
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100108 else if(input1->data_type() == DataType::QASYMM8)
109 {
110 set_data_type_if_unknown(*output, DataType::QASYMM8);
111 }
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000112 else if(input1->data_type() == DataType::QASYMM8_SIGNED)
113 {
114 set_data_type_if_unknown(*output, DataType::QASYMM8_SIGNED);
115 }
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100116 else if(input1->data_type() == DataType::QSYMM16)
117 {
118 set_data_type_if_unknown(*output, DataType::QSYMM16);
119 }
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000120 }
121
122 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
123 Window win_input1 = win.broadcast_if_dimension_le_one(*input1);
124 Window win_input2 = win.broadcast_if_dimension_le_one(*input2);
Giorgio Arena70623822017-11-27 15:50:10 +0000125
126 AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration);
127 AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration);
128 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
129
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000130 bool window_changed = update_window_and_padding(win_input1, input1_access)
131 || update_window_and_padding(win_input2, input2_access)
132 || update_window_and_padding(win, output_access);
Giorgio Arena70623822017-11-27 15:50:10 +0000133
Giorgio Arena70623822017-11-27 15:50:10 +0000134 output_access.set_valid_region(win, valid_region);
135
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000136 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000137 return std::make_pair(err, win);
138}
139} // namespace
140
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141CLPixelWiseMultiplicationKernel::CLPixelWiseMultiplicationKernel()
142 : _input1(nullptr), _input2(nullptr), _output(nullptr)
143{
144}
145
Michalis Spyrou1009e872020-07-27 12:48:34 +0100146void CLPixelWiseMultiplicationKernel::configure(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, float scale,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000147 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100149 configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
150}
151
Michalis Spyrou1009e872020-07-27 12:48:34 +0100152void CLPixelWiseMultiplicationKernel::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, float scale,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100153 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
154{
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100155 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Michalis Spyrou1009e872020-07-27 12:48:34 +0100156 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1, input2, output,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000157 scale, overflow_policy, rounding_policy, act_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000159 // Configure kernel window
Michalis Spyrou1009e872020-07-27 12:48:34 +0100160 auto win_config = validate_and_configure_window(input1, input2, output);
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000161 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
162
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 _input1 = input1;
164 _input2 = input2;
165 _output = output;
166
167 int scale_int = -1;
168 // Extract sign, exponent and mantissa
169 int exponent = 0;
170 float normalized_mantissa = std::frexp(scale, &exponent);
171 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
172 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
173 // Moreover, it will be negative as we deal with 1/2^n
174 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
175 {
176 // Store the positive exponent. We know that we compute 1/2^n
177 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
178 scale_int = std::abs(exponent - 1);
179 }
180
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100181 std::string acc_type;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 // Check if it has float inputs and output
Michalis Spyrou1009e872020-07-27 12:48:34 +0100183 if(is_data_type_float(input1->data_type()) || is_data_type_float(input2->data_type()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100185 scale_int = -1;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100186 acc_type = (input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32) ? "float" : "half";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
188 else
189 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100190 if(input1->element_size() == 2 || input2->element_size() == 2)
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100191 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100192 // Use 32-bit accumulator for 16-bit input
193 acc_type = "int";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100194 }
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100195 else
196 {
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100197 // Use 16-bit accumulator for 8-bit input
198 acc_type = "ushort";
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100199 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 }
201
Michalis Spyrou1009e872020-07-27 12:48:34 +0100202 const bool is_quantized = is_data_type_quantized(input1->data_type());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203
204 // Set kernel build options
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100205 std::string kernel_name = "pixelwise_mul";
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100206 CLBuildOptions build_opts;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100207 build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->data_type()));
208 build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->data_type()));
209 build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->data_type()));
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100210 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100211 if(is_quantized && (output->data_type() != DataType::S32))
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100212 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100213 const UniformQuantizationInfo iq1_info = input1->quantization_info().uniform();
214 const UniformQuantizationInfo iq2_info = input2->quantization_info().uniform();
215 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100216
Michalis Spyrou1009e872020-07-27 12:48:34 +0100217 build_opts.add_option_if(is_data_type_quantized_asymmetric(input1->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100218 "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100219 build_opts.add_option_if(is_data_type_quantized_asymmetric(input2->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100220 "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100221 build_opts.add_option_if(is_data_type_quantized_asymmetric(output->data_type()),
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100222 "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset));
223 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale));
224 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale));
225 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100226 kernel_name += "_quantized";
227 }
228 else
229 {
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100230 kernel_name += (scale_int >= 0) ? "_int" : "_float";
Michalis Spyrou1009e872020-07-27 12:48:34 +0100231 build_opts.add_option_if_else(overflow_policy == ConvertPolicy::WRAP || is_data_type_float(output->data_type()), "-DWRAP", "-DSATURATE");
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100232 build_opts.add_option_if_else(rounding_policy == RoundingPolicy::TO_ZERO, "-DROUND=_rtz", "-DROUND=_rte");
Michele Di Giorgio7a0212a2020-04-14 16:08:32 +0100233 build_opts.add_option("-DACC_DATA_TYPE=" + acc_type);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000234 if(act_info.enabled())
235 {
236 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
237 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
238 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
239 }
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100240 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
242 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100243 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244
245 // Set scale argument
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100246 unsigned int idx = 3 * num_arguments_per_3D_tensor(); // Skip the inputs and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100248 if(scale_int >= 0 && !is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 {
250 _kernel.setArg(idx++, scale_int);
251 }
252 else
253 {
254 _kernel.setArg(idx++, scale);
255 }
256
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100257 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258}
259
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000260Status CLPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000261 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000262{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000263 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000264 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy, act_info));
Giorgio Arena70623822017-11-27 15:50:10 +0000265 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000266
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000267 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000268}
269
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100270void CLPixelWiseMultiplicationKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271{
272 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
273 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
274
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100275 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
276 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
277 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100278
279 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
280 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
281 const TensorShape &out_shape = dst->info()->tensor_shape();
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000282
283 bool can_collapse = true;
284 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
285 {
286 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
287 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
288 {
289 can_collapse = (in_shape1[d] == in_shape2[d]);
290 }
291 }
292
293 bool has_collapsed = false;
294 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
295
296 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
297 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
298
299 Window slice = collapsed.first_slice_window_3D();
300 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
301 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302
303 do
304 {
305 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100306 add_3D_tensor_argument(idx, src_0, slice_input1);
307 add_3D_tensor_argument(idx, src_1, slice_input2);
308 add_3D_tensor_argument(idx, dst, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100309 enqueue(queue, *this, slice, lws_hint());
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000310
Michalis Spyrouebdde652019-07-08 11:52:46 +0100311 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
312 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100313 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000314 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315}
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000316
317BorderSize CLPixelWiseMultiplicationKernel::border_size() const
318{
Michalis Spyrou1009e872020-07-27 12:48:34 +0100319 const unsigned int replicateSize = _output->dimension(0) - std::min(_input1->dimension(0), _input2->dimension(0));
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000320 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100321 return BorderSize{ 0, border, 0, 0 };
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000322}
Georgios Pinitas8be91482019-03-26 17:23:28 +0000323
324namespace
325{
326constexpr unsigned int num_elems_processed_per_iteration_complex = 1;
327
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000328Status validate_arguments_complex(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000329{
330 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 2, DataType::F32);
331 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 2, DataType::F32);
332
333 const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
334
335 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000336 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000337
338 // Validate in case of configured output
339 if(output->total_size() > 0)
340 {
341 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 2, DataType::F32);
342 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output");
343 }
344
345 return Status{};
346}
347
348std::pair<Status, Window> validate_and_configure_window_complex(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
349{
350 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2);
351 const TensorShape &out_shape = broadcast_pair.first;
352 const ValidRegion &valid_region = broadcast_pair.second;
353
354 // Auto initialize output if not initialized
355 const TensorInfo out_info(out_shape, input1->num_channels(), input1->data_type());
356 auto_init_if_empty(*output, out_info);
357
358 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration_complex));
359 Window win_input1 = win.broadcast_if_dimension_le_one(*input1);
360 Window win_input2 = win.broadcast_if_dimension_le_one(*input2);
361
362 AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration_complex);
363 AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration_complex);
364 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_complex);
365
366 bool window_changed = update_window_and_padding(win_input1, input1_access)
367 || update_window_and_padding(win_input2, input2_access)
368 || update_window_and_padding(win, output_access);
369
370 output_access.set_valid_region(win, valid_region);
371
372 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
373 return std::make_pair(err, win);
374}
375} // namespace
376
377CLComplexPixelWiseMultiplicationKernel::CLComplexPixelWiseMultiplicationKernel()
378 : _input1(nullptr), _input2(nullptr), _output(nullptr)
379{
380}
381
Michalis Spyrou1009e872020-07-27 12:48:34 +0100382void CLComplexPixelWiseMultiplicationKernel::configure(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000383{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100384 configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, act_info);
385}
386
Michalis Spyrou1009e872020-07-27 12:48:34 +0100387void CLComplexPixelWiseMultiplicationKernel::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ActivationLayerInfo &act_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100388{
Georgios Pinitas8be91482019-03-26 17:23:28 +0000389 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Michalis Spyrou1009e872020-07-27 12:48:34 +0100390 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(input1, input2, output, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000391
392 // Configure kernel window
Michalis Spyrou1009e872020-07-27 12:48:34 +0100393 auto win_config = validate_and_configure_window_complex(input1, input2, output);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000394 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
395
396 _input1 = input1;
397 _input2 = input2;
398 _output = output;
399
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000400 CLBuildOptions build_opts;
401 if(act_info.enabled())
402 {
403 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
404 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
405 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
406 }
407
Georgios Pinitas8be91482019-03-26 17:23:28 +0000408 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100409 _kernel = create_kernel(compile_context, "pixelwise_mul_complex", build_opts.options());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000410
411 ICLKernel::configure_internal(win_config.second);
412}
413
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000414Status CLComplexPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000415{
416 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000417 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(input1, input2, output, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000418 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_complex(input1->clone().get(), input2->clone().get(), output->clone().get()).first);
419
420 return Status{};
421}
422
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100423void CLComplexPixelWiseMultiplicationKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000424{
425 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
426 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
427
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100428 const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
429 const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
430 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100431
432 const TensorShape &in_shape1 = src_0->info()->tensor_shape();
433 const TensorShape &in_shape2 = src_1->info()->tensor_shape();
434 const TensorShape &out_shape = dst->info()->tensor_shape();
Georgios Pinitas8be91482019-03-26 17:23:28 +0000435
436 bool can_collapse = true;
437 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
438 {
439 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
440 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
441 {
442 can_collapse = (in_shape1[d] == in_shape2[d]);
443 }
444 }
445
446 bool has_collapsed = false;
447 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
448
449 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
450 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
451
452 Window slice = collapsed.first_slice_window_3D();
453 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
454 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
455
456 do
457 {
458 unsigned int idx = 0;
Michalis Spyrou1009e872020-07-27 12:48:34 +0100459 add_3D_tensor_argument(idx, src_0, slice_input1);
460 add_3D_tensor_argument(idx, src_1, slice_input2);
461 add_3D_tensor_argument(idx, dst, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100462 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000463
Michalis Spyrouebdde652019-07-08 11:52:46 +0100464 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
465 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000466 }
467 while(collapsed.slide_window_slice_3D(slice));
468}
469
470BorderSize CLComplexPixelWiseMultiplicationKernel::border_size() const
471{
Michalis Spyrou1009e872020-07-27 12:48:34 +0100472 const unsigned int replicateSize = _output->dimension(0) - std::min(_input1->dimension(0), _input2->dimension(0));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000473 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration_complex - 1U, replicateSize);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100474 return BorderSize{ 0, border, 0, 0 };
Georgios Pinitas8be91482019-03-26 17:23:28 +0000475}
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100476} // namespace arm_compute