Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 1 | /* |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 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 "arm_compute/runtime/CL/tuners/CLTuningParametersList.h" |
| 25 | |
| 26 | namespace arm_compute |
| 27 | { |
| 28 | namespace cl_tuner |
| 29 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | constexpr unsigned int max_lws_supported_x{64u}; |
| 31 | constexpr unsigned int max_lws_supported_y{32u}; |
| 32 | constexpr unsigned int max_lws_supported_z{32u}; |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 33 | |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 34 | /** Non instantiable base class for Tuning parameters combinations that use Index2Coord mapping */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 35 | class CLTuningParametersList : public ICLTuningParametersList |
| 36 | { |
| 37 | protected: |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 38 | /* Shape of 4-D search space */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | TensorShape search_space_shape{0, 0, 0, 0}; |
| 40 | std::vector<unsigned int> _lws_x{0}; |
| 41 | std::vector<unsigned int> _lws_y{0}; |
| 42 | std::vector<unsigned int> _lws_z{0}; |
| 43 | std::vector<int> _wbsm{0}; /* Modify the batches size of workgroups distributed to compute units. |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 44 | The value is in the range [-31,+31]. |
| 45 | When 0, the runtime-selected wbs used is unmodified. */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 46 | |
| 47 | /** Constructor */ |
| 48 | CLTuningParametersList() = default; |
| 49 | /** Copy Constructor */ |
| 50 | CLTuningParametersList(const CLTuningParametersList &) = default; |
| 51 | /** Move Constructor */ |
| 52 | CLTuningParametersList(CLTuningParametersList &&) noexcept(true) = default; |
| 53 | /** Assignment */ |
| 54 | CLTuningParametersList &operator=(const CLTuningParametersList &) = default; |
| 55 | /** Move Assignment */ |
| 56 | CLTuningParametersList &operator=(CLTuningParametersList &&) noexcept(true) = default; |
| 57 | /** Destructor */ |
| 58 | virtual ~CLTuningParametersList() = default; |
| 59 | |
| 60 | // Inherited methods overridden: |
| 61 | virtual size_t size() override; |
| 62 | }; |
| 63 | |
| 64 | /** Exhaustive list of all possible Tuning parameters (lws) values */ |
| 65 | class CLTuningParametersListExhaustive : public CLTuningParametersList |
| 66 | { |
| 67 | public: |
| 68 | /** Prevent default constructor calls */ |
| 69 | CLTuningParametersListExhaustive() = delete; |
| 70 | /** Constructor */ |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 71 | CLTuningParametersListExhaustive(const cl::NDRange &gws, CLTuningInfo tuning_info); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 72 | /** Copy Constructor */ |
| 73 | CLTuningParametersListExhaustive(const CLTuningParametersListExhaustive &) = default; |
| 74 | /** Move Constructor */ |
| 75 | CLTuningParametersListExhaustive(CLTuningParametersListExhaustive &&) noexcept(true) = default; |
| 76 | /** Assignment */ |
| 77 | CLTuningParametersListExhaustive &operator=(const CLTuningParametersListExhaustive &) = default; |
| 78 | /** Move Assignment */ |
| 79 | CLTuningParametersListExhaustive &operator=(CLTuningParametersListExhaustive &&) noexcept(true) = default; |
| 80 | /** Destructor */ |
| 81 | ~CLTuningParametersListExhaustive() = default; |
| 82 | |
| 83 | // Inherited methods overridden: |
| 84 | CLTuningParams operator[](size_t) override; |
| 85 | }; |
| 86 | |
| 87 | /** A subset of LWS values that are either factors of gws when gws[2] < 16 or power of 2 */ |
| 88 | class CLTuningParametersListNormal : public CLTuningParametersList |
| 89 | { |
| 90 | public: |
| 91 | /** Constructor */ |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 92 | CLTuningParametersListNormal(const cl::NDRange &gws, CLTuningInfo tuning_info); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 93 | /** Copy Constructor */ |
| 94 | CLTuningParametersListNormal(const CLTuningParametersListNormal &) = default; |
| 95 | /** Move Constructor */ |
| 96 | CLTuningParametersListNormal(CLTuningParametersListNormal &&) noexcept(true) = default; |
| 97 | /** Assignment */ |
| 98 | CLTuningParametersListNormal &operator=(const CLTuningParametersListNormal &) = default; |
| 99 | /** Move Assignment */ |
| 100 | CLTuningParametersListNormal &operator=(CLTuningParametersListNormal &&) noexcept(true) = default; |
| 101 | /** Destructor */ |
| 102 | ~CLTuningParametersListNormal() = default; |
| 103 | |
| 104 | // Inherited methods overridden: |
| 105 | CLTuningParams operator[](size_t) override; |
| 106 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 107 | /** Prevent default constructor calls */ |
| 108 | CLTuningParametersListNormal() = default; |
| 109 | |
| 110 | private: |
| 111 | /** Utility function used to initialize the LWS values to test. |
| 112 | * Only the LWS values which are power of 2 or satisfy the modulo conditions with GWS are taken into account by the CLTuner |
| 113 | * |
| 114 | * @param[in, out] lws Vector of LWS to test |
| 115 | * @param[in] gws Size of the specific GWS |
| 116 | * @param[in] lws_max Max LWS value allowed to be tested |
| 117 | * @param[in] mod_let_one True if the results of the modulo operation between gws and the lws can be less than one. |
| 118 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 119 | void |
| 120 | initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /** A minimal subset of LWS values that only have 1,2 and 4/8 */ |
| 124 | class CLTuningParametersListRapid : public CLTuningParametersListNormal |
| 125 | { |
| 126 | public: |
| 127 | /** Prevent default constructor calls */ |
| 128 | CLTuningParametersListRapid() = delete; |
| 129 | /** Constructor */ |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 130 | CLTuningParametersListRapid(const cl::NDRange &gws, CLTuningInfo tuning_info); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 131 | /** Copy Constructor */ |
| 132 | CLTuningParametersListRapid(const CLTuningParametersListRapid &) = default; |
| 133 | /** Move Constructor */ |
| 134 | CLTuningParametersListRapid(CLTuningParametersListRapid &&) noexcept(true) = default; |
| 135 | /** Assignment */ |
| 136 | CLTuningParametersListRapid &operator=(const CLTuningParametersListRapid &) = default; |
| 137 | /** Move Assignment */ |
| 138 | CLTuningParametersListRapid &operator=(CLTuningParametersListRapid &&) noexcept(true) = default; |
| 139 | /** Destructor */ |
| 140 | virtual ~CLTuningParametersListRapid() = default; |
| 141 | |
| 142 | private: |
| 143 | /** Utility function used to initialize the LWS values to test. |
| 144 | * Only the LWS values that have 1,2 and 4/8 for each dimension are taken into account by the CLTuner |
| 145 | * |
| 146 | * @param[in, out] lws Vector of LWS to test |
| 147 | * @param[in] lws_max Max LWS value allowed to be tested |
| 148 | */ |
| 149 | void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max); |
| 150 | }; |
| 151 | |
| 152 | size_t CLTuningParametersList::size() |
| 153 | { |
| 154 | return search_space_shape.total_size(); |
| 155 | } |
| 156 | |
| 157 | CLTuningParams CLTuningParametersListExhaustive::operator[](size_t index) |
| 158 | { |
| 159 | ARM_COMPUTE_ERROR_ON(index >= size()); |
| 160 | auto coords = index2coords(search_space_shape, index); |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 161 | return CLTuningParams(coords[0] + 1U, coords[1] + 1U, coords[2] + 1U, static_cast<int>(coords[3])); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 164 | CLTuningParametersListExhaustive::CLTuningParametersListExhaustive(const cl::NDRange &gws, CLTuningInfo tuning_info) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 165 | { |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 166 | const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), max_lws_supported_x); |
| 167 | const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), max_lws_supported_y); |
| 168 | const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), max_lws_supported_z); |
| 169 | |
| 170 | search_space_shape[0] = lws_x_max; |
| 171 | search_space_shape[1] = lws_y_max; |
| 172 | search_space_shape[2] = lws_z_max; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 173 | search_space_shape[3] = 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 174 | if (tuning_info.tune_wbsm) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 175 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 176 | _wbsm = {-3, -2, -1, 0, 1, 2, 3}; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 177 | search_space_shape[3] = _wbsm.size(); |
| 178 | } |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | CLTuningParams CLTuningParametersListNormal::operator[](size_t index) |
| 182 | { |
| 183 | ARM_COMPUTE_ERROR_ON(index >= size()); |
| 184 | auto coords = index2coords(search_space_shape, index); |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 185 | return CLTuningParams(_lws_x[coords[0]], _lws_y[coords[1]], _lws_z[coords[2]], _wbsm[coords[3]]); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 188 | CLTuningParametersListNormal::CLTuningParametersListNormal(const cl::NDRange &gws, CLTuningInfo tuning_info) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 189 | { |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 190 | const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), max_lws_supported_x); |
| 191 | const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), max_lws_supported_y); |
| 192 | const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), max_lws_supported_z); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 193 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 194 | // Initialize the tuning parameters values to test |
| 195 | _lws_x = {}; |
| 196 | _lws_y = {}; |
| 197 | _lws_z = {}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | initialize_lws_values(_lws_x, gws[0], lws_x_max, |
| 199 | gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16 |
| 200 | initialize_lws_values(_lws_y, gws[1], lws_y_max, |
| 201 | gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16 |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 202 | initialize_lws_values(_lws_z, gws[2], lws_z_max, false); |
| 203 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 204 | search_space_shape[0] = _lws_x.size(); |
| 205 | search_space_shape[1] = _lws_y.size(); |
| 206 | search_space_shape[2] = _lws_z.size(); |
| 207 | search_space_shape[3] = 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 208 | if (tuning_info.tune_wbsm) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 209 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 210 | _wbsm = {-2, -1, 0, 1, 2}; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 211 | search_space_shape[3] = _wbsm.size(); |
| 212 | } |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 215 | void CLTuningParametersListNormal::initialize_lws_values(std::vector<unsigned int> &lws, |
| 216 | unsigned int gws, |
| 217 | unsigned int lws_max, |
| 218 | bool mod_let_one) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 219 | { |
| 220 | lws.push_back(1); |
| 221 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 222 | for (unsigned int i = 2; i <= lws_max; ++i) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 223 | { |
| 224 | // Power of two condition |
| 225 | const bool is_power_of_two = (i & (i - 1)) == 0; |
| 226 | |
| 227 | // Condition for the module accordingly with the mod_let_one flag |
| 228 | const bool mod_cond = mod_let_one ? (gws % i) <= 1 : (gws % i) == 0; |
| 229 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 230 | if (mod_cond || is_power_of_two) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 231 | { |
| 232 | lws.push_back(i); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 237 | CLTuningParametersListRapid::CLTuningParametersListRapid(const cl::NDRange &gws, CLTuningInfo tuning_info) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 238 | { |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 239 | const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), 8u); // Limit exploration to 1 - 8 |
| 240 | const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), 4u); // Limit exploration to 1 - 4 |
| 241 | const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), 4u); // Limit exploration to 1 - 4 |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 242 | |
| 243 | // Initialize the LWS values to test |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 244 | _lws_x = {}; |
| 245 | _lws_y = {}; |
| 246 | _lws_z = {}; |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 247 | initialize_lws_values(_lws_x, lws_x_max); |
| 248 | initialize_lws_values(_lws_y, lws_y_max); |
| 249 | initialize_lws_values(_lws_z, lws_z_max); |
| 250 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 251 | search_space_shape[0] = _lws_x.size(); |
| 252 | search_space_shape[1] = _lws_y.size(); |
| 253 | search_space_shape[2] = _lws_z.size(); |
| 254 | search_space_shape[3] = 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 255 | if (tuning_info.tune_wbsm) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 256 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 257 | _wbsm = {-1, 0, 1}; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 258 | search_space_shape[3] = _wbsm.size(); |
| 259 | } |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void CLTuningParametersListRapid::initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max) |
| 263 | { |
| 264 | lws.push_back(1); |
| 265 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 266 | for (unsigned int i = 2; i <= lws_max; i *= 4) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 267 | { |
| 268 | lws.push_back(i); |
| 269 | } |
| 270 | } |
| 271 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 272 | std::unique_ptr<ICLTuningParametersList> get_tuning_parameters_list(CLTuningInfo tuning_info, const cl::NDRange &gws) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 273 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 274 | switch (tuning_info.tuner_mode) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 275 | { |
| 276 | case CLTunerMode::EXHAUSTIVE: |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 277 | return std::make_unique<CLTuningParametersListExhaustive>(gws, tuning_info); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 278 | case CLTunerMode::NORMAL: |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 279 | return std::make_unique<CLTuningParametersListNormal>(gws, tuning_info); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 280 | case CLTunerMode::RAPID: |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 281 | return std::make_unique<CLTuningParametersListRapid>(gws, tuning_info); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 282 | default: |
| 283 | return nullptr; |
| 284 | } |
| 285 | } |
| 286 | } // namespace cl_tuner |
| 287 | } // namespace arm_compute |