SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 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 | */ |
| 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 Bottini | 8958167 | 2021-02-11 12:34:47 +0000 | [diff] [blame] | 31 | #include "src/core/CL/gemm/CLGEMMHelpers.h" |
SiCong Li | db35345 | 2021-02-08 15:16:13 +0000 | [diff] [blame] | 32 | #include "src/core/CL/gemm/native/CLGEMMNativeKernelConfiguration.h" |
SiCong Li | 8c23ba1 | 2021-02-08 14:19:23 +0000 | [diff] [blame] | 33 | #include "src/core/CL/gemm/reshaped/CLGEMMReshapedKernelConfiguration.h" |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 34 | #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 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace cl_gemm |
| 43 | { |
| 44 | namespace auto_heuristics |
| 45 | { |
SiCong Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 46 | GEMMTypeResult select_mlgo_gemm_kernel(const CommonQuery &query, bool reshape_b_only_on_first_run) |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 47 | { |
SiCong Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 48 | 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 Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 52 | if(mlgo_heuristics != nullptr) |
| 53 | { |
SiCong Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 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 }); |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 55 | } |
SiCong Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 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()); |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 71 | |
| 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 Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 80 | const auto kernel_type = default_heuristics->select_kernel(params); |
| 81 | return GEMMTypeResult(true, kernel_type); |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | GEMMConfigResult 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 | |
| 94 | GEMMConfigResult 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 Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 100 | const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics(); |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 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()); |
Georgios Pinitas | 261df74 | 2021-02-23 00:00:42 +0000 | [diff] [blame^] | 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); |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
| 114 | ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed"); |
| 115 | } |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 116 | return GEMMConfigResult{ valid, lhs_info, rhs_info }; |
| 117 | } |
SiCong Li | 8c23ba1 | 2021-02-08 14:19:23 +0000 | [diff] [blame] | 118 | |
| 119 | GEMMConfigResult 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 | |
| 129 | GEMMConfigResult 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 Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 135 | const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics(); |
SiCong Li | 8c23ba1 | 2021-02-08 14:19:23 +0000 | [diff] [blame] | 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()); |
Georgios Pinitas | 261df74 | 2021-02-23 00:00:42 +0000 | [diff] [blame^] | 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); |
SiCong Li | 8c23ba1 | 2021-02-08 14:19:23 +0000 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
| 148 | ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed"); |
| 149 | } |
SiCong Li | 8c23ba1 | 2021-02-08 14:19:23 +0000 | [diff] [blame] | 150 | return GEMMConfigResult{ valid, lhs_info, rhs_info }; |
| 151 | } |
SiCong Li | db35345 | 2021-02-08 15:16:13 +0000 | [diff] [blame] | 152 | |
| 153 | GEMMConfigResult 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 | |
| 163 | GEMMConfigResult 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 Li | 1a28e73 | 2021-02-10 16:57:33 +0000 | [diff] [blame] | 169 | const auto mlgo_heuristics = CLScheduler::get().gemm_heuristics(); |
SiCong Li | db35345 | 2021-02-08 15:16:13 +0000 | [diff] [blame] | 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()); |
Georgios Pinitas | 261df74 | 2021-02-23 00:00:42 +0000 | [diff] [blame^] | 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); |
SiCong Li | db35345 | 2021-02-08 15:16:13 +0000 | [diff] [blame] | 179 | } |
| 180 | else |
| 181 | { |
| 182 | ARM_COMPUTE_LOG_INFO_MSG_CORE("MLGOHeuristics query failed"); |
| 183 | } |
SiCong Li | db35345 | 2021-02-08 15:16:13 +0000 | [diff] [blame] | 184 | return GEMMConfigResult{ valid, lhs_info, rhs_info }; |
| 185 | } |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 186 | } // namespace auto_heuristics |
SiCong Li | 8c23ba1 | 2021-02-08 14:19:23 +0000 | [diff] [blame] | 187 | |
SiCong Li | bd8b1e2 | 2021-02-04 13:07:09 +0000 | [diff] [blame] | 188 | } // namespace cl_gemm |
| 189 | } // namespace arm_compute |