blob: 43303001d04787751a3218fc78d29d717842a633 [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 }
Gunes Bayirc1204c72023-10-10 17:41:56 +010094}
95} // namespace
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000096using namespace arm_compute::opencl::kernels;
Jakub Sujake9b3ee22023-04-17 12:08:48 +010097
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000098ClMatMul::ClMatMul()
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000099{
100}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100101
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102Status ClMatMul::validate(const ITensorInfo *lhs,
103 const ITensorInfo *rhs,
104 const ITensorInfo *dst,
105 const MatMulInfo &matmul_info,
106 const ActivationLayerInfo &act_info)
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000107{
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100108 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
110 DataType::F16, DataType::F32);
111 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
112 DataType::F16, DataType::F32);
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100113
Gian Marco Iodice352c07d2023-05-03 12:21:38 +0100114 const GPUTarget gpu_target = CLScheduler::get().target();
115
116 std::unique_ptr<IClMatMulNativeKernelConfig> t = ClMatMulNativeKernelConfigurationFactory::create(gpu_target);
117
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +0100118 const MatMulKernelInfo kernel_info = t->configure(lhs, rhs, matmul_info);
Gian Marco Iodice352c07d2023-05-03 12:21:38 +0100119
Gunes Bayirc1204c72023-10-10 17:41:56 +0100120 switch (get_matmul_kernel(lhs, rhs, matmul_info, act_info))
121 {
122 case MatMulKernelType::NATIVE_FP:
123 return ClMatMulNativeKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info);
124 case MatMulKernelType::NATIVE_MMUL_FP:
125 return ClMatMulNativeMMULKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info);
126 case MatMulKernelType::NATIVE_QUANTIZED:
127 return ClMatMulLowpNativeKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info);
128 case MatMulKernelType::NATIVE_MMUL_QUANTIZED:
129 return ClMatMulLowpNativeMMULKernel::validate(lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info);
130 default:
131 ARM_COMPUTE_ERROR("Unsupported MatMul Kernel!");
132 }
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000133}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100134
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135void ClMatMul::configure(const CLCompileContext &compile_context,
136 ITensorInfo *lhs,
137 ITensorInfo *rhs,
138 ITensorInfo *dst,
139 const MatMulInfo &matmul_info,
140 const ActivationLayerInfo &act_info)
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000141{
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100142 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
143 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, dst, matmul_info);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000144
145 // Perform validation step
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100146 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, dst, matmul_info));
147
Gunes Bayirc1204c72023-10-10 17:41:56 +0100148 const GPUTarget gpu_target = CLScheduler::get().target();
149 const auto kernel_config = ClMatMulNativeKernelConfigurationFactory::create(gpu_target);
150 const MatMulKernelInfo kernel_info = kernel_config->configure(lhs, rhs, matmul_info);
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100151
Gunes Bayirc1204c72023-10-10 17:41:56 +0100152 switch (get_matmul_kernel(lhs, rhs, matmul_info, act_info))
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100153 {
Gunes Bayirc1204c72023-10-10 17:41:56 +0100154 case MatMulKernelType::NATIVE_FP:
155 {
156 auto kernel = std::make_unique<ClMatMulNativeKernel>();
157 kernel->set_target(gpu_target);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000158
Gunes Bayirc1204c72023-10-10 17:41:56 +0100159 kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info);
160 _matmul_kernel = std::move(kernel);
161 }
162 break;
163 case MatMulKernelType::NATIVE_MMUL_FP:
164 {
165 auto kernel = std::make_unique<ClMatMulNativeMMULKernel>();
166 kernel->set_target(gpu_target);
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100167
Gunes Bayirc1204c72023-10-10 17:41:56 +0100168 kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info);
169 _matmul_kernel = std::move(kernel);
170 }
171 break;
172 case MatMulKernelType::NATIVE_QUANTIZED:
173 {
174 auto kernel = std::make_unique<ClMatMulLowpNativeKernel>();
175 kernel->set_target(gpu_target);
176
177 kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info);
178 _matmul_kernel = std::move(kernel);
179 }
180 break;
181 case MatMulKernelType::NATIVE_MMUL_QUANTIZED:
182 {
183 auto kernel = std::make_unique<ClMatMulLowpNativeMMULKernel>();
184 kernel->set_target(gpu_target);
185
186 kernel->configure(compile_context, lhs, rhs, nullptr /* bias */, dst, kernel_info, act_info);
187 _matmul_kernel = std::move(kernel);
188 }
189 break;
190 default:
191 ARM_COMPUTE_ERROR("Unsupported MatMul Kernel!");
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100192 }
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000193}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100194
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000195void ClMatMul::run(ITensorPack &tensors)
196{
Gunes Bayirc1204c72023-10-10 17:41:56 +0100197 CLScheduler::get().enqueue_op(*_matmul_kernel, tensors, /* flush */ true);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000198}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100199
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000200} // namespace opencl
201} // namespace arm_compute