blob: 2c6515c1df7d89241edbf12b66d4069c63a0e279 [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Pablo Tello6ff12a02017-11-02 16:09:35 +000029#include "arm_compute/core/NEON/kernels/NEGEMMAssemblyBaseKernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010030#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010031#include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
32#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
33#include "arm_compute/core/NEON/kernels/arm64/NEGEMMLowpAArch64V8P4Kernel.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/runtime/NEON/NEScheduler.h"
38#include "arm_compute/runtime/TensorAllocator.h"
39#include "support/ToolchainSupport.h"
40
Pablo Tello6ff12a02017-11-02 16:09:35 +000041namespace arm_compute
42{
43#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
Pablo Tello6681d242017-11-13 16:44:08 +000044#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_gemm_u8_12x8.hpp"
Pablo Tello6ff12a02017-11-02 16:09:35 +000045} // namespace arm_compute
46
Gian Marco Iodiceab182122017-10-09 15:05:40 +010047using namespace arm_compute;
48
49NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Gian Marcoe75a02b2017-11-08 12:24:09 +000050 : _memory_group(std::move(memory_manager)), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _mtx_a_reduction_kernel(), _mtx_b_reduction_kernel(),
Gian Marcoc7f9b892017-11-30 14:31:13 +000051 _offset_contribution_kernel(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), _tmp_b(), _workspace(), _a_offset(0), _b_offset(0), _run_vector_matrix_multiplication(false), _dot_product_path(false)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010052{
53}
54
55void NEGEMMLowpMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, ITensor *output)
56{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000057 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
58 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), output->info()));
Gian Marco Iodiceab182122017-10-09 15:05:40 +010059
Gian Marcoc7f9b892017-11-30 14:31:13 +000060 _a_offset = a->info()->quantization_info().offset;
61 _b_offset = b->info()->quantization_info().offset;
62 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Gian Marcoe75a02b2017-11-08 12:24:09 +000063
Gian Marco Iodiceab182122017-10-09 15:05:40 +010064#ifdef ARM_COMPUTE_AARCH64_V8_2
65 // Check for DOT product instruction
66 const struct CPUInfo ci = NEScheduler::get().cpu_info();
67 const int cpu_has_dotprod = static_cast<int>(ci.CPU) & static_cast<int>(CPUTarget::DOT);
68
69 if(cpu_has_dotprod != 0)
70 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000071 _dot_product_path = true;
Gian Marcoe75a02b2017-11-08 12:24:09 +000072
Gian Marco Iodiceab182122017-10-09 15:05:40 +010073 // Configure matrix multiply kernel
Pablo Tello6ff12a02017-11-02 16:09:35 +000074 struct CPUInfo ci = NEScheduler::get().cpu_info();
75 const int M = output->info()->tensor_shape().y();
76 const int N = output->info()->tensor_shape().x();
77 const int K = a->info()->tensor_shape().x();
78
Pablo Tello6681d242017-11-13 16:44:08 +000079 const size_t workbench_size = GemmInterleaved<gemm_u8_12x8, gemm_u8_12x8::operand_type, gemm_u8_12x8::result_type>(&ci, M, N, K, false, false).get_working_size();
80 constexpr size_t alignment = 4096;
81 _workspace.allocator()->init(TensorInfo(TensorShape{ (workbench_size + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8));
Pablo Tello6ff12a02017-11-02 16:09:35 +000082 _memory_group.manage(&_workspace);
Gian Marcoe75a02b2017-11-08 12:24:09 +000083
Pablo Tello6ff12a02017-11-02 16:09:35 +000084 // Configure matrix multiplication kernel
85 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpAArch64V8P4Kernel>();
86 k->configure(a, b, output, &_workspace, 1.f, 1.f);
87 _mm_kernel = std::move(k);
Gian Marco Iodiceab182122017-10-09 15:05:40 +010088 }
89 else
90#endif /* ARM_COMPUTE_AARCH64_V8_2 */
91 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000092 if(_run_vector_matrix_multiplication)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010093 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000094 // Configure matrix multiply kernel
95 {
96 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
97 k->configure(a, b, output);
98 _mm_kernel = std::move(k);
99 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100100 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000101 else
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100102 {
Gian Marcoc7f9b892017-11-30 14:31:13 +0000103 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
104 TensorShape shape_tmp_a = a->info()->tensor_shape();
105 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
106 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.f));
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100107
Gian Marcoc7f9b892017-11-30 14:31:13 +0000108 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
109 TensorShape shape_tmp_b = b->info()->tensor_shape();
110 shape_tmp_b.set(0, b->info()->dimension(1) * 16);
111 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / 16.f));
112
113 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type());
114 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type());
115 _tmp_a.allocator()->init(info_a);
116 _tmp_b.allocator()->init(info_b);
117 _memory_group.manage(&_tmp_a);
118 _memory_group.manage(&_tmp_b);
119
120 // Configure interleave kernel
121 {
122 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
123 k->configure(a, &_tmp_a);
124 _mtx_a_reshape_kernel = std::move(k);
125 }
126
127 // Configure transpose kernel
128 {
129 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
130 k->configure(b, &_tmp_b);
131 _mtx_b_reshape_kernel = std::move(k);
132 }
133
134 // Configure matrix multiply kernel
135 {
136 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
137 k->configure(&_tmp_a, &_tmp_b, output);
138 _mm_kernel = std::move(k);
139 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100140 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000141 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100142
Gian Marcoe75a02b2017-11-08 12:24:09 +0000143 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
144 if(_a_offset != 0)
145 {
146 TensorShape shape_vector_sum_col = b->info()->tensor_shape();
Gian Marco05288a22017-11-21 10:57:50 +0000147 if(b->info()->num_dimensions() > 1)
148 {
149 shape_vector_sum_col.remove_dimension(1);
150 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000151 TensorInfo info_vector_sum_col(shape_vector_sum_col, 1, DataType::S32);
152 _vector_sum_col.allocator()->init(info_vector_sum_col);
153 _memory_group.manage(&_vector_sum_col);
154
155 // Configure Matrix B reduction kernel
156 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
157 }
158
159 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
160 if(_b_offset != 0)
161 {
162 TensorShape shape_vector_sum_row = a->info()->tensor_shape();
163 shape_vector_sum_row.set(Window::DimX, a->info()->dimension(1));
Gian Marco05288a22017-11-21 10:57:50 +0000164 if(a->info()->num_dimensions() > 1)
165 {
166 shape_vector_sum_row.remove_dimension(1);
167 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000168 TensorInfo info_vector_sum_row(shape_vector_sum_row, 1, DataType::S32);
169 _vector_sum_row.allocator()->init(info_vector_sum_row);
170 _memory_group.manage(&_vector_sum_row);
171
172 // Configure matrix A reduction kernel
173 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
174 }
175
176 // Configure offset contribution kernel
177 _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a->info()->dimension(0), _a_offset, _b_offset);
178
179 // Allocate tensors
Gian Marcoc7f9b892017-11-30 14:31:13 +0000180 if(!_dot_product_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000181 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000182 _tmp_a.allocator()->allocate();
183 _tmp_b.allocator()->allocate();
184 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000185 else
186 {
187 _workspace.allocator()->allocate();
188 }
189
190 if(_a_offset != 0)
191 {
192 _vector_sum_col.allocator()->allocate();
193 }
194
195 if(_b_offset != 0)
196 {
197 _vector_sum_row.allocator()->allocate();
198 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100199}
200
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000201Error NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output)
202{
203 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
204 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
205 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
206 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
207 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
208 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(1) != (output)->dimension(1),
209 "The output matrix must have the same number of rows as the matrix A");
210 ARM_COMPUTE_RETURN_ERROR_ON_MSG((b)->dimension(0) != (output)->dimension(0),
211 "The output matrix must have the same number of columns as the matrix B");
212
Gian Marcoc7f9b892017-11-30 14:31:13 +0000213 int32_t a_offset = a->quantization_info().offset;
214 int32_t b_offset = b->quantization_info().offset;
215 bool run_vector_matrix_multiplication = a->dimension(1) < 2;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000216
217#ifdef ARM_COMPUTE_AARCH64_V8_2
218 // Check for DOT product instruction
219 const struct CPUInfo ci = NEScheduler::get().cpu_info();
220 const int cpu_has_dotprod = static_cast<int>(ci.CPU) & static_cast<int>(CPUTarget::DOT);
221
222 if(cpu_has_dotprod != 0)
223 {
224 // Validate matrix multiply kernel
225 ARM_COMPUTE_RETURN_ERROR_ON(NEGEMMLowpAArch64V8P4Kernel::validate(a, b, output));
226 }
227 else
228#endif /* ARM_COMPUTE_AARCH64_V8_2 */
229 {
Gian Marcoc7f9b892017-11-30 14:31:13 +0000230 if(!run_vector_matrix_multiplication)
231 {
232 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
233 TensorShape shape_tmp_a = a->tensor_shape();
234 shape_tmp_a.set(0, a->dimension(0) * 4);
235 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000236
Gian Marcoc7f9b892017-11-30 14:31:13 +0000237 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
238 TensorShape shape_tmp_b = b->tensor_shape();
239 shape_tmp_b.set(0, b->dimension(1) * 16);
240 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000241
Gian Marcoc7f9b892017-11-30 14:31:13 +0000242 TensorInfo info_a(shape_tmp_a, 1, a->data_type());
243 TensorInfo info_b(shape_tmp_b, 1, b->data_type());
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000244
Gian Marcoc7f9b892017-11-30 14:31:13 +0000245 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &info_a));
246 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &info_b));
247 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(&info_a, &info_b, output));
248 }
249 else
250 {
251 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(a, b, output));
252 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000253 }
254
255 TensorInfo info_vector_sum_col, info_vector_sum_row;
256
257 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
258 if(a_offset != 0)
259 {
260 TensorShape shape_vector_sum_col = b->tensor_shape();
261 shape_vector_sum_col.remove_dimension(1);
262 info_vector_sum_col = TensorInfo(shape_vector_sum_col, 1, DataType::S32);
263
264 // Configure Matrix B reduction kernel
265 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
266 }
267
268 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
269 if(b_offset != 0)
270 {
271 TensorShape shape_vector_sum_row = a->tensor_shape();
272 shape_vector_sum_row.set(Window::DimX, a->dimension(1));
273 shape_vector_sum_row.remove_dimension(1);
274 info_vector_sum_row = TensorInfo(shape_vector_sum_row, 1, DataType::S32);
275
276 // Configure matrix A reduction kernel
277 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row, a->dimension(0), false));
278 }
279
280 // Validate offset contribution kernel
281 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
282 a_offset == 0 ? nullptr : &info_vector_sum_col,
283 b_offset == 0 ? nullptr : &info_vector_sum_row,
284 a_offset, b_offset));
285
286 return Error{};
287}
288
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100289void NEGEMMLowpMatrixMultiplyCore::run()
290{
291 _memory_group.acquire();
292
Gian Marcoc7f9b892017-11-30 14:31:13 +0000293 // Do not reshape if we run the vector-by-matrix case and we do not have the optimized gemm with dot product instruction
294 if(!_run_vector_matrix_multiplication && !_dot_product_path)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000295 {
Gian Marcoc7f9b892017-11-30 14:31:13 +0000296 if(_mtx_a_reshape_kernel)
297 {
298 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
299 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100300
Gian Marcoc7f9b892017-11-30 14:31:13 +0000301 if(_mtx_b_reshape_kernel)
302 {
303 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
304 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000305 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100306
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100307 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
308
Gian Marcoe75a02b2017-11-08 12:24:09 +0000309 // Run matrix A reduction kernel only if _b_offset is not equal to 0
310 if(_b_offset != 0)
311 {
312 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
313 }
314
315 // Run matrix B reduction kernel only if _a_offset is not equal to 0
316 if(_a_offset != 0)
317 {
318 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
319 }
320
321 // Run offset contribution kernel
322 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
323
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100324 _memory_group.release();
Pablo Tello6ff12a02017-11-02 16:09:35 +0000325}