Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 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/NEON/kernels/NEPixelWiseMultiplicationKernel.h" |
| 25 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/NEAsymm.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/NEON/NESymm.h" |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/TensorInfo.h" |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 36 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | #include <arm_neon.h> |
| 40 | #include <climits> |
| 41 | #include <cmath> |
| 42 | #include <cstdint> |
| 43 | #include <cstdlib> |
| 44 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 45 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 46 | #include <arm_fp16.h> // needed for float16_t |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 47 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 48 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | namespace arm_compute |
| 50 | { |
| 51 | class Coordinates; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | |
| 53 | namespace |
| 54 | { |
| 55 | const float scale255_constant = 1.f / 255.f; |
| 56 | const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant); |
| 57 | const float32x4_t positive_round_f32q = vdupq_n_f32(0.5f); |
| 58 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 59 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 60 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 61 | inline Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 62 | { |
| 63 | ARM_COMPUTE_UNUSED(overflow_policy); |
| 64 | ARM_COMPUTE_UNUSED(rounding_policy); |
| 65 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input1); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::QSYMM16, DataType::F16, DataType::F32); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::QSYMM16, DataType::F16, DataType::F32); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::QSYMM16, DataType::F16, DataType::F32); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8), |
| 71 | "Output can only be U8 if both inputs are U8"); |
| 72 | |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->data_type() == DataType::QASYMM8 && input2->data_type() != DataType::QASYMM8, |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 74 | "Input2 must be QASYMM8 if input1 is QASYMM8"); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 75 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->data_type() != DataType::QASYMM8 && input2->data_type() == DataType::QASYMM8, |
| 77 | "Input1 must be QASYMM8 if input2 is QASYMM8"); |
| 78 | |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->data_type() == DataType::QSYMM16 && input2->data_type() != DataType::QSYMM16, |
| 80 | "Input2 must be QSYMM16 if input1 is QSYMM16"); |
| 81 | |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->data_type() != DataType::QSYMM16 && input2->data_type() == DataType::QSYMM16, |
| 83 | "Input1 must be QSYMM16 if input2 is QSYMM16"); |
| 84 | |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(input1->data_type()) && overflow_policy == ConvertPolicy::WRAP, |
| 86 | "ConvertPolicy cannot be WRAP if datatype is quantized"); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 87 | |
| 88 | if(output->total_size() > 0) |
| 89 | { |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 90 | if(is_data_type_quantized(output->data_type())) |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 91 | { |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2, output); |
| 93 | } |
| 94 | |
| 95 | const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape()); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output"); |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 98 | } |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 99 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 100 | if(std::abs(scale - scale255_constant) < 0.00001f) |
| 101 | { |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_NEAREST_UP && rounding_policy != RoundingPolicy::TO_NEAREST_EVEN); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_ZERO); |
| 107 | |
| 108 | int exponent = 0; |
| 109 | const float normalized_mantissa = std::frexp(scale, &exponent); |
| 110 | |
| 111 | // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15 |
| 112 | // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14 |
| 113 | // Moreover, it will be negative as we deal with 1/2^n |
| 114 | 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"); |
| 115 | } |
| 116 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 117 | return Status{}; |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 120 | inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 121 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 122 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); |
| 123 | const ValidRegion &valid_region = broadcast_pair.second; |
| 124 | |
| 125 | // Auto initialize output if not initialized |
| 126 | { |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_UNUSED(set_shape_if_empty(*output, input1->tensor_shape())); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 128 | |
| 129 | if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16) |
| 130 | { |
| 131 | set_format_if_unknown(*output, Format::S16); |
| 132 | } |
| 133 | else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32) |
| 134 | { |
| 135 | set_format_if_unknown(*output, Format::F32); |
| 136 | } |
| 137 | else if(input1->data_type() == DataType::F16 || input2->data_type() == DataType::F16) |
| 138 | { |
| 139 | set_format_if_unknown(*output, Format::F16); |
| 140 | } |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 141 | else if(input1->data_type() == DataType::QASYMM8) |
| 142 | { |
| 143 | set_data_type_if_unknown(*output, DataType::QASYMM8); |
| 144 | } |
| 145 | else if(input1->data_type() == DataType::QSYMM16) |
| 146 | { |
| 147 | set_data_type_if_unknown(*output, DataType::QSYMM16); |
| 148 | } |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 149 | } |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 150 | |
| 151 | // Configure kernel window |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 152 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); |
| 153 | Window win_input1 = win.broadcast_if_dimension_le_one(*input1); |
| 154 | Window win_input2 = win.broadcast_if_dimension_le_one(*input2); |
| 155 | |
| 156 | AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration); |
| 157 | AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 158 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 159 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 160 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 161 | || update_window_and_padding(win_input2, input2_access) |
| 162 | || update_window_and_padding(win, output_access); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 163 | |
| 164 | output_access.set_valid_region(win, valid_region); |
| 165 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 166 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 167 | return std::make_pair(err, win); |
| 168 | } |
| 169 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | /* Scales a given vector by 1/255. |
| 171 | * |
| 172 | * @note This does not work for all cases. e.g. for float of 0.49999999999999994 and large floats. |
| 173 | * |
| 174 | * @param in Input vector to scale. |
| 175 | * @return Scaled output rounded to nearest (round half up). |
| 176 | */ |
| 177 | inline int32x4_t scale255_S32_S32(int32x4_t in) |
| 178 | { |
| 179 | // Scale |
| 180 | const float32x4_t tmp = vmulq_f32(vcvtq_f32_s32(in), scale255_constant_f32q); |
| 181 | // Round to nearest (round half up) |
| 182 | // Add +0.5 for all values |
| 183 | // Afterwards vcvt rounds toward zero |
| 184 | return vcvtq_s32_f32(vaddq_f32(tmp, positive_round_f32q)); |
| 185 | } |
| 186 | |
| 187 | inline uint16x8_t scale255_U16_U16(uint16x8_t in) |
| 188 | { |
| 189 | const int32x4_t tmp_s1 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(in)))); |
| 190 | const int32x4_t tmp_s2 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(in)))); |
| 191 | return vreinterpretq_u16_s16(vcombine_s16(vmovn_s32(tmp_s2), vmovn_s32(tmp_s1))); |
| 192 | } |
| 193 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 194 | inline void mul_saturate_QASYMM8_QASYMM8_QASYMM8_n_opt(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 195 | float32x4_t input1_vscale, int32x4_t input1_voffset, float32x4_t input2_vscale, int32x4_t input2_voffset, float32x4_t output_voffset, float32x4_t vinvscale) |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 196 | { |
| 197 | const auto input1 = static_cast<const qasymm8_t *__restrict>(input1_ptr); |
| 198 | const auto input2 = static_cast<const qasymm8_t *__restrict>(input2_ptr); |
| 199 | const auto output = static_cast<qasymm8_t *__restrict>(output_ptr); |
| 200 | |
| 201 | const qasymm8x16_t input1_q = vld1q_u8(input1); |
| 202 | const qasymm8x16_t input2_q = vld1q_u8(input2); |
| 203 | |
| 204 | // Dequantitize inputs |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 205 | float32x4x4_t in1_f32x4x4; |
| 206 | float32x4x4_t in2_f32x4x4; |
| 207 | in1_f32x4x4.val[0] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(input1_q))))), input1_voffset)), input1_vscale); |
| 208 | in1_f32x4x4.val[1] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(input1_q))))), input1_voffset)), input1_vscale); |
| 209 | in1_f32x4x4.val[2] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(input1_q))))), input1_voffset)), input1_vscale); |
| 210 | in1_f32x4x4.val[3] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(input1_q))))), input1_voffset)), input1_vscale); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 211 | |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 212 | in2_f32x4x4.val[0] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(input2_q))))), input2_voffset)), input2_vscale); |
| 213 | in2_f32x4x4.val[1] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(input2_q))))), input2_voffset)), input2_vscale); |
| 214 | in2_f32x4x4.val[2] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(input2_q))))), input2_voffset)), input2_vscale); |
| 215 | in2_f32x4x4.val[3] = vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(input2_q))))), input2_voffset)), input2_vscale); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 216 | |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 217 | float32x4x4_t out_f32x4x4; |
| 218 | out_f32x4x4.val[0] = vmulq_f32(in1_f32x4x4.val[0], in2_f32x4x4.val[0]); |
| 219 | out_f32x4x4.val[1] = vmulq_f32(in1_f32x4x4.val[1], in2_f32x4x4.val[1]); |
| 220 | out_f32x4x4.val[2] = vmulq_f32(in1_f32x4x4.val[2], in2_f32x4x4.val[2]); |
| 221 | out_f32x4x4.val[3] = vmulq_f32(in1_f32x4x4.val[3], in2_f32x4x4.val[3]); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 222 | |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 223 | int32x4x4_t rf; |
| 224 | #ifdef __aarch64__ |
| 225 | rf.val[0] = vcvtnq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[0], vinvscale)); |
| 226 | rf.val[1] = vcvtnq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[1], vinvscale)); |
| 227 | rf.val[2] = vcvtnq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[2], vinvscale)); |
| 228 | rf.val[3] = vcvtnq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[3], vinvscale)); |
| 229 | #else //__aarch64__ |
| 230 | rf.val[0] = vcvtq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[0], vinvscale)); |
| 231 | rf.val[1] = vcvtq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[1], vinvscale)); |
| 232 | rf.val[2] = vcvtq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[2], vinvscale)); |
| 233 | rf.val[3] = vcvtq_s32_f32(vmlaq_f32(output_voffset, out_f32x4x4.val[3], vinvscale)); |
| 234 | #endif //__aarch64__ |
| 235 | const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 236 | const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 237 | |
| 238 | vst1q_u8(output, vcombine_u8(pa, pb)); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 241 | void mul_saturate_QSYMM16_QSYMM16_QSYMM16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale, |
| 242 | const UniformQuantizationInfo &input1_qua_info, const UniformQuantizationInfo &input2_qua_info, const UniformQuantizationInfo &output_qua_info) |
| 243 | { |
| 244 | const auto input1 = static_cast<const qsymm16_t *__restrict>(input1_ptr); |
| 245 | const auto input2 = static_cast<const qsymm16_t *__restrict>(input2_ptr); |
| 246 | const auto output = static_cast<qsymm16_t *__restrict>(output_ptr); |
| 247 | |
| 248 | const qsymm16x8x2_t input1_q = vld2q_s16(input1); |
| 249 | const qsymm16x8x2_t input2_q = vld2q_s16(input2); |
| 250 | |
| 251 | // Dequantitize inputs |
| 252 | const float32x4x4_t in1_f32x4x4 = vdequantize(input1_q, input1_qua_info); |
| 253 | const float32x4x4_t in2_f32x4x4 = vdequantize(input2_q, input2_qua_info); |
| 254 | |
| 255 | const UniformQuantizationInfo tmp_qua_info = { output_qua_info.scale / scale, output_qua_info.offset }; |
| 256 | |
| 257 | const float32x4x4_t out_f32x4x4 = |
| 258 | { |
| 259 | vmulq_f32(in1_f32x4x4.val[0], in2_f32x4x4.val[0]), |
| 260 | vmulq_f32(in1_f32x4x4.val[1], in2_f32x4x4.val[1]), |
| 261 | vmulq_f32(in1_f32x4x4.val[2], in2_f32x4x4.val[2]), |
| 262 | vmulq_f32(in1_f32x4x4.val[3], in2_f32x4x4.val[3]), |
| 263 | }; |
| 264 | |
| 265 | const qsymm16x8x2_t result = vquantize_qsymm16(out_f32x4x4, tmp_qua_info); |
| 266 | vst2q_s16(output, result); |
| 267 | } |
| 268 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 269 | template <bool is_scale255, bool is_sat> |
| 270 | void mul_U8_U8_U8_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 271 | { |
| 272 | const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr); |
| 273 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 274 | const auto output = static_cast<uint8_t *__restrict>(output_ptr); |
| 275 | |
| 276 | const uint8x16_t ta1 = vld1q_u8(input1); |
| 277 | const uint8x16_t ta2 = vld1q_u8(input2); |
| 278 | |
| 279 | uint16x8_t tmp1_high = vmovl_u8(vget_high_u8(ta1)); |
| 280 | const uint16x8_t tmp2_high = vmovl_u8(vget_high_u8(ta2)); |
| 281 | uint16x8_t tmp1_low = vmovl_u8(vget_low_u8(ta1)); |
| 282 | const uint16x8_t tmp2_low = vmovl_u8(vget_low_u8(ta2)); |
| 283 | |
| 284 | tmp1_high = vmulq_u16(tmp1_high, tmp2_high); |
| 285 | tmp1_low = vmulq_u16(tmp1_low, tmp2_low); |
| 286 | |
| 287 | if(is_scale255) |
| 288 | { |
| 289 | tmp1_high = scale255_U16_U16(tmp1_high); |
| 290 | tmp1_low = scale255_U16_U16(tmp1_low); |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | const int16x8_t vn = vdupq_n_s16(-n); |
| 295 | |
| 296 | if(is_sat) |
| 297 | { |
| 298 | tmp1_high = vqshlq_u16(tmp1_high, vn); |
| 299 | tmp1_low = vqshlq_u16(tmp1_low, vn); |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | tmp1_high = vshlq_u16(tmp1_high, vn); |
| 304 | tmp1_low = vshlq_u16(tmp1_low, vn); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if(is_sat) |
| 309 | { |
| 310 | vst1q_u8(output, vcombine_u8(vqmovn_u16(tmp1_low), vqmovn_u16(tmp1_high))); |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | vst1q_u8(output, vcombine_u8(vmovn_u16(tmp1_low), vmovn_u16(tmp1_high))); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | template <bool is_scale255, bool is_sat> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 319 | inline int16x8_t mul_S16_S16_S16_n_loop(const int16x8_t &input1, const int16x8_t &input2, int n) |
| 320 | { |
| 321 | int32x4_t tmp1_high = vmovl_s16(vget_high_s16(input1)); |
| 322 | const int32x4_t tmp2_high = vmovl_s16(vget_high_s16(input2)); |
| 323 | int32x4_t tmp1_low = vmovl_s16(vget_low_s16(input1)); |
| 324 | const int32x4_t tmp2_low = vmovl_s16(vget_low_s16(input2)); |
| 325 | |
| 326 | tmp1_high = vmulq_s32(tmp1_high, tmp2_high); |
| 327 | tmp1_low = vmulq_s32(tmp1_low, tmp2_low); |
| 328 | |
| 329 | if(is_scale255) |
| 330 | { |
| 331 | tmp1_high = scale255_S32_S32(tmp1_high); |
| 332 | tmp1_low = scale255_S32_S32(tmp1_low); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | // Right shift amount |
| 337 | const int32x4_t vn = vdupq_n_s32(-n); |
| 338 | // Left shift amount |
| 339 | const int32x4_t vnl = vdupq_n_s32(n); |
| 340 | // Calculate conversion bit |
| 341 | const uint32x4_t tmp1_high_u = vreinterpretq_u32_s32(tmp1_high); |
| 342 | const uint32x4_t tmp1_low_u = vreinterpretq_u32_s32(tmp1_low); |
| 343 | const uint32x4_t sign_high = vshrq_n_u32(tmp1_high_u, 31); |
| 344 | const uint32x4_t sign_low = vshrq_n_u32(tmp1_low_u, 31); |
| 345 | const int32x4_t sign_high_s = vreinterpretq_s32_u32(sign_high); |
| 346 | const int32x4_t sign_low_s = vreinterpretq_s32_u32(sign_low); |
| 347 | const int32x4_t convert_high = vsubq_s32(vshlq_s32(sign_high_s, vnl), sign_high_s); |
| 348 | const int32x4_t convert_low = vsubq_s32(vshlq_s32(sign_low_s, vnl), sign_low_s); |
| 349 | if(is_sat) |
| 350 | { |
| 351 | tmp1_high = vqshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 352 | tmp1_low = vqshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | tmp1_high = vshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 357 | tmp1_low = vshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if(is_sat) |
| 362 | { |
| 363 | return vcombine_s16(vqmovn_s32(tmp1_low), vqmovn_s32(tmp1_high)); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | return vcombine_s16(vmovn_s32(tmp1_low), vmovn_s32(tmp1_high)); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | template <bool is_scale255, bool is_sat> |
| 372 | inline int16x8x2_t mul_S16_S16_S16_n_k(const int16x8x2_t &input1, const int16x8x2_t &input2, int n) |
| 373 | { |
| 374 | const int16x8x2_t result = |
| 375 | { |
| 376 | { |
| 377 | // First 8 elements |
| 378 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[0], input2.val[0], n), |
| 379 | // Second 8 elements |
| 380 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[1], input2.val[1], n) |
| 381 | } |
| 382 | }; |
| 383 | |
| 384 | return result; |
| 385 | } |
| 386 | |
| 387 | template <bool is_scale255, bool is_sat> |
| 388 | void mul_S16_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 389 | { |
| 390 | const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr); |
| 391 | const auto input2 = static_cast<const int16_t *__restrict>(input2_ptr); |
| 392 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 393 | |
| 394 | const int16x8x2_t ta1 = vld2q_s16(input1); |
| 395 | const int16x8x2_t ta2 = vld2q_s16(input2); |
| 396 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
| 397 | |
| 398 | vst2q_s16(output, result); |
| 399 | } |
| 400 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 401 | void mul_F32_F32_F32_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale) |
| 402 | { |
| 403 | const auto input1 = static_cast<const float *__restrict>(input1_ptr); |
| 404 | const auto input2 = static_cast<const float *__restrict>(input2_ptr); |
| 405 | const auto output = static_cast<float *__restrict>(output_ptr); |
| 406 | |
| 407 | const float32x4x4_t ta1 = vld4q_f32(input1); |
| 408 | const float32x4x4_t ta2 = vld4q_f32(input2); |
| 409 | const float32x4_t scale_vec = vdupq_n_f32(scale); |
| 410 | const float32x4x4_t result = |
| 411 | { |
| 412 | { |
| 413 | vmulq_f32(vmulq_f32(ta1.val[0], ta2.val[0]), scale_vec), |
| 414 | vmulq_f32(vmulq_f32(ta1.val[1], ta2.val[1]), scale_vec), |
| 415 | vmulq_f32(vmulq_f32(ta1.val[2], ta2.val[2]), scale_vec), |
| 416 | vmulq_f32(vmulq_f32(ta1.val[3], ta2.val[3]), scale_vec) |
| 417 | } |
| 418 | }; |
| 419 | vst4q_f32(output, result); |
| 420 | } |
| 421 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 422 | void c_mul_F32_F32_F32_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr) |
| 423 | { |
| 424 | const auto input1 = static_cast<const float *__restrict>(input1_ptr); |
| 425 | const auto input2 = static_cast<const float *__restrict>(input2_ptr); |
| 426 | const auto output = static_cast<float *__restrict>(output_ptr); |
| 427 | |
| 428 | const float32x4_t a = wrapper::vloadq(input1); |
| 429 | float32x4_t b = wrapper::vloadq(input2); |
| 430 | |
| 431 | using ExactTagType = typename wrapper::traits::neon_vector<float, 2>::tag_type; |
| 432 | |
| 433 | const float32x4_t mask = { -1.0f, 1.0f, -1.0f, 1.0f }; |
| 434 | const float32x2_t tmp00 = wrapper::vdup_n(wrapper::vgetlane(a, 0), ExactTagType{}); |
| 435 | const float32x2_t tmp01 = wrapper::vdup_n(wrapper::vgetlane(a, 1), ExactTagType{}); |
| 436 | const float32x2_t tmp10 = wrapper::vdup_n(wrapper::vgetlane(a, 2), ExactTagType{}); |
| 437 | const float32x2_t tmp11 = wrapper::vdup_n(wrapper::vgetlane(a, 3), ExactTagType{}); |
| 438 | |
| 439 | const float32x4_t tmp0 = wrapper::vcombine(tmp00, tmp10); |
| 440 | const float32x4_t tmp1 = wrapper::vcombine(tmp01, tmp11); |
| 441 | |
| 442 | float32x4_t res = wrapper::vmul(tmp0, b); |
| 443 | |
| 444 | b = wrapper::vrev64(b); |
| 445 | b = wrapper::vmul(b, mask); |
| 446 | |
| 447 | res = wrapper::vmla(res, tmp1, b); |
| 448 | wrapper::vstore(output, res); |
| 449 | } |
| 450 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 451 | void mul_F16_F16_F16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale) |
| 452 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 453 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 454 | const auto input1 = static_cast<const float16_t *__restrict>(input1_ptr); |
| 455 | const auto input2 = static_cast<const float16_t *__restrict>(input2_ptr); |
| 456 | const auto output = static_cast<float16_t *__restrict>(output_ptr); |
| 457 | const float16x8x2_t ta1 = vld2q_f16(input1); |
| 458 | const float16x8x2_t ta2 = vld2q_f16(input2); |
| 459 | const float16x8_t scale_vec = vdupq_n_f16(scale); |
| 460 | const float16x8x2_t result = |
| 461 | { |
| 462 | { |
| 463 | vmulq_f16(vmulq_f16(ta1.val[0], ta2.val[0]), scale_vec), |
| 464 | vmulq_f16(vmulq_f16(ta1.val[1], ta2.val[1]), scale_vec), |
| 465 | } |
| 466 | }; |
| 467 | vst2q_f16(output, result); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 468 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Georgios Pinitas | 30f0215 | 2017-09-27 11:20:48 +0100 | [diff] [blame] | 469 | ARM_COMPUTE_UNUSED(input1_ptr); |
| 470 | ARM_COMPUTE_UNUSED(input2_ptr); |
| 471 | ARM_COMPUTE_UNUSED(output_ptr); |
| 472 | ARM_COMPUTE_UNUSED(scale); |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 473 | ARM_COMPUTE_ERROR("Not supported. Recompile the library with arch=arm64-v8.2-a."); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 474 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | template <bool is_scale255, bool is_sat> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 478 | void mul_U8_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 479 | { |
| 480 | const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr); |
| 481 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 482 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 483 | |
| 484 | const uint8x16_t bv = vld1q_u8(input2); |
| 485 | const uint8x16_t av = vld1q_u8(input1); |
| 486 | |
| 487 | uint16x8_t tmp_low = vmovl_u8(vget_low_u8(av)); |
| 488 | uint16x8_t tmp_high = vmovl_u8(vget_high_u8(av)); |
| 489 | tmp_low = vmulq_u16(tmp_low, vmovl_u8(vget_low_u8(bv))); |
| 490 | tmp_high = vmulq_u16(tmp_high, vmovl_u8(vget_high_u8(bv))); |
| 491 | |
| 492 | if(is_scale255) |
| 493 | { |
| 494 | tmp_low = scale255_U16_U16(tmp_low); |
| 495 | tmp_high = scale255_U16_U16(tmp_high); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | const int16x8_t vn = vdupq_n_s16(-n); |
| 500 | |
| 501 | if(is_sat) |
| 502 | { |
| 503 | tmp_low = vqshlq_u16(tmp_low, vn); |
| 504 | tmp_high = vqshlq_u16(tmp_high, vn); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | tmp_low = vshlq_u16(tmp_low, vn); |
| 509 | tmp_high = vshlq_u16(tmp_high, vn); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if(is_sat) |
| 514 | { |
| 515 | static const uint16x8_t max = vdupq_n_u16(SHRT_MAX); |
| 516 | |
| 517 | tmp_low = vminq_u16(tmp_low, max); |
| 518 | tmp_high = vminq_u16(tmp_high, max); |
| 519 | } |
| 520 | |
| 521 | vst1q_s16(output, vreinterpretq_s16_u16(tmp_low)); |
| 522 | vst1q_s16(output + 8, vreinterpretq_s16_u16(tmp_high)); |
| 523 | } |
| 524 | |
| 525 | template <bool is_scale255, bool is_sat> |
| 526 | void mul_S16_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 527 | { |
| 528 | const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr); |
| 529 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 530 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 531 | |
| 532 | const int16x8x2_t ta1 = vld2q_s16(input1); |
| 533 | const uint8x8x2_t ta2u = vld2_u8(input2); |
| 534 | const int16x8x2_t ta2 = |
| 535 | { |
| 536 | { |
| 537 | vreinterpretq_s16_u16(vmovl_u8(ta2u.val[0])), |
| 538 | vreinterpretq_s16_u16(vmovl_u8(ta2u.val[1])) |
| 539 | } |
| 540 | }; |
| 541 | |
| 542 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
| 543 | |
| 544 | vst2q_s16(output, result); |
| 545 | } |
| 546 | |
| 547 | template <bool is_scale255, bool is_sat> |
| 548 | void mul_U8_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 549 | { |
| 550 | // Simply swap the two input buffers |
| 551 | mul_S16_U8_S16_n<is_scale255, is_sat>(input2_ptr, input1_ptr, output_ptr, n); |
| 552 | } |
| 553 | } // namespace |
| 554 | |
| 555 | NEPixelWiseMultiplicationKernel::NEPixelWiseMultiplicationKernel() |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 556 | : _func_float(nullptr), _func_int(nullptr), _func_quantized(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _scale{ 0 }, _scale_exponent{ 0 }, _run_optimized_qasymm8(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 557 | { |
| 558 | } |
| 559 | |
| 560 | void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) |
| 561 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 562 | ARM_COMPUTE_UNUSED(rounding_policy); |
Georgios Pinitas | f0dea70 | 2017-07-03 18:17:28 +0100 | [diff] [blame] | 563 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 564 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 565 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 566 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 567 | // Configure kernel window |
| 568 | auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info()); |
| 569 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 570 | |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 571 | _input1 = input1; |
| 572 | _input2 = input2; |
| 573 | _output = output; |
| 574 | _scale = scale; |
| 575 | _scale_exponent = 0; |
| 576 | _func_quantized = nullptr; |
| 577 | _func_int = nullptr; |
| 578 | _func_float = nullptr; |
| 579 | _run_optimized_qasymm8 = false; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 580 | |
| 581 | bool is_scale_255 = false; |
| 582 | // Check and validate scaling factor |
| 583 | if(std::abs(scale - scale255_constant) < 0.00001f) |
| 584 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 585 | is_scale_255 = true; |
| 586 | } |
| 587 | else |
| 588 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 589 | int exponent = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 590 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 591 | std::frexp(scale, &exponent); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 592 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 593 | // Store the positive exponent. We know that we compute 1/2^n |
| 594 | // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5 |
| 595 | _scale_exponent = std::abs(exponent - 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | const DataType dt_input1 = input1->info()->data_type(); |
| 599 | const DataType dt_input2 = input2->info()->data_type(); |
| 600 | const DataType dt_output = output->info()->data_type(); |
| 601 | const bool is_sat = (overflow_policy == ConvertPolicy::SATURATE); |
| 602 | |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 603 | if(dt_input1 == DataType::QASYMM8 && dt_input2 == DataType::QASYMM8) |
| 604 | { |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 605 | _run_optimized_qasymm8 = true; |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 606 | } |
| 607 | else if(dt_input1 == DataType::QSYMM16 && dt_input2 == DataType::QSYMM16) |
| 608 | { |
| 609 | _func_quantized = &mul_saturate_QSYMM16_QSYMM16_QSYMM16_n; |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 610 | } |
| 611 | else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::U8 == dt_output) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 612 | { |
| 613 | if(is_scale_255) |
| 614 | { |
| 615 | _func_int = is_sat ? &mul_U8_U8_U8_n<true, true> : &mul_U8_U8_U8_n<true, false>; |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | _func_int = is_sat ? &mul_U8_U8_U8_n<false, true> : &mul_U8_U8_U8_n<false, false>; |
| 620 | } |
| 621 | } |
| 622 | else if(DataType::S16 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
| 623 | { |
| 624 | if(is_scale_255) |
| 625 | { |
| 626 | _func_int = is_sat ? &mul_S16_S16_S16_n<true, true> : &mul_S16_S16_S16_n<true, false>; |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | _func_int = is_sat ? &mul_S16_S16_S16_n<false, true> : &mul_S16_S16_S16_n<false, false>; |
| 631 | } |
| 632 | } |
| 633 | else if(DataType::S16 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
| 634 | { |
| 635 | if(is_scale_255) |
| 636 | { |
| 637 | _func_int = is_sat ? &mul_S16_U8_S16_n<true, true> : &mul_S16_U8_S16_n<true, false>; |
| 638 | } |
| 639 | else |
| 640 | { |
| 641 | _func_int = is_sat ? &mul_S16_U8_S16_n<false, true> : &mul_S16_U8_S16_n<false, false>; |
| 642 | } |
| 643 | } |
| 644 | else if(DataType::U8 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
| 645 | { |
| 646 | if(is_scale_255) |
| 647 | { |
| 648 | _func_int = is_sat ? &mul_U8_S16_S16_n<true, true> : &mul_U8_S16_S16_n<true, false>; |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | _func_int = is_sat ? &mul_U8_S16_S16_n<false, true> : &mul_U8_S16_S16_n<false, false>; |
| 653 | } |
| 654 | } |
| 655 | else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
| 656 | { |
| 657 | if(is_scale_255) |
| 658 | { |
| 659 | _func_int = is_sat ? &mul_U8_U8_S16_n<true, true> : &mul_U8_U8_S16_n<true, false>; |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | _func_int = is_sat ? &mul_U8_U8_S16_n<false, true> : &mul_U8_U8_S16_n<false, false>; |
| 664 | } |
| 665 | } |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 666 | else if(DataType::F16 == dt_input1 && DataType::F16 == dt_input2 && DataType::F16 == dt_output) |
| 667 | { |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 668 | _func_float = &mul_F16_F16_F16_n; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 669 | _func_int = nullptr; |
| 670 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 671 | else if(DataType::F32 == dt_input1 && DataType::F32 == dt_input2 && DataType::F32 == dt_output) |
| 672 | { |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 673 | _func_float = &mul_F32_F32_F32_n; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 674 | _func_int = nullptr; |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | ARM_COMPUTE_ERROR("You called with the wrong img formats"); |
| 679 | } |
| 680 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 681 | INEKernel::configure(win_config.second); |
| 682 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 683 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 684 | Status NEPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, |
| 685 | RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 686 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 687 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 688 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy)); |
| 689 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 690 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 691 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 694 | void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 695 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 696 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 697 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 698 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 699 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 700 | const TensorShape &in_shape1 = _input1->info()->tensor_shape(); |
| 701 | const TensorShape &in_shape2 = _input2->info()->tensor_shape(); |
| 702 | const TensorShape &out_shape = _output->info()->tensor_shape(); |
| 703 | |
| 704 | bool can_collapse = true; |
| 705 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) |
| 706 | { |
| 707 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 708 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) |
| 709 | { |
| 710 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | bool has_collapsed = false; |
| 715 | Window collapsed = can_collapse ? window.collapse_if_possible(INEKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 716 | |
| 717 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 718 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 719 | |
| 720 | Window slice = collapsed.first_slice_window_3D(); |
| 721 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 722 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
| 723 | |
| 724 | Iterator input1(_input1, slice_input1); |
| 725 | Iterator input2(_input2, slice_input2); |
| 726 | Iterator output(_output, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 727 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 728 | if(is_data_type_quantized(_input1->info()->data_type())) |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 729 | { |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 730 | if(_run_optimized_qasymm8) |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 731 | { |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 732 | const int32x4_t input1_voffset = vdupq_n_s32(_input1->info()->quantization_info().uniform().offset); |
| 733 | const float32x4_t input1_vscale = vdupq_n_f32(_input1->info()->quantization_info().uniform().scale); |
| 734 | const int32x4_t input2_voffset = vdupq_n_s32(_input2->info()->quantization_info().uniform().offset); |
| 735 | const float32x4_t input2_vscale = vdupq_n_f32(_input2->info()->quantization_info().uniform().scale); |
| 736 | const float32x4_t output_voffset = vdupq_n_f32(static_cast<float>(_output->info()->quantization_info().uniform().offset)); |
| 737 | const float output_scale = _output->info()->quantization_info().uniform().scale; |
| 738 | const float32x4_t vinvscale = vdupq_n_f32(1.f / (output_scale / _scale)); |
| 739 | |
| 740 | execute_window_loop(collapsed, [&](const Coordinates &) |
| 741 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 742 | mul_saturate_QASYMM8_QASYMM8_QASYMM8_n_opt(input1.ptr(), input2.ptr(), output.ptr(), |
Gian Marco Iodice | b19c55d | 2019-08-30 17:50:15 +0100 | [diff] [blame] | 743 | input1_vscale, input1_voffset, input2_vscale, input2_voffset, output_voffset, vinvscale); |
| 744 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 745 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
| 746 | }, |
| 747 | input1, input2, output); |
| 748 | } |
| 749 | else |
| 750 | { |
| 751 | execute_window_loop(collapsed, [&](const Coordinates &) |
| 752 | { |
| 753 | (*_func_quantized)(input1.ptr(), input2.ptr(), output.ptr(), _scale, |
| 754 | _input1->info()->quantization_info().uniform(), _input2->info()->quantization_info().uniform(), _output->info()->quantization_info().uniform()); |
| 755 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 756 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
| 757 | }, |
| 758 | input1, input2, output); |
| 759 | } |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 760 | } |
| 761 | else if(_func_int != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 762 | { |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 763 | execute_window_loop(collapsed, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 764 | { |
| 765 | (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent); |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 766 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 767 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 768 | }, |
| 769 | input1, input2, output); |
| 770 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 771 | else |
| 772 | { |
| 773 | ARM_COMPUTE_ERROR_ON(_func_float == nullptr); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 774 | execute_window_loop(collapsed, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 775 | { |
| 776 | (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale); |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 777 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 778 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 779 | }, |
| 780 | input1, input2, output); |
| 781 | } |
| 782 | } |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 783 | |
| 784 | BorderSize NEPixelWiseMultiplicationKernel::border_size() const |
| 785 | { |
| 786 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 787 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 788 | return BorderSize{ 0, border, 0, 0 }; |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 789 | } |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 790 | |
| 791 | namespace |
| 792 | { |
| 793 | constexpr unsigned int num_elems_processed_per_iteration_complex = 2; |
| 794 | |
| 795 | Status validate_arguments_complex(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 796 | { |
| 797 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 2, DataType::F32); |
| 798 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 2, DataType::F32); |
| 799 | |
| 800 | const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape()); |
| 801 | |
| 802 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 803 | |
| 804 | // Validate in case of configured output |
| 805 | if(output->total_size() > 0) |
| 806 | { |
| 807 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 2, DataType::F32); |
| 808 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output"); |
| 809 | } |
| 810 | |
| 811 | return Status{}; |
| 812 | } |
| 813 | |
| 814 | std::pair<Status, Window> validate_and_configure_window_complex(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) |
| 815 | { |
| 816 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); |
| 817 | const TensorShape &out_shape = broadcast_pair.first; |
| 818 | const ValidRegion &valid_region = broadcast_pair.second; |
| 819 | |
| 820 | // Auto initialize output if not initialized |
| 821 | const TensorInfo out_info(out_shape, input1->num_channels(), input1->data_type()); |
| 822 | auto_init_if_empty(*output, out_info); |
| 823 | |
| 824 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration_complex)); |
| 825 | Window win_input1 = win.broadcast_if_dimension_le_one(*input1); |
| 826 | Window win_input2 = win.broadcast_if_dimension_le_one(*input2); |
| 827 | |
| 828 | AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration_complex); |
| 829 | AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration_complex); |
| 830 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_complex); |
| 831 | |
| 832 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 833 | || update_window_and_padding(win_input2, input2_access) |
| 834 | || update_window_and_padding(win, output_access); |
| 835 | |
| 836 | output_access.set_valid_region(win, valid_region); |
| 837 | |
| 838 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 839 | return std::make_pair(err, win); |
| 840 | } |
| 841 | } // namespace |
| 842 | |
| 843 | NEComplexPixelWiseMultiplicationKernel::NEComplexPixelWiseMultiplicationKernel() |
| 844 | : _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 845 | { |
| 846 | } |
| 847 | |
| 848 | void NEComplexPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output) |
| 849 | { |
| 850 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 851 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(input1->info(), input2->info(), output->info())); |
| 852 | |
| 853 | // Configure kernel window |
| 854 | auto win_config = validate_and_configure_window_complex(input1->info(), input2->info(), output->info()); |
| 855 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 856 | |
| 857 | _input1 = input1; |
| 858 | _input2 = input2; |
| 859 | _output = output; |
| 860 | |
| 861 | // Create kernel |
| 862 | INEKernel::configure(win_config.second); |
| 863 | } |
| 864 | |
| 865 | Status NEComplexPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 866 | { |
| 867 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 868 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(input1, input2, output)); |
| 869 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_complex(input1->clone().get(), input2->clone().get(), output->clone().get()).first); |
| 870 | |
| 871 | return Status{}; |
| 872 | } |
| 873 | |
| 874 | void NEComplexPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info) |
| 875 | { |
| 876 | ARM_COMPUTE_UNUSED(info); |
| 877 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 878 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 879 | |
| 880 | Iterator input1(_input1, window.broadcast_if_dimension_le_one(_input1->info()->tensor_shape())); |
| 881 | Iterator input2(_input2, window.broadcast_if_dimension_le_one(_input2->info()->tensor_shape())); |
| 882 | Iterator output(_output, window); |
| 883 | |
| 884 | execute_window_loop(window, [&](const Coordinates &) |
| 885 | { |
| 886 | c_mul_F32_F32_F32_n(input1.ptr(), input2.ptr(), output.ptr()); |
| 887 | }, |
| 888 | input1, input2, output); |
| 889 | } |
| 890 | |
| 891 | BorderSize NEComplexPixelWiseMultiplicationKernel::border_size() const |
| 892 | { |
| 893 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 894 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration_complex - 1U, replicateSize); |
| 895 | return { 0, border, 0, 0 }; |
| 896 | } |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 897 | } // namespace arm_compute |