blob: ff5afa3d9554429bd23bf2c2b9327ef610fcf2d4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiocbbed282019-12-20 13:26:08 +00002 * 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"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <cmath>
38#include <cstdlib>
39#include <set>
40#include <string>
41
Georgios Pinitas8be91482019-03-26 17:23:28 +000042namespace arm_compute
43{
Giorgio Arena70623822017-11-27 15:50:10 +000044namespace
45{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000046constexpr unsigned int num_elems_processed_per_iteration = 16;
47
Georgios Pinitas631c41a2017-12-06 11:53:03 +000048Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000049 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Giorgio Arena70623822017-11-27 15:50:10 +000050{
51 ARM_COMPUTE_UNUSED(overflow_policy);
52 ARM_COMPUTE_UNUSED(rounding_policy);
53
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000054 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010055 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input1);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000056 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1,
57 1,
58 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
59 DataType::S16, DataType::QSYMM16, DataType::F16,
60 DataType::F32);
61 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2,
62 1,
63 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
64 DataType::S16, DataType::QSYMM16, DataType::F16,
65 DataType::F32);
Giorgio Arena70623822017-11-27 15:50:10 +000066 ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale < 0, "Scale cannot be negative.");
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000067 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
Giorgio Arena70623822017-11-27 15:50:10 +000068
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000069 const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
70
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Giorgio Arena70623822017-11-27 15:50:10 +000072
73 // Validate in case of configured output
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000074 if(output->total_size() > 0)
Giorgio Arena70623822017-11-27 15:50:10 +000075 {
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000076 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output,
77 1,
78 DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
79 DataType::S16, DataType::QSYMM16, DataType::F16,
80 DataType::F32);
Giorgio Arena70623822017-11-27 15:50:10 +000081 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8),
82 "Output can only be U8 if both inputs are U8");
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +010083 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QASYMM8 && (input1->data_type() != DataType::QASYMM8 || input2->data_type() != DataType::QASYMM8),
84 "Output can only be QASYMM8 if both inputs are QASYMM8");
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000085 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QASYMM8_SIGNED && (input1->data_type() != DataType::QASYMM8_SIGNED || input2->data_type() != DataType::QASYMM8_SIGNED),
86 "Output can only be QASYMM8_SIGNED if both inputs are QASYMM8_SIGNED");
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +010087 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QSYMM16 && (input1->data_type() != DataType::QSYMM16 || input2->data_type() != DataType::QSYMM16),
88 "Output can only be QSYMM16 if both inputs are QSYMM16");
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000089 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 +000090 }
91
Georgios Pinitas631c41a2017-12-06 11:53:03 +000092 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000093}
94
Georgios Pinitas631c41a2017-12-06 11:53:03 +000095std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
Giorgio Arena70623822017-11-27 15:50:10 +000096{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000097 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2);
98 const TensorShape &out_shape = broadcast_pair.first;
99 const ValidRegion &valid_region = broadcast_pair.second;
Giorgio Arena70623822017-11-27 15:50:10 +0000100
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000101 // Auto initialize output if not initialized
102 {
103 set_shape_if_empty(*output, out_shape);
104
105 if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16)
106 {
107 set_format_if_unknown(*output, Format::S16);
108 }
109 else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32)
110 {
111 set_format_if_unknown(*output, Format::F32);
112 }
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100113 else if(input1->data_type() == DataType::QASYMM8)
114 {
115 set_data_type_if_unknown(*output, DataType::QASYMM8);
116 }
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000117 else if(input1->data_type() == DataType::QASYMM8_SIGNED)
118 {
119 set_data_type_if_unknown(*output, DataType::QASYMM8_SIGNED);
120 }
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100121 else if(input1->data_type() == DataType::QSYMM16)
122 {
123 set_data_type_if_unknown(*output, DataType::QSYMM16);
124 }
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000125 }
126
127 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
128 Window win_input1 = win.broadcast_if_dimension_le_one(*input1);
129 Window win_input2 = win.broadcast_if_dimension_le_one(*input2);
Giorgio Arena70623822017-11-27 15:50:10 +0000130
131 AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration);
132 AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration);
133 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
134
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000135 bool window_changed = update_window_and_padding(win_input1, input1_access)
136 || update_window_and_padding(win_input2, input2_access)
137 || update_window_and_padding(win, output_access);
Giorgio Arena70623822017-11-27 15:50:10 +0000138
Giorgio Arena70623822017-11-27 15:50:10 +0000139 output_access.set_valid_region(win, valid_region);
140
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000141 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000142 return std::make_pair(err, win);
143}
144} // namespace
145
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146CLPixelWiseMultiplicationKernel::CLPixelWiseMultiplicationKernel()
147 : _input1(nullptr), _input2(nullptr), _output(nullptr)
148{
149}
150
151void CLPixelWiseMultiplicationKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, float scale,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000152 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153{
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100154 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000155 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(),
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000156 scale, overflow_policy, rounding_policy, act_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000158 // Configure kernel window
159 auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info());
160 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
161
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 _input1 = input1;
163 _input2 = input2;
164 _output = output;
165
166 int scale_int = -1;
167 // Extract sign, exponent and mantissa
168 int exponent = 0;
169 float normalized_mantissa = std::frexp(scale, &exponent);
170 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
171 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
172 // Moreover, it will be negative as we deal with 1/2^n
173 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
174 {
175 // Store the positive exponent. We know that we compute 1/2^n
176 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
177 scale_int = std::abs(exponent - 1);
178 }
179
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 std::string compute_type;
181 // Check if it has float inputs and output
182 if(is_data_type_float(input1->info()->data_type()) || is_data_type_float(input2->info()->data_type()))
183 {
184 scale_int = -1;
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100185 compute_type = (input1->info()->data_type() == DataType::F32 || input2->info()->data_type() == DataType::F32) ? "float" : "half";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 }
187 else
188 {
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100189 if(input1->info()->data_type() == DataType::S16 || input2->info()->data_type() == DataType::S16)
190 {
191 compute_type = "int";
192 }
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100193 else
194 {
195 compute_type = "ushort";
196 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 }
198
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100199 const bool is_quantized = is_data_type_quantized(input1->info()->data_type());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200
201 // Set kernel build options
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100202 std::string kernel_name = "pixelwise_mul";
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100203 CLBuildOptions build_opts;
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100204 build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->info()->data_type()));
205 build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->info()->data_type()));
206 build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
207 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100208 if(is_quantized)
209 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100210 const UniformQuantizationInfo iq1_info = input1->info()->quantization_info().uniform();
211 const UniformQuantizationInfo iq2_info = input2->info()->quantization_info().uniform();
212 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
213
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100214 build_opts.add_option_if(is_data_type_quantized_asymmetric(input1->info()->data_type()),
215 "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset));
216 build_opts.add_option_if(is_data_type_quantized_asymmetric(input2->info()->data_type()),
217 "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset));
218 build_opts.add_option_if(is_data_type_quantized_asymmetric(output->info()->data_type()),
219 "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset));
220 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale));
221 build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale));
222 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100223 kernel_name += "_quantized";
224 }
225 else
226 {
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100227 kernel_name += (scale_int >= 0) ? "_int" : "_float";
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100228 build_opts.add_option_if_else(overflow_policy == ConvertPolicy::WRAP || is_data_type_float(output->info()->data_type()), "-DWRAP", "-DSATURATE");
229 build_opts.add_option_if_else(rounding_policy == RoundingPolicy::TO_ZERO, "-DROUND=_rtz", "-DROUND=_rte");
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100230 build_opts.add_option("-DDATA_TYPE_RES=" + compute_type);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000231 if(act_info.enabled())
232 {
233 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
234 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
235 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
236 }
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100237 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238
239 // Create kernel
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100240 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
242 // Set scale argument
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100243 unsigned int idx = 3 * num_arguments_per_3D_tensor(); // Skip the inputs and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100245 if(scale_int >= 0 && !is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246 {
247 _kernel.setArg(idx++, scale_int);
248 }
249 else
250 {
251 _kernel.setArg(idx++, scale);
252 }
253
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100254 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255}
256
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000257Status CLPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000258 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000259{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000260 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000261 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy, act_info));
Giorgio Arena70623822017-11-27 15:50:10 +0000262 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 +0000263
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000264 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000265}
266
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267void CLPixelWiseMultiplicationKernel::run(const Window &window, cl::CommandQueue &queue)
268{
269 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
270 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
271
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000272 const TensorShape &in_shape1 = _input1->info()->tensor_shape();
273 const TensorShape &in_shape2 = _input2->info()->tensor_shape();
274 const TensorShape &out_shape = _output->info()->tensor_shape();
275
276 bool can_collapse = true;
277 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
278 {
279 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
280 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
281 {
282 can_collapse = (in_shape1[d] == in_shape2[d]);
283 }
284 }
285
286 bool has_collapsed = false;
287 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
288
289 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
290 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
291
292 Window slice = collapsed.first_slice_window_3D();
293 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
294 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295
296 do
297 {
298 unsigned int idx = 0;
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000299 add_3D_tensor_argument(idx, _input1, slice_input1);
300 add_3D_tensor_argument(idx, _input2, slice_input2);
Anthony Barbier9a7182e2017-07-11 18:36:40 +0100301 add_3D_tensor_argument(idx, _output, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100302 enqueue(queue, *this, slice, lws_hint());
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000303
Michalis Spyrouebdde652019-07-08 11:52:46 +0100304 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
305 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000307 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308}
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000309
310BorderSize CLPixelWiseMultiplicationKernel::border_size() const
311{
312 const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
313 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100314 return BorderSize{ 0, border, 0, 0 };
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000315}
Georgios Pinitas8be91482019-03-26 17:23:28 +0000316
317namespace
318{
319constexpr unsigned int num_elems_processed_per_iteration_complex = 1;
320
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000321Status validate_arguments_complex(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000322{
323 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 2, DataType::F32);
324 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 2, DataType::F32);
325
326 const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
327
328 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000329 ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000330
331 // Validate in case of configured output
332 if(output->total_size() > 0)
333 {
334 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 2, DataType::F32);
335 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output");
336 }
337
338 return Status{};
339}
340
341std::pair<Status, Window> validate_and_configure_window_complex(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
342{
343 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2);
344 const TensorShape &out_shape = broadcast_pair.first;
345 const ValidRegion &valid_region = broadcast_pair.second;
346
347 // Auto initialize output if not initialized
348 const TensorInfo out_info(out_shape, input1->num_channels(), input1->data_type());
349 auto_init_if_empty(*output, out_info);
350
351 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration_complex));
352 Window win_input1 = win.broadcast_if_dimension_le_one(*input1);
353 Window win_input2 = win.broadcast_if_dimension_le_one(*input2);
354
355 AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration_complex);
356 AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration_complex);
357 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_complex);
358
359 bool window_changed = update_window_and_padding(win_input1, input1_access)
360 || update_window_and_padding(win_input2, input2_access)
361 || update_window_and_padding(win, output_access);
362
363 output_access.set_valid_region(win, valid_region);
364
365 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
366 return std::make_pair(err, win);
367}
368} // namespace
369
370CLComplexPixelWiseMultiplicationKernel::CLComplexPixelWiseMultiplicationKernel()
371 : _input1(nullptr), _input2(nullptr), _output(nullptr)
372{
373}
374
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000375void CLComplexPixelWiseMultiplicationKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000376{
377 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000378 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(input1->info(), input2->info(), output->info(), act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000379
380 // Configure kernel window
381 auto win_config = validate_and_configure_window_complex(input1->info(), input2->info(), output->info());
382 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
383
384 _input1 = input1;
385 _input2 = input2;
386 _output = output;
387
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000388 CLBuildOptions build_opts;
389 if(act_info.enabled())
390 {
391 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
392 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
393 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
394 }
395
Georgios Pinitas8be91482019-03-26 17:23:28 +0000396 // Create kernel
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000397 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("pixelwise_mul_complex", build_opts.options()));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000398
399 ICLKernel::configure_internal(win_config.second);
400}
401
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000402Status CLComplexPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000403{
404 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000405 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(input1, input2, output, act_info));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000406 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_complex(input1->clone().get(), input2->clone().get(), output->clone().get()).first);
407
408 return Status{};
409}
410
411void CLComplexPixelWiseMultiplicationKernel::run(const Window &window, cl::CommandQueue &queue)
412{
413 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
414 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
415
416 const TensorShape &in_shape1 = _input1->info()->tensor_shape();
417 const TensorShape &in_shape2 = _input2->info()->tensor_shape();
418 const TensorShape &out_shape = _output->info()->tensor_shape();
419
420 bool can_collapse = true;
421 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
422 {
423 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
424 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
425 {
426 can_collapse = (in_shape1[d] == in_shape2[d]);
427 }
428 }
429
430 bool has_collapsed = false;
431 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
432
433 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
434 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
435
436 Window slice = collapsed.first_slice_window_3D();
437 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
438 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
439
440 do
441 {
442 unsigned int idx = 0;
443 add_3D_tensor_argument(idx, _input1, slice_input1);
444 add_3D_tensor_argument(idx, _input2, slice_input2);
445 add_3D_tensor_argument(idx, _output, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100446 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000447
Michalis Spyrouebdde652019-07-08 11:52:46 +0100448 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
449 ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000450 }
451 while(collapsed.slide_window_slice_3D(slice));
452}
453
454BorderSize CLComplexPixelWiseMultiplicationKernel::border_size() const
455{
456 const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
457 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration_complex - 1U, replicateSize);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100458 return BorderSize{ 0, border, 0, 0 };
Georgios Pinitas8be91482019-03-26 17:23:28 +0000459}
Michele Di Giorgiod8a468f2019-06-19 15:34:41 +0100460} // namespace arm_compute