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