2 * Copyright (c) 2016-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/NEON/kernels/NEPixelWiseMultiplicationKernel.h"
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/IAccessWindow.h"
29 #include "arm_compute/core/ITensor.h"
30 #include "arm_compute/core/NEON/NEFixedPoint.h"
31 #include "arm_compute/core/TensorInfo.h"
32 #include "arm_compute/core/Validate.h"
40 #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
41 #include <arm_fp16.h> // needed for float16_t
42 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
44 using namespace arm_compute;
49 } // namespace arm_compute
53 const float scale255_constant = 1.f / 255.f;
54 const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant);
55 const float32x4_t positive_round_f32q = vdupq_n_f32(0.5f);
57 constexpr unsigned int num_elems_processed_per_iteration = 16;
59 inline Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
61 ARM_COMPUTE_UNUSED(overflow_policy);
62 ARM_COMPUTE_UNUSED(rounding_policy);
64 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
65 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
66 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
67 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8),
68 "Output can only be U8 if both inputs are U8");
70 const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output");
72 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
74 if(is_data_type_fixed_point(input1->data_type()) || is_data_type_fixed_point(input2->data_type()) || is_data_type_fixed_point(output->data_type()))
76 // Check that all data types are the same and all fixed-point positions are the same
77 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input1, input2, output);
78 // Check if scale is representable in fixed-point with the provided settings
79 ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(scale, input1);
82 if(std::abs(scale - scale255_constant) < 0.00001f)
84 ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_NEAREST_UP && rounding_policy != RoundingPolicy::TO_NEAREST_EVEN);
88 ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_ZERO);
91 const float normalized_mantissa = std::frexp(scale, &exponent);
93 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
94 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
95 // Moreover, it will be negative as we deal with 1/2^n
96 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1)), "Scale value not supported (Should be 1/(2^n) or 1/255");
102 inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
104 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2);
105 const ValidRegion &valid_region = broadcast_pair.second;
107 // Auto initialize output if not initialized
109 set_shape_if_empty(*output, input1->tensor_shape());
111 if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16)
113 set_format_if_unknown(*output, Format::S16);
115 else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32)
117 set_format_if_unknown(*output, Format::F32);
119 else if(input1->data_type() == DataType::F16 || input2->data_type() == DataType::F16)
121 set_format_if_unknown(*output, Format::F16);
123 else if(input1->data_type() == DataType::QS8 && input2->data_type() == DataType::QS8)
125 set_data_type_if_unknown(*output, DataType::QS8);
126 set_fixed_point_position_if_zero(*output, input1->fixed_point_position());
130 // Configure kernel window
131 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
132 Window win_input1 = win.broadcast_if_dimension_le_one(*input1);
133 Window win_input2 = win.broadcast_if_dimension_le_one(*input2);
135 AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration);
136 AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration);
137 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
139 bool window_changed = update_window_and_padding(win_input1, input1_access)
140 || update_window_and_padding(win_input2, input2_access)
141 || update_window_and_padding(win, output_access);
143 output_access.set_valid_region(win, valid_region);
145 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
146 return std::make_pair(err, win);
149 /* Scales a given vector by 1/255.
151 * @note This does not work for all cases. e.g. for float of 0.49999999999999994 and large floats.
153 * @param in Input vector to scale.
154 * @return Scaled output rounded to nearest (round half up).
156 inline int32x4_t scale255_S32_S32(int32x4_t in)
159 const float32x4_t tmp = vmulq_f32(vcvtq_f32_s32(in), scale255_constant_f32q);
160 // Round to nearest (round half up)
161 // Add +0.5 for all values
162 // Afterwards vcvt rounds toward zero
163 return vcvtq_s32_f32(vaddq_f32(tmp, positive_round_f32q));
166 inline uint16x8_t scale255_U16_U16(uint16x8_t in)
168 const int32x4_t tmp_s1 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(in))));
169 const int32x4_t tmp_s2 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(in))));
170 return vreinterpretq_u16_s16(vcombine_s16(vmovn_s32(tmp_s2), vmovn_s32(tmp_s1)));
173 template <bool is_scale255, bool is_sat>
174 void mul_U8_U8_U8_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
176 const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr);
177 const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr);
178 const auto output = static_cast<uint8_t *__restrict>(output_ptr);
180 const uint8x16_t ta1 = vld1q_u8(input1);
181 const uint8x16_t ta2 = vld1q_u8(input2);
183 uint16x8_t tmp1_high = vmovl_u8(vget_high_u8(ta1));
184 const uint16x8_t tmp2_high = vmovl_u8(vget_high_u8(ta2));
185 uint16x8_t tmp1_low = vmovl_u8(vget_low_u8(ta1));
186 const uint16x8_t tmp2_low = vmovl_u8(vget_low_u8(ta2));
188 tmp1_high = vmulq_u16(tmp1_high, tmp2_high);
189 tmp1_low = vmulq_u16(tmp1_low, tmp2_low);
193 tmp1_high = scale255_U16_U16(tmp1_high);
194 tmp1_low = scale255_U16_U16(tmp1_low);
198 const int16x8_t vn = vdupq_n_s16(-n);
202 tmp1_high = vqshlq_u16(tmp1_high, vn);
203 tmp1_low = vqshlq_u16(tmp1_low, vn);
207 tmp1_high = vshlq_u16(tmp1_high, vn);
208 tmp1_low = vshlq_u16(tmp1_low, vn);
214 vst1q_u8(output, vcombine_u8(vqmovn_u16(tmp1_low), vqmovn_u16(tmp1_high)));
218 vst1q_u8(output, vcombine_u8(vmovn_u16(tmp1_low), vmovn_u16(tmp1_high)));
222 template <bool is_scale255, bool is_sat>
223 void mul_QS8_QS8_QS8_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n, int fixed_point_position)
225 const auto output = static_cast<qint8_t *__restrict>(output_ptr);
227 const qint8x16_t ta1 = vld1q_qs8(static_cast<const qint8_t *__restrict>(input1_ptr));
228 const qint8x16_t ta2 = vld1q_qs8(static_cast<const qint8_t *__restrict>(input2_ptr));
232 qint16x8_t tmp1_high = vmovl_s8(vget_high_s8(ta1));
233 qint16x8_t tmp1_low = vmovl_s8(vget_low_s8(ta1));
234 const qint16x8_t tmp2_high = vmovl_s8(vget_high_s8(ta2));
235 const qint16x8_t tmp2_low = vmovl_s8(vget_low_s8(ta2));
237 const float32x4x2_t scale255_f32 =
240 scale255_constant_f32q,
241 scale255_constant_f32q
244 const qint16x8_t scale255 = vqcvtq_qs16_f32(scale255_f32, fixed_point_position);
246 tmp1_high = vmulq_qs16(tmp1_high, tmp2_high, fixed_point_position);
247 tmp1_low = vmulq_qs16(tmp1_low, tmp2_low, fixed_point_position);
248 tmp1_high = vmulq_qs16(tmp1_high, scale255, fixed_point_position);
249 tmp1_low = vmulq_qs16(tmp1_low, scale255, fixed_point_position);
253 vst1q_qs8(output, vcombine_s8(vqmovn_s16(tmp1_low), vqmovn_s16(tmp1_high)));
257 vst1q_qs8(output, vcombine_s8(vmovn_s16(tmp1_low), vmovn_s16(tmp1_high)));
262 const qint8x16_t vn = vdupq_n_s8(-n);
263 qint8x16_t res = ta2;
267 res = vqshlq_s8(vqmulq_qs8(ta1, res, fixed_point_position), vn);
271 res = vshlq_s8(vmulq_qs8(ta1, res, fixed_point_position), vn);
273 vst1q_qs8(output, res);
277 template <bool is_scale255, bool is_sat>
278 void mul_QS16_QS16_QS16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n, int fixed_point_position)
280 const qint16x8x2_t ta1 = vld2q_qs16(static_cast<const qint16_t *__restrict>(input1_ptr));
281 qint16x8x2_t res = vld2q_qs16(static_cast<const qint16_t *__restrict>(input2_ptr));
285 const float32x4x2_t scale255_f32 =
288 scale255_constant_f32q,
289 scale255_constant_f32q
292 const qint16x8_t scale255 = vqcvtq_qs16_f32(scale255_f32, fixed_point_position);
295 res.val[0] = vqmulq_qs16(vqmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), scale255, fixed_point_position);
296 res.val[1] = vqmulq_qs16(vqmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), scale255, fixed_point_position);
300 res.val[0] = vmulq_qs16(vmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), scale255, fixed_point_position);
301 res.val[1] = vmulq_qs16(vmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), scale255, fixed_point_position);
306 const qint16x8_t vn = vdupq_n_s16(-n);
309 res.val[0] = vqshlq_s16(vqmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), vn);
310 res.val[1] = vqshlq_s16(vqmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), vn);
314 res.val[0] = vshlq_s16(vmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), vn);
315 res.val[1] = vshlq_s16(vmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), vn);
318 vst2q_s16(static_cast<qint16_t *__restrict>(output_ptr), res);
321 template <bool is_scale255, bool is_sat>
322 inline int16x8_t mul_S16_S16_S16_n_loop(const int16x8_t &input1, const int16x8_t &input2, int n)
324 int32x4_t tmp1_high = vmovl_s16(vget_high_s16(input1));
325 const int32x4_t tmp2_high = vmovl_s16(vget_high_s16(input2));
326 int32x4_t tmp1_low = vmovl_s16(vget_low_s16(input1));
327 const int32x4_t tmp2_low = vmovl_s16(vget_low_s16(input2));
329 tmp1_high = vmulq_s32(tmp1_high, tmp2_high);
330 tmp1_low = vmulq_s32(tmp1_low, tmp2_low);
334 tmp1_high = scale255_S32_S32(tmp1_high);
335 tmp1_low = scale255_S32_S32(tmp1_low);
339 // Right shift amount
340 const int32x4_t vn = vdupq_n_s32(-n);
342 const int32x4_t vnl = vdupq_n_s32(n);
343 // Calculate conversion bit
344 const uint32x4_t tmp1_high_u = vreinterpretq_u32_s32(tmp1_high);
345 const uint32x4_t tmp1_low_u = vreinterpretq_u32_s32(tmp1_low);
346 const uint32x4_t sign_high = vshrq_n_u32(tmp1_high_u, 31);
347 const uint32x4_t sign_low = vshrq_n_u32(tmp1_low_u, 31);
348 const int32x4_t sign_high_s = vreinterpretq_s32_u32(sign_high);
349 const int32x4_t sign_low_s = vreinterpretq_s32_u32(sign_low);
350 const int32x4_t convert_high = vsubq_s32(vshlq_s32(sign_high_s, vnl), sign_high_s);
351 const int32x4_t convert_low = vsubq_s32(vshlq_s32(sign_low_s, vnl), sign_low_s);
354 tmp1_high = vqshlq_s32(vaddq_s32(tmp1_high, convert_high), vn);
355 tmp1_low = vqshlq_s32(vaddq_s32(tmp1_low, convert_low), vn);
359 tmp1_high = vshlq_s32(vaddq_s32(tmp1_high, convert_high), vn);
360 tmp1_low = vshlq_s32(vaddq_s32(tmp1_low, convert_low), vn);
366 return vcombine_s16(vqmovn_s32(tmp1_low), vqmovn_s32(tmp1_high));
370 return vcombine_s16(vmovn_s32(tmp1_low), vmovn_s32(tmp1_high));
374 template <bool is_scale255, bool is_sat>
375 inline int16x8x2_t mul_S16_S16_S16_n_k(const int16x8x2_t &input1, const int16x8x2_t &input2, int n)
377 const int16x8x2_t result =
381 mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[0], input2.val[0], n),
383 mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[1], input2.val[1], n)
390 template <bool is_scale255, bool is_sat>
391 void mul_S16_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
393 const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr);
394 const auto input2 = static_cast<const int16_t *__restrict>(input2_ptr);
395 const auto output = static_cast<int16_t *__restrict>(output_ptr);
397 const int16x8x2_t ta1 = vld2q_s16(input1);
398 const int16x8x2_t ta2 = vld2q_s16(input2);
399 const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n);
401 vst2q_s16(output, result);
404 template <bool is_scale255, bool is_sat>
405 void mul_F32_F32_F32_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale)
407 const auto input1 = static_cast<const float *__restrict>(input1_ptr);
408 const auto input2 = static_cast<const float *__restrict>(input2_ptr);
409 const auto output = static_cast<float *__restrict>(output_ptr);
411 const float32x4x4_t ta1 = vld4q_f32(input1);
412 const float32x4x4_t ta2 = vld4q_f32(input2);
413 const float32x4_t scale_vec = vdupq_n_f32(scale);
414 const float32x4x4_t result =
417 vmulq_f32(vmulq_f32(ta1.val[0], ta2.val[0]), scale_vec),
418 vmulq_f32(vmulq_f32(ta1.val[1], ta2.val[1]), scale_vec),
419 vmulq_f32(vmulq_f32(ta1.val[2], ta2.val[2]), scale_vec),
420 vmulq_f32(vmulq_f32(ta1.val[3], ta2.val[3]), scale_vec)
423 vst4q_f32(output, result);
426 template <bool is_scale255, bool is_sat>
427 void mul_F16_F16_F16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale)
429 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
430 const auto input1 = static_cast<const float16_t *__restrict>(input1_ptr);
431 const auto input2 = static_cast<const float16_t *__restrict>(input2_ptr);
432 const auto output = static_cast<float16_t *__restrict>(output_ptr);
433 const float16x8x2_t ta1 = vld2q_f16(input1);
434 const float16x8x2_t ta2 = vld2q_f16(input2);
435 const float16x8_t scale_vec = vdupq_n_f16(scale);
436 const float16x8x2_t result =
439 vmulq_f16(vmulq_f16(ta1.val[0], ta2.val[0]), scale_vec),
440 vmulq_f16(vmulq_f16(ta1.val[1], ta2.val[1]), scale_vec),
443 vst2q_f16(output, result);
444 #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
445 ARM_COMPUTE_UNUSED(input1_ptr);
446 ARM_COMPUTE_UNUSED(input2_ptr);
447 ARM_COMPUTE_UNUSED(output_ptr);
448 ARM_COMPUTE_UNUSED(scale);
449 ARM_COMPUTE_ERROR("Not supported. Recompile the library with arch=arm64-v8.2-a.");
450 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
453 template <bool is_scale255, bool is_sat>
454 void mul_U8_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
456 const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr);
457 const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr);
458 const auto output = static_cast<int16_t *__restrict>(output_ptr);
460 const uint8x16_t bv = vld1q_u8(input2);
461 const uint8x16_t av = vld1q_u8(input1);
463 uint16x8_t tmp_low = vmovl_u8(vget_low_u8(av));
464 uint16x8_t tmp_high = vmovl_u8(vget_high_u8(av));
465 tmp_low = vmulq_u16(tmp_low, vmovl_u8(vget_low_u8(bv)));
466 tmp_high = vmulq_u16(tmp_high, vmovl_u8(vget_high_u8(bv)));
470 tmp_low = scale255_U16_U16(tmp_low);
471 tmp_high = scale255_U16_U16(tmp_high);
475 const int16x8_t vn = vdupq_n_s16(-n);
479 tmp_low = vqshlq_u16(tmp_low, vn);
480 tmp_high = vqshlq_u16(tmp_high, vn);
484 tmp_low = vshlq_u16(tmp_low, vn);
485 tmp_high = vshlq_u16(tmp_high, vn);
491 static const uint16x8_t max = vdupq_n_u16(SHRT_MAX);
493 tmp_low = vminq_u16(tmp_low, max);
494 tmp_high = vminq_u16(tmp_high, max);
497 vst1q_s16(output, vreinterpretq_s16_u16(tmp_low));
498 vst1q_s16(output + 8, vreinterpretq_s16_u16(tmp_high));
501 template <bool is_scale255, bool is_sat>
502 void mul_S16_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
504 const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr);
505 const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr);
506 const auto output = static_cast<int16_t *__restrict>(output_ptr);
508 const int16x8x2_t ta1 = vld2q_s16(input1);
509 const uint8x8x2_t ta2u = vld2_u8(input2);
510 const int16x8x2_t ta2 =
513 vreinterpretq_s16_u16(vmovl_u8(ta2u.val[0])),
514 vreinterpretq_s16_u16(vmovl_u8(ta2u.val[1]))
518 const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n);
520 vst2q_s16(output, result);
523 template <bool is_scale255, bool is_sat>
524 void mul_U8_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
526 // Simply swap the two input buffers
527 mul_S16_U8_S16_n<is_scale255, is_sat>(input2_ptr, input1_ptr, output_ptr, n);
531 NEPixelWiseMultiplicationKernel::NEPixelWiseMultiplicationKernel()
532 : _func_float(nullptr), _func_int(nullptr), _func_q_int(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _scale{ 0 }, _scale_exponent{ 0 }
536 void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
538 ARM_COMPUTE_UNUSED(rounding_policy);
539 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
541 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy));
543 // Configure kernel window
544 auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info());
545 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
553 _func_q_int = nullptr;
554 _func_float = nullptr;
556 bool is_scale_255 = false;
557 // Check and validate scaling factor
558 if(std::abs(scale - scale255_constant) < 0.00001f)
566 std::frexp(scale, &exponent);
568 // Store the positive exponent. We know that we compute 1/2^n
569 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
570 _scale_exponent = std::abs(exponent - 1);
573 const DataType dt_input1 = input1->info()->data_type();
574 const DataType dt_input2 = input2->info()->data_type();
575 const DataType dt_output = output->info()->data_type();
576 const bool is_sat = (overflow_policy == ConvertPolicy::SATURATE);
578 if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::U8 == dt_output)
582 _func_int = is_sat ? &mul_U8_U8_U8_n<true, true> : &mul_U8_U8_U8_n<true, false>;
586 _func_int = is_sat ? &mul_U8_U8_U8_n<false, true> : &mul_U8_U8_U8_n<false, false>;
589 else if(DataType::S16 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output)
593 _func_int = is_sat ? &mul_S16_S16_S16_n<true, true> : &mul_S16_S16_S16_n<true, false>;
597 _func_int = is_sat ? &mul_S16_S16_S16_n<false, true> : &mul_S16_S16_S16_n<false, false>;
600 else if(DataType::S16 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output)
604 _func_int = is_sat ? &mul_S16_U8_S16_n<true, true> : &mul_S16_U8_S16_n<true, false>;
608 _func_int = is_sat ? &mul_S16_U8_S16_n<false, true> : &mul_S16_U8_S16_n<false, false>;
611 else if(DataType::U8 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output)
615 _func_int = is_sat ? &mul_U8_S16_S16_n<true, true> : &mul_U8_S16_S16_n<true, false>;
619 _func_int = is_sat ? &mul_U8_S16_S16_n<false, true> : &mul_U8_S16_S16_n<false, false>;
622 else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output)
626 _func_int = is_sat ? &mul_U8_U8_S16_n<true, true> : &mul_U8_U8_S16_n<true, false>;
630 _func_int = is_sat ? &mul_U8_U8_S16_n<false, true> : &mul_U8_U8_S16_n<false, false>;
633 else if(DataType::QS8 == dt_input1 && DataType::QS8 == dt_input2 && DataType::QS8 == dt_output)
637 _func_q_int = is_sat ? &mul_QS8_QS8_QS8_n<true, true> : &mul_QS8_QS8_QS8_n<true, false>;
641 _func_q_int = is_sat ? &mul_QS8_QS8_QS8_n<false, true> : &mul_QS8_QS8_QS8_n<false, false>;
644 else if(DataType::QS16 == dt_input1 && DataType::QS16 == dt_input2 && DataType::QS16 == dt_output)
648 _func_q_int = is_sat ? &mul_QS16_QS16_QS16_n<true, true> : &mul_QS16_QS16_QS16_n<true, false>;
652 _func_q_int = is_sat ? &mul_QS16_QS16_QS16_n<false, true> : &mul_QS16_QS16_QS16_n<false, false>;
655 else if(DataType::F16 == dt_input1 && DataType::F16 == dt_input2 && DataType::F16 == dt_output)
657 _func_float = &mul_F16_F16_F16_n<false, false>;
660 else if(DataType::F32 == dt_input1 && DataType::F32 == dt_input2 && DataType::F32 == dt_output)
662 _func_float = &mul_F32_F32_F32_n<false, false>;
667 ARM_COMPUTE_ERROR("You called with the wrong img formats");
670 INEKernel::configure(win_config.second);
673 Status NEPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy,
674 RoundingPolicy rounding_policy)
676 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
677 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy));
678 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first);
683 void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info)
685 ARM_COMPUTE_UNUSED(info);
686 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
687 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
689 const TensorShape &in_shape1 = _input1->info()->tensor_shape();
690 const TensorShape &in_shape2 = _input2->info()->tensor_shape();
691 const TensorShape &out_shape = _output->info()->tensor_shape();
693 bool can_collapse = true;
694 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
696 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
697 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
699 can_collapse = (in_shape1[d] == in_shape2[d]);
703 bool has_collapsed = false;
704 Window collapsed = can_collapse ? window.collapse_if_possible(INEKernel::window(), Window::DimZ, &has_collapsed) : window;
706 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
707 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
709 Window slice = collapsed.first_slice_window_3D();
710 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
711 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
713 Iterator input1(_input1, slice_input1);
714 Iterator input2(_input2, slice_input2);
715 Iterator output(_output, slice);
717 if(_func_int != nullptr)
719 execute_window_loop(collapsed, [&](const Coordinates & id)
721 (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent);
722 collapsed.slide_window_slice_3D(slice_input1);
723 collapsed.slide_window_slice_3D(slice_input2);
725 input1, input2, output);
727 else if(_func_q_int != nullptr)
729 int fixed_point_position = _input1->info()->fixed_point_position();
730 execute_window_loop(collapsed, [&](const Coordinates & id)
732 (*_func_q_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent, fixed_point_position);
733 collapsed.slide_window_slice_3D(slice_input1);
734 collapsed.slide_window_slice_3D(slice_input2);
736 input1, input2, output);
740 ARM_COMPUTE_ERROR_ON(_func_float == nullptr);
741 execute_window_loop(collapsed, [&](const Coordinates & id)
743 (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale);
744 collapsed.slide_window_slice_3D(slice_input1);
745 collapsed.slide_window_slice_3D(slice_input2);
747 input1, input2, output);
751 BorderSize NEPixelWiseMultiplicationKernel::border_size() const
753 const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
754 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
755 return BorderSize(0, border, 0, 0);