blob: df92b7999c94702f53737496450c0f0ff7e75097 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitase46a7be2019-02-18 15:16:14 +00002 * Copyright (c) 2017-2019 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{
Michalis Spyrou1a569a32019-09-10 17:20:34 +010045NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
46 : _memory_group(memory_manager), _weights_manager(weights_manager), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _asm_glue(memory_manager, weights_manager), _ma_kernel(), _tmp_a(),
47 _tmp_b(), _original_b(nullptr), _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
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010061 bool run_optimised = c == nullptr && bool(NEGEMMAssemblyDispatch::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, d->info(), alpha, beta, gemm_info));
Gian Marco Iodice597a8562018-08-01 15:06:06 +010062
Anthony Barbier71d9b572018-07-06 17:05:59 +010063 if(run_optimised)
64 {
Pablo Tello0cf77982018-10-24 15:32:39 +010065 if(MEMInfo::get_policy() == MemoryPolicy::MINIMIZE)
66 {
Georgios Pinitas37d080f2019-06-21 18:43:12 +010067 GEMMInfo gemm_info_ntb = gemm_info;
68 gemm_info_ntb.set_pretranpose_B(false);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010069 _asm_glue.configure(a, b, c, d, alpha, beta, gemm_info_ntb);
Pablo Tello0cf77982018-10-24 15:32:39 +010070 }
71 else
72 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010073 _asm_glue.configure(a, b, c, d, alpha, beta, gemm_info);
Pablo Tello0cf77982018-10-24 15:32:39 +010074 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +010075 ARM_COMPUTE_ERROR_ON(!_asm_glue.is_configured());
Anthony Barbier71d9b572018-07-06 17:05:59 +010076 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +010077 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010078 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010079 if(_run_vector_matrix_multiplication)
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000080 {
81 // Configure the matrix multiply kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000082 _mm_kernel.configure(a, b, d, alpha, false);
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000083 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +010084 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010085 {
86 TensorShape shape_tmp_a = a->info()->tensor_shape();
87 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010089 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
90 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +010091
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010092 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
93 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
94 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
Georgios Pinitasf85433b2019-02-19 13:42:59 +000096 TensorInfo info_a = a->info()->clone()->set_tensor_shape(shape_tmp_a).set_is_resizable(true);
97 TensorInfo info_b = b->info()->clone()->set_tensor_shape(shape_tmp_b).set_is_resizable(true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010099 _tmp_a.allocator()->init(info_a);
100 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100102 // Manage intermediate buffers
103 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100104 if(!_reshape_b_only_on_first_run)
105 {
106 _memory_group.manage(&_tmp_b);
107 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000109 int m = a->info()->dimension(1);
110 int n = b->info()->dimension(0);
111 int k = a->info()->dimension(0);
112
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100113 // Configure interleave kernel
114 _interleave_kernel.configure(a, &_tmp_a);
115
116 // Configure transpose kernel
117 _transpose_kernel.configure(b, &_tmp_b);
118
119 // Configure matrix multiplication kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000120 _mm_kernel.configure(&_tmp_a, &_tmp_b, d, alpha, true, GEMMReshapeInfo(m, n, k));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100121
122 // Allocate once the all configure methods have been called
123 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100124 if(!_reshape_b_only_on_first_run)
125 {
126 _tmp_b.allocator()->allocate();
127 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100128 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100129
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100130 // Configure matrix addition kernel
131 if(beta != 0 && c != nullptr)
132 {
133 _ma_kernel.configure(c, d, beta);
134 _run_addition = true;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100135 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 }
137}
138
Giorgio Arenaa855af12018-07-16 17:20:38 +0100139Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
140{
141 ARM_COMPUTE_UNUSED(alpha);
142
143 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000144 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F16, DataType::F32);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100145 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output);
146 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");
147 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
148 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
149
150 if(c != nullptr)
151 {
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000152 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.depth_output_gemm3d() != 0);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100153 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.reinterpret_input_as_3d());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100154 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
155 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");
156 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");
157 }
158
159 if(output->total_size() != 0)
160 {
161 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000162 if(gemm_info.depth_output_gemm3d() != 0)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100163 {
164 if(gemm_info.reinterpret_input_as_3d())
165 {
166 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
167 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
168 }
169 else
170 {
171 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
172 }
173 }
174 else
175 {
176 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
177 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100178 }
179
Giorgio Arenaa855af12018-07-16 17:20:38 +0100180 // Check if we need to run the optimized assembly kernel
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100181 const bool run_optimised = c == nullptr && bool(NEGEMMAssemblyDispatch::validate(a, b, c, output, alpha, beta, gemm_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100182
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100183 if(!run_optimised)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100184 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100185 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000186 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.depth_output_gemm3d() != 0, "NEGEMM cannot reinterpret the output tensor as 3D");
Giorgio Arenaa855af12018-07-16 17:20:38 +0100187
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100188 // Check if the first input tensor is a vector.
189 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
190 // Check if we need to reshape the matrix A and matrix B
191 const bool run_interleave_transpose = !run_vector_matrix_multiplication && !(gemm_info.reshape_b_only_on_first_run());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100192
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100193 // Arguments used by GEMMReshapeInfo
194 // 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
195 // in order to know how the matrices have been reshaped
196 const int m = a->dimension(1);
197 const int n = b->dimension(0);
198 const int k = a->dimension(0);
199 int mult_transpose1xW_width = 1;
200 int mult_interleave4x4_height = 1;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100201
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100202 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, gemm_info.depth_output_gemm3d());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100203
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100204 const ITensorInfo *matrix_a_info = a;
205 const ITensorInfo *matrix_b_info = b;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100206
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100207 TensorInfo tmp_a_info{};
208 TensorInfo tmp_b_info{};
209 TensorInfo tmp_output_info = *output->clone();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100210
Giorgio Arenaa855af12018-07-16 17:20:38 +0100211 if(run_interleave_transpose)
212 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100213 matrix_a_info = &tmp_a_info;
214 matrix_b_info = &tmp_b_info;
215
Giorgio Arenaa855af12018-07-16 17:20:38 +0100216 // Validate interleave kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100217 auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_interleaved_shape(*a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d())));
218 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &tmp_a_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100219
220 // Validate transpose kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100221 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
222 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100223 }
224
225 // Validate matrix multiply
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100226 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)));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100227 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &tmp_output_info, alpha, run_interleave_transpose, reshape_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100228 }
229
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100230 // Validate matrix addition kernel
231 if(beta != 0 && c != nullptr)
232 {
233 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAdditionKernel::validate(c, output, beta));
234 }
235
Giorgio Arenaa855af12018-07-16 17:20:38 +0100236 return Status{};
237}
238
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239void NEGEMM::run()
240{
Georgios Pinitas72219332018-06-05 14:56:06 +0100241 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100242
Georgios Pinitasda953f22019-04-02 17:27:03 +0100243 MemoryGroupResourceScope scope_mg(_memory_group);
244
Anthony Barbier71d9b572018-07-06 17:05:59 +0100245 if(_asm_glue.is_configured())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000247 _asm_glue.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100249 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100251 if(!_run_vector_matrix_multiplication)
252 {
253 // Run interleave kernel
254 NEScheduler::get().schedule(&_interleave_kernel, Window::DimY);
255
Georgios Pinitas72219332018-06-05 14:56:06 +0100256 if(!_reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000257 {
258 // Run transpose kernel
259 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
260 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100261 }
262
263 NEScheduler::get().schedule(&_mm_kernel, _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
264
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100265 // Run matrix addition kernel
266 if(_run_addition)
267 {
268 NEScheduler::get().schedule(&_ma_kernel, Window::DimY);
269 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270 }
271}
Georgios Pinitas72219332018-06-05 14:56:06 +0100272
273void NEGEMM::prepare()
274{
275 if(!_is_prepared)
276 {
Anthony Barbier71d9b572018-07-06 17:05:59 +0100277 if(_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100278 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100279 if(!_weights_manager || !_weights_manager->are_weights_managed(_original_b))
280 {
281 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
282 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100283
284 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100285 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100286 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100287 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100288 if(!_weights_manager || !_weights_manager->are_weights_managed(_original_b))
289 {
290 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
291 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100292
293 _tmp_b.allocator()->allocate();
294 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
295 _original_b->mark_as_unused();
296 }
297
298 _is_prepared = true;
299 }
300}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100301} // namespace arm_compute