blob: dc4ed5cefb4977a77a75d670a3ce95c415c458df [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 }
68 case DataType::U8:
69 {
70 _dot_product_path = setup_assembly_kernel(a, b, nullptr, output, 1.f, 1.f, _workspace, _memory_group, _asm_glue_unsigned);
71 break;
72 }
73 default:
74 {
75 ARM_COMPUTE_ERROR("Datatype not supported");
76 break;
77 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +010078 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000079#endif /* __aarch64__ */
80 if(!_dot_product_path)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010081 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000082 if(_run_vector_matrix_multiplication)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010083 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000084 // Configure matrix multiply kernel
85 {
86 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
87 k->configure(a, b, output);
88 _mm_kernel = std::move(k);
89 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +010090 }
Gian Marcoc7f9b892017-11-30 14:31:13 +000091 else
Gian Marco Iodiceab182122017-10-09 15:05:40 +010092 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000093 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +000094 TensorInfo info_a(compute_interleaved_shape(*a->info()), 1, a->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +000095 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +000096 TensorInfo info_b(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +000097 _tmp_a.allocator()->init(info_a);
98 _tmp_b.allocator()->init(info_b);
99 _memory_group.manage(&_tmp_a);
100 _memory_group.manage(&_tmp_b);
101
102 // Configure interleave kernel
103 {
104 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
105 k->configure(a, &_tmp_a);
106 _mtx_a_reshape_kernel = std::move(k);
107 }
108
109 // Configure transpose kernel
110 {
111 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
112 k->configure(b, &_tmp_b);
113 _mtx_b_reshape_kernel = std::move(k);
114 }
115
116 // Configure matrix multiply kernel
117 {
118 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
119 k->configure(&_tmp_a, &_tmp_b, output);
120 _mm_kernel = std::move(k);
121 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100122 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000123 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100124
Gian Marcoe75a02b2017-11-08 12:24:09 +0000125 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
126 if(_a_offset != 0)
127 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000128 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
129
Gian Marcoe75a02b2017-11-08 12:24:09 +0000130 _vector_sum_col.allocator()->init(info_vector_sum_col);
131 _memory_group.manage(&_vector_sum_col);
132
133 // Configure Matrix B reduction kernel
134 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
135 }
136
137 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
138 if(_b_offset != 0)
139 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000140 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
141
Gian Marcoe75a02b2017-11-08 12:24:09 +0000142 _vector_sum_row.allocator()->init(info_vector_sum_row);
143 _memory_group.manage(&_vector_sum_row);
144
145 // Configure matrix A reduction kernel
146 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
147 }
148
149 // Configure offset contribution kernel
150 _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);
151
152 // Allocate tensors
Gian Marcoc7f9b892017-11-30 14:31:13 +0000153 if(!_dot_product_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000154 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000155 _tmp_a.allocator()->allocate();
156 _tmp_b.allocator()->allocate();
157 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000158 else
159 {
160 _workspace.allocator()->allocate();
161 }
162
163 if(_a_offset != 0)
164 {
165 _vector_sum_col.allocator()->allocate();
166 }
167
168 if(_b_offset != 0)
169 {
170 _vector_sum_row.allocator()->allocate();
171 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100172}
173
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000174Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output, const GEMMInfo &gemm_info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000175{
176 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
177 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
178 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
179 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
180 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
181 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(1) != (output)->dimension(1),
182 "The output matrix must have the same number of rows as the matrix A");
183 ARM_COMPUTE_RETURN_ERROR_ON_MSG((b)->dimension(0) != (output)->dimension(0),
184 "The output matrix must have the same number of columns as the matrix B");
Chunosov5124be52017-11-22 20:42:13 +0700185 ARM_COMPUTE_UNUSED(gemm_info);
186 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
187 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 +0000188
Gian Marcoc7f9b892017-11-30 14:31:13 +0000189 int32_t a_offset = a->quantization_info().offset;
190 int32_t b_offset = b->quantization_info().offset;
191 bool run_vector_matrix_multiplication = a->dimension(1) < 2;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000192
Pablo Telloeb82fd22018-02-23 13:43:50 +0000193 if(!run_vector_matrix_multiplication)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000194 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000195 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
196 TensorShape shape_tmp_a = a->tensor_shape();
197 shape_tmp_a.set(0, a->dimension(0) * 4);
198 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
199
200 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
201 TensorShape shape_tmp_b = b->tensor_shape();
202 shape_tmp_b.set(0, b->dimension(1) * 16);
203 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
204
205 TensorInfo info_a(shape_tmp_a, 1, a->data_type());
206 TensorInfo info_b(shape_tmp_b, 1, b->data_type());
207
208 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &info_a));
209 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &info_b));
210 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(&info_a, &info_b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000211 }
212 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000213 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000214 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(a, b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000215 }
216
217 TensorInfo info_vector_sum_col, info_vector_sum_row;
218
219 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
220 if(a_offset != 0)
221 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000222 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000223
224 // Configure Matrix B reduction kernel
225 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
226 }
227
228 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
229 if(b_offset != 0)
230 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000231 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000232
233 // Configure matrix A reduction kernel
234 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row, a->dimension(0), false));
235 }
236
237 // Validate offset contribution kernel
238 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
239 a_offset == 0 ? nullptr : &info_vector_sum_col,
240 b_offset == 0 ? nullptr : &info_vector_sum_row,
241 a_offset, b_offset));
242
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000243 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000244}
245
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100246void NEGEMMLowpMatrixMultiplyCore::run()
247{
248 _memory_group.acquire();
249
Gian Marcoc7f9b892017-11-30 14:31:13 +0000250 // Do not reshape if we run the vector-by-matrix case and we do not have the optimized gemm with dot product instruction
251 if(!_run_vector_matrix_multiplication && !_dot_product_path)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000252 {
Gian Marcoc7f9b892017-11-30 14:31:13 +0000253 if(_mtx_a_reshape_kernel)
254 {
255 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
256 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100257
Gian Marcoc7f9b892017-11-30 14:31:13 +0000258 if(_mtx_b_reshape_kernel)
259 {
260 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
261 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000262 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100263
Pablo Telloeb82fd22018-02-23 13:43:50 +0000264 if(_asm_glue_unsigned._optimised_kernel != nullptr)
265 {
266 _asm_glue_unsigned.run();
267 }
268 else if(_asm_glue_signed._optimised_kernel != nullptr)
269 {
270 _asm_glue_signed.run();
271 }
272 else
273 {
274 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
275 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100276
Gian Marcoe75a02b2017-11-08 12:24:09 +0000277 // Run matrix A reduction kernel only if _b_offset is not equal to 0
278 if(_b_offset != 0)
279 {
280 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
281 }
282
283 // Run matrix B reduction kernel only if _a_offset is not equal to 0
284 if(_a_offset != 0)
285 {
286 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
287 }
288
289 // Run offset contribution kernel
290 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
291
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100292 _memory_group.release();
Pablo Tello6ff12a02017-11-02 16:09:35 +0000293}