blob: b84128e6c03493c11c4ead23c1805188dac39fa6 [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"
34#include "arm_compute/runtime/TensorAllocator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/CPP/Validate.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010036#include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
37#include "src/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h"
38#include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
39#include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010040#include "src/core/helpers/AutoConfiguration.h"
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010041#include "src/runtime/cpu/operators/internal/CpuGemmAssemblyDispatch.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010042
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043#include <cmath>
44
Giorgio Arenaa855af12018-07-16 17:20:38 +010045using namespace arm_compute::misc::shape_calculator;
46
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010047namespace arm_compute
48{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000049namespace
50{
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010051cpu::AsmGemmInfo init_assembly_metadata(const GEMMInfo &info)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000052{
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010053 cpu::AsmGemmInfo asm_info;
54 asm_info.method = cpu::AsmConvMethod::Im2Col;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000055 asm_info.reinterpret_input_as_3d = info.reinterpret_input_as_3d();
56 asm_info.depth_output_gemm3d = info.depth_output_gemm3d();
57 asm_info.activation_info = info.activation_info();
58
59 return asm_info;
60}
61} // namespace
Michalis Spyrouebcebf12020-10-21 00:04:14 +010062
Michalis Spyrou1a569a32019-09-10 17:20:34 +010063NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010064 : _memory_group(memory_manager), _weights_manager(weights_manager), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _asm_glue(std::make_unique<cpu::CpuGemmAssemblyDispatch>()), _ma_kernel(),
Michalis Spyrou173ba9b2020-06-23 17:25:43 +010065 _alpha_scale_func(nullptr), _add_bias(), _activation_func(), _tmp_a(), _tmp_b(), _tmp_d(), _original_b(nullptr), _run_vector_matrix_multiplication(false), _run_alpha_scale(false),
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010066 _run_addition(false), _run_bias_addition(false), _run_activation(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067{
68}
69
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000070NEGEMM::~NEGEMM() = default;
71
Gian Marco1d25ed52017-12-16 19:33:50 +000072void 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 +010073{
Giorgio Arenaa855af12018-07-16 17:20:38 +010074 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 +010075
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010076 const cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info);
77 const bool is_c_bias = gemm_info.reshape_b_only_on_first_run();
78 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 +010079
Gian Marco1d25ed52017-12-16 19:33:50 +000080 // Check if we need to reshape the matrix B only on the first run
Georgios Pinitas72219332018-06-05 14:56:06 +010081 _is_prepared = false;
Gian Marco1d25ed52017-12-16 19:33:50 +000082 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010083 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Georgios Pinitas72219332018-06-05 14:56:06 +010084 _original_b = b;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010085 _run_alpha_scale = alpha != 1.f;
86 _run_bias_addition = c != nullptr && gemm_info.reshape_b_only_on_first_run();
87 _run_addition = beta != 0 && c != nullptr && !gemm_info.reshape_b_only_on_first_run();
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010088 _run_activation = gemm_info.activation_info().enabled() && (!run_optimised || (run_optimised && !cpu::CpuGemmAssemblyDispatch::is_activation_supported(gemm_info.activation_info())));
Gian Marco Iodice597a8562018-08-01 15:06:06 +010089
Anthony Barbier71d9b572018-07-06 17:05:59 +010090 if(run_optimised)
91 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010092 const ITensor *c_to_use = is_c_bias ? c : nullptr;
Georgios Pinitasec2256b2020-12-03 18:51:58 +000093 _asm_glue->configure(a, b, c_to_use, d, asm_info);
94 ARM_COMPUTE_ERROR_ON(!_asm_glue->is_configured());
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010095
96 // Scale product by alpha
97 if(_run_alpha_scale)
98 {
99 _alpha_scale_func.configure(d, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, alpha, 0.f));
100 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100101 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100102 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100103 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100104 // Pick output tensor in case bias addition should be performed
105 ITensor *gemm_output_to_use = d;
106 if(_run_bias_addition)
107 {
108 gemm_output_to_use = &_tmp_d;
109 _memory_group.manage(&_tmp_d);
110 }
111
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000112 _mm_kernel = std::make_unique<NEGEMMMatrixMultiplyKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100113
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100114 // Select between GEMV and GEMM
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100115 if(_run_vector_matrix_multiplication)
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +0000116 {
117 // Configure the matrix multiply kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100118 _mm_kernel->configure(a, b, gemm_output_to_use, alpha, false);
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +0000119 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100120 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100121 {
122 TensorShape shape_tmp_a = a->info()->tensor_shape();
123 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100125 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
126 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +0100127
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100128 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
129 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
130 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
Georgios Pinitasf85433b2019-02-19 13:42:59 +0000132 TensorInfo info_a = a->info()->clone()->set_tensor_shape(shape_tmp_a).set_is_resizable(true);
133 TensorInfo info_b = b->info()->clone()->set_tensor_shape(shape_tmp_b).set_is_resizable(true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100135 _tmp_a.allocator()->init(info_a);
136 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100138 // Manage intermediate buffers
139 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100140 if(!_reshape_b_only_on_first_run)
141 {
142 _memory_group.manage(&_tmp_b);
143 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000145 int m = a->info()->dimension(1);
146 int n = b->info()->dimension(0);
147 int k = a->info()->dimension(0);
148
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100149 // Configure interleave kernel
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000150 _interleave_kernel = std::make_unique<NEGEMMInterleave4x4Kernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100151 _interleave_kernel->configure(a, &_tmp_a);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100152
153 // Configure transpose kernel
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000154 _transpose_kernel = std::make_unique<NEGEMMTranspose1xWKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100155 _transpose_kernel->configure(b, &_tmp_b);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100156
157 // Configure matrix multiplication kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100158 _mm_kernel->configure(&_tmp_a, &_tmp_b, gemm_output_to_use, alpha, true, GEMMReshapeInfo(m, n, k));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100159
160 // Allocate once the all configure methods have been called
161 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100162 if(!_reshape_b_only_on_first_run)
163 {
164 _tmp_b.allocator()->allocate();
165 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100166 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100167
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100168 if(_run_bias_addition)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100169 {
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100170 _add_bias.configure(gemm_output_to_use, c, d, ConvertPolicy::SATURATE);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100171 _tmp_d.allocator()->allocate();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100172 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100174
175 // Configure matrix addition kernel
176 if(_run_addition)
177 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000178 _ma_kernel = std::make_unique<NEGEMMMatrixAdditionKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100179 _ma_kernel->configure(c, d, beta);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100180 }
181
182 // Configure activation
183 const ActivationLayerInfo &activation = gemm_info.activation_info();
184 if(_run_activation)
185 {
186 _activation_func.configure(d, nullptr, activation);
187 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188}
189
Giorgio Arenaa855af12018-07-16 17:20:38 +0100190Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
191{
192 ARM_COMPUTE_UNUSED(alpha);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100193 const bool is_c_bias = gemm_info.reshape_b_only_on_first_run();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100194
195 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100196 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000197 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 +0100198 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100199 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");
200 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
201 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 +0100202 if(a->data_type() != DataType::BFLOAT16)
203 {
204 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, output);
205 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100206
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100207 if(c != nullptr && !is_c_bias)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100208 {
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000209 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.depth_output_gemm3d() != 0);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100210 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.reinterpret_input_as_3d());
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100211 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(c, output);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100212 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");
213 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");
214 }
215
216 if(output->total_size() != 0)
217 {
218 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000219 if(gemm_info.depth_output_gemm3d() != 0)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100220 {
221 if(gemm_info.reinterpret_input_as_3d())
222 {
223 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
224 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
225 }
226 else
227 {
228 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
229 }
230 }
231 else
232 {
233 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
234 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100235 }
236
Giorgio Arenaa855af12018-07-16 17:20:38 +0100237 // Check if we need to run the optimized assembly kernel
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100238 cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info);
239 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 +0100240
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100241 if(!run_optimised)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100242 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100243 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 +0000244 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 +0100245
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100246 // Check if the first input tensor is a vector.
247 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
248 // Check if we need to reshape the matrix A and matrix B
249 const bool run_interleave_transpose = !run_vector_matrix_multiplication && !(gemm_info.reshape_b_only_on_first_run());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100250
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100251 // Arguments used by GEMMReshapeInfo
252 // 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
253 // in order to know how the matrices have been reshaped
254 const int m = a->dimension(1);
255 const int n = b->dimension(0);
256 const int k = a->dimension(0);
257 int mult_transpose1xW_width = 1;
258 int mult_interleave4x4_height = 1;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100259
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100260 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 +0100261
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100262 const ITensorInfo *matrix_a_info = a;
263 const ITensorInfo *matrix_b_info = b;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100264
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100265 TensorInfo tmp_a_info{};
266 TensorInfo tmp_b_info{};
267 TensorInfo tmp_output_info = *output->clone();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100268
Giorgio Arenaa855af12018-07-16 17:20:38 +0100269 if(run_interleave_transpose)
270 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100271 matrix_a_info = &tmp_a_info;
272 matrix_b_info = &tmp_b_info;
273
Giorgio Arenaa855af12018-07-16 17:20:38 +0100274 // Validate interleave kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100275 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())));
276 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &tmp_a_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100277
278 // Validate transpose kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100279 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
280 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100281 }
282
283 // Validate matrix multiply
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100284 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 +0100285 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 +0100286
287 if(c != nullptr && gemm_info.reshape_b_only_on_first_run())
288 {
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100289 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&tmp_output_info, c, output, ConvertPolicy::SATURATE));
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100290 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100291 }
292
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100293 // Validate matrix addition kernel
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100294 if(beta != 0 && c != nullptr && !is_c_bias)
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100295 {
296 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAdditionKernel::validate(c, output, beta));
297 }
298
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100299 // Validate activation
300 const ActivationLayerInfo &activation = gemm_info.activation_info();
301 if(activation.enabled())
302 {
303 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation));
304 }
305
Giorgio Arenaa855af12018-07-16 17:20:38 +0100306 return Status{};
307}
308
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309void NEGEMM::run()
310{
Georgios Pinitas72219332018-06-05 14:56:06 +0100311 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100312
Georgios Pinitasda953f22019-04-02 17:27:03 +0100313 MemoryGroupResourceScope scope_mg(_memory_group);
314
Georgios Pinitasec2256b2020-12-03 18:51:58 +0000315 if(_asm_glue->is_configured())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 {
Georgios Pinitasec2256b2020-12-03 18:51:58 +0000317 _asm_glue->run();
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100318 if(_run_alpha_scale)
319 {
320 _alpha_scale_func.run();
321 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100323 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100325 if(!_run_vector_matrix_multiplication)
326 {
327 // Run interleave kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100328 NEScheduler::get().schedule(_interleave_kernel.get(), Window::DimY);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100329
Georgios Pinitas72219332018-06-05 14:56:06 +0100330 if(!_reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000331 {
332 // Run transpose kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100333 NEScheduler::get().schedule(_transpose_kernel.get(), Window::DimY);
Gian Marco1d25ed52017-12-16 19:33:50 +0000334 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100335 }
336
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100337 NEScheduler::get().schedule(_mm_kernel.get(), _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100338
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100339 // Run bias addition kernel
340 if(_run_bias_addition)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100341 {
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100342 _add_bias.run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100343 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100345
346 // Run matrix addition kernel
347 if(_run_addition)
348 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100349 NEScheduler::get().schedule(_ma_kernel.get(), Window::DimY);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100350 }
351
352 // Run activation function
353 if(_run_activation)
354 {
355 _activation_func.run();
356 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100357}
Georgios Pinitas72219332018-06-05 14:56:06 +0100358
359void NEGEMM::prepare()
360{
361 if(!_is_prepared)
362 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000363 const bool original_b_managed_by_weights_manager = _weights_manager && _weights_manager->are_weights_managed(_original_b);
Georgios Pinitasec2256b2020-12-03 18:51:58 +0000364 if(_asm_glue->is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100365 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000366 if(!original_b_managed_by_weights_manager)
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100367 {
368 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
369 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100370
Georgios Pinitasec2256b2020-12-03 18:51:58 +0000371 _asm_glue->prepare();
SiCongLi2e5fd632020-03-02 15:39:15 +0000372 if(!original_b_managed_by_weights_manager)
373 {
374 _original_b->mark_as_unused();
375 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100376 }
Georgios Pinitasec2256b2020-12-03 18:51:58 +0000377 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue->is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100378 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000379 if(!original_b_managed_by_weights_manager)
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100380 {
381 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
382 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100383
384 _tmp_b.allocator()->allocate();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100385 NEScheduler::get().schedule(_transpose_kernel.get(), Window::DimY);
SiCongLi2e5fd632020-03-02 15:39:15 +0000386 if(!original_b_managed_by_weights_manager)
387 {
388 _original_b->mark_as_unused();
389 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100390 }
391
392 _is_prepared = true;
393 }
394}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100395} // namespace arm_compute