blob: 0215098792c97ff0f135140d9a2ffc1b5ed0c5d1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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"
Anthony Barbier71d9b572018-07-06 17:05:59 +010034#include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.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/NEGEMMInterleave4x4Kernel.h"
38#include "src/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h"
39#include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
40#include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010041#include "src/core/helpers/AutoConfiguration.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010042#include "support/MemorySupport.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010043
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044#include <cmath>
45
Giorgio Arenaa855af12018-07-16 17:20:38 +010046using namespace arm_compute::misc::shape_calculator;
47
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010048namespace arm_compute
49{
Michalis Spyrouebcebf12020-10-21 00:04:14 +010050NEGEMM::~NEGEMM() = default;
51
Michalis Spyrou1a569a32019-09-10 17:20:34 +010052NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010053 : _memory_group(memory_manager), _weights_manager(weights_manager), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _asm_glue(memory_manager, weights_manager), _ma_kernel(),
Michalis Spyrou173ba9b2020-06-23 17:25:43 +010054 _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 +010055 _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 +010056{
57}
58
Gian Marco1d25ed52017-12-16 19:33:50 +000059void 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 +010060{
Giorgio Arenaa855af12018-07-16 17:20:38 +010061 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 +010062
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010063 const bool is_c_bias = gemm_info.reshape_b_only_on_first_run();
64 bool run_optimised = bool(NEGEMMAssemblyDispatch::validate(a->info(), b->info(), (is_c_bias && c != nullptr) ? c->info() : nullptr, d->info(), gemm_info));
65
Gian Marco1d25ed52017-12-16 19:33:50 +000066 // Check if we need to reshape the matrix B only on the first run
Georgios Pinitas72219332018-06-05 14:56:06 +010067 _is_prepared = false;
Gian Marco1d25ed52017-12-16 19:33:50 +000068 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010069 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Georgios Pinitas72219332018-06-05 14:56:06 +010070 _original_b = b;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010071 _run_alpha_scale = alpha != 1.f;
72 _run_bias_addition = c != nullptr && gemm_info.reshape_b_only_on_first_run();
73 _run_addition = beta != 0 && c != nullptr && !gemm_info.reshape_b_only_on_first_run();
74 _run_activation = gemm_info.activation_info().enabled() && (!run_optimised || (run_optimised && !NEGEMMAssemblyDispatch::is_activation_supported(gemm_info.activation_info())));
Gian Marco Iodice597a8562018-08-01 15:06:06 +010075
Anthony Barbier71d9b572018-07-06 17:05:59 +010076 if(run_optimised)
77 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010078 const ITensor *c_to_use = is_c_bias ? c : nullptr;
Georgios Pinitas0f954eb2020-06-23 17:28:38 +010079 _asm_glue.configure(a, b, c_to_use, d, gemm_info);
Gian Marco Iodice597a8562018-08-01 15:06:06 +010080 ARM_COMPUTE_ERROR_ON(!_asm_glue.is_configured());
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010081
82 // Scale product by alpha
83 if(_run_alpha_scale)
84 {
85 _alpha_scale_func.configure(d, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, alpha, 0.f));
86 }
Anthony Barbier71d9b572018-07-06 17:05:59 +010087 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +010088 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010089 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010090 // Pick output tensor in case bias addition should be performed
91 ITensor *gemm_output_to_use = d;
92 if(_run_bias_addition)
93 {
94 gemm_output_to_use = &_tmp_d;
95 _memory_group.manage(&_tmp_d);
96 }
97
Michalis Spyrouebcebf12020-10-21 00:04:14 +010098 _mm_kernel = arm_compute::support::cpp14::make_unique<NEGEMMMatrixMultiplyKernel>();
99
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100100 // Select between GEMV and GEMM
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100101 if(_run_vector_matrix_multiplication)
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +0000102 {
103 // Configure the matrix multiply kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100104 _mm_kernel->configure(a, b, gemm_output_to_use, alpha, false);
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +0000105 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100106 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100107 {
108 TensorShape shape_tmp_a = a->info()->tensor_shape();
109 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100111 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
112 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +0100113
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100114 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
115 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
116 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
Georgios Pinitasf85433b2019-02-19 13:42:59 +0000118 TensorInfo info_a = a->info()->clone()->set_tensor_shape(shape_tmp_a).set_is_resizable(true);
119 TensorInfo info_b = b->info()->clone()->set_tensor_shape(shape_tmp_b).set_is_resizable(true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100121 _tmp_a.allocator()->init(info_a);
122 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100124 // Manage intermediate buffers
125 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100126 if(!_reshape_b_only_on_first_run)
127 {
128 _memory_group.manage(&_tmp_b);
129 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000131 int m = a->info()->dimension(1);
132 int n = b->info()->dimension(0);
133 int k = a->info()->dimension(0);
134
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100135 // Configure interleave kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100136 _interleave_kernel = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
137 _interleave_kernel->configure(a, &_tmp_a);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100138
139 // Configure transpose kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100140 _transpose_kernel = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
141 _transpose_kernel->configure(b, &_tmp_b);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100142
143 // Configure matrix multiplication kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100144 _mm_kernel->configure(&_tmp_a, &_tmp_b, gemm_output_to_use, alpha, true, GEMMReshapeInfo(m, n, k));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100145
146 // Allocate once the all configure methods have been called
147 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100148 if(!_reshape_b_only_on_first_run)
149 {
150 _tmp_b.allocator()->allocate();
151 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100152 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100153
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100154 if(_run_bias_addition)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100155 {
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100156 _add_bias.configure(gemm_output_to_use, c, d, ConvertPolicy::SATURATE);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100157 _tmp_d.allocator()->allocate();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100158 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100160
161 // Configure matrix addition kernel
162 if(_run_addition)
163 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100164 _ma_kernel = arm_compute::support::cpp14::make_unique<NEGEMMMatrixAdditionKernel>();
165 _ma_kernel->configure(c, d, beta);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100166 }
167
168 // Configure activation
169 const ActivationLayerInfo &activation = gemm_info.activation_info();
170 if(_run_activation)
171 {
172 _activation_func.configure(d, nullptr, activation);
173 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174}
175
Giorgio Arenaa855af12018-07-16 17:20:38 +0100176Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
177{
178 ARM_COMPUTE_UNUSED(alpha);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100179 const bool is_c_bias = gemm_info.reshape_b_only_on_first_run();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100180
181 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100182 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000183 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 +0100184 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100185 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");
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 Pinitasafc630f2020-03-30 14:09:27 +0100188 if(a->data_type() != DataType::BFLOAT16)
189 {
190 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, output);
191 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100192
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100193 if(c != nullptr && !is_c_bias)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100194 {
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000195 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.depth_output_gemm3d() != 0);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100196 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.reinterpret_input_as_3d());
Georgios Pinitasafc630f2020-03-30 14:09:27 +0100197 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(c, output);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100198 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");
199 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");
200 }
201
202 if(output->total_size() != 0)
203 {
204 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000205 if(gemm_info.depth_output_gemm3d() != 0)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100206 {
207 if(gemm_info.reinterpret_input_as_3d())
208 {
209 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
210 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
211 }
212 else
213 {
214 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
215 }
216 }
217 else
218 {
219 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
220 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100221 }
222
Giorgio Arenaa855af12018-07-16 17:20:38 +0100223 // Check if we need to run the optimized assembly kernel
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100224 const bool run_optimised = bool(NEGEMMAssemblyDispatch::validate(a, b, is_c_bias ? c : nullptr, output, gemm_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100225
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100226 if(!run_optimised)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100227 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100228 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 +0000229 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 +0100230
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100231 // Check if the first input tensor is a vector.
232 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
233 // Check if we need to reshape the matrix A and matrix B
234 const bool run_interleave_transpose = !run_vector_matrix_multiplication && !(gemm_info.reshape_b_only_on_first_run());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100235
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100236 // Arguments used by GEMMReshapeInfo
237 // 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
238 // in order to know how the matrices have been reshaped
239 const int m = a->dimension(1);
240 const int n = b->dimension(0);
241 const int k = a->dimension(0);
242 int mult_transpose1xW_width = 1;
243 int mult_interleave4x4_height = 1;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100244
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100245 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 +0100246
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100247 const ITensorInfo *matrix_a_info = a;
248 const ITensorInfo *matrix_b_info = b;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100249
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100250 TensorInfo tmp_a_info{};
251 TensorInfo tmp_b_info{};
252 TensorInfo tmp_output_info = *output->clone();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100253
Giorgio Arenaa855af12018-07-16 17:20:38 +0100254 if(run_interleave_transpose)
255 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100256 matrix_a_info = &tmp_a_info;
257 matrix_b_info = &tmp_b_info;
258
Giorgio Arenaa855af12018-07-16 17:20:38 +0100259 // Validate interleave kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100260 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())));
261 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &tmp_a_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100262
263 // Validate transpose kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100264 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
265 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100266 }
267
268 // Validate matrix multiply
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100269 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 +0100270 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 +0100271
272 if(c != nullptr && gemm_info.reshape_b_only_on_first_run())
273 {
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100274 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&tmp_output_info, c, output, ConvertPolicy::SATURATE));
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100275 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100276 }
277
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100278 // Validate matrix addition kernel
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100279 if(beta != 0 && c != nullptr && !is_c_bias)
Georgios Pinitasea9e0dc2018-08-28 16:24:56 +0100280 {
281 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAdditionKernel::validate(c, output, beta));
282 }
283
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100284 // Validate activation
285 const ActivationLayerInfo &activation = gemm_info.activation_info();
286 if(activation.enabled())
287 {
288 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation));
289 }
290
Giorgio Arenaa855af12018-07-16 17:20:38 +0100291 return Status{};
292}
293
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294void NEGEMM::run()
295{
Georgios Pinitas72219332018-06-05 14:56:06 +0100296 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100297
Georgios Pinitasda953f22019-04-02 17:27:03 +0100298 MemoryGroupResourceScope scope_mg(_memory_group);
299
Anthony Barbier71d9b572018-07-06 17:05:59 +0100300 if(_asm_glue.is_configured())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000302 _asm_glue.run();
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100303 if(_run_alpha_scale)
304 {
305 _alpha_scale_func.run();
306 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100308 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100310 if(!_run_vector_matrix_multiplication)
311 {
312 // Run interleave kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100313 NEScheduler::get().schedule(_interleave_kernel.get(), Window::DimY);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100314
Georgios Pinitas72219332018-06-05 14:56:06 +0100315 if(!_reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000316 {
317 // Run transpose kernel
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100318 NEScheduler::get().schedule(_transpose_kernel.get(), Window::DimY);
Gian Marco1d25ed52017-12-16 19:33:50 +0000319 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100320 }
321
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100322 NEScheduler::get().schedule(_mm_kernel.get(), _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100323
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100324 // Run bias addition kernel
325 if(_run_bias_addition)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100326 {
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100327 _add_bias.run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100328 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100330
331 // Run matrix addition kernel
332 if(_run_addition)
333 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100334 NEScheduler::get().schedule(_ma_kernel.get(), Window::DimY);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100335 }
336
337 // Run activation function
338 if(_run_activation)
339 {
340 _activation_func.run();
341 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100342}
Georgios Pinitas72219332018-06-05 14:56:06 +0100343
344void NEGEMM::prepare()
345{
346 if(!_is_prepared)
347 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000348 const bool original_b_managed_by_weights_manager = _weights_manager && _weights_manager->are_weights_managed(_original_b);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100349 if(_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100350 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000351 if(!original_b_managed_by_weights_manager)
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100352 {
353 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
354 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100355
356 _asm_glue.prepare();
SiCongLi2e5fd632020-03-02 15:39:15 +0000357 if(!original_b_managed_by_weights_manager)
358 {
359 _original_b->mark_as_unused();
360 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100361 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100362 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100363 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000364 if(!original_b_managed_by_weights_manager)
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100365 {
366 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
367 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100368
369 _tmp_b.allocator()->allocate();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100370 NEScheduler::get().schedule(_transpose_kernel.get(), Window::DimY);
SiCongLi2e5fd632020-03-02 15:39:15 +0000371 if(!original_b_managed_by_weights_manager)
372 {
373 _original_b->mark_as_unused();
374 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100375 }
376
377 _is_prepared = true;
378 }
379}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100380} // namespace arm_compute