blob: 45a63980cac27aa0ef90f6c0d62b74bc3f0c58b3 [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());
108 }
109 else
110 {
111 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
112 }
SiCong Lidb353452021-02-08 15:16:13 +0000113 // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
SiCong Libd8b1e22021-02-04 13:07:09 +0000114 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,
115 config.export_cl_image);
116 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());
143 }
144 else
145 {
146 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
147 }
148 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,
149 config.transpose_rhs,
150 config.export_cl_image);
151 return GEMMConfigResult{ valid, lhs_info, rhs_info };
152}
SiCong Lidb353452021-02-08 15:16:13 +0000153
154GEMMConfigResult select_default_gemm_config_native(const CommonQuery &query)
155{
156 GEMMLHSMatrixInfo lhs_info;
157 GEMMRHSMatrixInfo rhs_info;
158 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMNativeKernelConfigurationFactory::create(query.gpu_target);
159 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
160 std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
161 return GEMMConfigResult{ true, lhs_info, rhs_info };
162}
163
164GEMMConfigResult select_mlgo_gemm_config_native(const CommonQuery &query)
165{
166 bool valid = false;
167 GEMMLHSMatrixInfo lhs_info;
168 GEMMRHSMatrixInfo rhs_info;
169 mlgo::GEMMConfigNative config{};
SiCong Li1a28e732021-02-10 16:57:33 +0000170 const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
SiCong Lidb353452021-02-08 15:16:13 +0000171 if(mlgo_heuristics != nullptr)
172 {
173 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 });
174 }
175 if(valid)
176 {
177 ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.", to_string(config).c_str());
178 }
179 else
180 {
181 ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
182 }
183 // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
184 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);
185 return GEMMConfigResult{ valid, lhs_info, rhs_info };
186}
SiCong Libd8b1e22021-02-04 13:07:09 +0000187} // namespace auto_heuristics
SiCong Li8c23ba12021-02-08 14:19:23 +0000188
SiCong Libd8b1e22021-02-04 13:07:09 +0000189} // namespace cl_gemm
190} // namespace arm_compute