blob: 489be356d9c5c609e1d8f46cfa1075f9088eb35d [file] [log] [blame]
SiCong Libd8b1e22021-02-04 13:07:09 +00001/*
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/core/CL/ICLGEMMKernelConfiguration.h"
Manuel Bottini89581672021-02-11 12:34:47 +000031#include "src/core/CL/gemm/CLGEMMHelpers.h"
SiCong Lidb353452021-02-08 15:16:13 +000032#include "src/core/CL/gemm/native/CLGEMMNativeKernelConfiguration.h"
SiCong Li8c23ba12021-02-08 14:19:23 +000033#include "src/core/CL/gemm/reshaped/CLGEMMReshapedKernelConfiguration.h"
SiCong Libd8b1e22021-02-04 13:07:09 +000034#include "src/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfiguration.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
40namespace arm_compute
41{
42namespace cl_gemm
43{
44namespace auto_heuristics
45{
SiCong Li1a28e732021-02-10 16:57:33 +000046GEMMTypeResult select_mlgo_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
SiCong Libd8b1e22021-02-04 13:07:09 +000047{
SiCong Li1a28e732021-02-10 16:57:33 +000048 ARM_COMPUTE_UNUSED(reshape_b_only_on_first_run);
49 bool valid = false;
50 CLGEMMKernelType gemm_type{};
51 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
SiCong Libd8b1e22021-02-04 13:07:09 +000052 if(mlgo_heuristics != nullptr)
53 {
SiCong Li1a28e732021-02-10 16:57:33 +000054 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 });
SiCong Libd8b1e22021-02-04 13:07:09 +000055 }
SiCong Li1a28e732021-02-10 16:57:33 +000056 if(valid)
57 {
58 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm type: %s.", to_string(gemm_type).c_str());
59 }
60 else
61 {
62 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
63 }
64 return GEMMTypeResult(valid, gemm_type);
65}
66
67GEMMTypeResult select_default_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
68{
69 std::unique_ptr<ICLGEMMKernelSelection> default_heuristics = CLGEMMKernelSelectionFactory::create(query.gpu_target);
70 ARM_COMPUTE_ERROR_ON_NULLPTR(default_heuristics.get());
SiCong Libd8b1e22021-02-04 13:07:09 +000071
72 CLGEMMKernelSelectionParams params;
73 params.m = query.m;
74 params.n = query.n;
75 params.k = query.k;
76 params.b = query.b;
77 params.is_rhs_constant = reshape_b_only_on_first_run;
78 params.data_type = query.data_type;
79
SiCong Li1a28e732021-02-10 16:57:33 +000080 const auto kernel_type = default_heuristics->select_kernel(params);
81 return GEMMTypeResult(true, kernel_type);
SiCong Libd8b1e22021-02-04 13:07:09 +000082}
83
84GEMMConfigResult select_default_gemm_config_reshaped_only_rhs(const CommonQuery &query)
85{
86 GEMMLHSMatrixInfo lhs_info;
87 GEMMRHSMatrixInfo rhs_info;
88 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(query.gpu_target);
89 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
90 std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
91 return GEMMConfigResult{ true, lhs_info, rhs_info };
92}
93
94GEMMConfigResult select_mlgo_gemm_config_reshaped_only_rhs(const CommonQuery &query)
95{
96 bool valid = false;
97 GEMMLHSMatrixInfo lhs_info;
98 GEMMRHSMatrixInfo rhs_info;
99 mlgo::GEMMConfigReshapedOnlyRHS config{};
SiCong Li1a28e732021-02-10 16:57:33 +0000100 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
SiCong Libd8b1e22021-02-04 13:07:09 +0000101 if(mlgo_heuristics != nullptr)
102 {
103 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 });
104 }
105 if(valid)
106 {
107 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.", to_string(config).c_str());
Georgios Pinitas261df742021-02-23 00:00:42 +0000108 // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
109 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,
110 config.export_cl_image);
SiCong Libd8b1e22021-02-04 13:07:09 +0000111 }
112 else
113 {
114 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
115 }
SiCong Libd8b1e22021-02-04 13:07:09 +0000116 return GEMMConfigResult{ valid, lhs_info, rhs_info };
117}
SiCong Li8c23ba12021-02-08 14:19:23 +0000118
119GEMMConfigResult select_default_gemm_config_reshaped(const CommonQuery &query)
120{
121 GEMMLHSMatrixInfo lhs_info;
122 GEMMRHSMatrixInfo rhs_info;
123 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedKernelConfigurationFactory::create(query.gpu_target);
124 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
125 std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
126 return GEMMConfigResult{ true, lhs_info, rhs_info };
127}
128
129GEMMConfigResult select_mlgo_gemm_config_reshaped(const CommonQuery &query)
130{
131 bool valid = false;
132 GEMMLHSMatrixInfo lhs_info;
133 GEMMRHSMatrixInfo rhs_info;
134 mlgo::GEMMConfigReshaped config{};
SiCong Li1a28e732021-02-10 16:57:33 +0000135 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
SiCong Li8c23ba12021-02-08 14:19:23 +0000136 if(mlgo_heuristics != nullptr)
137 {
138 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 });
139 }
140 if(valid)
141 {
142 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.", to_string(config).c_str());
Georgios Pinitas261df742021-02-23 00:00:42 +0000143 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,
144 config.transpose_rhs, config.export_cl_image);
SiCong Li8c23ba12021-02-08 14:19:23 +0000145 }
146 else
147 {
148 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
149 }
SiCong Li8c23ba12021-02-08 14:19:23 +0000150 return GEMMConfigResult{ valid, lhs_info, rhs_info };
151}
SiCong Lidb353452021-02-08 15:16:13 +0000152
153GEMMConfigResult select_default_gemm_config_native(const CommonQuery &query)
154{
155 GEMMLHSMatrixInfo lhs_info;
156 GEMMRHSMatrixInfo rhs_info;
157 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMNativeKernelConfigurationFactory::create(query.gpu_target);
158 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
159 std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
160 return GEMMConfigResult{ true, lhs_info, rhs_info };
161}
162
163GEMMConfigResult select_mlgo_gemm_config_native(const CommonQuery &query)
164{
165 bool valid = false;
166 GEMMLHSMatrixInfo lhs_info;
167 GEMMRHSMatrixInfo rhs_info;
168 mlgo::GEMMConfigNative config{};
SiCong Li1a28e732021-02-10 16:57:33 +0000169 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
SiCong Lidb353452021-02-08 15:16:13 +0000170 if(mlgo_heuristics != nullptr)
171 {
172 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 });
173 }
174 if(valid)
175 {
176 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.", to_string(config).c_str());
Georgios Pinitas261df742021-02-23 00:00:42 +0000177 // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
178 std::tie(lhs_info, rhs_info) = configure_lhs_rhs_info(query.m, query.n, config.m0, config.n0, config.k0, 1, 1, false, false, false, false, false);
SiCong Lidb353452021-02-08 15:16:13 +0000179 }
180 else
181 {
182 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
183 }
SiCong Lidb353452021-02-08 15:16:13 +0000184 return GEMMConfigResult{ valid, lhs_info, rhs_info };
185}
SiCong Libd8b1e22021-02-04 13:07:09 +0000186} // namespace auto_heuristics
SiCong Li8c23ba12021-02-08 14:19:23 +0000187
SiCong Libd8b1e22021-02-04 13:07:09 +0000188} // namespace cl_gemm
189} // namespace arm_compute