blob: 9962ee550acdd599f48754566d3cdd8d0cb87fa3 [file] [log] [blame]
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +00001/*
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 Sujake9b3ee22023-04-17 12:08:48 +010025
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000026#include "arm_compute/core/Error.h"
Jakub Sujake9b3ee22023-04-17 12:08:48 +010027#include "arm_compute/core/Utils.h"
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000028#include "arm_compute/runtime/CL/CLScheduler.h"
Jakub Sujake9b3ee22023-04-17 12:08:48 +010029
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000030#include "src/common/utils/Log.h"
Gunes Bayirc1204c72023-10-10 17:41:56 +010031#include "src/gpu/cl/kernels/ClMatMulLowpNativeKernel.h"
32#include "src/gpu/cl/kernels/ClMatMulLowpNativeMMULKernel.h"
Jakub Sujak1ed6a142023-04-13 21:14:42 +010033#include "src/gpu/cl/kernels/ClMatMulNativeKernel.h"
Gunes Bayirc1204c72023-10-10 17:41:56 +010034#include "src/gpu/cl/kernels/ClMatMulNativeMMULKernel.h"
Gian Marco Iodice352c07d2023-05-03 12:21:38 +010035#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
39using namespace arm_compute::cl_matmul;
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000040
41namespace arm_compute
42{
43namespace opencl
44{
Gunes Bayirc1204c72023-10-10 17:41:56 +010045namespace
46{
47enum 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
62MatMulKernelType 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 Raj69766d62023-11-21 11:19:50 +000094
95 return is_quantized ? MatMulKernelType::NATIVE_QUANTIZED : MatMulKernelType::NATIVE_FP;
Gunes Bayirc1204c72023-10-10 17:41:56 +010096}
97} // namespace
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000098using namespace arm_compute::opencl::kernels;
Jakub Sujake9b3ee22023-04-17 12:08:48 +010099
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000100ClMatMul::ClMatMul()
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000101{
102}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100103
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104Status ClMatMul::validate(const ITensorInfo *lhs,
105 const ITensorInfo *rhs,
106 const ITensorInfo *dst,
107 const MatMulInfo &matmul_info,
108 const ActivationLayerInfo &act_info)
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000109{
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100110 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111 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 Sujake9b3ee22023-04-17 12:08:48 +0100115
Gian Marco Iodice352c07d2023-05-03 12:21:38 +0100116 const GPUTarget gpu_target = CLScheduler::get().target();
117
118 std::unique_ptr<IClMatMulNativeKernelConfig> t = ClMatMulNativeKernelConfigurationFactory::create(gpu_target);
119
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +0100120 const MatMulKernelInfo kernel_info = t->configure(lhs, rhs, matmul_info);
Gian Marco Iodice352c07d2023-05-03 12:21:38 +0100121
Gunes Bayirc1204c72023-10-10 17:41:56 +0100122 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 Elgammalf26ea2f2023-03-24 11:42:03 +0000135}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100136
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137void 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 Elgammalf26ea2f2023-03-24 11:42:03 +0000143{
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100144 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
145 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, dst, matmul_info);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000146
147 // Perform validation step
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100148 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, dst, matmul_info));
149
Gunes Bayirc1204c72023-10-10 17:41:56 +0100150 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 Sujake9b3ee22023-04-17 12:08:48 +0100153
Gunes Bayirc1204c72023-10-10 17:41:56 +0100154 switch (get_matmul_kernel(lhs, rhs, matmul_info, act_info))
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100155 {
Gunes Bayirc1204c72023-10-10 17:41:56 +0100156 case MatMulKernelType::NATIVE_FP:
157 {
158 auto kernel = std::make_unique<ClMatMulNativeKernel>();
159 kernel->set_target(gpu_target);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000160
Gunes Bayirc1204c72023-10-10 17:41:56 +0100161 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 Sujake9b3ee22023-04-17 12:08:48 +0100169
Gunes Bayirc1204c72023-10-10 17:41:56 +0100170 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 Sujake9b3ee22023-04-17 12:08:48 +0100194 }
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000195}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100196
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000197void ClMatMul::run(ITensorPack &tensors)
198{
Gunes Bayirc1204c72023-10-10 17:41:56 +0100199 CLScheduler::get().enqueue_op(*_matmul_kernel, tensors, /* flush */ true);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000200}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100201
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000202} // namespace opencl
203} // namespace arm_compute