Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 Arm Limited. |
| 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 "src/gpu/cl/operators/ClMatMul.h" |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 25 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Utils.h" |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 29 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 30 | #include "src/common/utils/Log.h" |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 31 | #include "src/gpu/cl/kernels/ClMatMulLowpNativeKernel.h" |
| 32 | #include "src/gpu/cl/kernels/ClMatMulLowpNativeMMULKernel.h" |
Jakub Sujak | 1ed6a14 | 2023-04-13 21:14:42 +0100 | [diff] [blame] | 33 | #include "src/gpu/cl/kernels/ClMatMulNativeKernel.h" |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 34 | #include "src/gpu/cl/kernels/ClMatMulNativeMMULKernel.h" |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 35 | #include "src/runtime/heuristics/matmul_native/ClMatMulNativeDefaultConfigValhall.h" |
| 36 | #include "src/runtime/heuristics/matmul_native/ClMatMulNativeKernelConfig.h" |
| 37 | #include "src/runtime/heuristics/matmul_native/IClMatMulNativeKernelConfig.h" |
| 38 | |
| 39 | using namespace arm_compute::cl_matmul; |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace opencl |
| 44 | { |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 45 | namespace |
| 46 | { |
| 47 | enum class MatMulKernelType |
| 48 | { |
| 49 | /** Native matrix multiplication for FP types */ |
| 50 | NATIVE_FP, |
| 51 | |
| 52 | /** Native matrix multiplication for quantized types */ |
| 53 | NATIVE_QUANTIZED, |
| 54 | |
| 55 | /** Native matrix multiplication using MMUL extension for FP types */ |
| 56 | NATIVE_MMUL_FP, |
| 57 | |
| 58 | /** Native matrix multiplication using MMUL extension for Quantized types */ |
| 59 | NATIVE_MMUL_QUANTIZED |
| 60 | }; |
| 61 | |
| 62 | MatMulKernelType get_matmul_kernel(const ITensorInfo *lhs, |
| 63 | const ITensorInfo *rhs, |
| 64 | const MatMulInfo &matmul_info, |
| 65 | const ActivationLayerInfo &act_info) |
| 66 | { |
| 67 | ARM_COMPUTE_UNUSED(lhs, rhs, matmul_info, act_info); |
| 68 | |
| 69 | const bool is_quantized = is_data_type_quantized_asymmetric(lhs->data_type()); |
| 70 | const bool is_mmul_supported = arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()); |
| 71 | |
| 72 | const int k = matmul_info.adj_lhs() ? lhs->tensor_shape().y() : lhs->tensor_shape().x(); |
| 73 | |
| 74 | if (is_quantized) |
| 75 | { |
| 76 | // MMUL kernel works only when K is a multiple of 16 |
| 77 | if (is_mmul_supported && !act_info.enabled() && k % 16 == 0) |
| 78 | { |
| 79 | return MatMulKernelType::NATIVE_MMUL_QUANTIZED; |
| 80 | } |
| 81 | |
| 82 | return MatMulKernelType::NATIVE_QUANTIZED; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | // MMUL kernel works only when K is a multiple of 4 |
| 87 | if (is_mmul_supported && !act_info.enabled() && k % 4 == 0) |
| 88 | { |
| 89 | return MatMulKernelType::NATIVE_MMUL_FP; |
| 90 | } |
| 91 | |
| 92 | return MatMulKernelType::NATIVE_FP; |
| 93 | } |
Anitha Raj | 69766d6 | 2023-11-21 11:19:50 +0000 | [diff] [blame] | 94 | |
| 95 | return is_quantized ? MatMulKernelType::NATIVE_QUANTIZED : MatMulKernelType::NATIVE_FP; |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 96 | } |
| 97 | } // namespace |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 98 | using namespace arm_compute::opencl::kernels; |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 99 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 100 | ClMatMul::ClMatMul() |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 101 | { |
| 102 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 103 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 104 | Status ClMatMul::validate(const ITensorInfo *lhs, |
| 105 | const ITensorInfo *rhs, |
| 106 | const ITensorInfo *dst, |
| 107 | const MatMulInfo &matmul_info, |
| 108 | const ActivationLayerInfo &act_info) |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 109 | { |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 110 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 111 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 112 | DataType::F16, DataType::F32); |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 114 | DataType::F16, DataType::F32); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 115 | |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 116 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 117 | |
| 118 | std::unique_ptr<IClMatMulNativeKernelConfig> t = ClMatMulNativeKernelConfigurationFactory::create(gpu_target); |
| 119 | |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 120 | const MatMulKernelInfo kernel_info = t->configure(lhs, rhs, matmul_info); |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 121 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 122 | switch (get_matmul_kernel(lhs, rhs, matmul_info, act_info)) |
| 123 | { |
| 124 | case MatMulKernelType::NATIVE_FP: |
| 125 | return ClMatMulNativeKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 126 | case MatMulKernelType::NATIVE_MMUL_FP: |
| 127 | return ClMatMulNativeMMULKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info); |
| 128 | case MatMulKernelType::NATIVE_QUANTIZED: |
| 129 | return ClMatMulLowpNativeKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 130 | case MatMulKernelType::NATIVE_MMUL_QUANTIZED: |
| 131 | return ClMatMulLowpNativeMMULKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 132 | default: |
| 133 | ARM_COMPUTE_ERROR("Unsupported MatMul Kernel!"); |
| 134 | } |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 135 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 136 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 137 | void ClMatMul::configure(const CLCompileContext &compile_context, |
| 138 | ITensorInfo *lhs, |
| 139 | ITensorInfo *rhs, |
| 140 | ITensorInfo *dst, |
| 141 | const MatMulInfo &matmul_info, |
| 142 | const ActivationLayerInfo &act_info) |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 143 | { |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst); |
| 145 | ARM_COMPUTE_LOG_PARAMS(lhs, rhs, dst, matmul_info); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 146 | |
| 147 | // Perform validation step |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 148 | ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, dst, matmul_info)); |
| 149 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 150 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 151 | const auto kernel_config = ClMatMulNativeKernelConfigurationFactory::create(gpu_target); |
| 152 | const MatMulKernelInfo kernel_info = kernel_config->configure(lhs, rhs, matmul_info); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 153 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 154 | switch (get_matmul_kernel(lhs, rhs, matmul_info, act_info)) |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 155 | { |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 156 | case MatMulKernelType::NATIVE_FP: |
| 157 | { |
| 158 | auto kernel = std::make_unique<ClMatMulNativeKernel>(); |
| 159 | kernel->set_target(gpu_target); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 160 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 161 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 162 | _matmul_kernel = std::move(kernel); |
| 163 | } |
| 164 | break; |
| 165 | case MatMulKernelType::NATIVE_MMUL_FP: |
| 166 | { |
| 167 | auto kernel = std::make_unique<ClMatMulNativeMMULKernel>(); |
| 168 | kernel->set_target(gpu_target); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 169 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 170 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info); |
| 171 | _matmul_kernel = std::move(kernel); |
| 172 | } |
| 173 | break; |
| 174 | case MatMulKernelType::NATIVE_QUANTIZED: |
| 175 | { |
| 176 | auto kernel = std::make_unique<ClMatMulLowpNativeKernel>(); |
| 177 | kernel->set_target(gpu_target); |
| 178 | |
| 179 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 180 | _matmul_kernel = std::move(kernel); |
| 181 | } |
| 182 | break; |
| 183 | case MatMulKernelType::NATIVE_MMUL_QUANTIZED: |
| 184 | { |
| 185 | auto kernel = std::make_unique<ClMatMulLowpNativeMMULKernel>(); |
| 186 | kernel->set_target(gpu_target); |
| 187 | |
| 188 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 189 | _matmul_kernel = std::move(kernel); |
| 190 | } |
| 191 | break; |
| 192 | default: |
| 193 | ARM_COMPUTE_ERROR("Unsupported MatMul Kernel!"); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 194 | } |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 195 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 196 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 197 | void ClMatMul::run(ITensorPack &tensors) |
| 198 | { |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 199 | CLScheduler::get().enqueue_op(*_matmul_kernel, tensors, /* flush */ true); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 200 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 201 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 202 | } // namespace opencl |
| 203 | } // namespace arm_compute |