Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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/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 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +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 | |
Sam Laynton | 56e8e86 | 2018-04-05 13:26:08 +0100 | [diff] [blame] | 47 | if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX)) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 48 | { |
| 49 | // COMPMID-852 |
| 50 | if(k > 256 && m > 4 && data_type == DataType::F32 && reshape_b_only_on_first_run) |
| 51 | { |
| 52 | const float scale = k < 1024 ? 2.0f : 2.5f; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 53 | flag = (scale * n) > ((1.66f * n) + 38.4f); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 54 | } |
| 55 | else |
| 56 | { |
| 57 | flag = false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return flag; |
| 62 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 63 | |
| 64 | 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()) |
| 65 | { |
| 66 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
| 67 | |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
| 72 | |
| 73 | if(c != nullptr) |
| 74 | { |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, c->info()); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != c->info()->dimension(1), "The matrix C must have the same number of rows as the matrix A"); |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != c->info()->dimension(0), "The matrix C must have the same number of columns as the matrix B"); |
| 78 | } |
| 79 | |
| 80 | if(output->total_size() != 0) |
| 81 | { |
| 82 | 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"); |
| 83 | 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"); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | 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"); |
| 87 | |
| 88 | ARM_COMPUTE_UNUSED(alpha); |
| 89 | ARM_COMPUTE_UNUSED(beta); |
| 90 | return Status{}; |
| 91 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 92 | } // namespace |
| 93 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 94 | CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame^] | 95 | : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _ma_kernel(), _tmp_a(), _tmp_b(), _original_b(nullptr), _is_interleaved_transposed(false), |
| 96 | _run_addition(false), _is_first_run(true), _reshape_b_only_on_first_run(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 100 | void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 102 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 104 | // Perform validation step |
| 105 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(a->info(), b->info(), c, output->info(), alpha, beta, gemm_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame^] | 107 | // Store original b matrix |
| 108 | _original_b = b; |
| 109 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 110 | // Check if we need to reshape the matrix B only on the first run |
| 111 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 112 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 113 | const ICLTensor *matrix_a = a; |
| 114 | const ICLTensor *matrix_b = b; |
| 115 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 116 | // Get the GPU target |
| 117 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 118 | |
| 119 | // Set the target for the kernels |
| 120 | _interleave_kernel.set_target(gpu_target); |
| 121 | _mm_kernel.set_target(gpu_target); |
| 122 | |
| 123 | // Arguments used by GEMMReshapeInfo |
| 124 | // 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 |
| 125 | // in order to know how the matrices have been reshaped |
| 126 | const int m = a->info()->dimension(1); |
| 127 | const int n = b->info()->dimension(0); |
| 128 | const int k = a->info()->dimension(0); |
| 129 | int mult_transpose1xW_width = 1; |
| 130 | int mult_interleave4x4_height = 1; |
| 131 | |
Sam Laynton | 56e8e86 | 2018-04-05 13:26:08 +0100 | [diff] [blame] | 132 | if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX)) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 133 | { |
| 134 | mult_transpose1xW_width = 4; |
| 135 | mult_interleave4x4_height = 2; |
| 136 | } |
| 137 | |
| 138 | // Check if we need to reshape the matrix A and matrix B |
| 139 | _is_interleaved_transposed = is_interleaved_transposed(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target); |
Gian Marco | b5311a6 | 2017-12-13 12:48:03 +0000 | [diff] [blame] | 140 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 141 | if(_is_interleaved_transposed) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 143 | matrix_a = &_tmp_a; |
| 144 | matrix_b = &_tmp_b; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 146 | // Manage intermediate buffers |
| 147 | _memory_group.manage(&_tmp_a); |
Georgios Pinitas | ae4ce7b | 2018-03-19 17:50:45 +0000 | [diff] [blame] | 148 | if(!_reshape_b_only_on_first_run) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 149 | { |
| 150 | _memory_group.manage(&_tmp_b); |
| 151 | } |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 152 | // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 153 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | // Configure interleave kernel |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 155 | _interleave_kernel.configure(a, &_tmp_a, mult_interleave4x4_height); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | |
| 157 | // Configure transpose kernel |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 158 | _transpose_kernel.configure(b, &_tmp_b, mult_transpose1xW_width); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 159 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 161 | _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height)); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 162 | |
| 163 | if(_is_interleaved_transposed) |
| 164 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | // Allocate intermediate tensors |
| 166 | _tmp_a.allocator()->allocate(); |
| 167 | _tmp_b.allocator()->allocate(); |
| 168 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | |
| 170 | // Configure matrix addition kernel |
| 171 | if(beta != 0 && c != nullptr) |
| 172 | { |
| 173 | _ma_kernel.configure(c, output, beta); |
| 174 | _run_addition = true; |
| 175 | } |
| 176 | } |
| 177 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 178 | 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) |
| 179 | { |
| 180 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(a, b, c, output, alpha, beta, gemm_info)); |
| 181 | return Status{}; |
| 182 | } |
| 183 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | void CLGEMM::run() |
| 185 | { |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 186 | _memory_group.acquire(); |
| 187 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 188 | if(_is_interleaved_transposed) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | { |
| 190 | // Run interleave kernel |
| 191 | CLScheduler::get().enqueue(_interleave_kernel, false); |
| 192 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 193 | if(_is_first_run) |
| 194 | { |
| 195 | // Run transpose kernel |
| 196 | CLScheduler::get().enqueue(_transpose_kernel, false); |
| 197 | |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame^] | 198 | // Mark original b matrix as unused |
| 199 | if(_reshape_b_only_on_first_run) |
| 200 | { |
| 201 | _original_b->mark_as_unused(); |
| 202 | } |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 203 | } |
| 204 | else if(!_reshape_b_only_on_first_run) |
| 205 | { |
| 206 | // Run transpose kernel |
| 207 | CLScheduler::get().enqueue(_transpose_kernel, false); |
| 208 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Run matrix multiply kernel |
| 212 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 213 | |
| 214 | // Run matrix addition kernel |
| 215 | if(_run_addition) |
| 216 | { |
| 217 | CLScheduler::get().enqueue(_ma_kernel); |
| 218 | } |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 219 | |
| 220 | _memory_group.release(); |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame^] | 221 | |
| 222 | _is_first_run = false; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | } |