Compute Library
 23.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"
30 
39 #include "utils/TypePrinter.h"
40 
41 namespace arm_compute
42 {
43 namespace cl_gemm
44 {
45 namespace auto_heuristics
46 {
48 
49 GEMMTypeResult select_mlgo_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
50 {
51  ARM_COMPUTE_UNUSED(reshape_b_only_on_first_run);
52  bool valid = false;
54  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
55  if (mlgo_heuristics != nullptr)
56  {
57  std::tie(valid, gemm_type) = mlgo_heuristics->get()->query_gemm_type(
58  mlgo::Query{string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b});
59  }
60  if (valid)
61  {
62  ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm type: %s.",
63  to_string(gemm_type).c_str());
64  }
65  else
66  {
67  ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
68  }
69  return GEMMTypeResult(valid, gemm_type);
70 }
71 
72 GEMMTypeResult select_default_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
73 {
74  std::unique_ptr<ICLGEMMKernelSelection> default_heuristics = CLGEMMKernelSelectionFactory::create(query.gpu_target);
75  ARM_COMPUTE_ERROR_ON_NULLPTR(default_heuristics.get());
76 
78  params.m = query.m;
79  params.n = query.n;
80  params.k = query.k;
81  params.b = query.b;
82  params.is_rhs_constant = reshape_b_only_on_first_run;
83  params.data_type = query.data_type;
84 
85  const auto kernel_type = default_heuristics->select_kernel(params);
86  return GEMMTypeResult(true, kernel_type);
87 }
88 
90 {
91  GEMMLHSMatrixInfo lhs_info;
92  GEMMRHSMatrixInfo rhs_info;
93  std::unique_ptr<IClGemmKernelConfig> gemm_config =
95  ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
96  std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
97  return GEMMConfigResult{true, lhs_info, rhs_info};
98 }
99 
101 {
102  bool valid = false;
103  GEMMLHSMatrixInfo lhs_info;
104  GEMMRHSMatrixInfo rhs_info;
106  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
107  if (mlgo_heuristics != nullptr)
108  {
109  std::tie(valid, config) = mlgo_heuristics->get()->query_gemm_config_reshaped_only_rhs(
110  mlgo::Query{string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b});
111  }
112  if (valid)
113  {
114  ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.",
115  to_string(config).c_str());
116  // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
117  std::tie(lhs_info, rhs_info) = configure_lhs_rhs_info(
118  query.m, query.n, config.m0, config.n0, config.k0, 1, config.h0, false, config.interleave_rhs,
119  !config.transpose_rhs, config.transpose_rhs, config.export_cl_image);
120  }
121  else
122  {
123  ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
124  }
125  return GEMMConfigResult{valid, lhs_info, rhs_info};
126 }
127 
129 {
130  GEMMLHSMatrixInfo lhs_info;
131  GEMMRHSMatrixInfo rhs_info;
132  std::unique_ptr<IClGemmKernelConfig> gemm_config =
134  ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
135  std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
136  return GEMMConfigResult{true, lhs_info, rhs_info};
137 }
138 
140 {
141  bool valid = false;
142  GEMMLHSMatrixInfo lhs_info;
143  GEMMRHSMatrixInfo rhs_info;
144  mlgo::GEMMConfigReshaped config{};
145  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
146  if (mlgo_heuristics != nullptr)
147  {
148  std::tie(valid, config) = mlgo_heuristics->get()->query_gemm_config_reshaped(
149  mlgo::Query{string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b});
150  }
151  if (valid)
152  {
153  ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.",
154  to_string(config).c_str());
155  std::tie(lhs_info, rhs_info) = configure_lhs_rhs_info(
156  query.m, query.n, config.m0, config.n0, config.k0, config.v0, config.h0, config.interleave_lhs,
157  config.interleave_rhs, !config.transpose_rhs, config.transpose_rhs, config.export_cl_image);
158  }
159  else
160  {
161  ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
162  }
163  return GEMMConfigResult{valid, lhs_info, rhs_info};
164 }
165 
167 {
168  GEMMLHSMatrixInfo lhs_info;
169  GEMMRHSMatrixInfo rhs_info;
170  std::unique_ptr<IClGemmKernelConfig> gemm_config = ClGemmNativeKernelConfigurationFactory::create(query.gpu_target);
171  ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
172  std::tie(lhs_info, rhs_info) = gemm_config->configure(query.m, query.n, query.k, query.b, query.data_type);
173  return GEMMConfigResult{true, lhs_info, rhs_info};
174 }
175 
177 {
178  bool valid = false;
179  GEMMLHSMatrixInfo lhs_info;
180  GEMMRHSMatrixInfo rhs_info;
181  mlgo::GEMMConfigNative config{};
182  const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics();
183  if (mlgo_heuristics != nullptr)
184  {
185  std::tie(valid, config) = mlgo_heuristics->get()->query_gemm_config_native(
186  mlgo::Query{string_from_target(query.gpu_target), query.data_type, query.m, query.n, query.k, query.b});
187  }
188  if (valid)
189  {
190  ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("MLGOHeuristics query returns gemm config: %s.",
191  to_string(config).c_str());
192  // Setting irrelevant unsigned int parameters to 1 and bool parameters to false as they do no matter
193  std::tie(lhs_info, rhs_info) = opencl::kernels::gemm::configure_lhs_rhs_info(
194  query.m, query.n, config.m0, config.n0, config.k0, 1, 1, false, false, false, false, false);
195  }
196  else
197  {
198  ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed");
199  }
200  return GEMMConfigResult{valid, lhs_info, rhs_info};
201 }
202 } // namespace auto_heuristics
203 
204 } // namespace cl_gemm
205 } // namespace arm_compute
arm_compute::CLScheduler::gemm_heuristics
CLGEMMHeuristicsHandle * gemm_heuristics() const
Accessor for the associated CLGEMMHeuristicsHandle.
Definition: CLScheduler.cpp:51
arm_compute::opencl::kernels::gemm::configure_lhs_rhs_info
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.
Definition: ClGemmHelpers.cpp:42
arm_compute::cl_gemm::auto_heuristics::GEMMConfigResult
Result of querying about GEMM config ( GEMMLHSMatrixInfo and GEMMRHSMatrixInfo)
Definition: CLGEMMAutoHeuristics.h:66
arm_compute::CLGEMMKernelSelectionParams
OpenCL GEMM kernel selection parameters.
Definition: CLTypes.h:44
arm_compute::CLGEMMKernelSelectionParams::data_type
DataType data_type
Data type.
Definition: CLTypes.h:51
arm_compute::cl_gemm::auto_heuristics::select_mlgo_gemm_kernel
GEMMTypeResult select_mlgo_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
Select gemm type based on mlgo heuristics.
Definition: CLGEMMAutoHeuristics.cpp:49
arm_compute::cl_gemm::auto_heuristics::select_mlgo_gemm_config_reshaped_only_rhs
GEMMConfigResult select_mlgo_gemm_config_reshaped_only_rhs(const CommonQuery &query)
Select gemm config based on mlgo heuristics.
Definition: CLGEMMAutoHeuristics.cpp:100
MLGOHeuristics.h
CLGEMMKernelSelection.h
arm_compute::CLGEMMKernelSelectionParams::n
unsigned int n
Number of columns for the rhs matrix.
Definition: CLTypes.h:47
arm_compute::mlgo::MLGOHeuristics::query_gemm_type
std::pair< bool, GEMMType > query_gemm_type(const Query &query) const
Query the gemm type.
Definition: MLGOHeuristics.cpp:59
arm_compute::opencl::kernels::gemm
Definition: ClGemmHelpers.cpp:40
ClGemmHelpers.h
arm_compute::CLGEMMKernelSelectionParams::m
unsigned int m
Number of rows for the lhs matrix.
Definition: CLTypes.h:46
ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE
#define ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE(fmt,...)
Log information level formatted message to the core system logger.
Definition: Log.h:99
arm_compute::CLGEMMKernelSelectionParams::is_rhs_constant
bool is_rhs_constant
True if the content of the rhs matrix is constant.
Definition: CLTypes.h:50
arm_compute::CLGEMMKernelType
CLGEMMKernelType
OpenCL GEMM kernel types.
Definition: CLTypes.h:31
arm_compute::string_from_target
const std::string & string_from_target(GPUTarget target)
Translates a given gpu device target to string.
Definition: GPUTarget.cpp:152
arm_compute::cl_gemm::auto_heuristics::GEMMTypeResult
Result of querying about GEMM type ( CLGEMMKernelType)
Definition: CLGEMMAutoHeuristics.h:51
arm_compute::mlgo::GEMMConfigReshapedOnlyRHS
GEMM Configuration for Reshaped Only RHS kernel.
Definition: Common.h:54
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
IClGemmKernelConfig.h
Log.h
arm_compute::cl_gemm::auto_heuristics::CommonQuery
A collection of adaptor functions that enable the auto selection between mlgo-based heuristics and de...
Definition: CLGEMMAutoHeuristics.h:40
TypePrinter.h
ClGemmReshapedOnlyRhsKernelConfig.h
arm_compute::cl_gemm::auto_heuristics::select_default_gemm_config_reshaped_only_rhs
GEMMConfigResult select_default_gemm_config_reshaped_only_rhs(const CommonQuery &query)
Select gemm config based on default heuristics.
Definition: CLGEMMAutoHeuristics.cpp:89
arm_compute::cl_gemm::auto_heuristics::select_default_gemm_config_reshaped
GEMMConfigResult select_default_gemm_config_reshaped(const CommonQuery &query)
Select gemm config based on default heuristics.
Definition: CLGEMMAutoHeuristics.cpp:128
arm_compute::mlgo::parser::gemm_type
GEMMType gemm_type(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:560
arm_compute::mlgo::Query
Query interface.
Definition: MLGOHeuristics.h:39
arm_compute::mlgo::GEMMConfigReshaped
GEMM Configuration for Reshaped kernel.
Definition: Common.h:66
CLScheduler.h
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
arm_compute::cl_gemm::auto_heuristics::CommonQuery::n
unsigned int n
Number of columns for the rhs matrix.
Definition: CLGEMMAutoHeuristics.h:45
arm_compute::cl_gemm::auto_heuristics::CommonQuery::k
unsigned int k
Number of rows for the rhs matrix.
Definition: CLGEMMAutoHeuristics.h:46
Utils.h
arm_compute::CLGEMMKernelSelectionParams::b
unsigned int b
Batch size.
Definition: CLTypes.h:49
arm_compute::opencl::kernels::gemm::ClGemmNativeKernelConfigurationFactory::create
static std::unique_ptr< IClGemmKernelConfig > create(GPUTarget gpu)
Static method to construct CLGEMMNative kernel object accordingly with the GPU target.
Definition: ClGemmNativeKernelConfig.h:52
arm_compute::mlgo::MLGOHeuristics::query_gemm_config_reshaped_only_rhs
std::pair< bool, GEMMConfigReshapedOnlyRHS > query_gemm_config_reshaped_only_rhs(const Query &query) const
Query the gemm configuration for reshaped only rhs kernel.
Definition: MLGOHeuristics.cpp:96
arm_compute::opencl::kernels::gemm::ClGemmReshapedOnlyRhsKernelConfigurationFactory::create
static std::unique_ptr< IClGemmKernelConfig > create(GPUTarget gpu)
Static method to call the CLGEMMReshapedOnlyRHS kernel configuration class accordingly with the GPU t...
Definition: ClGemmReshapedOnlyRhsKernelConfig.h:51
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::opencl::kernels::gemm::ClGemmReshapedKernelConfigurationFactory::create
static std::unique_ptr< IClGemmKernelConfig > create(GPUTarget gpu)
Static method to call the CLGEMMReshaped kernel configuration class accordingly with the GPU target.
Definition: ClGemmReshapedKernelConfig.h:51
arm_compute::cl_gemm::CLGEMMKernelSelectionFactory::create
static std::unique_ptr< ICLGEMMKernelSelection > create(GPUTarget gpu)
Static method to select the GEMM kernel accordingly with the GPU target and GEMM's dimensionality.
Definition: CLGEMMKernelSelection.h:47
arm_compute::cl_gemm::auto_heuristics::CommonQuery::m
unsigned int m
Number of rows for the lhs matrix.
Definition: CLGEMMAutoHeuristics.h:44
arm_compute::CLScheduler::get
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:112
arm_compute::cl_gemm::auto_heuristics::CommonQuery::gpu_target
GPUTarget gpu_target
Which GPUTarget to query about.
Definition: CLGEMMAutoHeuristics.h:42
ICLGEMMKernelSelection.h
arm_compute::cl_gemm::auto_heuristics::select_mlgo_gemm_config_reshaped
GEMMConfigResult select_mlgo_gemm_config_reshaped(const CommonQuery &query)
Select gemm config based on mlgo heuristics.
Definition: CLGEMMAutoHeuristics.cpp:139
arm_compute::GEMMLHSMatrixInfo
GEMM LHS (Left Hand Side) matrix information.
Definition: Types.h:1896
ARM_COMPUTE_LOG_INFO_MSG_CORE
#define ARM_COMPUTE_LOG_INFO_MSG_CORE(msg)
Log information level message to the core system logger.
Definition: Log.h:87
arm_compute::CLGEMMKernelSelectionParams::k
unsigned int k
Number of rows for the rhs matrix.
Definition: CLTypes.h:48
arm_compute::cl_gemm::auto_heuristics::CommonQuery::b
unsigned int b
Batch size.
Definition: CLGEMMAutoHeuristics.h:47
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::to_string
std::string to_string(const ClComponentElementwiseBinary::Attributes::ElementwiseOp &op)
Formatted output of the arm_compute::experimental::dynamic_fusion::ClComponentElementwiseBinary::Attr...
Definition: ElementwiseBinary.h:68
ClGemmReshapedKernelConfig.h
arm_compute::mlgo::MLGOHeuristics::query_gemm_config_reshaped
std::pair< bool, GEMMConfigReshaped > query_gemm_config_reshaped(const Query &query) const
Query the gemm configuration for reshaped kernel.
Definition: MLGOHeuristics.cpp:115
arm_compute::mlgo::GEMMConfigNative
GEMM Configuration for Native kernel.
Definition: Common.h:46
arm_compute::cl_gemm::auto_heuristics::select_default_gemm_config_native
GEMMConfigResult select_default_gemm_config_native(const CommonQuery &query)
Select gemm config based on default heuristics.
Definition: CLGEMMAutoHeuristics.cpp:166
arm_compute::mlgo::MLGOHeuristics::query_gemm_config_native
std::pair< bool, GEMMConfigNative > query_gemm_config_native(const Query &query) const
Query the gemm configuration for native kernel.
Definition: MLGOHeuristics.cpp:77
ClGemmNativeKernelConfig.h
arm_compute::cl_gemm::auto_heuristics::select_default_gemm_kernel
GEMMTypeResult select_default_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run)
Select gemm type based on default heuristics.
Definition: CLGEMMAutoHeuristics.cpp:72
arm_compute::cl_gemm::auto_heuristics::CommonQuery::data_type
DataType data_type
Data type.
Definition: CLGEMMAutoHeuristics.h:43
arm_compute::GEMMRHSMatrixInfo
GEMM RHS (Right Hand Side) matrix information.
Definition: Types.h:1911
Validate.h
CLGEMMAutoHeuristics.h
arm_compute::CLGEMMHeuristicsHandle::get
const mlgo::MLGOHeuristics * get() const
Return a pointer to underlying heuristics for querying purposes.
Definition: CLGEMMHeuristicsHandle.cpp:39
arm_compute::cl_gemm::auto_heuristics::select_mlgo_gemm_config_native
GEMMConfigResult select_mlgo_gemm_config_native(const CommonQuery &query)
Select gemm config based on mlgo heuristics.
Definition: CLGEMMAutoHeuristics.cpp:176