Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/GPUTarget.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
Gian Marco Iodice | bb36a8e | 2018-04-19 12:05:08 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/CL/gemm_reshaped/CLGEMMReshapedConfiguration.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 38 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 41 | using namespace arm_compute::misc::shape_calculator; |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 42 | using namespace arm_compute::cl_gemm; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 44 | namespace |
| 45 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 46 | inline bool is_interleaved_transposed(unsigned int m, unsigned int n, unsigned int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 47 | { |
| 48 | bool flag = true; |
| 49 | |
Georgios Pinitas | a34286e | 2018-09-04 12:18:50 +0100 | [diff] [blame] | 50 | if(gpu_target_is_in(gpu_target, GPUTarget::G52, GPUTarget::G52LIT, GPUTarget::G71, GPUTarget::G72, GPUTarget::G76)) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 51 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 52 | if((m > 1) && n < 16) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 53 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 54 | flag = true; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 55 | } |
| 56 | else |
| 57 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 58 | // COMPMID-852 |
| 59 | if(k > 256 && m > 4 && is_data_type_float(data_type) && reshape_b_only_on_first_run) |
| 60 | { |
| 61 | constexpr float alpha = 3.2f; |
| 62 | constexpr float fact0 = 1.51f; |
| 63 | constexpr float fact1 = 1.66f; |
| 64 | constexpr float ops = 12.0f; |
| 65 | const float scale = k > 1024 ? 1.07f : 1.0f; |
| 66 | flag = alpha + ((n * fact0) / ops) < ((fact1 * n * scale) / ops); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | flag = false; |
| 71 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
Gian Marco Iodice | cda0c38 | 2018-04-23 16:16:22 +0100 | [diff] [blame] | 74 | else |
| 75 | { |
| 76 | // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once |
| 77 | flag = m != 1 && reshape_b_only_on_first_run; |
| 78 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 79 | |
| 80 | return flag; |
| 81 | } |
| 82 | } // namespace |
| 83 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 84 | CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 85 | : _memory_group(std::move(memory_manager)), |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 86 | _mm_kernel(), |
| 87 | _ma_kernel(), |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 88 | _reshape_lhs_kernel(), |
| 89 | _reshape_rhs_kernel(), |
| 90 | _mm_reshaped_kernel(), |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 91 | _tmp_a(), |
| 92 | _tmp_b(), |
| 93 | _original_b(nullptr), |
| 94 | _is_interleaved_transposed(false), |
| 95 | _run_addition(false), |
| 96 | _reshape_b_only_on_first_run(false), |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 97 | _is_prepared(false), |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 98 | _is_new_gemm_reshaped(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | { |
| 100 | } |
| 101 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 102 | 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] | 103 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 104 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 106 | // Perform validation step |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 109 | // Check if we need to reshape the matrix B only on the first run |
| 110 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Michele Di Giorgio | ba1ffe9 | 2018-08-22 14:28:30 +0100 | [diff] [blame] | 111 | _is_prepared = gemm_info.retain_internal_weights(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 112 | _original_b = b; |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 113 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 114 | const ICLTensor *matrix_a = a; |
| 115 | const ICLTensor *matrix_b = b; |
| 116 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 117 | // Get the GPU target |
| 118 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 119 | |
| 120 | // Set the target for the kernels |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 121 | _reshape_lhs_kernel.set_target(gpu_target); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 122 | _mm_kernel.set_target(gpu_target); |
| 123 | |
| 124 | // Arguments used by GEMMReshapeInfo |
| 125 | // 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 |
| 126 | // in order to know how the matrices have been reshaped |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 127 | DataType data_type = a->info()->data_type(); |
| 128 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 129 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 130 | const unsigned int n = b->info()->dimension(0); |
| 131 | const unsigned int k = a->info()->dimension(0); |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 132 | const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 133 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 134 | int mult_transpose1xW_width = 1; |
| 135 | int mult_interleave4x4_height = 1; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 136 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 137 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 138 | { |
| 139 | mult_transpose1xW_width = 4; |
| 140 | mult_interleave4x4_height = 2; |
| 141 | } |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 142 | GEMMRHSMatrixInfo rhs_info; |
| 143 | rhs_info.n0 = 16 / b->info()->element_size(); |
| 144 | rhs_info.k0 = 1; |
| 145 | rhs_info.h0 = mult_transpose1xW_width; |
| 146 | rhs_info.interleave = false; |
| 147 | rhs_info.transpose = false; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 148 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 149 | GEMMLHSMatrixInfo lhs_info; |
| 150 | lhs_info.m0 = 4; |
| 151 | lhs_info.k0 = 4; |
| 152 | lhs_info.v0 = mult_interleave4x4_height; |
| 153 | lhs_info.interleave = true; |
| 154 | lhs_info.transpose = true; |
| 155 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 156 | // Check if we need to reshape the matrix A and matrix B |
| 157 | _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] | 158 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 159 | // Check if we can run the new reshaped GEMM |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 160 | const auto workload = static_cast<float>((m * n) / 20.0f); |
| 161 | _is_new_gemm_reshaped = (workload > 1600.0f) && (get_arch_from_target(gpu_target) == GPUTarget::BIFROST) && _is_interleaved_transposed && (data_type == DataType::F32); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 162 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 163 | // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
| 164 | if(_is_interleaved_transposed) |
| 165 | { |
| 166 | reinterpret_input_as_3d = false; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 167 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 168 | matrix_a = &_tmp_a; |
| 169 | matrix_b = &_tmp_b; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 171 | // Manage intermediate buffers |
| 172 | _memory_group.manage(&_tmp_a); |
Georgios Pinitas | ae4ce7b | 2018-03-19 17:50:45 +0000 | [diff] [blame] | 173 | if(!_reshape_b_only_on_first_run) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 174 | { |
| 175 | _memory_group.manage(&_tmp_b); |
| 176 | } |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 177 | // _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] | 178 | |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 179 | if(_is_new_gemm_reshaped) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 180 | { |
| 181 | GEMMLHSMatrixInfo lhs_info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 183 | // Pick up the GEMM configuration |
| 184 | std::tie(lhs_info, rhs_info) = CLGEMMReshapedConfigurationFactory::create()->configure(m, n, k, batch_size, data_type); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 185 | |
| 186 | _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, gemm_info.reinterpret_input_as_3d()); |
| 187 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
| 188 | |
| 189 | // Configure and tune matrix multiply kernel |
| 190 | _mm_reshaped_kernel.configure(matrix_a, matrix_b, output, alpha, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, |
| 191 | depth_output_gemm3d, reinterpret_input_as_3d)); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | // Configure interleave kernel |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 196 | _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, gemm_info.reinterpret_input_as_3d()); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 197 | // Configure transpose kernel |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 198 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 199 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 200 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 202 | if(!_is_new_gemm_reshaped) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 203 | { |
| 204 | // Configure and tune matrix multiply kernel |
| 205 | _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, |
| 206 | mult_transpose1xW_width, mult_interleave4x4_height, |
| 207 | depth_output_gemm3d, reinterpret_input_as_3d), |
| 208 | gemm_info.fp_mixed_precision()); |
| 209 | CLScheduler::get().tune_kernel_static(_mm_kernel); |
| 210 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 211 | |
| 212 | if(_is_interleaved_transposed) |
| 213 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | // Allocate intermediate tensors |
| 215 | _tmp_a.allocator()->allocate(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 216 | if(!_reshape_b_only_on_first_run) |
| 217 | { |
| 218 | _tmp_b.allocator()->allocate(); |
| 219 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 221 | |
| 222 | // Configure matrix addition kernel |
| 223 | if(beta != 0 && c != nullptr) |
| 224 | { |
| 225 | _ma_kernel.configure(c, output, beta); |
| 226 | _run_addition = true; |
| 227 | } |
| 228 | } |
| 229 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 230 | Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 231 | { |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 232 | ARM_COMPUTE_UNUSED(alpha); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 233 | ARM_COMPUTE_UNUSED(output); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 234 | |
| 235 | // Check if we need to reshape the matrix B only on the first run |
| 236 | const bool reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
| 237 | |
| 238 | const ITensorInfo *matrix_a_info = a; |
| 239 | const ITensorInfo *matrix_b_info = b; |
| 240 | |
| 241 | TensorInfo tmp_a_info{}; |
| 242 | TensorInfo tmp_b_info{}; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 243 | |
| 244 | // Get the GPU target |
| 245 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 246 | |
| 247 | // Arguments used by GEMMReshapeInfo |
| 248 | // 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 |
| 249 | // in order to know how the matrices have been reshaped |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 250 | DataType data_type = a->data_type(); |
| 251 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 252 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 253 | const unsigned int n = b->dimension(0); |
| 254 | const unsigned int k = a->dimension(0); |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 255 | const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 256 | int mult_transpose1xW_width = 1; |
| 257 | int mult_interleave4x4_height = 1; |
| 258 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 259 | |
| 260 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
| 261 | { |
| 262 | mult_transpose1xW_width = 4; |
| 263 | mult_interleave4x4_height = 2; |
| 264 | } |
| 265 | |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 266 | GEMMRHSMatrixInfo rhs_info; |
| 267 | rhs_info.n0 = 16 / b->element_size(); |
| 268 | rhs_info.k0 = 1; |
| 269 | rhs_info.h0 = mult_transpose1xW_width; |
| 270 | rhs_info.interleave = false; |
| 271 | rhs_info.transpose = false; |
| 272 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 273 | GEMMLHSMatrixInfo lhs_info; |
| 274 | lhs_info.m0 = 4; |
| 275 | lhs_info.k0 = 4; |
| 276 | lhs_info.v0 = mult_interleave4x4_height; |
| 277 | lhs_info.interleave = true; |
| 278 | lhs_info.transpose = true; |
| 279 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 280 | // Check if we need to reshape the matrix A and matrix B |
| 281 | const bool run_interleave_transpose = is_interleaved_transposed(m, n, k, a->data_type(), reshape_b_only_on_first_run, gpu_target); |
| 282 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 283 | // Check if we can run the new reshaped GEMM |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 284 | const auto workload = static_cast<float>((m * n) / 20.0f); |
| 285 | const bool is_new_gemm_reshaped = (workload > 1600.f) && (get_arch_from_target(gpu_target) == GPUTarget::BIFROST) && run_interleave_transpose && (data_type == DataType::F32); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 286 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 287 | // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
| 288 | if(run_interleave_transpose) |
| 289 | { |
| 290 | reinterpret_input_as_3d = false; |
| 291 | } |
| 292 | |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 293 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, reinterpret_input_as_3d); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 294 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 295 | if(run_interleave_transpose) |
| 296 | { |
| 297 | matrix_a_info = &tmp_a_info; |
| 298 | matrix_b_info = &tmp_b_info; |
| 299 | |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 300 | if(is_new_gemm_reshaped) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 301 | { |
| 302 | GEMMLHSMatrixInfo lhs_info; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 303 | |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 304 | // Pick up the GEMM configuration |
| 305 | std::tie(lhs_info, rhs_info) = CLGEMMReshapedConfigurationFactory::create()->configure(m, n, k, batch_size, data_type); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 306 | |
| 307 | auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*a, lhs_info, gemm_info.reinterpret_input_as_3d()))); |
| 308 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d())); |
| 309 | |
| 310 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 311 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
| 312 | |
| 313 | // Validate matrix multiply |
| 314 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyReshapedKernel::validate(matrix_a_info, matrix_b_info, output, alpha, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, |
| 315 | depth_output_gemm3d, reinterpret_input_as_3d))); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | // Validate interleave kernel |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 320 | auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*a, lhs_info, gemm_info.reinterpret_input_as_3d()))); |
| 321 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d())); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 322 | // Validate transpose kernel |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 323 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 324 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 325 | } |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 328 | if(!is_new_gemm_reshaped) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 329 | { |
| 330 | // Validate matrix multiply |
| 331 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, alpha, run_interleave_transpose, reshape_info, gpu_target, gemm_info.fp_mixed_precision())); |
| 332 | } |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 333 | |
| 334 | if(beta != 0 && c != nullptr) |
| 335 | { |
| 336 | // Validate matrix addition kernel |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 337 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta)); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 340 | return Status{}; |
| 341 | } |
| 342 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 343 | void CLGEMM::run() |
| 344 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 345 | prepare(); |
| 346 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 347 | _memory_group.acquire(); |
| 348 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 349 | if(_is_interleaved_transposed) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 350 | { |
| 351 | // Run interleave kernel |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 352 | CLScheduler::get().enqueue(_reshape_lhs_kernel, false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 353 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 354 | if(!_reshape_b_only_on_first_run) |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 355 | { |
| 356 | // Run transpose kernel |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 357 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 358 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Run matrix multiply kernel |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 362 | if(_is_new_gemm_reshaped) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 363 | { |
| 364 | CLScheduler::get().enqueue(_mm_reshaped_kernel, !_run_addition); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 369 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 370 | |
| 371 | // Run matrix addition kernel |
| 372 | if(_run_addition) |
| 373 | { |
| 374 | CLScheduler::get().enqueue(_ma_kernel); |
| 375 | } |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 376 | |
| 377 | _memory_group.release(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 378 | } |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 379 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 380 | void CLGEMM::prepare() |
| 381 | { |
| 382 | if(!_is_prepared) |
| 383 | { |
| 384 | if(_is_interleaved_transposed && _reshape_b_only_on_first_run) |
| 385 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 386 | // Run transpose kernel and mark original weights tensor as unused |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 387 | _tmp_b.allocator()->allocate(); |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 388 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 389 | _original_b->mark_as_unused(); |
| 390 | } |
| 391 | CLScheduler::get().queue().finish(); |
| 392 | _is_prepared = true; |
| 393 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 394 | } |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 395 | } // namespace arm_compute |