Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 1aede36 | 2018-02-05 15:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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/GLES_COMPUTE/functions/GCGEMM.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
| 28 | #include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" |
| 29 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.h" |
| 30 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.h" |
| 31 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h" |
| 32 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h" |
| 33 | #include "arm_compute/core/Helpers.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "arm_compute/core/Validate.h" |
| 37 | #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" |
| 38 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 39 | |
| 40 | using namespace arm_compute; |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 41 | using namespace arm_compute::gles_compute; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 42 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *a, const ITensorInfo *b, const IGCTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info = GEMMInfo()) |
| 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
| 48 | |
| 49 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F16, DataType::F32); |
| 50 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output); |
| 51 | ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 52 | ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
| 53 | |
| 54 | if(c != nullptr) |
| 55 | { |
| 56 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c->info()); |
| 57 | ARM_COMPUTE_ERROR_ON_MSG(a->dimension(1) != c->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A"); |
| 58 | ARM_COMPUTE_ERROR_ON_MSG(b->dimension(0) != c->info()->dimension(0), "The C matrix must have the same number of columns as the matrix B"); |
| 59 | } |
| 60 | |
| 61 | if(output->total_size() != 0) |
| 62 | { |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != output->dimension(0), "The output matrix must have the same number of columns as the matrix B"); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != output->dimension(1), "The output matrix must have the same number of rows as the matrix A"); |
| 65 | } |
| 66 | |
| 67 | 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"); |
| 68 | |
| 69 | ARM_COMPUTE_UNUSED(alpha); |
| 70 | ARM_COMPUTE_UNUSED(beta); |
| 71 | ARM_COMPUTE_UNUSED(gemm_info); |
| 72 | return Status{}; |
| 73 | } |
| 74 | } // namespace |
| 75 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 76 | GCGEMM::GCGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 77 | : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _ma_kernel(), _tmp_a(), _tmp_b(), _is_interleaved_transposed(false), _run_addition(false), |
| 78 | _is_first_run(true), _reshape_b_only_on_first_run(false) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 79 | { |
| 80 | } |
| 81 | |
Gian Marco | 1aede36 | 2018-02-05 15:07:36 +0000 | [diff] [blame] | 82 | void GCGEMM::configure(const IGCTensor *a, const IGCTensor *b, const IGCTensor *c, IGCTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 83 | { |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 84 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 85 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 86 | // Perform validation step |
| 87 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(a->info(), b->info(), c, output->info(), alpha, beta, gemm_info)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 88 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 89 | // Check if we need to reshape the matrix B only on the first run |
| 90 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 91 | |
| 92 | const IGCTensor *matrix_a = a; |
| 93 | const IGCTensor *matrix_b = b; |
| 94 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 95 | // Arguments used by GEMMReshapeInfo |
| 96 | // If we pass the matrix A and matrix B reshaped to GCGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to GCGEMMReshapeInfo |
| 97 | // in order to know how the matrices have been reshaped |
| 98 | const int m = a->info()->dimension(1); |
| 99 | const int n = b->info()->dimension(0); |
| 100 | const int k = a->info()->dimension(0); |
| 101 | int mult_transpose1xW_width = 1; |
| 102 | int mult_interleave4x4_height = 1; |
| 103 | |
| 104 | // If the input tensor has less than 16 rows, we run a special version of GEMM without reshaping the input tensors |
| 105 | _is_interleaved_transposed = a->info()->dimension(1) > 16; |
| 106 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 107 | if(_is_interleaved_transposed) |
| 108 | { |
| 109 | matrix_a = &_tmp_a; |
| 110 | matrix_b = &_tmp_b; |
| 111 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 112 | // Manage intermediate buffers |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 113 | _memory_group.manage(&_tmp_a); |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 114 | if(!_reshape_b_only_on_first_run) |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 115 | { |
| 116 | _memory_group.manage(&_tmp_b); |
| 117 | } |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 118 | // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 119 | |
| 120 | // Configure interleave kernel |
| 121 | _interleave_kernel.configure(a, &_tmp_a); |
| 122 | |
| 123 | // Configure transpose kernel |
| 124 | _transpose_kernel.configure(b, &_tmp_b); |
| 125 | } |
| 126 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 127 | _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 128 | |
| 129 | if(_is_interleaved_transposed) |
| 130 | { |
| 131 | // Allocate intermediate tensors |
| 132 | _tmp_a.allocator()->allocate(); |
| 133 | _tmp_b.allocator()->allocate(); |
| 134 | } |
| 135 | |
| 136 | // Configure matrix addition kernel |
| 137 | if(beta != 0 && c != nullptr) |
| 138 | { |
| 139 | _ma_kernel.configure(c, output, beta); |
| 140 | _run_addition = true; |
| 141 | } |
| 142 | } |
| 143 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 144 | Status GCGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const IGCTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info) |
| 145 | { |
| 146 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(a, b, c, output, alpha, beta, gemm_info)); |
| 147 | return Status{}; |
| 148 | } |
| 149 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 150 | void GCGEMM::run() |
| 151 | { |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 152 | _memory_group.acquire(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 153 | if(_is_interleaved_transposed) |
| 154 | { |
| 155 | // Run interleave kernel |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 156 | GCScheduler::get().dispatch(_interleave_kernel, false); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 157 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame^] | 158 | if(_is_first_run) |
| 159 | { |
| 160 | // Run transpose kernel |
| 161 | GCScheduler::get().dispatch(_transpose_kernel, false); |
| 162 | _is_first_run = false; |
| 163 | } |
| 164 | else if(!_reshape_b_only_on_first_run) |
| 165 | { |
| 166 | // Run transpose kernel |
| 167 | GCScheduler::get().dispatch(_transpose_kernel, false); |
| 168 | } |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 169 | GCScheduler::get().memory_barrier(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Run matrix multiply kernel |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 173 | GCScheduler::get().dispatch(_mm_kernel, !_run_addition); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 174 | |
| 175 | // Run matrix addition kernel |
| 176 | if(_run_addition) |
| 177 | { |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 178 | GCScheduler::get().memory_barrier(); |
| 179 | GCScheduler::get().dispatch(_ma_kernel); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 180 | } |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 181 | _memory_group.release(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 182 | } |