blob: de51266267b96fce3ea9b7dd06ce132230cec34c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbierf1df3462018-01-31 09:13:37 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/runtime/NEON/functions/NEGEMM.h"
25
Giorgio Arenaa855af12018-07-16 17:20:38 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
Giorgio Arenaa855af12018-07-16 17:20:38 +010033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010035#include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010037#include "support/ToolchainSupport.h"
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#include <cmath>
40
Giorgio Arenaa855af12018-07-16 17:20:38 +010041using namespace arm_compute::misc::shape_calculator;
42
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010043namespace arm_compute
44{
Georgios Pinitas658039b2017-09-15 16:30:50 +010045NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier71d9b572018-07-06 17:05:59 +010046 : _memory_group(memory_manager), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _asm_glue(memory_manager), _ma_kernel(), _tmp_a(), _tmp_b(), _original_b(nullptr),
47 _run_vector_matrix_multiplication(false), _run_addition(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
49}
50
Gian Marco1d25ed52017-12-16 19:33:50 +000051void NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052{
Giorgio Arenaa855af12018-07-16 17:20:38 +010053 ARM_COMPUTE_ERROR_THROW_ON(NEGEMM::validate(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
Gian Marco1d25ed52017-12-16 19:33:50 +000055 // Check if we need to reshape the matrix B only on the first run
Georgios Pinitas72219332018-06-05 14:56:06 +010056 _is_prepared = false;
Gian Marco1d25ed52017-12-16 19:33:50 +000057 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010058 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Georgios Pinitas72219332018-06-05 14:56:06 +010059 _original_b = b;
Pablo Tello7fad9b12018-03-14 17:55:27 +000060
Giorgio Arenaa855af12018-07-16 17:20:38 +010061 bool run_optimised = c == nullptr && bool(NEGEMMAssemblyDispatch::validate(a->info(), b->info(), d->info(), alpha, beta, _reshape_b_only_on_first_run));
Gian Marco Iodice597a8562018-08-01 15:06:06 +010062
Anthony Barbier71d9b572018-07-06 17:05:59 +010063 if(run_optimised)
64 {
65 _asm_glue.configure(a, b, d, alpha, beta, _reshape_b_only_on_first_run);
Gian Marco Iodice597a8562018-08-01 15:06:06 +010066 ARM_COMPUTE_ERROR_ON(!_asm_glue.is_configured());
Anthony Barbier71d9b572018-07-06 17:05:59 +010067 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +010068 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010069 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010070 if(_run_vector_matrix_multiplication)
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000071 {
72 // Configure the matrix multiply kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000073 _mm_kernel.configure(a, b, d, alpha, false);
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000074 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +010075 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010076 {
77 TensorShape shape_tmp_a = a->info()->tensor_shape();
78 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010080 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
81 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +010082
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010083 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
84 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
85 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010087 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type());
88 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010090 _tmp_a.allocator()->init(info_a);
91 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010093 // Manage intermediate buffers
94 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010095 if(!_reshape_b_only_on_first_run)
96 {
97 _memory_group.manage(&_tmp_b);
98 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000100 int m = a->info()->dimension(1);
101 int n = b->info()->dimension(0);
102 int k = a->info()->dimension(0);
103
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100104 // Configure interleave kernel
105 _interleave_kernel.configure(a, &_tmp_a);
106
107 // Configure transpose kernel
108 _transpose_kernel.configure(b, &_tmp_b);
109
110 // Configure matrix multiplication kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000111 _mm_kernel.configure(&_tmp_a, &_tmp_b, d, alpha, true, GEMMReshapeInfo(m, n, k));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100112
113 // Allocate once the all configure methods have been called
114 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100115 if(!_reshape_b_only_on_first_run)
116 {
117 _tmp_b.allocator()->allocate();
118 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100119 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100120
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100121 // Configure matrix addition kernel
122 if(beta != 0 && c != nullptr)
123 {
124 _ma_kernel.configure(c, d, beta);
125 _run_addition = true;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100126 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 }
128}
129
Giorgio Arenaa855af12018-07-16 17:20:38 +0100130Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
131{
132 ARM_COMPUTE_UNUSED(alpha);
133
134 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
135 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::F16);
136 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output);
137 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");
138 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
139 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
140
141 if(c != nullptr)
142 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100143 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.depth_output_gemm3d() != 1);
144 ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.reinterpret_input_as_3d());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100145 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
146 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");
147 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");
148 }
149
150 if(output->total_size() != 0)
151 {
152 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100153 if(gemm_info.depth_output_gemm3d() != 1)
154 {
155 if(gemm_info.reinterpret_input_as_3d())
156 {
157 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
158 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
159 }
160 else
161 {
162 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
163 }
164 }
165 else
166 {
167 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
168 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100169 }
170
Giorgio Arenaa855af12018-07-16 17:20:38 +0100171 // Check if we need to run the optimized assembly kernel
172 const bool run_optimised = c == nullptr && bool(NEGEMMAssemblyDispatch::validate(a, b, output, alpha, beta, true));
173
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100174 if(!run_optimised)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100175 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100176 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
177 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.depth_output_gemm3d() != 1, "NEGEMM cannot reinterpret the output tensor as 3D");
Giorgio Arenaa855af12018-07-16 17:20:38 +0100178
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100179 // Check if the first input tensor is a vector.
180 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
181 // Check if we need to reshape the matrix A and matrix B
182 const bool run_interleave_transpose = !run_vector_matrix_multiplication && !(gemm_info.reshape_b_only_on_first_run());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100183
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100184 // Arguments used by GEMMReshapeInfo
185 // 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
186 // in order to know how the matrices have been reshaped
187 const int m = a->dimension(1);
188 const int n = b->dimension(0);
189 const int k = a->dimension(0);
190 int mult_transpose1xW_width = 1;
191 int mult_interleave4x4_height = 1;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100192
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100193 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 +0100194
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100195 const ITensorInfo *matrix_a_info = a;
196 const ITensorInfo *matrix_b_info = b;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100197
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100198 TensorInfo tmp_a_info{};
199 TensorInfo tmp_b_info{};
200 TensorInfo tmp_output_info = *output->clone();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100201
Giorgio Arenaa855af12018-07-16 17:20:38 +0100202 if(run_interleave_transpose)
203 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100204 matrix_a_info = &tmp_a_info;
205 matrix_b_info = &tmp_b_info;
206
Giorgio Arenaa855af12018-07-16 17:20:38 +0100207 // Validate interleave kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100208 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())));
209 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &tmp_a_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100210
211 // Validate transpose kernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100212 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
213 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100214 }
215
216 // Validate matrix multiply
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100217 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 +0100218 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 +0100219 }
220
221 return Status{};
222}
223
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224void NEGEMM::run()
225{
Georgios Pinitas72219332018-06-05 14:56:06 +0100226 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100227
Anthony Barbier71d9b572018-07-06 17:05:59 +0100228 if(_asm_glue.is_configured())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100230 _memory_group.acquire();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000231 _asm_glue.run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100232 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100234 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100235 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100236 _memory_group.acquire();
237
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100238 if(!_run_vector_matrix_multiplication)
239 {
240 // Run interleave kernel
241 NEScheduler::get().schedule(&_interleave_kernel, Window::DimY);
242
Georgios Pinitas72219332018-06-05 14:56:06 +0100243 if(!_reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000244 {
245 // Run transpose kernel
246 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
247 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100248 }
249
250 NEScheduler::get().schedule(&_mm_kernel, _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
251
252 _memory_group.release();
253
254 // Run matrix addition kernel
255 if(_run_addition)
256 {
257 NEScheduler::get().schedule(&_ma_kernel, Window::DimY);
258 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100259 }
260}
Georgios Pinitas72219332018-06-05 14:56:06 +0100261
262void NEGEMM::prepare()
263{
264 if(!_is_prepared)
265 {
Anthony Barbier71d9b572018-07-06 17:05:59 +0100266 if(_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100267 {
268 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
269
270 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100271 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100272 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100273 {
274 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
275
276 _tmp_b.allocator()->allocate();
277 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
278 _original_b->mark_as_unused();
279 }
280
281 _is_prepared = true;
282 }
283}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100284} // namespace arm_compute