blob: cf8411c55feef1da4e7f0d4448bc54ed1bb71ed9 [file] [log] [blame]
Michalis Spyroub7b31532017-11-23 12:10:21 +00001/*
Georgios Pinitas8f5802f2019-02-22 11:08:32 +00002 * Copyright (c) 2016-2019 ARM Limited.
Michalis Spyroub7b31532017-11-23 12:10:21 +00003 *
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 Pinitas8f5802f2019-02-22 11:08:32 +000027#include "arm_compute/core/CPP/Validate.h"
Michalis Spyroub7b31532017-11-23 12:10:21 +000028#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
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +000041namespace arm_compute
42{
Abe Mbise7784c832018-05-31 16:48:41 +010043namespace
44{
45Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output)
46{
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000047 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input0);
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +000048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Abe Mbise7784c832018-05-31 16:48:41 +010049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +000050 if(is_data_type_quantized_asymmetric(input0->data_type()))
51 {
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
53 }
54 else
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
57 }
Abe Mbise7784c832018-05-31 16:48:41 +010058
59 ARM_COMPUTE_RETURN_ERROR_ON(input0->num_dimensions() == input1->num_dimensions());
60 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(2) != input1->dimension(1));
61 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(DataLayoutDimension::HEIGHT) != output->dimension(DataLayoutDimension::HEIGHT));
62 ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(DataLayoutDimension::WIDTH) != output->dimension(DataLayoutDimension::WIDTH));
63
64 return Status{};
65}
66
67std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output)
68{
69 const unsigned int num_elems_read_per_iteration = 16 / input0->element_size();
70
71 Window win = calculate_max_window(*input0, Steps(num_elems_read_per_iteration));
72
73 AccessWindowHorizontal input0_access(input0, 0, num_elems_read_per_iteration);
74 AccessWindowHorizontal input1_access(input1, 0, num_elems_read_per_iteration);
75 AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1));
76
77 bool window_changed = update_window_and_padding(win, input0_access, input1_access, output_access);
78
79 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
80
81 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
82 return std::make_pair(err, win);
83}
84} // namespace
85
Georgios Pinitasd05dce42018-01-22 16:29:17 +000086template <typename I0, typename I1, typename O>
87void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply(const Window &window_in, const Window &window_w, const Window &window_out)
Michalis Spyroub7b31532017-11-23 12:10:21 +000088{
Georgios Pinitasd05dce42018-01-22 16:29:17 +000089 ARM_COMPUTE_ERROR("Unsupported data types");
90 ARM_COMPUTE_UNUSED(window_in);
91 ARM_COMPUTE_UNUSED(window_w);
92 ARM_COMPUTE_UNUSED(window_out);
Michalis Spyroub7b31532017-11-23 12:10:21 +000093}
94
Georgios Pinitas8cffcd62018-11-16 17:11:50 +000095#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
96template <>
97void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<half, half, half>(const Window &window_in,
98 const Window &window_w,
99 const Window &window_out)
100{
101 Iterator in(_input0, window_in);
102 Iterator in2(_input1, window_w);
103 Iterator out(_output, window_out);
104
105 const int input_w = _input0->info()->dimension(0);
106 const int input_h = _input0->info()->dimension(1);
107 const int input_stride_x = _input0->info()->strides_in_bytes().x();
108 const int weights_stride_x = _input1->info()->strides_in_bytes().x();
109 const int weights_stride_y = _input1->info()->strides_in_bytes().y();
110 const int output_stride_x = _output->info()->strides_in_bytes().x();
111
112 execute_window_loop(window_in, [&](const Coordinates & id)
113 {
114 // Get pointers
115 const uint8_t *const input_ptr = in.ptr();
116 const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y;
117 auto output_ptr = reinterpret_cast<__fp16 *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x);
118
119 float16x8_t row_dot = vdupq_n_f16(0.f);
120 for(int i = 0; i < input_w; i += 8)
121 {
122 const auto input = vld1q_f16(reinterpret_cast<const __fp16 *>(input_ptr + i * input_stride_x));
123 const auto weights = vld1q_f16(reinterpret_cast<const __fp16 *>(weights_ptr + i * weights_stride_x));
124 row_dot = vaddq_f16(row_dot, vmulq_f16(input, weights));
125 }
126
127 auto temp = vadd_f16(vget_high_f16(row_dot), vget_low_f16(row_dot));
128 temp = vpadd_f16(temp, temp);
129 temp = vpadd_f16(temp, temp);
130
131 *output_ptr = vget_lane_f16(temp, 0);
132 },
133 in, in2, out);
134}
135#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
136
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000137template <>
138void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<float, float, float>(const Window &window_in,
139 const Window &window_w,
140 const Window &window_out)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000141{
Michalis Spyroub7b31532017-11-23 12:10:21 +0000142 Iterator in(_input0, window_in);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000143 Iterator in2(_input1, window_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000144 Iterator out(_output, window_out);
145
146 const int input_w = _input0->info()->dimension(0);
147 const int input_h = _input0->info()->dimension(1);
148 const int input_stride_x = _input0->info()->strides_in_bytes().x();
149 const int weights_stride_x = _input1->info()->strides_in_bytes().x();
150 const int weights_stride_y = _input1->info()->strides_in_bytes().y();
151 const int output_stride_x = _output->info()->strides_in_bytes().x();
152
153 execute_window_loop(window_in, [&](const Coordinates & id)
154 {
155 // Get pointers
156 const uint8_t *const input_ptr = in.ptr();
157 const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y;
158 auto output_ptr = reinterpret_cast<float *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x);
159
160 float32x4_t row_dot = vdupq_n_f32(0.f);
161 for(int i = 0; i < input_w; i += 4)
162 {
163 const auto input = vld1q_f32(reinterpret_cast<const float *>(input_ptr + i * input_stride_x));
164 const auto weights = vld1q_f32(reinterpret_cast<const float *>(weights_ptr + i * weights_stride_x));
165 row_dot = vaddq_f32(row_dot, vmulq_f32(input, weights));
166 }
167
168 auto temp = vadd_f32(vget_high_f32(row_dot), vget_low_f32(row_dot));
169 temp = vpadd_f32(temp, temp);
170
171 *output_ptr = vget_lane_f32(temp, 0);
172 },
173 in, in2, out);
174}
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000175
176template <>
177void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<uint8_t, uint8_t, int32_t>(const Window &window_in,
178 const Window &window_w,
179 const Window &window_out)
180{
181 Iterator in(_input0, window_in);
182 Iterator in2(_input1, window_w);
183 Iterator out(_output, window_out);
184
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100185 const int input_offset = -_input0->info()->quantization_info().uniform().offset;
186 const int weights_offset = -_input1->info()->quantization_info().uniform().offset;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000187
188 const int input_w = _input0->info()->dimension(0);
189 const int input_h = _input0->info()->dimension(1);
190 const int input_stride_x = _input0->info()->strides_in_bytes().x();
191 const int weights_stride_x = _input1->info()->strides_in_bytes().x();
192 const int weights_stride_y = _input1->info()->strides_in_bytes().y();
193 const int output_stride_x = _output->info()->strides_in_bytes().x();
194 const int read_step = 16 / _input0->info()->element_size();
195
196 const int32x4_t v_input_offset = vdupq_n_s32(input_offset);
197 const int32x4_t v_weights_offset = vdupq_n_s32(weights_offset);
198
199 execute_window_loop(window_in, [&](const Coordinates & id)
200 {
201 // Get pointers
202 const uint8_t *const input_ptr = in.ptr();
203 const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y;
204 auto output_ptr = reinterpret_cast<int32_t *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x);
205
206 int32x4_t row_dot = vdupq_n_s32(0);
207 for(int i = 0; i < input_w; i += read_step)
208 {
209 // Read values
210 const auto input = vld1q_u8(reinterpret_cast<const uint8_t *>(input_ptr + i * input_stride_x));
211 const auto weights = vld1q_u8(reinterpret_cast<const uint8_t *>(weights_ptr + i * weights_stride_x));
212
213 // Add offsets
214 const int32x4x4_t input_s32 =
215 {
216 {
217 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_low_u8(input))))),
218 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_low_u8(input))))),
219 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_high_u8(input))))),
220 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_high_u8(input)))))
221 }
222 };
223 const int32x4x4_t weights_s32 =
224 {
225 {
226 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_low_u8(weights))))),
227 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_low_u8(weights))))),
228 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_high_u8(weights))))),
229 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_high_u8(weights)))))
230 }
231 };
232
233 // Dot
234 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[0], weights_s32.val[0]));
235 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[1], weights_s32.val[1]));
236 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[2], weights_s32.val[2]));
237 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[3], weights_s32.val[3]));
238 }
239
240 // Reduction
241 auto temp = vadd_s32(vget_high_s32(row_dot), vget_low_s32(row_dot));
242 temp = vpadd_s32(temp, temp);
243
244 *output_ptr = vget_lane_s32(temp, 0);
245 },
246 in, in2, out);
247}
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +0000248
249template <>
250void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<int8_t, int8_t, int32_t>(const Window &window_in,
251 const Window &window_w,
252 const Window &window_out)
253{
254 Iterator in(_input0, window_in);
255 Iterator in2(_input1, window_w);
256 Iterator out(_output, window_out);
257
258 const int input_offset = -_input0->info()->quantization_info().uniform().offset;
259 const int weights_offset = -_input1->info()->quantization_info().uniform().offset;
260
261 const int input_w = _input0->info()->dimension(0);
262 const int input_h = _input0->info()->dimension(1);
263 const int input_stride_x = _input0->info()->strides_in_bytes().x();
264 const int weights_stride_x = _input1->info()->strides_in_bytes().x();
265 const int weights_stride_y = _input1->info()->strides_in_bytes().y();
266 const int output_stride_x = _output->info()->strides_in_bytes().x();
267 const int read_step = 16 / _input0->info()->element_size();
268
269 const int32x4_t v_input_offset = vdupq_n_s32(input_offset);
270 const int32x4_t v_weights_offset = vdupq_n_s32(weights_offset);
271
272 execute_window_loop(window_in, [&](const Coordinates & id)
273 {
274 // Get pointers
275 const uint8_t *const input_ptr = in.ptr();
276 const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y;
277 auto output_ptr = reinterpret_cast<int32_t *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x);
278
279 int32x4_t row_dot = vdupq_n_s32(0);
280 for(int i = 0; i < input_w; i += read_step)
281 {
282 // Read values
283 const auto input = vld1q_s8(reinterpret_cast<const int8_t *>(input_ptr + i * input_stride_x));
284 const auto weights = vld1q_s8(reinterpret_cast<const int8_t *>(weights_ptr + i * weights_stride_x));
285
286 // Add offsets
287 const int32x4x4_t input_s32 =
288 {
289 {
290 vaddw_s16(v_input_offset, vget_low_s16(vmovl_s8(vget_low_s8(input)))),
291 vaddw_s16(v_input_offset, vget_high_s16(vmovl_s8(vget_low_s8(input)))),
292 vaddw_s16(v_input_offset, vget_low_s16(vmovl_s8(vget_high_s8(input)))),
293 vaddw_s16(v_input_offset, vget_high_s16(vmovl_s8(vget_high_s8(input))))
294 }
295 };
296 const int32x4x4_t weights_s32 =
297 {
298 {
299 vaddw_s16(v_weights_offset, vget_low_s16(vmovl_s8(vget_low_s8(weights)))),
300 vaddw_s16(v_weights_offset, vget_high_s16(vmovl_s8(vget_low_s8(weights)))),
301 vaddw_s16(v_weights_offset, vget_low_s16(vmovl_s8(vget_high_s8(weights)))),
302 vaddw_s16(v_weights_offset, vget_high_s16(vmovl_s8(vget_high_s8(weights))))
303 }
304 };
305
306 // Dot
307 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[0], weights_s32.val[0]));
308 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[1], weights_s32.val[1]));
309 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[2], weights_s32.val[2]));
310 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[3], weights_s32.val[3]));
311 }
312
313 // Reduction
314 auto temp = vadd_s32(vget_high_s32(row_dot), vget_low_s32(row_dot));
315 temp = vpadd_s32(temp, temp);
316
317 *output_ptr = vget_lane_s32(temp, 0);
318 },
319 in, in2, out);
320}
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000321
322NEGEMMMatrixVectorMultiplyKernel::NEGEMMMatrixVectorMultiplyKernel()
323 : _func(nullptr), _input0(nullptr), _input1(nullptr), _output(nullptr), _border_size(0)
324{
325}
326
327BorderSize NEGEMMMatrixVectorMultiplyKernel::border_size() const
328{
329 return _border_size;
330}
331
332void NEGEMMMatrixVectorMultiplyKernel::configure(const ITensor *input0, const ITensor *input1, ITensor *output)
333{
Abe Mbise7784c832018-05-31 16:48:41 +0100334 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Abe Mbise7784c832018-05-31 16:48:41 +0100335 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info()));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000336
337 _input0 = input0;
338 _input1 = input1;
339 _output = output;
340
341 // Set appropriate function to run
342 switch(input0->info()->data_type())
343 {
344 case DataType::QASYMM8:
345 _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<uint8_t, uint8_t, int32_t>;
346 break;
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +0000347 case DataType::QASYMM8_SIGNED:
348 _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<int8_t, int8_t, int32_t>;
349 break;
Georgios Pinitas8cffcd62018-11-16 17:11:50 +0000350#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
351 case DataType::F16:
352 _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<half, half, half>;
353 break;
354#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000355 case DataType::F32:
356 _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<float, float, float>;
357 break;
358 default:
359 ARM_COMPUTE_ERROR("Unsupported data type");
360 }
361
362 // Configure kernel window
363 const unsigned int num_elems_read_per_iteration = 16 / _input0->info()->element_size();
364
365 const unsigned int border_x = ceil_to_multiple(input0->info()->dimension(0), num_elems_read_per_iteration) - input0->info()->dimension(0);
366 _border_size = BorderSize(0, border_x);
367
Abe Mbise7784c832018-05-31 16:48:41 +0100368 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info());
369 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
370 INEKernel::configure(win_config.second);
371}
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000372
Abe Mbise7784c832018-05-31 16:48:41 +0100373Status NEGEMMMatrixVectorMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output)
374{
375 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
376 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output));
377 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), input1->clone().get(), output->clone().get()).first);
378 return Status{};
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000379}
380
381void NEGEMMMatrixVectorMultiplyKernel::run(const Window &window, const ThreadInfo &info)
382{
383 ARM_COMPUTE_UNUSED(info);
384 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
385 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +0000386 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000387
388 Window window_slice = window.first_slice_window_3D();
389
390 Window window_in(window);
391 Window window_weights(window_slice);
392 Window window_out(window);
393
394 // Setup input0 slice
395 window_in.set(Window::DimX, Window::Dimension(0, _input0->info()->dimension(0), _input0->info()->dimension(0)));
396 window_in.set(Window::DimY, Window::Dimension(0, _input0->info()->dimension(1), 1));
397 window_in.set(Window::DimZ, Window::Dimension(0, _input0->info()->dimension(2), 1));
398
399 // Setup input1 and output slice. Their dimensions are increased in the kernel.
400 window_weights.set(Window::DimX, Window::Dimension(0, 0, 0));
401 window_weights.set(Window::DimY, Window::Dimension(0, 0, 0));
402 window_weights.set(Window::DimZ, Window::Dimension(0, 0, 0));
403
404 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
405 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
406 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
407
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +0000408 (this->*_func)(window_in, window_weights, window_out);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000409}
Michele Di Giorgiodeb3ac42019-12-20 10:02:17 +0000410} // namespace arm_compute