Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/kernels/arm32/NEGEMMAArch32Kernel.h" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h" |
Pablo Tello | 4d55e0a | 2017-11-10 15:57:14 +0000 | [diff] [blame^] | 31 | #include "arm_compute/core/NEON/kernels/arm64/NEHGEMMAArch64FP16Kernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #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 Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 37 | #include "support/ToolchainSupport.h" |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | #include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp" |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 42 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp" |
Pablo Tello | 4d55e0a | 2017-11-10 15:57:14 +0000 | [diff] [blame^] | 43 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a64_hgemm_24x8.hpp" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 44 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp" |
| 45 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
| 47 | #include <cmath> |
| 48 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 49 | namespace arm_compute |
| 50 | { |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 51 | NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 52 | : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _mm_optimised_kernel(nullptr), _ma_kernel(), _tmp_a(), _tmp_b(), _workspace(), |
| 53 | _run_vector_matrix_multiplication(false), _run_addition(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
| 57 | void NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta) |
| 58 | { |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::F16, DataType::QS8, DataType::QS16); |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, d); |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 61 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | |
| 63 | if(c != nullptr) |
| 64 | { |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::F32, DataType::F16, DataType::QS8, DataType::QS16); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c); |
| 67 | 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"); |
| 68 | 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"); |
| 69 | 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"); |
| 70 | 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"); |
| 71 | } |
| 72 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 73 | _run_vector_matrix_multiplication = a->info()->dimension(1) < 2; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 75 | // Check if the first input tensor is a vector. |
| 76 | // If so, all the kernels for reshaping the tensors can be skipped |
| 77 | if(_run_vector_matrix_multiplication) |
| 78 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | // Configure the matrix multiply kernel |
| 80 | _mm_kernel.configure(a, b, d, alpha); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 81 | |
| 82 | // Configure matrix addition kernel |
| 83 | if(beta != 0 && c != nullptr) |
| 84 | { |
| 85 | _ma_kernel.configure(c, d, beta); |
| 86 | _run_addition = true; |
| 87 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | } |
| 89 | else |
| 90 | { |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 91 | #if defined(__arm__) |
| 92 | if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && a->info()->data_type() == DataType::F32 && (c == nullptr || beta == 0.f)) |
| 93 | { |
| 94 | _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>(); |
| 95 | } |
| 96 | #elif defined(__aarch64__) |
| 97 | if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && a->info()->data_type() == DataType::F32 && (c == nullptr || beta == 0.f)) |
| 98 | { |
| 99 | _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>(); |
| 100 | } |
Pablo Tello | 4d55e0a | 2017-11-10 15:57:14 +0000 | [diff] [blame^] | 101 | else if(a->info()->data_type() == DataType::F16 && (c == nullptr || beta == 0.f)) |
| 102 | { |
| 103 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 104 | _mm_optimised_kernel = support::cpp14::make_unique<NEHGEMMAArch64FP16Kernel>(); |
| 105 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 106 | ARM_COMPUTE_ERROR("Recompile the library with arch=arm64-v8.2-a to enable support for FP16."); |
| 107 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 108 | } |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 109 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
| 110 | |
| 111 | #if defined(__arm__) || defined(__aarch64__) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 112 | if(_mm_optimised_kernel != nullptr) |
| 113 | { |
| 114 | struct CPUInfo ci = NEScheduler::get().cpu_info(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 116 | const int M = d->info()->tensor_shape().y(); |
| 117 | const int N = d->info()->tensor_shape().x(); |
| 118 | const int K = a->info()->tensor_shape().x(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | |
Pablo Tello | 4d55e0a | 2017-11-10 15:57:14 +0000 | [diff] [blame^] | 120 | size_t workbench_size = 0; |
| 121 | |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 122 | #if defined(__arm__) |
Pablo Tello | 4d55e0a | 2017-11-10 15:57:14 +0000 | [diff] [blame^] | 123 | workbench_size = GemmInterleaved<sgemm_8x6, sgemm_8x6::operand_type, sgemm_8x6::result_type>(&ci, M, N, K, false, false).get_working_size(); |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 124 | #elif defined(__aarch64__) |
Pablo Tello | 4d55e0a | 2017-11-10 15:57:14 +0000 | [diff] [blame^] | 125 | if(a->info()->data_type() == DataType::F32) |
| 126 | { |
| 127 | workbench_size = GemmInterleaved<sgemm_12x8, sgemm_12x8::operand_type, sgemm_12x8::result_type>(&ci, M, N, K, false, false).get_working_size(); |
| 128 | } |
| 129 | else if(a->info()->data_type() == DataType::F16) |
| 130 | { |
| 131 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 132 | workbench_size = GemmInterleaved<hgemm_24x8, hgemm_24x8::operand_type, hgemm_24x8::result_type>(&ci, M, N, K, false, false).get_working_size(); |
| 133 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 134 | ARM_COMPUTE_ERROR("Recompile the library with arch=arm64-v8.2-a to enable support for FP16."); |
| 135 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 136 | } |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 137 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 139 | constexpr size_t alignment = 4096; |
Pablo Tello | 4d55e0a | 2017-11-10 15:57:14 +0000 | [diff] [blame^] | 140 | ARM_COMPUTE_ERROR_ON_MSG(workbench_size == 0, "size cannot be 0"); |
| 141 | _workspace.allocator()->init(TensorInfo(TensorShape{ (workbench_size + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::S8)); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 142 | _memory_group.manage(&_workspace); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 144 | // Configure matrix multiplication kernel |
| 145 | _mm_optimised_kernel->configure(a, b, d, &_workspace, alpha, 0.f); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 146 | _workspace.allocator()->allocate(); |
| 147 | } |
| 148 | else |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 149 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 150 | { |
| 151 | TensorShape shape_tmp_a = a->info()->tensor_shape(); |
| 152 | TensorShape shape_tmp_b = b->info()->tensor_shape(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 154 | shape_tmp_a.set(0, a->info()->dimension(0) * 4); |
| 155 | shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f)); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 156 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 157 | const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type()); |
| 158 | shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w); |
| 159 | shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 161 | TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type(), a->info()->fixed_point_position()); |
| 162 | TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type(), a->info()->fixed_point_position()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 164 | _tmp_a.allocator()->init(info_a); |
| 165 | _tmp_b.allocator()->init(info_b); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 167 | // Manage intermediate buffers |
| 168 | _memory_group.manage(&_tmp_a); |
| 169 | _memory_group.manage(&_tmp_b); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 171 | // Configure interleave kernel |
| 172 | _interleave_kernel.configure(a, &_tmp_a); |
| 173 | |
| 174 | // Configure transpose kernel |
| 175 | _transpose_kernel.configure(b, &_tmp_b); |
| 176 | |
| 177 | // Configure matrix multiplication kernel |
| 178 | _mm_kernel.configure(&_tmp_a, &_tmp_b, d, alpha); |
| 179 | |
| 180 | // Allocate once the all configure methods have been called |
| 181 | _tmp_a.allocator()->allocate(); |
| 182 | _tmp_b.allocator()->allocate(); |
| 183 | |
| 184 | // Configure matrix addition kernel |
| 185 | if(beta != 0 && c != nullptr) |
| 186 | { |
| 187 | _ma_kernel.configure(c, d, beta); |
| 188 | _run_addition = true; |
| 189 | } |
| 190 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | void NEGEMM::run() |
| 195 | { |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 196 | _memory_group.acquire(); |
| 197 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 198 | if(_mm_optimised_kernel != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 200 | NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY); |
| 201 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 202 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 203 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 205 | if(!_run_vector_matrix_multiplication) |
| 206 | { |
| 207 | // Run interleave kernel |
| 208 | NEScheduler::get().schedule(&_interleave_kernel, Window::DimY); |
| 209 | |
| 210 | // Run transpose kernel |
| 211 | NEScheduler::get().schedule(&_transpose_kernel, Window::DimY); |
| 212 | } |
| 213 | |
| 214 | NEScheduler::get().schedule(&_mm_kernel, _run_vector_matrix_multiplication ? Window::DimX : Window::DimY); |
| 215 | |
| 216 | _memory_group.release(); |
| 217 | |
| 218 | // Run matrix addition kernel |
| 219 | if(_run_addition) |
| 220 | { |
| 221 | NEScheduler::get().schedule(&_ma_kernel, Window::DimY); |
| 222 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | } |
| 224 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 225 | } // namespace arm_compute |