blob: 29424f5d33cf2fce1aede790bdaa0fab15feec11 [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
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Moritz Pflanzer80373f62017-09-15 10:42:58 +010029#include "arm_compute/core/NEON/kernels/arm32/NEGEMMAArch32Kernel.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010030#include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h"
Pablo Tello4d55e0a2017-11-10 15:57:14 +000031#include "arm_compute/core/NEON/kernels/arm64/NEHGEMMAArch64FP16Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/runtime/NEON/NEScheduler.h"
36#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010037#include "support/ToolchainSupport.h"
38
39namespace arm_compute
40{
Anthony Barbierf1df3462018-01-31 09:13:37 +000041#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wswitch-default"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010043#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
Moritz Pflanzer80373f62017-09-15 10:42:58 +010044#include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp"
Pablo Tello4d55e0a2017-11-10 15:57:14 +000045#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_hgemm_24x8.hpp"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010046#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp"
Anthony Barbierf1df3462018-01-31 09:13:37 +000047#pragma GCC diagnostic pop
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010048} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
50#include <cmath>
51
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010052namespace arm_compute
53{
Georgios Pinitas658039b2017-09-15 16:30:50 +010054NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010055 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _mm_optimised_kernel(nullptr), _ma_kernel(), _tmp_a(), _tmp_b(), _workspace(),
Gian Marco1d25ed52017-12-16 19:33:50 +000056 _run_vector_matrix_multiplication(false), _run_addition(false), _is_first_run(true), _reshape_b_only_on_first_run(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
58}
59
Gian Marco1d25ed52017-12-16 19:33:50 +000060void 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 +010061{
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010062 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::F16, DataType::QS8, DataType::QS16);
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010063 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, d);
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010064 ARM_COMPUTE_ERROR_ON_MSG(a->info()->dimension(0) != b->info()->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
Gian Marco1d25ed52017-12-16 19:33:50 +000065 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
66 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067
68 if(c != nullptr)
69 {
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010070 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::F32, DataType::F16, DataType::QS8, DataType::QS16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
72 ARM_COMPUTE_ERROR_ON_MSG(a->info()->dimension(1) != c->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A");
73 ARM_COMPUTE_ERROR_ON_MSG(b->info()->dimension(0) != c->info()->dimension(0), "The C matrix must have the same number of columns as the matrix B");
74 ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(0) != d->info()->dimension(0), "The C matrix must have the same number of rows as the output matrix");
75 ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(1) != d->info()->dimension(1), "The C matrix must have the same number of columns as the output matrix");
76 }
77
Gian Marco1d25ed52017-12-16 19:33:50 +000078 // Check if we need to reshape the matrix B only on the first run
79 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010080 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010082 // Check if the first input tensor is a vector.
83 // If so, all the kernels for reshaping the tensors can be skipped
84 if(_run_vector_matrix_multiplication)
85 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 // Configure the matrix multiply kernel
87 _mm_kernel.configure(a, b, d, alpha);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010088
89 // Configure matrix addition kernel
90 if(beta != 0 && c != nullptr)
91 {
92 _ma_kernel.configure(c, d, beta);
93 _run_addition = true;
94 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 }
96 else
97 {
Moritz Pflanzer80373f62017-09-15 10:42:58 +010098#if defined(__arm__)
99 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && a->info()->data_type() == DataType::F32 && (c == nullptr || beta == 0.f))
100 {
101 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>();
102 }
103#elif defined(__aarch64__)
104 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && a->info()->data_type() == DataType::F32 && (c == nullptr || beta == 0.f))
105 {
106 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>();
107 }
Pablo Tello4d55e0a2017-11-10 15:57:14 +0000108 else if(a->info()->data_type() == DataType::F16 && (c == nullptr || beta == 0.f))
109 {
110#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
111 _mm_optimised_kernel = support::cpp14::make_unique<NEHGEMMAArch64FP16Kernel>();
112#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
113 ARM_COMPUTE_ERROR("Recompile the library with arch=arm64-v8.2-a to enable support for FP16.");
114#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
115 }
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100116#endif /* defined(__arm__) || defined(__aarch64__) */
117
118#if defined(__arm__) || defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100119 if(_mm_optimised_kernel != nullptr)
120 {
121 struct CPUInfo ci = NEScheduler::get().cpu_info();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100123 const int M = d->info()->tensor_shape().y();
124 const int N = d->info()->tensor_shape().x();
125 const int K = a->info()->tensor_shape().x();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
Pablo Tello4d55e0a2017-11-10 15:57:14 +0000127 size_t workbench_size = 0;
128
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100129#if defined(__arm__)
Pablo Tello4d55e0a2017-11-10 15:57:14 +0000130 workbench_size = GemmInterleaved<sgemm_8x6, sgemm_8x6::operand_type, sgemm_8x6::result_type>(&ci, M, N, K, false, false).get_working_size();
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100131#elif defined(__aarch64__)
Pablo Tello4d55e0a2017-11-10 15:57:14 +0000132 if(a->info()->data_type() == DataType::F32)
133 {
134 workbench_size = GemmInterleaved<sgemm_12x8, sgemm_12x8::operand_type, sgemm_12x8::result_type>(&ci, M, N, K, false, false).get_working_size();
135 }
136 else if(a->info()->data_type() == DataType::F16)
137 {
138#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
139 workbench_size = GemmInterleaved<hgemm_24x8, hgemm_24x8::operand_type, hgemm_24x8::result_type>(&ci, M, N, K, false, false).get_working_size();
140#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
141 ARM_COMPUTE_ERROR("Recompile the library with arch=arm64-v8.2-a to enable support for FP16.");
142#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
143 }
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100144#endif /* defined(__arm__) || defined(__aarch64__) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100146 constexpr size_t alignment = 4096;
Pablo Tello4d55e0a2017-11-10 15:57:14 +0000147 ARM_COMPUTE_ERROR_ON_MSG(workbench_size == 0, "size cannot be 0");
148 _workspace.allocator()->init(TensorInfo(TensorShape{ (workbench_size + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::S8));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100149 _memory_group.manage(&_workspace);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100151 // Configure matrix multiplication kernel
Georgios Pinitas08c5a062017-12-14 17:53:39 +0000152 _mm_optimised_kernel->configure(a, b, d, &_workspace, alpha, 0.f, false /* is_transposed_0 */, false /* is_transposed_1 */);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100153 _workspace.allocator()->allocate();
154 }
155 else
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100156#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100157 {
158 TensorShape shape_tmp_a = a->info()->tensor_shape();
159 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100161 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
162 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +0100163
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100164 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
165 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
166 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100168 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type(), a->info()->fixed_point_position());
169 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type(), a->info()->fixed_point_position());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100171 _tmp_a.allocator()->init(info_a);
172 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100174 // Manage intermediate buffers
175 _memory_group.manage(&_tmp_a);
176 _memory_group.manage(&_tmp_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100178 // Configure interleave kernel
179 _interleave_kernel.configure(a, &_tmp_a);
180
181 // Configure transpose kernel
182 _transpose_kernel.configure(b, &_tmp_b);
183
184 // Configure matrix multiplication kernel
185 _mm_kernel.configure(&_tmp_a, &_tmp_b, d, alpha);
186
187 // Allocate once the all configure methods have been called
188 _tmp_a.allocator()->allocate();
189 _tmp_b.allocator()->allocate();
190
191 // Configure matrix addition kernel
192 if(beta != 0 && c != nullptr)
193 {
194 _ma_kernel.configure(c, d, beta);
195 _run_addition = true;
196 }
197 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 }
199}
200
201void NEGEMM::run()
202{
Georgios Pinitas658039b2017-09-15 16:30:50 +0100203 _memory_group.acquire();
204
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100205 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100207 NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY);
208 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100210 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100212 if(!_run_vector_matrix_multiplication)
213 {
214 // Run interleave kernel
215 NEScheduler::get().schedule(&_interleave_kernel, Window::DimY);
216
Gian Marco1d25ed52017-12-16 19:33:50 +0000217 if(_is_first_run)
218 {
219 // Run transpose kernel
220 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
221
222 _is_first_run = false;
223 }
224 else if(!_reshape_b_only_on_first_run)
225 {
226 // Run transpose kernel
227 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
228 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100229 }
230
231 NEScheduler::get().schedule(&_mm_kernel, _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
232
233 _memory_group.release();
234
235 // Run matrix addition kernel
236 if(_run_addition)
237 {
238 NEScheduler::get().schedule(&_ma_kernel, Window::DimY);
239 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 }
241}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100242} // namespace arm_compute