blob: 1acdb1efcee7eb891f9e3a260d6e86417e51d6dd [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
Michele Di Giorgio13ec5f02020-01-02 12:11:13 +00002 * Copyright (c) 2017-2020 ARM Limited.
Gian Marco Iodiceab182122017-10-09 15:05:40 +01003 *
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/NEGEMMLowpReductionKernel.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"
Michele Di Giorgioa602f032020-03-12 19:34:33 +000030#include "arm_compute/core/KernelDescriptors.h"
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010031#include "arm_compute/core/NEON/wrapper/wrapper.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
Gian Marco Iodiceab182122017-10-09 15:05:40 +010038#include <cstddef>
39#include <cstdint>
40
Gian Marco Iodiceab182122017-10-09 15:05:40 +010041namespace arm_compute
42{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000043namespace
44{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000045Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000046{
Michele Di Giorgioa602f032020-03-12 19:34:33 +000047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgio47a89902020-03-09 19:32:33 +000048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000049
Michele Di Giorgioa602f032020-03-12 19:34:33 +000050 if(output->total_size() > 0)
51 {
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(0) != input->dimension(1), "Output vector must have length equal to the number of rows of the input matrix");
54 }
Georgios Pinitas631c41a2017-12-06 11:53:03 +000055 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000056}
Georgios Pinitas631c41a2017-12-06 11:53:03 +000057std::pair<Status, Window> validate_and_configure_window_matrix_a_reduction(ITensorInfo *input, ITensorInfo *output, bool is_reshaped)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000058{
59 const unsigned int num_elems_processed_per_iteration = is_reshaped ? 4 : 1;
60
Michele Di Giorgioa602f032020-03-12 19:34:33 +000061 // Output auto initialization if not yet initialized
62 auto_init_if_empty(*output, TensorShape(input->dimension(1)), 1, DataType::S32);
63
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000064 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
65
66 AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), 16), input->dimension(1));
67 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
68
69 bool window_changed = update_window_and_padding(win, input_access, output_access);
70
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000071 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000072
Georgios Pinitas631c41a2017-12-06 11:53:03 +000073 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000074 return std::make_pair(err, win);
75}
76
Georgios Pinitas631c41a2017-12-06 11:53:03 +000077Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000078{
Michele Di Giorgioa602f032020-03-12 19:34:33 +000079 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgio47a89902020-03-09 19:32:33 +000080 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000081
Michele Di Giorgioa602f032020-03-12 19:34:33 +000082 if(output->total_size() > 0)
83 {
84 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
85 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(0) != input->dimension(0), "Output vector must have length equal to the number of columns of the input matrix");
86 }
Georgios Pinitas631c41a2017-12-06 11:53:03 +000087 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000088}
89
Georgios Pinitas631c41a2017-12-06 11:53:03 +000090std::pair<Status, Window> validate_and_configure_window_matrix_b_reduction(ITensorInfo *input, ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000091{
92 constexpr unsigned int num_elems_processed_per_iteration = 16;
93
Michele Di Giorgioa602f032020-03-12 19:34:33 +000094 // Output auto initialization if not yet initialized
95 auto_init_if_empty(*output, TensorShape(input->dimension(0)), 1, DataType::S32);
96
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000097 // Configure kernel window
98 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
99
100 AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), 16), input->dimension(1));
101 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
102
103 bool window_changed = update_window_and_padding(win, input_access, output_access);
104
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000105 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000106
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000107 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000108 return std::make_pair(err, win);
109}
110} // namespace
111
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100112INEGEMMLowpReductionKernel::INEGEMMLowpReductionKernel()
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000113 : _input(), _output(), _k(0), _is_reshaped(false), _scalar(0), _mul_by_scalar(false)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100114{
115}
116
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000117void NEGEMMLowpMatrixAReductionKernel::configure(const ITensor *mtx_a, ITensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100118{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000119 // Perform validate step
120 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row);
121 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(mtx_a->info(), vector_sum_row->info()));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100122
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000123 _input = mtx_a;
124 _output = vector_sum_row;
125 _k = info.k;
126 _is_reshaped = info.is_reshaped;
127 _scalar = info.scalar;
128 _mul_by_scalar = info.mul_by_scalar;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100129
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100130 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000131 auto win_config = validate_and_configure_window_matrix_a_reduction(_input->info(), _output->info(), _is_reshaped);
132 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
133 INEKernel::configure(win_config.second);
134}
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100135
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000136Status NEGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row, const GEMMLowpReductionKernelInfo &info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000137{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000138 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row));
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000139 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_a_reduction(mtx_a->clone().get(), vector_sum_row->clone().get(), info.is_reshaped).first);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100140
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000141 return Status{};
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100142}
143
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100144template <typename T>
145void NEGEMMLowpMatrixAReductionKernel::run_internal(const arm_compute::Window &window)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100146{
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100147 // Intermediate and final accumulator types
148 using TIAcc = wrapper::traits::promote_t<T>;
149 using TAcc = wrapper::traits::promote_t<TIAcc>;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100150
151 Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimY);
152
153 Window win_input(collapsed_window);
154 win_input.set(Window::DimX, Window::Dimension(0, 0, 0));
155 win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
156 win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));
157
158 Iterator in(_input, win_input);
159 Iterator out(_output, collapsed_window);
160
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000161 const auto vec_scalar = wrapper::vdup_n(static_cast<TAcc>(_scalar), wrapper::traits::vector_128_tag{});
162
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100163 if(_is_reshaped)
164 {
165 execute_window_loop(collapsed_window, [&](const Coordinates & id)
166 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100167 auto sum_row = wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{});
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100168
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100169 const T *matrix_a = reinterpret_cast<const T *>((in.ptr() + (id.x() / 4) * _input->info()->strides_in_bytes()[1] + id.y() * _input->info()->strides_in_bytes()[2]));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100170
171#if __arm__
172 asm volatile("PLD [%0, #128*4]" ::"r"(matrix_a));
173#endif /* __arm__ */
174
175 int i = 0;
176 // This for loop performs 4 accumulations
177 for(; i <= (_k - 4); i += 4)
178 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100179 const auto a0_d8 = wrapper::vloadq(matrix_a + i * 4);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100180
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100181 // Convert 8-bit to 16-bit
182 typename wrapper::traits::neon_bitvector<TIAcc, wrapper::traits::BitWidth::W64>::type a0_d16[4] =
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100183 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100184 wrapper::vgetlow(wrapper::vmovl(wrapper::vgetlow(a0_d8))),
185 wrapper::vgethigh(wrapper::vmovl(wrapper::vgetlow(a0_d8))),
186 wrapper::vgetlow(wrapper::vmovl((wrapper::vgethigh(a0_d8)))),
187 wrapper::vgethigh(wrapper::vmovl(wrapper::vgethigh(a0_d8)))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100188 };
189
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100190 // Accumulate to 16-bit
191 a0_d16[0] = wrapper::vadd(a0_d16[0], a0_d16[1]);
192 a0_d16[0] = wrapper::vadd(a0_d16[0], a0_d16[2]);
193 a0_d16[0] = wrapper::vadd(a0_d16[0], a0_d16[3]);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100194
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100195 // Accumulate to 32-bit
196 sum_row = wrapper::vaddw(sum_row, a0_d16[0]);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100197 }
198
199 // This for loop performs the leftover accumulations
200 for(; i < _k; ++i)
201 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100202 const auto a0_d8 = wrapper::vload(matrix_a + i * 4);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100203
204 // Convert U8 to U16
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100205 const auto a0_d16 = wrapper::vgetlow(wrapper::vmovl(a0_d8));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100206
207 // Accumulate to U32
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100208 sum_row = wrapper::vaddw(sum_row, a0_d16);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100209 }
210
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000211 // Multiply by scalar if necessary
212 if(_mul_by_scalar)
213 {
214 sum_row = wrapper::vmul(sum_row, vec_scalar);
215 }
216
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100217 auto vector_sum_row = reinterpret_cast<int32_t *>(out.ptr());
218
Michele Di Giorgio13ec5f02020-01-02 12:11:13 +0000219 wrapper::vstore(vector_sum_row, wrapper::vreinterpret(sum_row));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100220 },
221 in, out);
222 }
223 else // it is not reshaped
224 {
225 execute_window_loop(collapsed_window, [&](const Coordinates & id)
226 {
227 // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100228 auto vsum_row = wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{});
229 TAcc sum_row = 0;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100230
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100231 const T *matrix_a = reinterpret_cast<const T *>((in.ptr() + id.x() * _input->info()->strides_in_bytes()[1] + id.y() * _input->info()->strides_in_bytes()[2]));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100232
233#if __arm__
234 asm volatile("PLD [%0, #128*4]" ::"r"(matrix_a));
235#endif /* __arm__ */
236
237 int i = 0;
238 // This for loop performs 16 accumulations
239 for(; i <= (_k - 16); i += 16)
240 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100241 const auto a0_d8 = wrapper::vloadq(matrix_a + i);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100242
243 // Partial accumulations in U16
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100244 const auto tmp_sum0 = wrapper::vaddl(wrapper::vgetlow(a0_d8), wrapper::vgethigh(a0_d8));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100245
246 // Accumulate to U32
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100247 vsum_row = wrapper::vadd(vsum_row, wrapper::vpaddl(tmp_sum0));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100248 }
249
250 // This for loop performs the leftover accumulations
251 for(; i < _k; ++i)
252 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100253 sum_row += static_cast<TAcc>(matrix_a[i]);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100254 }
255
256#if defined(__aarch64__)
257 // Reduction operation available on 64 bit architectures only
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100258 sum_row += wrapper::vaddv(vsum_row);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100259#else // __aarch64__
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100260 auto tmp = wrapper::vpadd(wrapper::vgethigh(vsum_row), wrapper::vgetlow(vsum_row));
261 tmp = wrapper::vpadd(tmp, tmp);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100262
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100263 sum_row += wrapper::vgetlane(tmp, 0);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100264#endif // __aarch64__
265
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000266 // Multiply by scalar if necessary
267 if(_mul_by_scalar)
268 {
269 sum_row *= _scalar;
270 }
271
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100272 *(reinterpret_cast<int *>(out.ptr())) = static_cast<int32_t>(sum_row);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100273 },
274 in, out);
275 }
276}
277
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100278void NEGEMMLowpMatrixAReductionKernel::run(const Window &window, const ThreadInfo &info)
279{
280 ARM_COMPUTE_UNUSED(info);
281 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
282 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
283
284 switch(_input->info()->data_type())
285 {
286 case DataType::QASYMM8:
287 run_internal<uint8_t>(window);
288 break;
289 case DataType::QASYMM8_SIGNED:
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000290 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100291 case DataType::QSYMM8_PER_CHANNEL:
292 run_internal<int8_t>(window);
293 break;
294 default:
295 ARM_COMPUTE_ERROR("Unsupported data type");
296 }
297}
298
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000299void NEGEMMLowpMatrixBReductionKernel::configure(const ITensor *mtx_b, ITensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100300{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000301 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col);
302 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(mtx_b->info(), vector_sum_col->info()));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100303
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000304 _input = mtx_b;
305 _output = vector_sum_col;
306 _k = info.k;
307 _is_reshaped = info.is_reshaped;
308 _scalar = info.scalar;
309 _mul_by_scalar = info.mul_by_scalar;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100310
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100311 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000312 auto win_config = validate_and_configure_window_matrix_b_reduction(_input->info(), _output->info());
313 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
314 INEKernel::configure(win_config.second);
315}
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100316
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000317Status NEGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col, const GEMMLowpReductionKernelInfo &info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000318{
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000319 ARM_COMPUTE_UNUSED(info);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000320 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col));
321 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_b_reduction(mtx_b->clone().get(), vector_sum_col->clone().get()).first);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100322
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000323 return Status{};
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100324}
325
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100326template <typename T>
327void NEGEMMLowpMatrixBReductionKernel::run_internal(const Window &window, const ThreadInfo &info)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100328{
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100329 // Intermediate and final accumulator types
330 using TIAcc = wrapper::traits::promote_t<T>;
331 using TAcc = wrapper::traits::promote_t<TIAcc>;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100332
333 Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimY);
334
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000335 const auto vec_scalar = wrapper::vdup_n(static_cast<TAcc>(_scalar), wrapper::traits::vector_128_tag{});
336
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100337 if(_is_reshaped)
338 {
339 Window win_input(collapsed_window);
340 win_input.set(Window::DimX, Window::Dimension(0, 0, 0));
341 win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
342 win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));
343
344 Iterator in(_input, win_input);
345 Iterator out(_output, collapsed_window);
346
347 execute_window_loop(collapsed_window, [&](const Coordinates & id)
348 {
349 // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100350 typename wrapper::traits::neon_bitvector<TAcc, wrapper::traits::BitWidth::W128>::type sum_col[4] =
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100351 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100352 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}),
353 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}),
354 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}),
355 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{})
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100356 };
357
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100358 const auto *matrix_b = reinterpret_cast<const T *>(in.ptr() + (id.x() / 16) * _input->info()->strides_in_bytes()[1] + id.y() * _input->info()->strides_in_bytes()[2]);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100359
360#if __arm__
361 asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b));
362#endif /* __arm__ */
363
364 int i = 0;
365 for(; i < _k; ++i)
366 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100367 const auto b0_b8 = wrapper::vloadq(matrix_b + i * 16);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100368
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100369 // Convert 8bit to 16bit
370 const typename wrapper::traits::neon_bitvector<TIAcc, wrapper::traits::BitWidth::W128>::type b0_b16[2] =
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100371 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100372 wrapper::vmovl(wrapper::vgetlow(b0_b8)),
373 wrapper::vmovl(wrapper::vgethigh(b0_b8))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100374 };
375
376 // Accumulate to U32
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100377 sum_col[0] = wrapper::vaddw(sum_col[0], wrapper::vgetlow(b0_b16[0]));
378 sum_col[1] = wrapper::vaddw(sum_col[1], wrapper::vgethigh(b0_b16[0]));
379 sum_col[2] = wrapper::vaddw(sum_col[2], wrapper::vgetlow(b0_b16[1]));
380 sum_col[3] = wrapper::vaddw(sum_col[3], wrapper::vgethigh(b0_b16[1]));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100381 }
382
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000383 // Multiply by scalar if necessary
384 if(_mul_by_scalar)
385 {
386 sum_col[0] = wrapper::vmul(sum_col[0], vec_scalar);
387 sum_col[1] = wrapper::vmul(sum_col[1], vec_scalar);
388 sum_col[2] = wrapper::vmul(sum_col[2], vec_scalar);
389 sum_col[3] = wrapper::vmul(sum_col[3], vec_scalar);
390 }
391
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100392 auto vector_sum_col = reinterpret_cast<int32_t *>(out.ptr());
393
Michele Di Giorgio13ec5f02020-01-02 12:11:13 +0000394 wrapper::vstore(vector_sum_col + 0, wrapper::vreinterpret(sum_col[0]));
395 wrapper::vstore(vector_sum_col + 4, wrapper::vreinterpret(sum_col[1]));
396 wrapper::vstore(vector_sum_col + 8, wrapper::vreinterpret(sum_col[2]));
397 wrapper::vstore(vector_sum_col + 12, wrapper::vreinterpret(sum_col[3]));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100398 },
399 in, out);
400 }
401 else // it is not reshaped
402 {
403 const auto width_matrix_b = static_cast<int>(_input->info()->dimension(0));
404 const auto in_b_stride = static_cast<int>(_input->info()->strides_in_bytes()[1]);
405
406 // The implementation computes 16 elements per iteration
407 const int window_start_x = 16 * info.thread_id;
408 const int window_step_x = 16 * info.num_threads;
409 // Make sure (window_end_x - window_start_x) is a multiple of window_step_x
410 const int window_end_x = ceil_to_multiple(width_matrix_b - window_start_x, window_step_x) + window_start_x;
411
412 Window win_out(collapsed_window);
413 win_out.set(Window::DimX, Window::Dimension(window_start_x, window_end_x, window_step_x));
414
415 Window win_in(win_out);
416 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
417 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
418
419 Iterator inb(_input, win_in);
420 Iterator out(_output, win_out);
421
422 execute_window_loop(win_out, [&](const Coordinates & id)
423 {
424 if(id.x() > width_matrix_b)
425 {
426 return;
427 }
428
429 // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100430 typename wrapper::traits::neon_bitvector<TAcc, wrapper::traits::BitWidth::W128>::type sum_col[4] =
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100431 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100432 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}),
433 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}),
434 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}),
435 wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{})
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100436 };
437
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100438 const auto *matrix_b = reinterpret_cast<const T *>(inb.ptr() + id.y() * _input->info()->strides_in_bytes()[2]);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100439
440#if __arm__
441 asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b));
442 asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b + in_b_stride));
443#endif /* __arm__ */
444
445 int i = 0;
446 // This for loop performs 4 accumulations
447 for(; i <= (_k - 4); i += 4)
448 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100449 const auto b0_u8 = wrapper::vloadq(matrix_b + 0 * in_b_stride);
450 const auto b1_u8 = wrapper::vloadq(matrix_b + 1 * in_b_stride);
451 const auto b2_u8 = wrapper::vloadq(matrix_b + 2 * in_b_stride);
452 const auto b3_u8 = wrapper::vloadq(matrix_b + 3 * in_b_stride);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100453
454#if __arm__
455 asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 1 * in_b_stride));
456 asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 2 * in_b_stride));
457 asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 3 * in_b_stride));
458 asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 4 * in_b_stride));
459#endif /* __arm__ */
460
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100461 // Partial accumulation in 16bit
462 typename wrapper::traits::neon_bitvector<TIAcc, wrapper::traits::BitWidth::W128>::type tmp_sum[2] =
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100463 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100464 wrapper::vdup_n(static_cast<TIAcc>(0), wrapper::traits::vector_128_tag{}),
465 wrapper::vdup_n(static_cast<TIAcc>(0), wrapper::traits::vector_128_tag{})
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100466 };
467
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100468 tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b1_u8));
469 tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b0_u8));
470 tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b2_u8));
471 tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b3_u8));
472 tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b0_u8));
473 tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b1_u8));
474 tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b2_u8));
475 tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b3_u8));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100476
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100477 // Accumulate to 32bit
478 sum_col[0] = wrapper::vaddw(sum_col[0], wrapper::vgetlow(tmp_sum[0]));
479 sum_col[1] = wrapper::vaddw(sum_col[1], wrapper::vgethigh(tmp_sum[0]));
480 sum_col[2] = wrapper::vaddw(sum_col[2], wrapper::vgetlow(tmp_sum[1]));
481 sum_col[3] = wrapper::vaddw(sum_col[3], wrapper::vgethigh(tmp_sum[1]));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100482
483 matrix_b += 4 * in_b_stride;
484 }
485
486 // This for loop perfoms the leftover accumulations
487 for(; i < _k; ++i)
488 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100489 const auto b0_b8 = wrapper::vloadq(matrix_b + 0 * in_b_stride);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100490
Pablo Tello6ff12a02017-11-02 16:09:35 +0000491 // Convert S8 to S16
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100492 const typename wrapper::traits::neon_bitvector<TIAcc, wrapper::traits::BitWidth::W128>::type b0_b16[2]
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100493 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100494 wrapper::vmovl(wrapper::vgetlow(b0_b8)),
495 wrapper::vmovl(wrapper::vgethigh(b0_b8))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100496 };
497
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100498 // Accumulate to 32bit
499 sum_col[0] = wrapper::vaddw(sum_col[0], wrapper::vgetlow(b0_b16[0]));
500 sum_col[1] = wrapper::vaddw(sum_col[1], wrapper::vgethigh(b0_b16[0]));
501 sum_col[2] = wrapper::vaddw(sum_col[2], wrapper::vgetlow(b0_b16[1]));
502 sum_col[3] = wrapper::vaddw(sum_col[3], wrapper::vgethigh(b0_b16[1]));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100503
504 matrix_b += in_b_stride;
505 }
506
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000507 // Multiply by scalar if necessary
508 if(_mul_by_scalar)
509 {
510 sum_col[0] = wrapper::vmul(sum_col[0], vec_scalar);
511 sum_col[1] = wrapper::vmul(sum_col[1], vec_scalar);
512 sum_col[2] = wrapper::vmul(sum_col[2], vec_scalar);
513 sum_col[3] = wrapper::vmul(sum_col[3], vec_scalar);
514 }
515
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100516 auto vector_sum_col = reinterpret_cast<int32_t *>(out.ptr());
517
Michele Di Giorgio13ec5f02020-01-02 12:11:13 +0000518 wrapper::vstore(vector_sum_col + 0, wrapper::vreinterpret(sum_col[0]));
519 wrapper::vstore(vector_sum_col + 4, wrapper::vreinterpret(sum_col[1]));
520 wrapper::vstore(vector_sum_col + 8, wrapper::vreinterpret(sum_col[2]));
521 wrapper::vstore(vector_sum_col + 12, wrapper::vreinterpret(sum_col[3]));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100522 },
523 inb, out);
524 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000525}
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100526
527void NEGEMMLowpMatrixBReductionKernel::run(const Window &window, const ThreadInfo &info)
528{
529 ARM_COMPUTE_UNUSED(info);
530 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
531 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
532
533 switch(_input->info()->data_type())
534 {
535 case DataType::QASYMM8:
536 run_internal<uint8_t>(window, info);
537 break;
538 case DataType::QASYMM8_SIGNED:
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000539 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100540 case DataType::QSYMM8_PER_CHANNEL:
541 run_internal<int8_t>(window, info);
542 break;
543 default:
544 ARM_COMPUTE_ERROR("Unsupported data type");
545 }
546}
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000547} // namespace arm_compute