blob: 238786953bff1fe1f20df86221a57f341d548737 [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{
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::S32, DataType::F32);
48 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));
50 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_float(input0->data_type()) && (output->data_type() != DataType::F32));
51
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 Pinitasd05dce42018-01-22 16:29:17 +000090template <>
91void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<float, float, float>(const Window &window_in,
92 const Window &window_w,
93 const Window &window_out)
Michalis Spyroub7b31532017-11-23 12:10:21 +000094{
Michalis Spyroub7b31532017-11-23 12:10:21 +000095 Iterator in(_input0, window_in);
Georgios Pinitasd05dce42018-01-22 16:29:17 +000096 Iterator in2(_input1, window_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +000097 Iterator out(_output, window_out);
98
99 const int input_w = _input0->info()->dimension(0);
100 const int input_h = _input0->info()->dimension(1);
101 const int input_stride_x = _input0->info()->strides_in_bytes().x();
102 const int weights_stride_x = _input1->info()->strides_in_bytes().x();
103 const int weights_stride_y = _input1->info()->strides_in_bytes().y();
104 const int output_stride_x = _output->info()->strides_in_bytes().x();
105
106 execute_window_loop(window_in, [&](const Coordinates & id)
107 {
108 // Get pointers
109 const uint8_t *const input_ptr = in.ptr();
110 const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y;
111 auto output_ptr = reinterpret_cast<float *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x);
112
113 float32x4_t row_dot = vdupq_n_f32(0.f);
114 for(int i = 0; i < input_w; i += 4)
115 {
116 const auto input = vld1q_f32(reinterpret_cast<const float *>(input_ptr + i * input_stride_x));
117 const auto weights = vld1q_f32(reinterpret_cast<const float *>(weights_ptr + i * weights_stride_x));
118 row_dot = vaddq_f32(row_dot, vmulq_f32(input, weights));
119 }
120
121 auto temp = vadd_f32(vget_high_f32(row_dot), vget_low_f32(row_dot));
122 temp = vpadd_f32(temp, temp);
123
124 *output_ptr = vget_lane_f32(temp, 0);
125 },
126 in, in2, out);
127}
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000128
129template <>
130void NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<uint8_t, uint8_t, int32_t>(const Window &window_in,
131 const Window &window_w,
132 const Window &window_out)
133{
134 Iterator in(_input0, window_in);
135 Iterator in2(_input1, window_w);
136 Iterator out(_output, window_out);
137
138 const int input_offset = -_input0->info()->quantization_info().offset;
139 const int weights_offset = -_input1->info()->quantization_info().offset;
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 const int read_step = 16 / _input0->info()->element_size();
148
149 const int32x4_t v_input_offset = vdupq_n_s32(input_offset);
150 const int32x4_t v_weights_offset = vdupq_n_s32(weights_offset);
151
152 execute_window_loop(window_in, [&](const Coordinates & id)
153 {
154 // Get pointers
155 const uint8_t *const input_ptr = in.ptr();
156 const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y;
157 auto output_ptr = reinterpret_cast<int32_t *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x);
158
159 int32x4_t row_dot = vdupq_n_s32(0);
160 for(int i = 0; i < input_w; i += read_step)
161 {
162 // Read values
163 const auto input = vld1q_u8(reinterpret_cast<const uint8_t *>(input_ptr + i * input_stride_x));
164 const auto weights = vld1q_u8(reinterpret_cast<const uint8_t *>(weights_ptr + i * weights_stride_x));
165
166 // Add offsets
167 const int32x4x4_t input_s32 =
168 {
169 {
170 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_low_u8(input))))),
171 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_low_u8(input))))),
172 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_high_u8(input))))),
173 vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_high_u8(input)))))
174 }
175 };
176 const int32x4x4_t weights_s32 =
177 {
178 {
179 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_low_u8(weights))))),
180 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_low_u8(weights))))),
181 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vget_high_u8(weights))))),
182 vaddw_s16(v_weights_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vget_high_u8(weights)))))
183 }
184 };
185
186 // Dot
187 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[0], weights_s32.val[0]));
188 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[1], weights_s32.val[1]));
189 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[2], weights_s32.val[2]));
190 row_dot = vaddq_s32(row_dot, vmulq_s32(input_s32.val[3], weights_s32.val[3]));
191 }
192
193 // Reduction
194 auto temp = vadd_s32(vget_high_s32(row_dot), vget_low_s32(row_dot));
195 temp = vpadd_s32(temp, temp);
196
197 *output_ptr = vget_lane_s32(temp, 0);
198 },
199 in, in2, out);
200}
201} //namespace arm_compute
202
203NEGEMMMatrixVectorMultiplyKernel::NEGEMMMatrixVectorMultiplyKernel()
204 : _func(nullptr), _input0(nullptr), _input1(nullptr), _output(nullptr), _border_size(0)
205{
206}
207
208BorderSize NEGEMMMatrixVectorMultiplyKernel::border_size() const
209{
210 return _border_size;
211}
212
213void NEGEMMMatrixVectorMultiplyKernel::configure(const ITensor *input0, const ITensor *input1, ITensor *output)
214{
Abe Mbise7784c832018-05-31 16:48:41 +0100215 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
216
217 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info()));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000218
219 _input0 = input0;
220 _input1 = input1;
221 _output = output;
222
223 // Set appropriate function to run
224 switch(input0->info()->data_type())
225 {
226 case DataType::QASYMM8:
227 _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<uint8_t, uint8_t, int32_t>;
228 break;
229 case DataType::F32:
230 _func = &NEGEMMMatrixVectorMultiplyKernel::matrix_vector_multiply<float, float, float>;
231 break;
232 default:
233 ARM_COMPUTE_ERROR("Unsupported data type");
234 }
235
236 // Configure kernel window
237 const unsigned int num_elems_read_per_iteration = 16 / _input0->info()->element_size();
238
239 const unsigned int border_x = ceil_to_multiple(input0->info()->dimension(0), num_elems_read_per_iteration) - input0->info()->dimension(0);
240 _border_size = BorderSize(0, border_x);
241
Abe Mbise7784c832018-05-31 16:48:41 +0100242 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info());
243 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
244 INEKernel::configure(win_config.second);
245}
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000246
Abe Mbise7784c832018-05-31 16:48:41 +0100247Status NEGEMMMatrixVectorMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output)
248{
249 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
250 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output));
251 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), input1->clone().get(), output->clone().get()).first);
252 return Status{};
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000253}
254
255void NEGEMMMatrixVectorMultiplyKernel::run(const Window &window, const ThreadInfo &info)
256{
257 ARM_COMPUTE_UNUSED(info);
258 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
259 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
260
261 Window window_slice = window.first_slice_window_3D();
262
263 Window window_in(window);
264 Window window_weights(window_slice);
265 Window window_out(window);
266
267 // Setup input0 slice
268 window_in.set(Window::DimX, Window::Dimension(0, _input0->info()->dimension(0), _input0->info()->dimension(0)));
269 window_in.set(Window::DimY, Window::Dimension(0, _input0->info()->dimension(1), 1));
270 window_in.set(Window::DimZ, Window::Dimension(0, _input0->info()->dimension(2), 1));
271
272 // Setup input1 and output slice. Their dimensions are increased in the kernel.
273 window_weights.set(Window::DimX, Window::Dimension(0, 0, 0));
274 window_weights.set(Window::DimY, Window::Dimension(0, 0, 0));
275 window_weights.set(Window::DimZ, Window::Dimension(0, 0, 0));
276
277 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
278 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
279 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
280
281 if(_func != nullptr)
282 {
283 (this->*_func)(window_in, window_weights, window_out);
284 }
285}