Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [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/NEGEMMMatrixVectorMultiplyKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CPP/Validate.h" |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/INEKernel.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include <arm_neon.h> |
| 37 | #include <cstddef> |
| 38 | #include <cstdint> |
| 39 | #include <tuple> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) |
| 46 | { |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input0); |
Georgios Pinitas | 8cffcd6 | 2018-11-16 17:11:50 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::S32, DataType::F16, DataType::F32); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input0->data_type()) && (output->data_type() != DataType::S32)); |
Georgios Pinitas | 8cffcd6 | 2018-11-16 17:11:50 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_float(input0->data_type()) && (output->data_type() != input0->data_type())); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 53 | |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(input0->num_dimensions() == input1->num_dimensions()); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(2) != input1->dimension(1)); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(DataLayoutDimension::HEIGHT) != output->dimension(DataLayoutDimension::HEIGHT)); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(DataLayoutDimension::WIDTH) != output->dimension(DataLayoutDimension::WIDTH)); |
| 58 | |
| 59 | return Status{}; |
| 60 | } |
| 61 | |
| 62 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output) |
| 63 | { |
| 64 | const unsigned int num_elems_read_per_iteration = 16 / input0->element_size(); |
| 65 | |
| 66 | Window win = calculate_max_window(*input0, Steps(num_elems_read_per_iteration)); |
| 67 | |
| 68 | AccessWindowHorizontal input0_access(input0, 0, num_elems_read_per_iteration); |
| 69 | AccessWindowHorizontal input1_access(input1, 0, num_elems_read_per_iteration); |
| 70 | AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1)); |
| 71 | |
| 72 | bool window_changed = update_window_and_padding(win, input0_access, input1_access, output_access); |
| 73 | |
| 74 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
| 75 | |
| 76 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 77 | return std::make_pair(err, win); |
| 78 | } |
| 79 | } // namespace |
| 80 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 81 | template <typename I0, typename I1, typename O> |
| 82 | void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply(const Window &window_in, const Window &window_w, const Window &window_out) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 83 | { |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 84 | ARM_COMPUTE_ERROR("Unsupported data types"); |
| 85 | ARM_COMPUTE_UNUSED(window_in); |
| 86 | ARM_COMPUTE_UNUSED(window_w); |
| 87 | ARM_COMPUTE_UNUSED(window_out); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 90 | namespace arm_compute |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 91 | { |
Georgios Pinitas | 8cffcd6 | 2018-11-16 17:11:50 +0000 | [diff] [blame] | 92 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 93 | template <> |
| 94 | void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<half, half, half>(const Window &window_in, |
| 95 | const Window &window_w, |
| 96 | const Window &window_out) |
| 97 | { |
| 98 | Iterator in(_input0, window_in); |
| 99 | Iterator in2(_input1, window_w); |
| 100 | Iterator out(_output, window_out); |
| 101 | |
| 102 | const int input_w = _input0->info()->dimension(0); |
| 103 | const int input_h = _input0->info()->dimension(1); |
| 104 | const int input_stride_x = _input0->info()->strides_in_bytes().x(); |
| 105 | const int weights_stride_x = _input1->info()->strides_in_bytes().x(); |
| 106 | const int weights_stride_y = _input1->info()->strides_in_bytes().y(); |
| 107 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 108 | |
| 109 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 110 | { |
| 111 | // Get pointers |
| 112 | const uint8_t *const input_ptr = in.ptr(); |
| 113 | const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y; |
| 114 | auto output_ptr = reinterpret_cast<__fp16 *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x); |
| 115 | |
| 116 | float16x8_t row_dot = vdupq_n_f16(0.f); |
| 117 | for(int i = 0; i < input_w; i += 8) |
| 118 | { |
| 119 | const auto input = vld1q_f16(reinterpret_cast<const __fp16 *>(input_ptr + i * input_stride_x)); |
| 120 | const auto weights = vld1q_f16(reinterpret_cast<const __fp16 *>(weights_ptr + i * weights_stride_x)); |
| 121 | row_dot = vaddq_f16(row_dot, vmulq_f16(input, weights)); |
| 122 | } |
| 123 | |
| 124 | auto temp = vadd_f16(vget_high_f16(row_dot), vget_low_f16(row_dot)); |
| 125 | temp = vpadd_f16(temp, temp); |
| 126 | temp = vpadd_f16(temp, temp); |
| 127 | |
| 128 | *output_ptr = vget_lane_f16(temp, 0); |
| 129 | }, |
| 130 | in, in2, out); |
| 131 | } |
| 132 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 133 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 134 | template <> |
| 135 | void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<float, float, float>(const Window &window_in, |
| 136 | const Window &window_w, |
| 137 | const Window &window_out) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 138 | { |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 139 | Iterator in(_input0, window_in); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 140 | Iterator in2(_input1, window_w); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 141 | Iterator out(_output, window_out); |
| 142 | |
| 143 | const int input_w = _input0->info()->dimension(0); |
| 144 | const int input_h = _input0->info()->dimension(1); |
| 145 | const int input_stride_x = _input0->info()->strides_in_bytes().x(); |
| 146 | const int weights_stride_x = _input1->info()->strides_in_bytes().x(); |
| 147 | const int weights_stride_y = _input1->info()->strides_in_bytes().y(); |
| 148 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 149 | |
| 150 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 151 | { |
| 152 | // Get pointers |
| 153 | const uint8_t *const input_ptr = in.ptr(); |
| 154 | const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y; |
| 155 | auto output_ptr = reinterpret_cast<float *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x); |
| 156 | |
| 157 | float32x4_t row_dot = vdupq_n_f32(0.f); |
| 158 | for(int i = 0; i < input_w; i += 4) |
| 159 | { |
| 160 | const auto input = vld1q_f32(reinterpret_cast<const float *>(input_ptr + i * input_stride_x)); |
| 161 | const auto weights = vld1q_f32(reinterpret_cast<const float *>(weights_ptr + i * weights_stride_x)); |
| 162 | row_dot = vaddq_f32(row_dot, vmulq_f32(input, weights)); |
| 163 | } |
| 164 | |
| 165 | auto temp = vadd_f32(vget_high_f32(row_dot), vget_low_f32(row_dot)); |
| 166 | temp = vpadd_f32(temp, temp); |
| 167 | |
| 168 | *output_ptr = vget_lane_f32(temp, 0); |
| 169 | }, |
| 170 | in, in2, out); |
| 171 | } |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 172 | |
| 173 | template <> |
| 174 | void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<uint8_t, uint8_t, int32_t>(const Window &window_in, |
| 175 | const Window &window_w, |
| 176 | const Window &window_out) |
| 177 | { |
| 178 | Iterator in(_input0, window_in); |
| 179 | Iterator in2(_input1, window_w); |
| 180 | Iterator out(_output, window_out); |
| 181 | |
| 182 | const int input_offset = -_input0->info()->quantization_info().offset; |
| 183 | const int weights_offset = -_input1->info()->quantization_info().offset; |
| 184 | |
| 185 | const int input_w = _input0->info()->dimension(0); |
| 186 | const int input_h = _input0->info()->dimension(1); |
| 187 | const int input_stride_x = _input0->info()->strides_in_bytes().x(); |
| 188 | const int weights_stride_x = _input1->info()->strides_in_bytes().x(); |
| 189 | const int weights_stride_y = _input1->info()->strides_in_bytes().y(); |
| 190 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 191 | const int read_step = 16 / _input0->info()->element_size(); |
| 192 | |
| 193 | const int32x4_t v_input_offset = vdupq_n_s32(input_offset); |
| 194 | const int32x4_t v_weights_offset = vdupq_n_s32(weights_offset); |
| 195 | |
| 196 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 197 | { |
| 198 | // Get pointers |
| 199 | const uint8_t *const input_ptr = in.ptr(); |
| 200 | const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y; |
| 201 | auto output_ptr = reinterpret_cast<int32_t *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x); |
| 202 | |
| 203 | int32x4_t row_dot = vdupq_n_s32(0); |
| 204 | for(int i = 0; i < input_w; i += read_step) |
| 205 | { |
| 206 | // Read values |
| 207 | const auto input = vld1q_u8(reinterpret_cast<const uint8_t *>(input_ptr + i * input_stride_x)); |
| 208 | const auto weights = vld1q_u8(reinterpret_cast<const uint8_t *>(weights_ptr + i * weights_stride_x)); |
| 209 | |
| 210 | // Add offsets |
| 211 | const int32x4x4_t input_s32 = |
| 212 | { |
| 213 | { |
| 214 | vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_low_u8(input))))), |
| 215 | vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_low_u8(input))))), |
| 216 | vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_high_u8(input))))), |
| 217 | vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_high_u8(input))))) |
| 218 | } |
| 219 | }; |
| 220 | const int32x4x4_t weights_s32 = |
| 221 | { |
| 222 | { |
| 223 | vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_low_u8(weights))))), |
| 224 | vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_low_u8(weights))))), |
| 225 | vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_high_u8(weights))))), |
| 226 | vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_high_u8(weights))))) |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | // Dot |
| 231 | row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[0], weights_s32.val[0])); |
| 232 | row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[1], weights_s32.val[1])); |
| 233 | row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[2], weights_s32.val[2])); |
| 234 | row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[3], weights_s32.val[3])); |
| 235 | } |
| 236 | |
| 237 | // Reduction |
| 238 | auto temp = vadd_s32(vget_high_s32(row_dot), vget_low_s32(row_dot)); |
| 239 | temp = vpadd_s32(temp, temp); |
| 240 | |
| 241 | *output_ptr = vget_lane_s32(temp, 0); |
| 242 | }, |
| 243 | in, in2, out); |
| 244 | } |
| 245 | } //namespace arm_compute |
| 246 | |
| 247 | NEGEMMMatrixVectorMultiplyKernel::NEGEMMMatrixVectorMultiplyKernel() |
| 248 | : _func(nullptr), _input0(nullptr), _input1(nullptr), _output(nullptr), _border_size(0) |
| 249 | { |
| 250 | } |
| 251 | |
| 252 | BorderSize NEGEMMMatrixVectorMultiplyKernel::border_size() const |
| 253 | { |
| 254 | return _border_size; |
| 255 | } |
| 256 | |
| 257 | void NEGEMMMatrixVectorMultiplyKernel::configure(const ITensor *input0, const ITensor *input1, ITensor *output) |
| 258 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 259 | ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output); |
| 260 | |
| 261 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info())); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 262 | |
| 263 | _input0 = input0; |
| 264 | _input1 = input1; |
| 265 | _output = output; |
| 266 | |
| 267 | // Set appropriate function to run |
| 268 | switch(input0->info()->data_type()) |
| 269 | { |
| 270 | case DataType::QASYMM8: |
| 271 | _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<uint8_t, uint8_t, int32_t>; |
| 272 | break; |
Georgios Pinitas | 8cffcd6 | 2018-11-16 17:11:50 +0000 | [diff] [blame] | 273 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 274 | case DataType::F16: |
| 275 | _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<half, half, half>; |
| 276 | break; |
| 277 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 278 | case DataType::F32: |
| 279 | _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<float, float, float>; |
| 280 | break; |
| 281 | default: |
| 282 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 283 | } |
| 284 | |
| 285 | // Configure kernel window |
| 286 | const unsigned int num_elems_read_per_iteration = 16 / _input0->info()->element_size(); |
| 287 | |
| 288 | const unsigned int border_x = ceil_to_multiple(input0->info()->dimension(0), num_elems_read_per_iteration) - input0->info()->dimension(0); |
| 289 | _border_size = BorderSize(0, border_x); |
| 290 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 291 | auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info()); |
| 292 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 293 | INEKernel::configure(win_config.second); |
| 294 | } |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 295 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 296 | Status NEGEMMMatrixVectorMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) |
| 297 | { |
| 298 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output); |
| 299 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output)); |
| 300 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), input1->clone().get(), output->clone().get()).first); |
| 301 | return Status{}; |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void NEGEMMMatrixVectorMultiplyKernel::run(const Window &window, const ThreadInfo &info) |
| 305 | { |
| 306 | ARM_COMPUTE_UNUSED(info); |
| 307 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 308 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 309 | |
| 310 | Window window_slice = window.first_slice_window_3D(); |
| 311 | |
| 312 | Window window_in(window); |
| 313 | Window window_weights(window_slice); |
| 314 | Window window_out(window); |
| 315 | |
| 316 | // Setup input0 slice |
| 317 | window_in.set(Window::DimX, Window::Dimension(0, _input0->info()->dimension(0), _input0->info()->dimension(0))); |
| 318 | window_in.set(Window::DimY, Window::Dimension(0, _input0->info()->dimension(1), 1)); |
| 319 | window_in.set(Window::DimZ, Window::Dimension(0, _input0->info()->dimension(2), 1)); |
| 320 | |
| 321 | // Setup input1 and output slice. Their dimensions are increased in the kernel. |
| 322 | window_weights.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 323 | window_weights.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 324 | window_weights.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 325 | |
| 326 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 327 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 328 | window_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 329 | |
| 330 | if(_func != nullptr) |
| 331 | { |
| 332 | (this->*_func)(window_in, window_weights, window_out); |
| 333 | } |
| 334 | } |