blob: e47ef86a1c4ae284f08ef54cfce6b31a372f6e9b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbierf1df3462018-01-31 09:13:37 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/NEGEMM.h"
25
Giorgio Arenaa855af12018-07-16 17:20:38 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#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/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
Giorgio Arenaa855af12018-07-16 17:20:38 +010033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010035#include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010037#include "support/ToolchainSupport.h"
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#include <cmath>
40
Giorgio Arenaa855af12018-07-16 17:20:38 +010041using namespace arm_compute::misc::shape_calculator;
42
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010043namespace arm_compute
44{
Georgios Pinitas658039b2017-09-15 16:30:50 +010045NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier71d9b572018-07-06 17:05:59 +010046 : _memory_group(memory_manager), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _asm_glue(memory_manager), _ma_kernel(), _tmp_a(), _tmp_b(), _original_b(nullptr),
47 _run_vector_matrix_multiplication(false), _run_addition(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
49}
50
Gian Marco1d25ed52017-12-16 19:33:50 +000051void NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052{
Giorgio Arenaa855af12018-07-16 17:20:38 +010053 ARM_COMPUTE_ERROR_THROW_ON(NEGEMM::validate(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
Gian Marco1d25ed52017-12-16 19:33:50 +000055 // Check if we need to reshape the matrix B only on the first run
Georgios Pinitas72219332018-06-05 14:56:06 +010056 _is_prepared = false;
Gian Marco1d25ed52017-12-16 19:33:50 +000057 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010058 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Georgios Pinitas72219332018-06-05 14:56:06 +010059 _original_b = b;
Pablo Tello7fad9b12018-03-14 17:55:27 +000060
Giorgio Arenaa855af12018-07-16 17:20:38 +010061 bool run_optimised = c == nullptr && bool(NEGEMMAssemblyDispatch::validate(a->info(), b->info(), d->info(), alpha, beta, _reshape_b_only_on_first_run));
Anthony Barbier71d9b572018-07-06 17:05:59 +010062 if(run_optimised)
63 {
64 _asm_glue.configure(a, b, d, alpha, beta, _reshape_b_only_on_first_run);
65 run_optimised = _asm_glue.is_configured();
66 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010068 // Check if the first input tensor is a vector.
69 // If so, all the kernels for reshaping the tensors can be skipped
70 if(_run_vector_matrix_multiplication)
71 {
Pablo Telloeb82fd22018-02-23 13:43:50 +000072 if(!run_optimised)
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000073 {
74 // Configure the matrix multiply kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000075 _mm_kernel.configure(a, b, d, alpha, false);
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000076 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010077
78 // Configure matrix addition kernel
79 if(beta != 0 && c != nullptr)
80 {
81 _ma_kernel.configure(c, d, beta);
82 _run_addition = true;
83 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 }
85 else
86 {
Pablo Telloeb82fd22018-02-23 13:43:50 +000087 if(!run_optimised)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010088 {
89 TensorShape shape_tmp_a = a->info()->tensor_shape();
90 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010092 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
93 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +010094
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010095 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
96 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
97 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010099 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type());
100 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100102 _tmp_a.allocator()->init(info_a);
103 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100105 // Manage intermediate buffers
106 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100107 if(!_reshape_b_only_on_first_run)
108 {
109 _memory_group.manage(&_tmp_b);
110 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000112 int m = a->info()->dimension(1);
113 int n = b->info()->dimension(0);
114 int k = a->info()->dimension(0);
115
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100116 // Configure interleave kernel
117 _interleave_kernel.configure(a, &_tmp_a);
118
119 // Configure transpose kernel
120 _transpose_kernel.configure(b, &_tmp_b);
121
122 // Configure matrix multiplication kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000123 _mm_kernel.configure(&_tmp_a, &_tmp_b, d, alpha, true, GEMMReshapeInfo(m, n, k));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100124
125 // Allocate once the all configure methods have been called
126 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100127 if(!_reshape_b_only_on_first_run)
128 {
129 _tmp_b.allocator()->allocate();
130 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100131
132 // Configure matrix addition kernel
133 if(beta != 0 && c != nullptr)
134 {
135 _ma_kernel.configure(c, d, beta);
136 _run_addition = true;
137 }
138 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 }
140}
141
Giorgio Arenaa855af12018-07-16 17:20:38 +0100142Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
143{
144 ARM_COMPUTE_UNUSED(alpha);
145
146 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
147 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::F16);
148 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output);
149 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(0) != b->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
150 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
151 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
152
153 if(c != nullptr)
154 {
155 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::F32, DataType::F16);
156 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
157 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != c->dimension(1), "The C matrix must have the same number of rows as the matrix A");
158 ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != c->dimension(0), "The C matrix must have the same number of columns as the matrix B");
159 }
160
161 if(output->total_size() != 0)
162 {
163 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
164 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
165 }
166
167 // Check if the first input tensor is a vector.
168 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
169 // Check if we need to reshape the matrix A and matrix B
170 const bool run_interleave_transpose = !run_vector_matrix_multiplication && !(gemm_info.reshape_b_only_on_first_run());
171 // Check if we need to run the optimized assembly kernel
172 const bool run_optimised = c == nullptr && bool(NEGEMMAssemblyDispatch::validate(a, b, output, alpha, beta, true));
173
174 const ITensorInfo *matrix_a_info = a;
175 const ITensorInfo *matrix_b_info = b;
176
177 TensorInfo tmp_a_info{};
178 TensorInfo tmp_b_info{};
179 TensorInfo tmp_output_info = *output->clone();
180
181 // Arguments used by GEMMReshapeInfo
182 // If we pass the matrix A and matrix B reshaped to NEGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to NEGEMMReshapeInfo
183 // in order to know how the matrices have been reshaped
184 const int m = a->dimension(1);
185 const int n = b->dimension(0);
186 const int k = a->dimension(0);
187 int mult_transpose1xW_width = 1;
188 int mult_interleave4x4_height = 1;
189
190 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, gemm_info.depth_output_gemm3d());
191
192 // Initialize shapes
193 if(run_interleave_transpose)
194 {
195 matrix_a_info = &tmp_a_info;
196 auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_interleaved_shape(*a, mult_interleave4x4_height)));
197
198 matrix_b_info = &tmp_b_info;
199 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
200
201 auto_init_if_empty(tmp_output_info, matrix_a_info->clone()->set_tensor_shape(compute_mm_shape(*matrix_a_info, *matrix_b_info, run_interleave_transpose, reshape_info)));
202 }
203
204 // Validate kernels
205 if(run_optimised && run_interleave_transpose)
206 {
207 /* Interleave */
208 TensorShape tensor_shape0{ matrix_a_info->tensor_shape() };
209 tensor_shape0.set(0, k);
210 tensor_shape0.set(1, m);
211
212 const TensorInfo tensor_info0 = matrix_a_info->clone()->set_tensor_shape(tensor_shape0);
213 const TensorInfo tensor_info_reshaped0 = matrix_a_info->clone()->set_tensor_shape(compute_interleaved_shape(tensor_info0, mult_interleave4x4_height));
214 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(matrix_a_info, &tensor_info_reshaped0);
215
216 if(n != 0) /* Transpose */
217 {
218 TensorShape tensor_shape1{ matrix_b_info->tensor_shape() };
219 tensor_shape1.set(0, n);
220 tensor_shape1.set(1, k);
221
222 const TensorInfo tensor_info1 = matrix_b_info->clone()->set_tensor_shape(tensor_shape1);
223 const TensorInfo tensor_info_reshaped1 = matrix_b_info->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(tensor_info1, mult_transpose1xW_width));
224 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(matrix_b_info, &tensor_info_reshaped1);
225 }
226
227 if(output->total_size() != 0)
228 {
229 if(n != 0)
230 {
231 ARM_COMPUTE_RETURN_ERROR_ON(tmp_output_info.dimension(0) != static_cast<size_t>(n));
232 }
233 ARM_COMPUTE_RETURN_ERROR_ON(tmp_output_info.dimension(1) != static_cast<size_t>(m));
234 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(matrix_a_info, &tmp_output_info);
235 }
236 }
237 else if(run_vector_matrix_multiplication)
238 {
239 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(a, b, output, alpha, false, reshape_info));
240
241 if(beta != 0 && c != nullptr)
242 {
243 // Validate matrix addition kernel
244 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(c, output);
245 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(c, output);
246 }
247 }
248 else
249 {
250 if(run_interleave_transpose)
251 {
252 // Validate interleave kernel
253 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, matrix_a_info));
254
255 // Validate transpose kernel
256 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, matrix_b_info));
257 }
258
259 // Validate matrix multiply
260 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &tmp_output_info, alpha, run_interleave_transpose, reshape_info));
261
262 if(beta != 0 && c != nullptr)
263 {
264 // Validate matrix addition kernel
265 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(c, &tmp_output_info);
266 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(c, &tmp_output_info);
267 }
268 }
269
270 return Status{};
271}
272
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273void NEGEMM::run()
274{
Georgios Pinitas72219332018-06-05 14:56:06 +0100275 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100276
Anthony Barbier71d9b572018-07-06 17:05:59 +0100277 if(_asm_glue.is_configured())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100279 _memory_group.acquire();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000280 _asm_glue.run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100281 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100283 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100285 _memory_group.acquire();
286
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100287 if(!_run_vector_matrix_multiplication)
288 {
289 // Run interleave kernel
290 NEScheduler::get().schedule(&_interleave_kernel, Window::DimY);
291
Georgios Pinitas72219332018-06-05 14:56:06 +0100292 if(!_reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000293 {
294 // Run transpose kernel
295 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
296 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100297 }
298
299 NEScheduler::get().schedule(&_mm_kernel, _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
300
301 _memory_group.release();
302
303 // Run matrix addition kernel
304 if(_run_addition)
305 {
306 NEScheduler::get().schedule(&_ma_kernel, Window::DimY);
307 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308 }
309}
Georgios Pinitas72219332018-06-05 14:56:06 +0100310
311void NEGEMM::prepare()
312{
313 if(!_is_prepared)
314 {
Anthony Barbier71d9b572018-07-06 17:05:59 +0100315 if(_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100316 {
317 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
318
319 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100320 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100321 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100322 {
323 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
324
325 _tmp_b.allocator()->allocate();
326 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
327 _original_b->mark_as_unused();
328 }
329
330 _is_prepared = true;
331 }
332}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100333} // namespace arm_compute