Compute Library
 21.05
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 {
46 GEMMTypeResult select_mlgo_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
47 {
48  ARM_COMPUTE_UNUSED(reshape_b_only_on_first_run);
49  bool valid = false;
51  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
52  if(mlgo_heuristics != nullptr)
53  {
54  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 });
55  }
56  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 
67 GEMMTypeResult 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());
71 
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 
80  const auto kernel_type = default_heuristics->select_kernel(params);
81  return GEMMTypeResult(true, kernel_type);
82 }
83 
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 
95 {
96  bool valid = false;
97  GEMMLHSMatrixInfo lhs_info;
98  GEMMRHSMatrixInfo rhs_info;
100  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
101  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  // 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);
111  }
112  else
113  {
114  ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
115  }
116  return GEMMConfigResult{ valid, lhs_info, rhs_info };
117 }
118 
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 
130 {
131  bool valid = false;
132  GEMMLHSMatrixInfo lhs_info;
133  GEMMRHSMatrixInfo rhs_info;
134  mlgo::GEMMConfigReshaped config{};
135  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
136  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  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);
145  }
146  else
147  {
148  ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
149  }
150  return GEMMConfigResult{ valid, lhs_info, rhs_info };
151 }
152 
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 
164 {
165  bool valid = false;
166  GEMMLHSMatrixInfo lhs_info;
167  GEMMRHSMatrixInfo rhs_info;
168  mlgo::GEMMConfigNative config{};
169  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
170  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());
177  // 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);
179  }
180  else
181  {
182  ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
183  }
184  return GEMMConfigResult{ valid, lhs_info, rhs_info };
185 }
186 } // namespace auto_heuristics
187 
188 } // namespace cl_gemm
189 } // namespace arm_compute
unsigned int m
Number of rows for the lhs matrix.
Definition: CLTypes.h:54
GEMMConfigResult select_default_gemm_config_reshaped(const CommonQuery &query)
Select gemm config based on default heuristics.
OpenCL GEMM kernel selection parameters.
Definition: CLTypes.h:52
static std::unique_ptr< ICLGEMMKernelConfiguration > create(GPUTarget gpu)
Static method to call the CLGEMMReshaped kernel configuration class accordingly with the GPU target.
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:1903
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:115
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:50
std::pair< bool, GEMMType > query_gemm_type(const Query &query) const
Query the gemm type.
Copyright (c) 2017-2021 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.
std::string to_string(const ROIPoolingLayerInfo &pool_info)
Formatted output of the ROIPoolingInfo type.
Definition: TypePrinter.h:149
#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
static std::unique_ptr< ICLGEMMKernelConfiguration > create(GPUTarget gpu)
Static method to construct CLGEMMNative kernel object accordingly with the GPU target.
GEMM RHS (Right Hand Side) matrix information.
Definition: Types.h:1918
static std::unique_ptr< ICLGEMMKernelConfiguration > create(GPUTarget gpu)
Static method to call the CLGEMMReshapedOnlyRHS kernel configuration class accordingly with the GPU t...
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.
static std::unique_ptr< ICLGEMMKernelSelection > create(GPUTarget gpu)
Static method to select the GEMM kernel accordingly with the GPU target and GEMM's dimensionality.
unsigned int k
Number of rows for the rhs matrix.
GPUTarget gpu_target
Which GPUTarget to query about.
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.
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 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.
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.