blob: ff92ef8351cda8908050c1542f9c044baa6c7ae1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/runtime/NEON/NEScheduler.h"
35#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010036#include "support/ToolchainSupport.h"
37
38namespace arm_compute
39{
40#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
Moritz Pflanzer80373f62017-09-15 10:42:58 +010041#include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010042#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp"
43} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45#include <cmath>
46
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010047namespace arm_compute
48{
Georgios Pinitas658039b2017-09-15 16:30:50 +010049NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010050 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _mm_optimised_kernel(nullptr), _ma_kernel(), _tmp_a(), _tmp_b(), _workspace(),
51 _run_vector_matrix_multiplication(false), _run_addition(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052{
53}
54
55void NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta)
56{
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010057 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 +010058 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, d);
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010059 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");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060
61 if(c != nullptr)
62 {
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010063 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 +010064 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
65 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");
66 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");
67 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");
68 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");
69 }
70
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010071 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010073 // Check if the first input tensor is a vector.
74 // If so, all the kernels for reshaping the tensors can be skipped
75 if(_run_vector_matrix_multiplication)
76 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 // Configure the matrix multiply kernel
78 _mm_kernel.configure(a, b, d, alpha);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010079
80 // Configure matrix addition kernel
81 if(beta != 0 && c != nullptr)
82 {
83 _ma_kernel.configure(c, d, beta);
84 _run_addition = true;
85 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 }
87 else
88 {
Moritz Pflanzer80373f62017-09-15 10:42:58 +010089#if defined(__arm__)
90 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && a->info()->data_type() == DataType::F32 && (c == nullptr || beta == 0.f))
91 {
92 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>();
93 }
94#elif defined(__aarch64__)
95 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && a->info()->data_type() == DataType::F32 && (c == nullptr || beta == 0.f))
96 {
97 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>();
98 }
99#endif /* defined(__arm__) || defined(__aarch64__) */
100
101#if defined(__arm__) || defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100102 if(_mm_optimised_kernel != nullptr)
103 {
104 struct CPUInfo ci = NEScheduler::get().cpu_info();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100106 const int M = d->info()->tensor_shape().y();
107 const int N = d->info()->tensor_shape().x();
108 const int K = a->info()->tensor_shape().x();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100110#if defined(__arm__)
111 GemmInterleaved<sgemm_8x6, float, float> gemm(&ci, M, N, K, false, false);
112#elif defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100113 GemmInterleaved<sgemm_12x8, float, float> gemm(&ci, M, N, K, false, false);
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100114#endif /* defined(__arm__) || defined(__aarch64__) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100116 constexpr size_t alignment = 4096;
117 _workspace.allocator()->init(TensorInfo(TensorShape{ (gemm.get_working_size() + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8));
118 _memory_group.manage(&_workspace);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100120 // Configure matrix multiplication kernel
121 _mm_optimised_kernel->configure(a, b, d, &_workspace, alpha, 0.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100123 _workspace.allocator()->allocate();
124 }
125 else
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100126#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100127 {
128 TensorShape shape_tmp_a = a->info()->tensor_shape();
129 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100131 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
132 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +0100133
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100134 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
135 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
136 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100138 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type(), a->info()->fixed_point_position());
139 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type(), a->info()->fixed_point_position());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100141 _tmp_a.allocator()->init(info_a);
142 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100144 // Manage intermediate buffers
145 _memory_group.manage(&_tmp_a);
146 _memory_group.manage(&_tmp_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100148 // Configure interleave kernel
149 _interleave_kernel.configure(a, &_tmp_a);
150
151 // Configure transpose kernel
152 _transpose_kernel.configure(b, &_tmp_b);
153
154 // Configure matrix multiplication kernel
155 _mm_kernel.configure(&_tmp_a, &_tmp_b, d, alpha);
156
157 // Allocate once the all configure methods have been called
158 _tmp_a.allocator()->allocate();
159 _tmp_b.allocator()->allocate();
160
161 // Configure matrix addition kernel
162 if(beta != 0 && c != nullptr)
163 {
164 _ma_kernel.configure(c, d, beta);
165 _run_addition = true;
166 }
167 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 }
169}
170
171void NEGEMM::run()
172{
Georgios Pinitas658039b2017-09-15 16:30:50 +0100173 _memory_group.acquire();
174
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100175 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100177 NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY);
178 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100180 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100182 if(!_run_vector_matrix_multiplication)
183 {
184 // Run interleave kernel
185 NEScheduler::get().schedule(&_interleave_kernel, Window::DimY);
186
187 // Run transpose kernel
188 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
189 }
190
191 NEScheduler::get().schedule(&_mm_kernel, _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
192
193 _memory_group.release();
194
195 // Run matrix addition kernel
196 if(_run_addition)
197 {
198 NEScheduler::get().schedule(&_ma_kernel, Window::DimY);
199 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 }
201}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100202} // namespace arm_compute