blob: 99a7db72b67d77fa32ee8b51be6081c0f54f7eec [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01002 * Copyright (c) 2017-2021 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
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/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
Giorgio Arenaa855af12018-07-16 17:20:38 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/runtime/NEON/NEScheduler.h"
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010034#include "arm_compute/runtime/Tensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/runtime/TensorAllocator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/CPP/Validate.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010037#include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010038#include "src/core/cpu/kernels/CpuGemmInterleave4x4Kernel.h"
39#include "src/core/cpu/kernels/CpuGemmMatrixAdditionKernel.h"
40#include "src/core/cpu/kernels/CpuGemmTranspose1xWKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010041#include "src/core/helpers/AutoConfiguration.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010042#include "src/core/helpers/MemoryHelpers.h"
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010043#include "src/runtime/cpu/operators/CpuActivation.h"
44#include "src/runtime/cpu/operators/CpuAdd.h"
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010045#include "src/runtime/cpu/operators/internal/CpuGemmAssemblyDispatch.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010046
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010047using namespace arm_compute::experimental;
Giorgio Arenaa855af12018-07-16 17:20:38 +010048using namespace arm_compute::misc::shape_calculator;
49
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010050namespace arm_compute
51{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000052namespace
53{
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010054cpu::AsmGemmInfo init_assembly_metadata(const GEMMInfo &info)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000055{
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010056 cpu::AsmGemmInfo asm_info;
57 asm_info.method = cpu::AsmConvMethod::Im2Col;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000058 asm_info.reinterpret_input_as_3d = info.reinterpret_input_as_3d();
59 asm_info.depth_output_gemm3d = info.depth_output_gemm3d();
60 asm_info.activation_info = info.activation_info();
61
62 return asm_info;
63}
64} // namespace
Michalis Spyrouebcebf12020-10-21 00:04:14 +010065
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010066struct NEGEMM::Impl
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010068 MemoryGroup memory_group{};
69 IWeightsManager *weights_manager{ nullptr };
70
71 std::unique_ptr<cpu::kernels::CpuGemmInterleave4x4Kernel> interleave_kernel{ nullptr };
72 std::unique_ptr<cpu::kernels::CpuGemmTranspose1xWKernel> transpose_kernel{ nullptr };
73 std::unique_ptr<NEGEMMMatrixMultiplyKernel> mm_kernel{ nullptr };
74 std::unique_ptr<cpu::CpuGemmAssemblyDispatch> asm_glue{ nullptr };
75 std::unique_ptr<cpu::kernels::CpuGemmMatrixAdditionKernel> ma_kernel{ nullptr };
76 std::unique_ptr<cpu::CpuActivation> alpha_scale_func{ nullptr };
77 std::unique_ptr<cpu::CpuAdd> add_bias{ nullptr };
78 std::unique_ptr<cpu::CpuActivation> activation_func{ nullptr };
79
80 const ITensor *a{ nullptr };
81 const ITensor *c{ nullptr };
82 ITensor *d{ nullptr };
83 ITensor *gemm_output_to_use{ nullptr };
84 Tensor tmp_a{};
85 Tensor tmp_b{};
86 Tensor tmp_d{};
87 const ITensor *original_b{ nullptr };
88 bool run_vector_matrix_multiplication{ false };
89 bool run_alpha_scale{ false };
90 bool run_addition{ false };
91 bool run_bias_addition{ false };
92 bool run_activation{ false };
93 bool reshape_b_only_on_first_run{ false };
94 bool is_prepared{ false };
95
96 ITensorPack asm_glue_run_pack{};
97 ITensorPack asm_glue_prep_pack{};
98 WorkspaceData<Tensor> asm_glue_workspace{};
99 experimental::MemoryRequirements aux_mem_req{};
100};
101
102NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
103 : _impl(std::make_unique<Impl>())
104{
105 _impl->memory_group = MemoryGroup(std::move(memory_manager));
106 _impl->weights_manager = weights_manager;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107}
108
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000109NEGEMM::~NEGEMM() = default;
110
Gian Marco1d25ed52017-12-16 19:33:50 +0000111void 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 +0100112{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100113 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100114 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 +0100115
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100116 const cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info);
117 const bool is_c_bias = gemm_info.reshape_b_only_on_first_run();
118 bool run_optimised = bool(cpu::CpuGemmAssemblyDispatch::validate(a->info(), b->info(), (is_c_bias && c != nullptr) ? c->info() : nullptr, d->info(), asm_info));
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100119
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100120 _impl->a = a;
121 _impl->c = c;
122 _impl->d = d;
123 _impl->gemm_output_to_use = d;
Gian Marco1d25ed52017-12-16 19:33:50 +0000124 // Check if we need to reshape the matrix B only on the first run
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100125 _impl->is_prepared = false;
126 _impl->reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
127 _impl->run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
128 _impl->original_b = b;
129 _impl->run_alpha_scale = alpha != 1.f;
130 _impl->run_bias_addition = c != nullptr && gemm_info.reshape_b_only_on_first_run();
131 _impl->run_addition = beta != 0 && c != nullptr && !gemm_info.reshape_b_only_on_first_run();
132 _impl->run_activation = gemm_info.activation_info().enabled() && (!run_optimised || (run_optimised
133 && !cpu::CpuGemmAssemblyDispatch::is_activation_supported(gemm_info.activation_info())));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100134
Anthony Barbier71d9b572018-07-06 17:05:59 +0100135 if(run_optimised)
136 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100137 const ITensor *c_to_use = is_c_bias ? c : nullptr;
138 const ITensorInfo *c_info_to_use = c_to_use != nullptr ? c_to_use->info() : nullptr;
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100139 _impl->asm_glue = std::make_unique<cpu::CpuGemmAssemblyDispatch>();
140 _impl->asm_glue->configure(a->info(), b->info(), c_info_to_use, d->info(), asm_info);
141 ARM_COMPUTE_ERROR_ON(!_impl->asm_glue->is_configured());
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100142
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100143 _impl->aux_mem_req = _impl->asm_glue->workspace();
144 _impl->asm_glue_run_pack =
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100145 {
146 { ACL_SRC_0, a },
147 { ACL_SRC_1, b },
148 { ACL_SRC_2, c_to_use },
149 { ACL_DST, d },
150 };
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100151 _impl->asm_glue_prep_pack = { { ACL_SRC_1, b }, { ACL_SRC_2, c_to_use } };
152 _impl->asm_glue_workspace = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->asm_glue_run_pack, _impl->asm_glue_prep_pack);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100153
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100154 // Scale product by alpha
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100155 if(_impl->run_alpha_scale)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100156 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100157 _impl->alpha_scale_func = std::make_unique<cpu::CpuActivation>();
158 _impl->alpha_scale_func->configure(d->info(), nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, alpha, 0.f));
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100159 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100160 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100161 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100162 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100163 // Pick output tensor in case bias addition should be performed
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100164 if(_impl->run_bias_addition)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100165 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100166 _impl->gemm_output_to_use = &_impl->tmp_d;
167 _impl->memory_group.manage(&_impl->tmp_d);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100168 }
169
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100170 _impl->mm_kernel = std::make_unique<NEGEMMMatrixMultiplyKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100171
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100172 // Select between GEMV and GEMM
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100173 if(_impl->run_vector_matrix_multiplication)
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +0000174 {
175 // Configure the matrix multiply kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100176 _impl->mm_kernel->configure(a, b, _impl->gemm_output_to_use, alpha, false);
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +0000177 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100178 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100179 {
180 TensorShape shape_tmp_a = a->info()->tensor_shape();
181 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100183 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
184 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +0100185
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100186 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
187 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
188 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
Georgios Pinitasf85433b2019-02-19 13:42:59 +0000190 TensorInfo info_a = a->info()->clone()->set_tensor_shape(shape_tmp_a).set_is_resizable(true);
191 TensorInfo info_b = b->info()->clone()->set_tensor_shape(shape_tmp_b).set_is_resizable(true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100193 _impl->tmp_a.allocator()->init(info_a);
194 _impl->tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100196 // Manage intermediate buffers
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100197 _impl->memory_group.manage(&_impl->tmp_a);
198 if(!_impl->reshape_b_only_on_first_run)
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100199 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100200 _impl->memory_group.manage(&_impl->tmp_b);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100201 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000203 int m = a->info()->dimension(1);
204 int n = b->info()->dimension(0);
205 int k = a->info()->dimension(0);
206
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100207 // Configure interleave kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100208 _impl->interleave_kernel = std::make_unique<cpu::kernels::CpuGemmInterleave4x4Kernel>();
209 _impl->interleave_kernel->configure(a->info(), &info_a);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100210
211 // Configure transpose kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100212 _impl->transpose_kernel = std::make_unique<cpu::kernels::CpuGemmTranspose1xWKernel>();
213 _impl->transpose_kernel->configure(b->info(), _impl->tmp_b.info());
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100214
215 // Configure matrix multiplication kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100216 _impl->mm_kernel->configure(&_impl->tmp_a, &_impl->tmp_b, _impl->gemm_output_to_use, alpha, true, GEMMReshapeInfo(m, n, k));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100217
218 // Allocate once the all configure methods have been called
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100219 _impl->tmp_a.allocator()->allocate();
220 if(!_impl->reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100221 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100222 _impl->tmp_b.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100223 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100224 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100225
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100226 if(_impl->run_bias_addition)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100227 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100228 _impl->add_bias = std::make_unique<cpu::CpuAdd>();
229 _impl->add_bias->configure(_impl->gemm_output_to_use->info(), c->info(), d->info(), ConvertPolicy::SATURATE);
230 _impl->tmp_d.allocator()->allocate();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100231 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100233
234 // Configure matrix addition kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100235 if(_impl->run_addition)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100236 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100237 _impl->ma_kernel = std::make_unique<cpu::kernels::CpuGemmMatrixAdditionKernel>();
238 _impl->ma_kernel->configure(c->info(), d->info(), beta);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100239 }
240
241 // Configure activation
242 const ActivationLayerInfo &activation = gemm_info.activation_info();
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100243 if(_impl->run_activation)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100244 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100245 _impl->activation_func = std::make_unique<cpu::CpuActivation>();
246 _impl->activation_func->configure(d->info(), nullptr, activation);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100247 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248}
249
Giorgio Arenaa855af12018-07-16 17:20:38 +0100250Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
251{
252 ARM_COMPUTE_UNUSED(alpha);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100253 const bool is_c_bias = gemm_info.reshape_b_only_on_first_run();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100254
255 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100256 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000257 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100258 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100259 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");
260 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
261 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100262 if(a->data_type() != DataType::BFLOAT16)
263 {
264 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, output);
265 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100266
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100267 if(c != nullptr && !is_c_bias)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100268 {
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000269 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.depth_output_gemm3d() != 0);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100270 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.reinterpret_input_as_3d());
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100271 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(c, output);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100272 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");
273 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");
274 }
275
276 if(output->total_size() != 0)
277 {
278 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000279 if(gemm_info.depth_output_gemm3d() != 0)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100280 {
281 if(gemm_info.reinterpret_input_as_3d())
282 {
283 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
284 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
285 }
286 else
287 {
288 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
289 }
290 }
291 else
292 {
293 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
294 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100295 }
296
Giorgio Arenaa855af12018-07-16 17:20:38 +0100297 // Check if we need to run the optimized assembly kernel
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100298 cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info);
299 const bool run_optimised = bool(cpu::CpuGemmAssemblyDispatch::validate(a, b, is_c_bias ? c : nullptr, output, asm_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100300
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100301 if(!run_optimised)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100302 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100303 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 +0000304 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 +0100305
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100306 // Check if the first input tensor is a vector.
307 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
308 // Check if we need to reshape the matrix A and matrix B
309 const bool run_interleave_transpose = !run_vector_matrix_multiplication && !(gemm_info.reshape_b_only_on_first_run());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100310
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100311 // Arguments used by GEMMReshapeInfo
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100312 // 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 GEMMReshapeInfo
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100313 // in order to know how the matrices have been reshaped
314 const int m = a->dimension(1);
315 const int n = b->dimension(0);
316 const int k = a->dimension(0);
317 int mult_transpose1xW_width = 1;
318 int mult_interleave4x4_height = 1;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100319
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100320 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 +0100321
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100322 const ITensorInfo *matrix_a_info = a;
323 const ITensorInfo *matrix_b_info = b;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100324
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100325 TensorInfo tmp_a_info{};
326 TensorInfo tmp_b_info{};
327 TensorInfo tmp_output_info = *output->clone();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100328
Giorgio Arenaa855af12018-07-16 17:20:38 +0100329 if(run_interleave_transpose)
330 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100331 matrix_a_info = &tmp_a_info;
332 matrix_b_info = &tmp_b_info;
333
Giorgio Arenaa855af12018-07-16 17:20:38 +0100334 // Validate interleave kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100335 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())));
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100336 ARM_COMPUTE_RETURN_ON_ERROR(cpu::kernels::CpuGemmInterleave4x4Kernel::validate(a, &tmp_a_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100337
338 // Validate transpose kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100339 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100340 ARM_COMPUTE_RETURN_ON_ERROR(cpu::kernels::CpuGemmTranspose1xWKernel::validate(b, &tmp_b_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100341 }
342
343 // Validate matrix multiply
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100344 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 +0100345 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &tmp_output_info, alpha, run_interleave_transpose, reshape_info));
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100346
347 if(c != nullptr && gemm_info.reshape_b_only_on_first_run())
348 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100349 ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuAdd::validate(&tmp_output_info, c, output, ConvertPolicy::SATURATE));
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100350 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100351 }
352
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100353 // Validate matrix addition kernel
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100354 if(beta != 0 && c != nullptr && !is_c_bias)
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100355 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100356 ARM_COMPUTE_RETURN_ON_ERROR(cpu::kernels::CpuGemmMatrixAdditionKernel::validate(c, output, beta));
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100357 }
358
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100359 // Validate activation
360 const ActivationLayerInfo &activation = gemm_info.activation_info();
361 if(activation.enabled())
362 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100363 ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuActivation::validate(output, nullptr, activation));
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100364 }
365
Giorgio Arenaa855af12018-07-16 17:20:38 +0100366 return Status{};
367}
368
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369void NEGEMM::run()
370{
Georgios Pinitas72219332018-06-05 14:56:06 +0100371 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100372
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100373 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Georgios Pinitasda953f22019-04-02 17:27:03 +0100374
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100375 if(_impl->asm_glue->is_configured())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100376 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100377 _impl->asm_glue->run(_impl->asm_glue_run_pack);
378 if(_impl->run_alpha_scale)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100379 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100380 ITensorPack pack{ { ACL_SRC, _impl->d }, { ACL_DST, _impl->d } };
381 _impl->alpha_scale_func->run(pack);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100382 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100383 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100384 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100385 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100386 if(!_impl->run_vector_matrix_multiplication)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100387 {
388 // Run interleave kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100389 ITensorPack interleave_pack{ { ACL_SRC, _impl->a }, { ACL_DST, &_impl->tmp_a } };
390 NEScheduler::get().schedule_op(_impl->interleave_kernel.get(), Window::DimY, _impl->interleave_kernel->window(), interleave_pack);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100391
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100392 if(!_impl->reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000393 {
394 // Run transpose kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100395 ITensorPack transpose_pack{ { ACL_SRC, _impl->original_b }, { ACL_DST, &_impl->tmp_b } };
396 NEScheduler::get().schedule_op(_impl->transpose_kernel.get(), Window::DimY, _impl->transpose_kernel->window(), transpose_pack);
Gian Marco1d25ed52017-12-16 19:33:50 +0000397 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100398 }
399
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100400 NEScheduler::get().schedule(_impl->mm_kernel.get(), _impl->run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100401
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100402 // Run bias addition kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100403 if(_impl->run_bias_addition)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100404 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100405 ITensorPack pack{ { ACL_SRC_0, _impl->gemm_output_to_use }, { ACL_SRC_1, _impl->c }, { ACL_DST, _impl->d } };
406 _impl->add_bias->run(pack);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100407 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100408 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100409
410 // Run matrix addition kernel
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100411 if(_impl->run_addition)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100412 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100413 ITensorPack c_add_pack{ { ACL_SRC, _impl->c }, { ACL_DST, _impl->d } };
414 NEScheduler::get().schedule_op(_impl->ma_kernel.get(), Window::DimY, _impl->ma_kernel->window(), c_add_pack);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100415 }
416
417 // Run activation function
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100418 if(_impl->run_activation)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100419 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100420 ITensorPack pack{ { ACL_SRC, _impl->d }, { ACL_DST, _impl->d } };
421 _impl->activation_func->run(pack);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100422 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100423}
Georgios Pinitas72219332018-06-05 14:56:06 +0100424
425void NEGEMM::prepare()
426{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100427 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100428 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100429 const bool original_b_managed_by_weights_manager = _impl->weights_manager && _impl->weights_manager->are_weights_managed(_impl->original_b);
430 if(_impl->asm_glue->is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100431 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100432 _impl->asm_glue->prepare(_impl->asm_glue_prep_pack);
Georgios Pinitas72219332018-06-05 14:56:06 +0100433
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100434 auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
435 _impl->aux_mem_req.end(),
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100436 [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
437
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100438 if(has_reshape != std::end(_impl->aux_mem_req))
SiCongLi2e5fd632020-03-02 15:39:15 +0000439 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100440 _impl->original_b->mark_as_unused();
SiCongLi2e5fd632020-03-02 15:39:15 +0000441 }
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100442 else
443 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100444 _impl->asm_glue_run_pack.add_const_tensor(ACL_SRC_1, _impl->original_b);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100445 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100446 }
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100447 else if(_impl->reshape_b_only_on_first_run && !_impl->run_vector_matrix_multiplication && !_impl->asm_glue->is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100448 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000449 if(!original_b_managed_by_weights_manager)
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100450 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100451 ARM_COMPUTE_ERROR_ON(!_impl->original_b->is_used());
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100452 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100453
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100454 _impl->tmp_b.allocator()->allocate();
455 ITensorPack transpose_pack{ { ACL_SRC, _impl->original_b }, { ACL_DST, &_impl->tmp_b } };
456 NEScheduler::get().schedule_op(_impl->transpose_kernel.get(), Window::DimY, _impl->transpose_kernel->window(), transpose_pack);
SiCongLi2e5fd632020-03-02 15:39:15 +0000457 if(!original_b_managed_by_weights_manager)
458 {
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100459 _impl->original_b->mark_as_unused();
SiCongLi2e5fd632020-03-02 15:39:15 +0000460 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100461 }
462
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100463 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100464 }
465}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100466} // namespace arm_compute