Compute Library
 22.11
CLGEMMAutoHeuristics.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Log.h"
38 #include "utils/TypePrinter.h"
39 
40 namespace arm_compute
41 {
42 namespace cl_gemm
43 {
44 namespace auto_heuristics
45 {
47 
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;
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 
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 
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 
87 {
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 
97 {
98  bool valid = false;
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 
122 {
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 
132 {
133  bool valid = false;
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 
156 {
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 
166 {
167  bool valid = false;
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
Query interface.
unsigned int m
Number of rows for the lhs matrix.
Definition: CLTypes.h:46
GEMMConfigResult select_default_gemm_config_reshaped(const CommonQuery &query)
Select gemm config based on default heuristics.
OpenCL GEMM kernel selection parameters.
Definition: CLTypes.h:44
static CLScheduler & get()
Access the scheduler singleton.
GEMM Configuration for Reshaped kernel.
Definition: Common.h:66
A collection of adaptor functions that enable the auto selection between mlgo-based heuristics and de...
GEMMType gemm_type(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:567
GEMM LHS (Left Hand Side) matrix information.
Definition: Types.h:2303
Result of querying about GEMM config ( GEMMLHSMatrixInfo and GEMMRHSMatrixInfo)
const std::string & string_from_target(GPUTarget target)
Translates a given gpu device target to string.
Definition: GPUTarget.cpp:151
GEMMConfigResult select_mlgo_gemm_config_native(const CommonQuery &query)
Select gemm config based on mlgo heuristics.
CLGEMMKernelType
OpenCL GEMM kernel types.
Definition: CLTypes.h:31
CLGEMMHeuristicsHandle * gemm_heuristics() const
Accessor for the associated CLGEMMHeuristicsHandle.
Definition: CLScheduler.cpp:54
std::pair< bool, GEMMType > query_gemm_type(const Query &query) const
Query the gemm type.
Copyright (c) 2017-2022 Arm Limited.
GEMMConfigResult select_mlgo_gemm_config_reshaped(const CommonQuery &query)
Select gemm config based on mlgo heuristics.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
GEMMConfigResult select_default_gemm_config_native(const CommonQuery &query)
Select gemm config based on default heuristics.
#define ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE(fmt,...)
Log information level formatted message to the core system logger.
Definition: Log.h:99
const mlgo::MLGOHeuristics * get() const
Return a pointer to underlying heuristics for querying purposes.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
GEMM RHS (Right Hand Side) matrix information.
Definition: Types.h:2318
GEMM Configuration for Reshaped Only RHS kernel.
Definition: Common.h:54
GEMMTypeResult select_default_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
Select gemm type based on default heuristics.
std::string to_string(const T &val)
Fallback method: try to use std::to_string:
Definition: TypePrinter.h:80
static std::unique_ptr< ICLGEMMKernelSelection > create(GPUTarget gpu)
Static method to select the GEMM kernel accordingly with the GPU target and GEMM&#39;s dimensionality...
std::pair< GEMMLHSMatrixInfo, GEMMRHSMatrixInfo > configure_lhs_rhs_info(unsigned int m, unsigned int n, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int v0, unsigned int h0, bool lhs_interleave, bool rhs_interleave, bool lhs_transpose, bool rhs_transpose, bool export_to_cl_image)
Configure GEMMLHSMatrixInfo and GEMMRHSMatrixInfo.
unsigned int k
Number of rows for the rhs matrix.
static std::unique_ptr< IClGemmKernelConfig > create(GPUTarget gpu)
Static method to call the CLGEMMReshapedOnlyRHS kernel configuration class accordingly with the GPU t...
GPUTarget gpu_target
Which GPUTarget to query about.
static std::unique_ptr< IClGemmKernelConfig > create(GPUTarget gpu)
Static method to call the CLGEMMReshaped kernel configuration class accordingly with the GPU target...
std::pair< bool, GEMMConfigReshapedOnlyRHS > query_gemm_config_reshaped_only_rhs(const Query &query) const
Query the gemm configuration for reshaped only rhs kernel.
std::pair< bool, GEMMConfigNative > query_gemm_config_native(const Query &query) const
Query the gemm configuration for native kernel.
GEMMTypeResult select_mlgo_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
Select gemm type based on mlgo heuristics.
unsigned int m
Number of rows for the lhs matrix.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
#define ARM_COMPUTE_LOG_INFO_MSG_CORE(msg)
Log information level message to the core system logger.
Definition: Log.h:87
GEMM Configuration for Native kernel.
Definition: Common.h:46
GEMMConfigResult select_mlgo_gemm_config_reshaped_only_rhs(const CommonQuery &query)
Select gemm config based on mlgo heuristics.
unsigned int n
Number of columns for the rhs matrix.
static std::unique_ptr< IClGemmKernelConfig > create(GPUTarget gpu)
Static method to construct CLGEMMNative kernel object accordingly with the GPU target.
Result of querying about GEMM type ( CLGEMMKernelType)
GEMMConfigResult select_default_gemm_config_reshaped_only_rhs(const CommonQuery &query)
Select gemm config based on default heuristics.
std::pair< bool, GEMMConfigReshaped > query_gemm_config_reshaped(const Query &query) const
Query the gemm configuration for reshaped kernel.