Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 34 | #include "arm_compute/runtime/TensorAllocator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/CPP/Validate.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 36 | #include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h" |
| 37 | #include "src/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h" |
| 38 | #include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h" |
| 39 | #include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 40 | #include "src/core/helpers/AutoConfiguration.h" |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 41 | #include "src/runtime/cpu/operators/internal/CpuGemmAssemblyDispatch.h" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 42 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | #include <cmath> |
| 44 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 45 | using namespace arm_compute::misc::shape_calculator; |
| 46 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 47 | namespace arm_compute |
| 48 | { |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 49 | namespace |
| 50 | { |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 51 | cpu::AsmGemmInfo init_assembly_metadata(const GEMMInfo &info) |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 52 | { |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 53 | cpu::AsmGemmInfo asm_info; |
| 54 | asm_info.method = cpu::AsmConvMethod::Im2Col; |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 55 | asm_info.reinterpret_input_as_3d = info.reinterpret_input_as_3d(); |
| 56 | asm_info.depth_output_gemm3d = info.depth_output_gemm3d(); |
| 57 | asm_info.activation_info = info.activation_info(); |
| 58 | |
| 59 | return asm_info; |
| 60 | } |
| 61 | } // namespace |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 62 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 63 | NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager) |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 64 | : _memory_group(memory_manager), _weights_manager(weights_manager), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _asm_glue(std::make_unique<cpu::CpuGemmAssemblyDispatch>()), _ma_kernel(), |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 65 | _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 Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 66 | _run_addition(false), _run_bias_addition(false), _run_activation(false), _reshape_b_only_on_first_run(false), _is_prepared(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 70 | NEGEMM::~NEGEMM() = default; |
| 71 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 72 | void NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_THROW_ON(NEGEMM::validate(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 76 | const cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info); |
| 77 | const bool is_c_bias = gemm_info.reshape_b_only_on_first_run(); |
| 78 | bool run_optimised = bool(cpu::CpuGemmAssemblyDispatch::validate(a->info(), b->info(), (is_c_bias && c != nullptr) ? c->info() : nullptr, d->info(), asm_info)); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 79 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 80 | // Check if we need to reshape the matrix B only on the first run |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 81 | _is_prepared = false; |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 82 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 83 | _run_vector_matrix_multiplication = a->info()->dimension(1) < 2; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 84 | _original_b = b; |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 85 | _run_alpha_scale = alpha != 1.f; |
| 86 | _run_bias_addition = c != nullptr && gemm_info.reshape_b_only_on_first_run(); |
| 87 | _run_addition = beta != 0 && c != nullptr && !gemm_info.reshape_b_only_on_first_run(); |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 88 | _run_activation = gemm_info.activation_info().enabled() && (!run_optimised || (run_optimised && !cpu::CpuGemmAssemblyDispatch::is_activation_supported(gemm_info.activation_info()))); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 89 | |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 90 | if(run_optimised) |
| 91 | { |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame^] | 92 | const ITensor *c_to_use = is_c_bias ? c : nullptr; |
| 93 | const ITensorInfo *c_info_to_use = c_to_use != nullptr ? c_to_use->info() : nullptr; |
| 94 | _asm_glue->configure(a->info(), b->info(), c_info_to_use, d->info(), asm_info); |
Georgios Pinitas | ec2256b | 2020-12-03 18:51:58 +0000 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON(!_asm_glue->is_configured()); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 96 | |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame^] | 97 | _asm_glue_tensors = |
| 98 | { |
| 99 | { ACL_SRC_0, a }, |
| 100 | { ACL_SRC_1, b }, |
| 101 | { ACL_SRC_2, c_to_use }, |
| 102 | { ACL_DST, d }, |
| 103 | }; |
| 104 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 105 | // Scale product by alpha |
| 106 | if(_run_alpha_scale) |
| 107 | { |
| 108 | _alpha_scale_func.configure(d, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, alpha, 0.f)); |
| 109 | } |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 110 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 111 | else |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 112 | { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 113 | // Pick output tensor in case bias addition should be performed |
| 114 | ITensor *gemm_output_to_use = d; |
| 115 | if(_run_bias_addition) |
| 116 | { |
| 117 | gemm_output_to_use = &_tmp_d; |
| 118 | _memory_group.manage(&_tmp_d); |
| 119 | } |
| 120 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 121 | _mm_kernel = std::make_unique<NEGEMMMatrixMultiplyKernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 122 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 123 | // Select between GEMV and GEMM |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 124 | if(_run_vector_matrix_multiplication) |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 125 | { |
| 126 | // Configure the matrix multiply kernel |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 127 | _mm_kernel->configure(a, b, gemm_output_to_use, alpha, false); |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 128 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 129 | else |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 130 | { |
| 131 | TensorShape shape_tmp_a = a->info()->tensor_shape(); |
| 132 | TensorShape shape_tmp_b = b->info()->tensor_shape(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 134 | shape_tmp_a.set(0, a->info()->dimension(0) * 4); |
| 135 | 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] | 136 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 137 | const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type()); |
| 138 | shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w); |
| 139 | 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] | 140 | |
Georgios Pinitas | f85433b | 2019-02-19 13:42:59 +0000 | [diff] [blame] | 141 | TensorInfo info_a = a->info()->clone()->set_tensor_shape(shape_tmp_a).set_is_resizable(true); |
| 142 | TensorInfo info_b = b->info()->clone()->set_tensor_shape(shape_tmp_b).set_is_resizable(true); |
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 | _tmp_a.allocator()->init(info_a); |
| 145 | _tmp_b.allocator()->init(info_b); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 147 | // Manage intermediate buffers |
| 148 | _memory_group.manage(&_tmp_a); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 149 | if(!_reshape_b_only_on_first_run) |
| 150 | { |
| 151 | _memory_group.manage(&_tmp_b); |
| 152 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 154 | int m = a->info()->dimension(1); |
| 155 | int n = b->info()->dimension(0); |
| 156 | int k = a->info()->dimension(0); |
| 157 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 158 | // Configure interleave kernel |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 159 | _interleave_kernel = std::make_unique<NEGEMMInterleave4x4Kernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 160 | _interleave_kernel->configure(a, &_tmp_a); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 161 | |
| 162 | // Configure transpose kernel |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 163 | _transpose_kernel = std::make_unique<NEGEMMTranspose1xWKernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 164 | _transpose_kernel->configure(b, &_tmp_b); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 165 | |
| 166 | // Configure matrix multiplication kernel |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 167 | _mm_kernel->configure(&_tmp_a, &_tmp_b, gemm_output_to_use, alpha, true, GEMMReshapeInfo(m, n, k)); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 168 | |
| 169 | // Allocate once the all configure methods have been called |
| 170 | _tmp_a.allocator()->allocate(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 171 | if(!_reshape_b_only_on_first_run) |
| 172 | { |
| 173 | _tmp_b.allocator()->allocate(); |
| 174 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 175 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 176 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 177 | if(_run_bias_addition) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 178 | { |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 179 | _add_bias.configure(gemm_output_to_use, c, d, ConvertPolicy::SATURATE); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 180 | _tmp_d.allocator()->allocate(); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 181 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | } |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 183 | |
| 184 | // Configure matrix addition kernel |
| 185 | if(_run_addition) |
| 186 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 187 | _ma_kernel = std::make_unique<NEGEMMMatrixAdditionKernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 188 | _ma_kernel->configure(c, d, beta); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Configure activation |
| 192 | const ActivationLayerInfo &activation = gemm_info.activation_info(); |
| 193 | if(_run_activation) |
| 194 | { |
| 195 | _activation_func.configure(d, nullptr, activation); |
| 196 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 199 | Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 200 | { |
| 201 | ARM_COMPUTE_UNUSED(alpha); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 202 | const bool is_c_bias = gemm_info.reshape_b_only_on_first_run(); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 203 | |
| 204 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a); |
Georgios Pinitas | afc630f | 2020-03-30 14:09:27 +0100 | [diff] [blame] | 205 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a); |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 206 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::BFLOAT16, DataType::F16, DataType::F32); |
Georgios Pinitas | afc630f | 2020-03-30 14:09:27 +0100 | [diff] [blame] | 207 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 208 | 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"); |
| 209 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 210 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
Georgios Pinitas | afc630f | 2020-03-30 14:09:27 +0100 | [diff] [blame] | 211 | if(a->data_type() != DataType::BFLOAT16) |
| 212 | { |
| 213 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, output); |
| 214 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 215 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 216 | if(c != nullptr && !is_c_bias) |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 217 | { |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 218 | ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.depth_output_gemm3d() != 0); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 219 | ARM_COMPUTE_RETURN_ERROR_ON(gemm_info.reinterpret_input_as_3d()); |
Georgios Pinitas | afc630f | 2020-03-30 14:09:27 +0100 | [diff] [blame] | 220 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(c, output); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 221 | 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"); |
| 222 | 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"); |
| 223 | } |
| 224 | |
| 225 | if(output->total_size() != 0) |
| 226 | { |
| 227 | ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0)); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 228 | if(gemm_info.depth_output_gemm3d() != 0) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 229 | { |
| 230 | if(gemm_info.reinterpret_input_as_3d()) |
| 231 | { |
| 232 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1)); |
| 233 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2)); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2)); |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1)); |
| 243 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 246 | // Check if we need to run the optimized assembly kernel |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 247 | cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info); |
| 248 | const bool run_optimised = bool(cpu::CpuGemmAssemblyDispatch::validate(a, b, is_c_bias ? c : nullptr, output, asm_info)); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 249 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 250 | if(!run_optimised) |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 251 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 252 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D"); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 253 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.depth_output_gemm3d() != 0, "NEGEMM cannot reinterpret the output tensor as 3D"); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 254 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 255 | // Check if the first input tensor is a vector. |
| 256 | const bool run_vector_matrix_multiplication = a->dimension(1) < 2; |
| 257 | // Check if we need to reshape the matrix A and matrix B |
| 258 | const bool run_interleave_transpose = !run_vector_matrix_multiplication && !(gemm_info.reshape_b_only_on_first_run()); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 259 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 260 | // Arguments used by GEMMReshapeInfo |
| 261 | // 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 |
| 262 | // in order to know how the matrices have been reshaped |
| 263 | const int m = a->dimension(1); |
| 264 | const int n = b->dimension(0); |
| 265 | const int k = a->dimension(0); |
| 266 | int mult_transpose1xW_width = 1; |
| 267 | int mult_interleave4x4_height = 1; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 268 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 269 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, gemm_info.depth_output_gemm3d()); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 270 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 271 | const ITensorInfo *matrix_a_info = a; |
| 272 | const ITensorInfo *matrix_b_info = b; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 273 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 274 | TensorInfo tmp_a_info{}; |
| 275 | TensorInfo tmp_b_info{}; |
| 276 | TensorInfo tmp_output_info = *output->clone(); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 277 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 278 | if(run_interleave_transpose) |
| 279 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 280 | matrix_a_info = &tmp_a_info; |
| 281 | matrix_b_info = &tmp_b_info; |
| 282 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 283 | // Validate interleave kernel |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 284 | 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()))); |
| 285 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &tmp_a_info)); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 286 | |
| 287 | // Validate transpose kernel |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 288 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width))); |
| 289 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info)); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // Validate matrix multiply |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 293 | 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 Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 294 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &tmp_output_info, alpha, run_interleave_transpose, reshape_info)); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 295 | |
| 296 | if(c != nullptr && gemm_info.reshape_b_only_on_first_run()) |
| 297 | { |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 298 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&tmp_output_info, c, output, ConvertPolicy::SATURATE)); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 299 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Georgios Pinitas | ea9e0dc | 2018-08-28 16:24:56 +0100 | [diff] [blame] | 302 | // Validate matrix addition kernel |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 303 | if(beta != 0 && c != nullptr && !is_c_bias) |
Georgios Pinitas | ea9e0dc | 2018-08-28 16:24:56 +0100 | [diff] [blame] | 304 | { |
| 305 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAdditionKernel::validate(c, output, beta)); |
| 306 | } |
| 307 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 308 | // Validate activation |
| 309 | const ActivationLayerInfo &activation = gemm_info.activation_info(); |
| 310 | if(activation.enabled()) |
| 311 | { |
| 312 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation)); |
| 313 | } |
| 314 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 315 | return Status{}; |
| 316 | } |
| 317 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 318 | void NEGEMM::run() |
| 319 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 320 | prepare(); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 321 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 322 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 323 | |
Georgios Pinitas | ec2256b | 2020-12-03 18:51:58 +0000 | [diff] [blame] | 324 | if(_asm_glue->is_configured()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 325 | { |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame^] | 326 | _asm_glue->run(_asm_glue_tensors); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 327 | if(_run_alpha_scale) |
| 328 | { |
| 329 | _alpha_scale_func.run(); |
| 330 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 331 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 332 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 334 | if(!_run_vector_matrix_multiplication) |
| 335 | { |
| 336 | // Run interleave kernel |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 337 | NEScheduler::get().schedule(_interleave_kernel.get(), Window::DimY); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 338 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 339 | if(!_reshape_b_only_on_first_run) |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 340 | { |
| 341 | // Run transpose kernel |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 342 | NEScheduler::get().schedule(_transpose_kernel.get(), Window::DimY); |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 343 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 346 | NEScheduler::get().schedule(_mm_kernel.get(), _run_vector_matrix_multiplication ? Window::DimX : Window::DimY); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 347 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 348 | // Run bias addition kernel |
| 349 | if(_run_bias_addition) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 350 | { |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 351 | _add_bias.run(); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 352 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 353 | } |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 354 | |
| 355 | // Run matrix addition kernel |
| 356 | if(_run_addition) |
| 357 | { |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 358 | NEScheduler::get().schedule(_ma_kernel.get(), Window::DimY); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Run activation function |
| 362 | if(_run_activation) |
| 363 | { |
| 364 | _activation_func.run(); |
| 365 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 366 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 367 | |
| 368 | void NEGEMM::prepare() |
| 369 | { |
| 370 | if(!_is_prepared) |
| 371 | { |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 372 | const bool original_b_managed_by_weights_manager = _weights_manager && _weights_manager->are_weights_managed(_original_b); |
Georgios Pinitas | ec2256b | 2020-12-03 18:51:58 +0000 | [diff] [blame] | 373 | if(_asm_glue->is_configured()) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 374 | { |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 375 | if(!original_b_managed_by_weights_manager) |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 376 | { |
| 377 | ARM_COMPUTE_ERROR_ON(!_original_b->is_used()); |
| 378 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 379 | |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame^] | 380 | _asm_glue->prepare(_asm_glue_tensors); |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 381 | if(!original_b_managed_by_weights_manager) |
| 382 | { |
| 383 | _original_b->mark_as_unused(); |
| 384 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 385 | } |
Georgios Pinitas | ec2256b | 2020-12-03 18:51:58 +0000 | [diff] [blame] | 386 | else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue->is_configured()) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 387 | { |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 388 | if(!original_b_managed_by_weights_manager) |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 389 | { |
| 390 | ARM_COMPUTE_ERROR_ON(!_original_b->is_used()); |
| 391 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 392 | |
| 393 | _tmp_b.allocator()->allocate(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 394 | NEScheduler::get().schedule(_transpose_kernel.get(), Window::DimY); |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 395 | if(!original_b_managed_by_weights_manager) |
| 396 | { |
| 397 | _original_b->mark_as_unused(); |
| 398 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | _is_prepared = true; |
| 402 | } |
| 403 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 404 | } // namespace arm_compute |