blob: 28a2aa25407609a0b759ea6bf587c0a90e2f250e [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"
Gunes Bayir85cafff2023-12-18 13:29:31 +000037#include "src/runtime/heuristics/matmul_native/ClMatMulNativeKernelVariant.h"
Gian Marco Iodice352c07d2023-05-03 12:21:38 +010038#include "src/runtime/heuristics/matmul_native/IClMatMulNativeKernelConfig.h"
39
40using namespace arm_compute::cl_matmul;
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000041
42namespace arm_compute
43{
44namespace opencl
45{
46using namespace arm_compute::opencl::kernels;
Jakub Sujake9b3ee22023-04-17 12:08:48 +010047
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000048ClMatMul::ClMatMul()
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000049{
50}
Jakub Sujake9b3ee22023-04-17 12:08:48 +010051
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052Status ClMatMul::validate(const ITensorInfo *lhs,
53 const ITensorInfo *rhs,
54 const ITensorInfo *dst,
55 const MatMulInfo &matmul_info,
56 const ActivationLayerInfo &act_info)
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000057{
Jakub Sujake9b3ee22023-04-17 12:08:48 +010058 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 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 Sujake9b3ee22023-04-17 12:08:48 +010063
Gian Marco Iodice352c07d2023-05-03 12:21:38 +010064 const GPUTarget gpu_target = CLScheduler::get().target();
65
66 std::unique_ptr<IClMatMulNativeKernelConfig> t = ClMatMulNativeKernelConfigurationFactory::create(gpu_target);
67
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +010068 const MatMulKernelInfo kernel_info = t->configure(lhs, rhs, matmul_info);
Gian Marco Iodice352c07d2023-05-03 12:21:38 +010069
Gunes Bayir85cafff2023-12-18 13:29:31 +000070 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 Bayirc1204c72023-10-10 17:41:56 +010074 {
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 Elgammalf26ea2f2023-03-24 11:42:03 +000086}
Jakub Sujake9b3ee22023-04-17 12:08:48 +010087
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088void 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 Elgammalf26ea2f2023-03-24 11:42:03 +000094{
Jakub Sujake9b3ee22023-04-17 12:08:48 +010095 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
96 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, dst, matmul_info);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000097
98 // Perform validation step
Jakub Sujake9b3ee22023-04-17 12:08:48 +010099 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, dst, matmul_info));
100
Gunes Bayirc1204c72023-10-10 17:41:56 +0100101 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 Sujake9b3ee22023-04-17 12:08:48 +0100104
Gunes Bayir85cafff2023-12-18 13:29:31 +0000105 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 Sujake9b3ee22023-04-17 12:08:48 +0100109 {
Gunes Bayirc1204c72023-10-10 17:41:56 +0100110 case MatMulKernelType::NATIVE_FP:
111 {
112 auto kernel = std::make_unique<ClMatMulNativeKernel>();
113 kernel->set_target(gpu_target);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000114
Gunes Bayirc1204c72023-10-10 17:41:56 +0100115 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 Sujake9b3ee22023-04-17 12:08:48 +0100123
Gunes Bayirc1204c72023-10-10 17:41:56 +0100124 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 Sujake9b3ee22023-04-17 12:08:48 +0100148 }
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000149}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100150
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000151void ClMatMul::run(ITensorPack &tensors)
152{
Gunes Bayirc1204c72023-10-10 17:41:56 +0100153 CLScheduler::get().enqueue_op(*_matmul_kernel, tensors, /* flush */ true);
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000154}
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100155
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000156} // namespace opencl
157} // namespace arm_compute