blob: b0a7bdd7bc5c132824f57d2d8b8e051e80d97024 [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2017-2018 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/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"
29#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010030#include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
31#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.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/Validate.h"
Isabella Gottardie6630e42018-01-18 15:50:39 +000035#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010036#include "arm_compute/runtime/NEON/NEScheduler.h"
37#include "arm_compute/runtime/TensorAllocator.h"
38#include "support/ToolchainSupport.h"
39
40using namespace arm_compute;
Isabella Gottardie6630e42018-01-18 15:50:39 +000041using namespace arm_compute::misc::shape_calculator;
Gian Marco Iodiceab182122017-10-09 15:05:40 +010042
43NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Pablo Telloeb82fd22018-02-23 13:43:50 +000044 : _memory_group(std::move(memory_manager)), _asm_glue_unsigned(), _asm_glue_signed(), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _mtx_a_reduction_kernel(),
45 _mtx_b_reduction_kernel(), _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),
46 _dot_product_path(false)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010047{
48}
49
Chunosov5124be52017-11-22 20:42:13 +070050void NEGEMMLowpMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, ITensor *output, const GEMMInfo &gemm_info)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010051{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000052 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Chunosov5124be52017-11-22 20:42:13 +070053 ARM_COMPUTE_UNUSED(gemm_info);
54 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), output->info(), gemm_info));
Gian Marco Iodiceab182122017-10-09 15:05:40 +010055
Gian Marcoc7f9b892017-11-30 14:31:13 +000056 _a_offset = a->info()->quantization_info().offset;
57 _b_offset = b->info()->quantization_info().offset;
58 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Gian Marcoe75a02b2017-11-08 12:24:09 +000059
Pablo Telloeb82fd22018-02-23 13:43:50 +000060#ifdef __aarch64__
61 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +010062 {
Pablo Telloeb82fd22018-02-23 13:43:50 +000063 case DataType::S8:
64 {
65 _dot_product_path = setup_assembly_kernel(a, b, nullptr, output, 1.f, 1.f, _workspace, _memory_group, _asm_glue_signed);
66 break;
67 }
Pablo Tello66c656a2018-03-15 10:34:58 +000068 case DataType::QASYMM8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000069 case DataType::U8:
70 {
71 _dot_product_path = setup_assembly_kernel(a, b, nullptr, output, 1.f, 1.f, _workspace, _memory_group, _asm_glue_unsigned);
72 break;
73 }
74 default:
75 {
76 ARM_COMPUTE_ERROR("Datatype not supported");
77 break;
78 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +010079 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000080#endif /* __aarch64__ */
81 if(!_dot_product_path)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010082 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000083 if(_run_vector_matrix_multiplication)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010084 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000085 // Configure matrix multiply kernel
86 {
87 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
88 k->configure(a, b, output);
89 _mm_kernel = std::move(k);
90 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +010091 }
Gian Marcoc7f9b892017-11-30 14:31:13 +000092 else
Gian Marco Iodiceab182122017-10-09 15:05:40 +010093 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000094 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +000095 TensorInfo info_a(compute_interleaved_shape(*a->info()), 1, a->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +000096 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +000097 TensorInfo info_b(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +000098 _tmp_a.allocator()->init(info_a);
99 _tmp_b.allocator()->init(info_b);
100 _memory_group.manage(&_tmp_a);
101 _memory_group.manage(&_tmp_b);
102
103 // Configure interleave kernel
104 {
105 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
106 k->configure(a, &_tmp_a);
107 _mtx_a_reshape_kernel = std::move(k);
108 }
109
110 // Configure transpose kernel
111 {
112 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
113 k->configure(b, &_tmp_b);
114 _mtx_b_reshape_kernel = std::move(k);
115 }
116
117 // Configure matrix multiply kernel
118 {
119 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
120 k->configure(&_tmp_a, &_tmp_b, output);
121 _mm_kernel = std::move(k);
122 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100123 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000124 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100125
Gian Marcoe75a02b2017-11-08 12:24:09 +0000126 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
127 if(_a_offset != 0)
128 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000129 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
130
Gian Marcoe75a02b2017-11-08 12:24:09 +0000131 _vector_sum_col.allocator()->init(info_vector_sum_col);
132 _memory_group.manage(&_vector_sum_col);
133
134 // Configure Matrix B reduction kernel
135 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
136 }
137
138 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
139 if(_b_offset != 0)
140 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000141 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
142
Gian Marcoe75a02b2017-11-08 12:24:09 +0000143 _vector_sum_row.allocator()->init(info_vector_sum_row);
144 _memory_group.manage(&_vector_sum_row);
145
146 // Configure matrix A reduction kernel
147 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
148 }
149
150 // Configure offset contribution kernel
151 _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);
152
153 // Allocate tensors
Gian Marcoc7f9b892017-11-30 14:31:13 +0000154 if(!_dot_product_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000155 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000156 _tmp_a.allocator()->allocate();
157 _tmp_b.allocator()->allocate();
158 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000159 else
160 {
161 _workspace.allocator()->allocate();
162 }
163
164 if(_a_offset != 0)
165 {
166 _vector_sum_col.allocator()->allocate();
167 }
168
169 if(_b_offset != 0)
170 {
171 _vector_sum_row.allocator()->allocate();
172 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100173}
174
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000175Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output, const GEMMInfo &gemm_info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000176{
177 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
178 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
179 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
180 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
181 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
182 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(1) != (output)->dimension(1),
183 "The output matrix must have the same number of rows as the matrix A");
184 ARM_COMPUTE_RETURN_ERROR_ON_MSG((b)->dimension(0) != (output)->dimension(0),
185 "The output matrix must have the same number of columns as the matrix B");
Chunosov5124be52017-11-22 20:42:13 +0700186 ARM_COMPUTE_UNUSED(gemm_info);
187 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
188 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000189
Gian Marcoc7f9b892017-11-30 14:31:13 +0000190 int32_t a_offset = a->quantization_info().offset;
191 int32_t b_offset = b->quantization_info().offset;
192 bool run_vector_matrix_multiplication = a->dimension(1) < 2;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000193
Pablo Telloeb82fd22018-02-23 13:43:50 +0000194 if(!run_vector_matrix_multiplication)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000195 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000196 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
197 TensorShape shape_tmp_a = a->tensor_shape();
198 shape_tmp_a.set(0, a->dimension(0) * 4);
199 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
200
201 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
202 TensorShape shape_tmp_b = b->tensor_shape();
203 shape_tmp_b.set(0, b->dimension(1) * 16);
204 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
205
206 TensorInfo info_a(shape_tmp_a, 1, a->data_type());
207 TensorInfo info_b(shape_tmp_b, 1, b->data_type());
208
209 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &info_a));
210 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &info_b));
211 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(&info_a, &info_b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000212 }
213 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000214 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000215 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(a, b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000216 }
217
218 TensorInfo info_vector_sum_col, info_vector_sum_row;
219
220 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
221 if(a_offset != 0)
222 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000223 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000224
225 // Configure Matrix B reduction kernel
226 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
227 }
228
229 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
230 if(b_offset != 0)
231 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000232 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000233
234 // Configure matrix A reduction kernel
235 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row, a->dimension(0), false));
236 }
237
238 // Validate offset contribution kernel
239 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
240 a_offset == 0 ? nullptr : &info_vector_sum_col,
241 b_offset == 0 ? nullptr : &info_vector_sum_row,
242 a_offset, b_offset));
243
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000244 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000245}
246
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100247void NEGEMMLowpMatrixMultiplyCore::run()
248{
249 _memory_group.acquire();
250
Gian Marcoc7f9b892017-11-30 14:31:13 +0000251 // Do not reshape if we run the vector-by-matrix case and we do not have the optimized gemm with dot product instruction
252 if(!_run_vector_matrix_multiplication && !_dot_product_path)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000253 {
Gian Marcoc7f9b892017-11-30 14:31:13 +0000254 if(_mtx_a_reshape_kernel)
255 {
256 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
257 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100258
Gian Marcoc7f9b892017-11-30 14:31:13 +0000259 if(_mtx_b_reshape_kernel)
260 {
261 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
262 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000263 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100264
Pablo Telloeb82fd22018-02-23 13:43:50 +0000265 if(_asm_glue_unsigned._optimised_kernel != nullptr)
266 {
267 _asm_glue_unsigned.run();
268 }
269 else if(_asm_glue_signed._optimised_kernel != nullptr)
270 {
271 _asm_glue_signed.run();
272 }
273 else
274 {
275 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
276 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100277
Gian Marcoe75a02b2017-11-08 12:24:09 +0000278 // Run matrix A reduction kernel only if _b_offset is not equal to 0
279 if(_b_offset != 0)
280 {
281 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
282 }
283
284 // Run matrix B reduction kernel only if _a_offset is not equal to 0
285 if(_a_offset != 0)
286 {
287 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
288 }
289
290 // Run offset contribution kernel
291 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
292
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100293 _memory_group.release();
Pablo Tello6ff12a02017-11-02 16:09:35 +0000294}