Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 1 | /* |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [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/CL/functions/CLGEMM.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixAdditionKernel.h" |
| 29 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h" |
| 30 | #include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h" |
| 31 | #include "arm_compute/core/Error.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 37 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 41 | namespace |
| 42 | { |
| 43 | inline bool is_interleaved_transposed(int m, int n, int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target) |
| 44 | { |
| 45 | bool flag = true; |
| 46 | |
| 47 | if(gpu_target == GPUTarget::BIFROST) |
| 48 | { |
| 49 | if(k > 256 && m > 4 && data_type == DataType::F32 && reshape_b_only_on_first_run) |
| 50 | { |
| 51 | const float scale = k < 1024 ? 2.0f : 2.5f; |
| 52 | flag = (scale * n) > ((1.66f * n) + 38.4f); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | flag = false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return flag; |
| 61 | } |
| 62 | |
| 63 | Status validate_arguments(const ITensorInfo *a, const ITensorInfo *b, const ICLTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info = GEMMInfo()) |
| 64 | { |
| 65 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
| 66 | |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
| 71 | |
| 72 | if(c != nullptr) |
| 73 | { |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, c->info()); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != c->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A"); |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != c->info()->dimension(0), "The C matrix must have the same number of columns as the matrix B"); |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(c->info()->dimension(0) != output->dimension(0), "The C matrix must have the same number of rows as the output matrix"); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(c->info()->dimension(1) != output->dimension(1), "The C matrix must have the same number of columns as the output matrix"); |
| 79 | } |
| 80 | |
| 81 | 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"); |
| 82 | |
| 83 | ARM_COMPUTE_UNUSED(alpha); |
| 84 | ARM_COMPUTE_UNUSED(beta); |
| 85 | return Status{}; |
| 86 | } |
| 87 | } // namespace |
| 88 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 89 | CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 90 | : _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), |
| 91 | _is_first_run(true), _reshape_b_only_on_first_run(false) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 92 | { |
| 93 | } |
| 94 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 95 | void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 96 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 97 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 98 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 99 | // Perform validation step |
| 100 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(a->info(), b->info(), c, output->info(), alpha, beta, gemm_info)); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 101 | |
| 102 | // Check if we need to reshape the matrix B only on the first run |
| 103 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 104 | |
| 105 | const ICLTensor *matrix_a = a; |
| 106 | const ICLTensor *matrix_b = b; |
| 107 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 108 | // Get the GPU target |
| 109 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 110 | |
| 111 | // Set the target for the kernels |
| 112 | _interleave_kernel.set_target(gpu_target); |
| 113 | _mm_kernel.set_target(gpu_target); |
| 114 | |
| 115 | // Arguments used by GEMMReshapeInfo |
| 116 | // If we pass the matrix A and matrix B reshaped to CLGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to CLGEMMReshapeInfo |
| 117 | // in order to know how the matrices have been reshaped |
| 118 | const int m = a->info()->dimension(1); |
| 119 | const int n = b->info()->dimension(0); |
| 120 | const int k = a->info()->dimension(0); |
| 121 | int mult_transpose1xW_width = 1; |
| 122 | int mult_interleave4x4_height = 1; |
| 123 | |
| 124 | if(gpu_target == GPUTarget::BIFROST) |
| 125 | { |
| 126 | mult_transpose1xW_width = 4; |
| 127 | mult_interleave4x4_height = 2; |
| 128 | } |
| 129 | |
| 130 | // Check if we need to reshape the matrix A and matrix B |
| 131 | _is_interleaved_transposed = is_interleaved_transposed(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 132 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 133 | if(_is_interleaved_transposed) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 134 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 135 | matrix_a = &_tmp_a; |
| 136 | matrix_b = &_tmp_b; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 137 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 138 | // Manage intermediate buffers |
| 139 | _memory_group.manage(&_tmp_a); |
| 140 | _memory_group.manage(&_tmp_b); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 141 | |
| 142 | // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel |
| 143 | |
| 144 | // Configure interleave kernel |
| 145 | _interleave_kernel.configure(a, &_tmp_a, mult_interleave4x4_height); |
| 146 | |
| 147 | // Configure transpose kernel |
| 148 | _transpose_kernel.configure(b, &_tmp_b, mult_transpose1xW_width); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 149 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 150 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 151 | _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height)); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 152 | |
| 153 | if(_is_interleaved_transposed) |
| 154 | { |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 155 | // Allocate intermediate tensors |
| 156 | _tmp_a.allocator()->allocate(); |
| 157 | _tmp_b.allocator()->allocate(); |
| 158 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 159 | |
| 160 | // Configure matrix addition kernel |
| 161 | if(beta != 0 && c != nullptr) |
| 162 | { |
| 163 | _ma_kernel.configure(c, output, beta); |
| 164 | _run_addition = true; |
| 165 | } |
| 166 | } |
| 167 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 168 | Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ICLTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info) |
| 169 | { |
| 170 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(a, b, c, output, alpha, beta, gemm_info)); |
| 171 | return Status{}; |
| 172 | } |
| 173 | |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 174 | void CLGEMM::run() |
| 175 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 176 | _memory_group.acquire(); |
| 177 | |
| 178 | if(_is_interleaved_transposed) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 179 | { |
| 180 | // Run interleave kernel |
| 181 | CLScheduler::get().enqueue(_interleave_kernel, false); |
| 182 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 183 | if(_is_first_run) |
| 184 | { |
| 185 | // Run transpose kernel |
| 186 | CLScheduler::get().enqueue(_transpose_kernel, false); |
| 187 | |
| 188 | _is_first_run = false; |
| 189 | } |
| 190 | else if(!_reshape_b_only_on_first_run) |
| 191 | { |
| 192 | // Run transpose kernel |
| 193 | CLScheduler::get().enqueue(_transpose_kernel, false); |
| 194 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Run matrix multiply kernel |
| 198 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 199 | |
| 200 | // Run matrix addition kernel |
| 201 | if(_run_addition) |
| 202 | { |
| 203 | CLScheduler::get().enqueue(_ma_kernel); |
| 204 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 205 | |
| 206 | _memory_group.release(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 207 | } |