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" |
Gunes Bayir | 85cafff | 2023-12-18 13:29:31 +0000 | [diff] [blame^] | 37 | #include "src/runtime/heuristics/matmul_native/ClMatMulNativeKernelVariant.h" |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 38 | #include "src/runtime/heuristics/matmul_native/IClMatMulNativeKernelConfig.h" |
| 39 | |
| 40 | using namespace arm_compute::cl_matmul; |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace opencl |
| 45 | { |
| 46 | using namespace arm_compute::opencl::kernels; |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 47 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 48 | ClMatMul::ClMatMul() |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 49 | { |
| 50 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 51 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | Status ClMatMul::validate(const ITensorInfo *lhs, |
| 53 | const ITensorInfo *rhs, |
| 54 | const ITensorInfo *dst, |
| 55 | const MatMulInfo &matmul_info, |
| 56 | const ActivationLayerInfo &act_info) |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 57 | { |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 60 | DataType::F16, DataType::F32); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 62 | DataType::F16, DataType::F32); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 63 | |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 64 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 65 | |
| 66 | std::unique_ptr<IClMatMulNativeKernelConfig> t = ClMatMulNativeKernelConfigurationFactory::create(gpu_target); |
| 67 | |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 68 | const MatMulKernelInfo kernel_info = t->configure(lhs, rhs, matmul_info); |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 69 | |
Gunes Bayir | 85cafff | 2023-12-18 13:29:31 +0000 | [diff] [blame^] | 70 | const auto kernel_selector = ClMatMulNativeKernelVariantFactory::create(gpu_target); |
| 71 | const MatMulKernelType kernel_type = kernel_selector->select_kernel(lhs, rhs, matmul_info, act_info); |
| 72 | |
| 73 | switch (kernel_type) |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 74 | { |
| 75 | case MatMulKernelType::NATIVE_FP: |
| 76 | return ClMatMulNativeKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 77 | case MatMulKernelType::NATIVE_MMUL_FP: |
| 78 | return ClMatMulNativeMMULKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info); |
| 79 | case MatMulKernelType::NATIVE_QUANTIZED: |
| 80 | return ClMatMulLowpNativeKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 81 | case MatMulKernelType::NATIVE_MMUL_QUANTIZED: |
| 82 | return ClMatMulLowpNativeMMULKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 83 | default: |
| 84 | ARM_COMPUTE_ERROR("Unsupported MatMul Kernel!"); |
| 85 | } |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 86 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | void ClMatMul::configure(const CLCompileContext &compile_context, |
| 89 | ITensorInfo *lhs, |
| 90 | ITensorInfo *rhs, |
| 91 | ITensorInfo *dst, |
| 92 | const MatMulInfo &matmul_info, |
| 93 | const ActivationLayerInfo &act_info) |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 94 | { |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst); |
| 96 | ARM_COMPUTE_LOG_PARAMS(lhs, rhs, dst, matmul_info); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 97 | |
| 98 | // Perform validation step |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, dst, matmul_info)); |
| 100 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 101 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 102 | const auto kernel_config = ClMatMulNativeKernelConfigurationFactory::create(gpu_target); |
| 103 | const MatMulKernelInfo kernel_info = kernel_config->configure(lhs, rhs, matmul_info); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 104 | |
Gunes Bayir | 85cafff | 2023-12-18 13:29:31 +0000 | [diff] [blame^] | 105 | const auto kernel_selector = ClMatMulNativeKernelVariantFactory::create(gpu_target); |
| 106 | const MatMulKernelType kernel_type = kernel_selector->select_kernel(lhs, rhs, matmul_info, act_info); |
| 107 | |
| 108 | switch (kernel_type) |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 109 | { |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 110 | case MatMulKernelType::NATIVE_FP: |
| 111 | { |
| 112 | auto kernel = std::make_unique<ClMatMulNativeKernel>(); |
| 113 | kernel->set_target(gpu_target); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 114 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 115 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 116 | _matmul_kernel = std::move(kernel); |
| 117 | } |
| 118 | break; |
| 119 | case MatMulKernelType::NATIVE_MMUL_FP: |
| 120 | { |
| 121 | auto kernel = std::make_unique<ClMatMulNativeMMULKernel>(); |
| 122 | kernel->set_target(gpu_target); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 123 | |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 124 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info); |
| 125 | _matmul_kernel = std::move(kernel); |
| 126 | } |
| 127 | break; |
| 128 | case MatMulKernelType::NATIVE_QUANTIZED: |
| 129 | { |
| 130 | auto kernel = std::make_unique<ClMatMulLowpNativeKernel>(); |
| 131 | kernel->set_target(gpu_target); |
| 132 | |
| 133 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 134 | _matmul_kernel = std::move(kernel); |
| 135 | } |
| 136 | break; |
| 137 | case MatMulKernelType::NATIVE_MMUL_QUANTIZED: |
| 138 | { |
| 139 | auto kernel = std::make_unique<ClMatMulLowpNativeMMULKernel>(); |
| 140 | kernel->set_target(gpu_target); |
| 141 | |
| 142 | kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info); |
| 143 | _matmul_kernel = std::move(kernel); |
| 144 | } |
| 145 | break; |
| 146 | default: |
| 147 | ARM_COMPUTE_ERROR("Unsupported MatMul Kernel!"); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 148 | } |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 149 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 150 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 151 | void ClMatMul::run(ITensorPack &tensors) |
| 152 | { |
Gunes Bayir | c1204c7 | 2023-10-10 17:41:56 +0100 | [diff] [blame] | 153 | CLScheduler::get().enqueue_op(*_matmul_kernel, tensors, /* flush */ true); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 154 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 155 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 156 | } // namespace opencl |
| 157 | } // namespace arm_compute |