1 /*
2 * Copyright (c) 2021 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/runtime/CL/gemm_auto_heuristics/CLGEMMAutoHeuristics.h"
25
26 #include "arm_compute/core/Log.h"
27 #include "arm_compute/core/Validate.h"
28 #include "arm_compute/runtime/CL/CLScheduler.h"
29 #include "arm_compute/runtime/CL/ICLGEMMKernelSelection.h"
30 #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
31 #include "src/gpu/cl/kernels/gemm/IClGemmKernelConfig.h"
32 #include "src/gpu/cl/kernels/gemm/native/ClGemmNativeKernelConfig.h"
33 #include "src/gpu/cl/kernels/gemm/reshaped/ClGemmReshapedKernelConfig.h"
34 #include "src/gpu/cl/kernels/gemm/reshaped_only_rhs/ClGemmReshapedOnlyRhsKernelConfig.h"
35 #include "src/runtime/CL/gemm/CLGEMMKernelSelection.h"
36 #include "src/runtime/CL/mlgo/MLGOHeuristics.h"
37 #include "src/runtime/CL/mlgo/Utils.h"
38 #include "utils/TypePrinter.h"
39
40 namespace arm_compute
41 {
42 namespace cl_gemm
43 {
44 namespace auto_heuristics
45 {
46 using namespace arm_compute::opencl::kernels::gemm;
47
select_mlgo_gemm_kernel(const CommonQuery & query,bool reshape_b_only_on_first_run)48 GEMMTypeResult select_mlgo_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
49 {
50 ARM_COMPUTE_UNUSED(reshape_b_only_on_first_run);
51 bool valid = false;
52 CLGEMMKernelType gemm_type{};
53 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
54 if(mlgo_heuristics != nullptr)
55 {
56 std::tie(valid, gemm_type) = mlgo_heuristics->get()->query_gemm_type(mlgo::Query{ string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b });
57 }
58 if(valid)
59 {
60 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm type: %s.", to_string(gemm_type).c_str());
61 }
62 else
63 {
64 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
65 }
66 return GEMMTypeResult(valid, gemm_type);
67 }
68
select_default_gemm_kernel(const CommonQuery & query,bool reshape_b_only_on_first_run)69 GEMMTypeResult select_default_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
70 {
71 std::unique_ptr<ICLGEMMKernelSelection> default_heuristics = CLGEMMKernelSelectionFactory::create(query.gpu_target);
72 ARM_COMPUTE_ERROR_ON_NULLPTR(default_heuristics.get());
73
74 CLGEMMKernelSelectionParams params;
75 params.m = query.m;
76 params.n = query.n;
77 params.k = query.k;
78 params.b = query.b;
79 params.is_rhs_constant = reshape_b_only_on_first_run;
80 params.data_type = query.data_type;
81
82 const auto kernel_type = default_heuristics->select_kernel(params);
83 return GEMMTypeResult(true, kernel_type);
84 }
85
select_default_gemm_config_reshaped_only_rhs(const CommonQuery & query)86 GEMMConfigResult select_default_gemm_config_reshaped_only_rhs(const CommonQuery &query)
87 {
88 GEMMLHSMatrixInfo lhs_info;
89 GEMMRHSMatrixInfo rhs_info;
90 std::unique_ptr<IClGemmKernelConfig> gemm_config = ClGemmReshapedOnlyRhsKernelConfigurationFactory::create(query.gpu_target);
91 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
92 std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
93 return GEMMConfigResult{ true, lhs_info, rhs_info };
94 }
95
select_mlgo_gemm_config_reshaped_only_rhs(const CommonQuery & query)96 GEMMConfigResult select_mlgo_gemm_config_reshaped_only_rhs(const CommonQuery &query)
97 {
98 bool valid = false;
99 GEMMLHSMatrixInfo lhs_info;
100 GEMMRHSMatrixInfo rhs_info;
101 mlgo::GEMMConfigReshapedOnlyRHS config{};
102 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
103 if(mlgo_heuristics != nullptr)
104 {
105 std::tie(valid, config) = mlgo_heuristics->get()->query_gemm_config_reshaped_only_rhs(mlgo::Query{ string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b });
106 }
107 if(valid)
108 {
109 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.", to_string(config).c_str());
110 // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
111 std::tie(lhs_info, rhs_info) = configure_lhs_rhs_info(query.m, query.n, config.m0, config.n0, config.k0, 1, config.h0, false, config.interleave_rhs, !config.transpose_rhs, config.transpose_rhs,
112 config.export_cl_image);
113 }
114 else
115 {
116 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
117 }
118 return GEMMConfigResult{ valid, lhs_info, rhs_info };
119 }
120
select_default_gemm_config_reshaped(const CommonQuery & query)121 GEMMConfigResult select_default_gemm_config_reshaped(const CommonQuery &query)
122 {
123 GEMMLHSMatrixInfo lhs_info;
124 GEMMRHSMatrixInfo rhs_info;
125 std::unique_ptr<IClGemmKernelConfig> gemm_config = ClGemmReshapedKernelConfigurationFactory::create(query.gpu_target);
126 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
127 std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
128 return GEMMConfigResult{ true, lhs_info, rhs_info };
129 }
130
select_mlgo_gemm_config_reshaped(const CommonQuery & query)131 GEMMConfigResult select_mlgo_gemm_config_reshaped(const CommonQuery &query)
132 {
133 bool valid = false;
134 GEMMLHSMatrixInfo lhs_info;
135 GEMMRHSMatrixInfo rhs_info;
136 mlgo::GEMMConfigReshaped config{};
137 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
138 if(mlgo_heuristics != nullptr)
139 {
140 std::tie(valid, config) = mlgo_heuristics->get()->query_gemm_config_reshaped(mlgo::Query{ string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b });
141 }
142 if(valid)
143 {
144 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.", to_string(config).c_str());
145 std::tie(lhs_info, rhs_info) = configure_lhs_rhs_info(query.m, query.n, config.m0, config.n0, config.k0, config.v0, config.h0, config.interleave_lhs, config.interleave_rhs, !config.transpose_rhs,
146 config.transpose_rhs, config.export_cl_image);
147 }
148 else
149 {
150 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
151 }
152 return GEMMConfigResult{ valid, lhs_info, rhs_info };
153 }
154
select_default_gemm_config_native(const CommonQuery & query)155 GEMMConfigResult select_default_gemm_config_native(const CommonQuery &query)
156 {
157 GEMMLHSMatrixInfo lhs_info;
158 GEMMRHSMatrixInfo rhs_info;
159 std::unique_ptr<IClGemmKernelConfig> gemm_config = ClGemmNativeKernelConfigurationFactory::create(query.gpu_target);
160 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
161 std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
162 return GEMMConfigResult{ true, lhs_info, rhs_info };
163 }
164
select_mlgo_gemm_config_native(const CommonQuery & query)165 GEMMConfigResult select_mlgo_gemm_config_native(const CommonQuery &query)
166 {
167 bool valid = false;
168 GEMMLHSMatrixInfo lhs_info;
169 GEMMRHSMatrixInfo rhs_info;
170 mlgo::GEMMConfigNative config{};
171 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
172 if(mlgo_heuristics != nullptr)
173 {
174 std::tie(valid, config) = mlgo_heuristics->get()->query_gemm_config_native(mlgo::Query{ string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b });
175 }
176 if(valid)
177 {
178 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.", to_string(config).c_str());
179 // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
180 std::tie(lhs_info, rhs_info) = opencl::kernels::gemm::configure_lhs_rhs_info(query.m, query.n, config.m0, config.n0, config.k0, 1, 1, false, false, false, false, false);
181 }
182 else
183 {
184 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
185 }
186 return GEMMConfigResult{ valid, lhs_info, rhs_info };
187 }
188 } // namespace auto_heuristics
189
190 } // namespace cl_gemm
191 } // namespace arm_compute