blob: 445638f01fa7c33cb1c8c926e2f215b93cded177 [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
SiCong Li47f177e2023-02-22 17:24:09 +00002 * Copyright (c) 2017-2023 Arm Limited.
Gian Marcode691f02017-09-08 16:13:11 +01003 *
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/CLTuner.h"
Manuel Bottinib56c1752020-11-18 17:56:30 +000025#include "arm_compute/runtime/CL/tuners/CLTuningParametersList.h"
Gian Marcode691f02017-09-08 16:13:11 +010026
Gian Marco85e6f512018-02-01 16:57:48 +000027#include "arm_compute/core/Error.h"
Gian Marcode691f02017-09-08 16:13:11 +010028#include "arm_compute/runtime/CL/CLScheduler.h"
SiCong Li47f177e2023-02-22 17:24:09 +000029#include "src/common/utils/Log.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010030#include "src/core/CL/ICLKernel.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000031#include "support/StringSupport.h"
Gian Marcode691f02017-09-08 16:13:11 +010032
Anthony Barbier317fa7f2018-03-01 10:11:22 +000033#include <cerrno>
Anthony Barbier8db83182018-02-27 13:08:00 +000034#include <fstream>
Gian Marcode691f02017-09-08 16:13:11 +010035#include <limits>
Gian Marcode691f02017-09-08 16:13:11 +010036
Gian Marco Iodicea74923c2019-01-31 17:06:54 +000037namespace arm_compute
38{
Manuel Bottinib56c1752020-11-18 17:56:30 +000039CLTuner::CLTuner(bool tune_new_kernels, CLTuningInfo tuning_info)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000040 : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info)
Gian Marcode691f02017-09-08 16:13:11 +010041{
42}
43
SiCong Li0a486cf2022-04-07 17:41:51 +010044struct CLTuner::IKernelData
45{
46 virtual ~IKernelData() = default;
47 virtual void do_run(ICLKernel &kernel, cl::CommandQueue &queue) = 0;
48};
49struct DefaultKernelData : public CLTuner::IKernelData
50{
51 DefaultKernelData(ITensorPack &tensors)
52 : _tensors{ tensors }
53 {
54 }
55 ~DefaultKernelData() override = default;
56 void do_run(ICLKernel &kernel, cl::CommandQueue &queue) override
57 {
58 const bool inject_memory = !_tensors.empty();
59 inject_memory ? kernel.run_op(_tensors, kernel.window(), queue) : kernel.run(kernel.window(), queue);
60 }
61
62private:
63 ITensorPack &_tensors;
64};
65
Anthony Barbierf5dcf792018-02-28 18:04:45 +000066bool CLTuner::kernel_event_is_set() const
67{
68 return _kernel_event() != nullptr;
69}
Gian Marco85e6f512018-02-01 16:57:48 +000070void CLTuner::set_cl_kernel_event(cl_event kernel_event)
71{
72 _kernel_event = kernel_event;
73}
74
Anthony Barbier8db83182018-02-27 13:08:00 +000075void CLTuner::set_tune_new_kernels(bool tune_new_kernels)
76{
77 _tune_new_kernels = tune_new_kernels;
78}
Anthony Barbier8b811952018-02-28 13:47:58 +000079bool CLTuner::tune_new_kernels() const
80{
81 return _tune_new_kernels;
82}
Anthony Barbier8db83182018-02-27 13:08:00 +000083
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010084void CLTuner::set_tuner_mode(CLTunerMode mode)
85{
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000086 _tuning_info.tuner_mode = mode;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010087}
Manuel Bottinib56c1752020-11-18 17:56:30 +000088
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000089void CLTuner::tune_kernel_static(ICLKernel &kernel)
90{
91 ARM_COMPUTE_UNUSED(kernel);
92}
93
94void CLTuner::tune_kernel_dynamic(ICLKernel &kernel)
Gian Marcode691f02017-09-08 16:13:11 +010095{
Georgios Pinitas0499dff2020-07-31 22:21:38 +010096 ITensorPack pack;
97 tune_kernel_dynamic(kernel, pack);
Georgios Pinitas9c82e012020-07-17 12:47:56 +010098}
99
SiCong Li0a486cf2022-04-07 17:41:51 +0100100void CLTuner::do_tune_kernel_dynamic(ICLKernel &kernel, IKernelData *data)
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100101{
Giorgio Arena5d42b462019-07-26 15:54:20 +0100102 // Get the configuration ID from the kernel and append GPU target name and number of available compute units
103 const std::string config_id = kernel.config_id() + "_" + string_from_target(kernel.get_target()) + "_MP" + support::cpp11::to_string(CLKernelLibrary::get().get_num_compute_units());
Anthony Barbier8db83182018-02-27 13:08:00 +0000104
Giorgio Arena5d42b462019-07-26 15:54:20 +0100105 // Check if we need to find the Optimal LWS. If the kernel's config_id is equal to default_config_id, the kernel does not require to be tuned
106 if(kernel.config_id() != arm_compute::default_config_id)
Gian Marco85e6f512018-02-01 16:57:48 +0000107 {
Manuel Bottinib56c1752020-11-18 17:56:30 +0000108 auto p = _tuning_params_table.find(config_id);
Anthony Barbier8db83182018-02-27 13:08:00 +0000109
Manuel Bottinib56c1752020-11-18 17:56:30 +0000110 if(p == _tuning_params_table.end())
Anthony Barbier8db83182018-02-27 13:08:00 +0000111 {
112 if(_tune_new_kernels)
113 {
114 // Find the optimal LWS for the kernel
SiCong Li0a486cf2022-04-07 17:41:51 +0100115 CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, data);
Anthony Barbier8db83182018-02-27 13:08:00 +0000116
117 // Insert the optimal LWS in the table
Manuel Bottinib56c1752020-11-18 17:56:30 +0000118 add_tuning_params(config_id, opt_tuning_params);
Anthony Barbier8db83182018-02-27 13:08:00 +0000119
120 // Set Local-Workgroup-Size
Manuel Bottinib56c1752020-11-18 17:56:30 +0000121 kernel.set_lws_hint(opt_tuning_params.get_lws());
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000122 if(_tuning_info.tune_wbsm)
123 {
124 kernel.set_wbsm_hint(opt_tuning_params.get_wbsm());
125 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000126 }
127 }
128 else
129 {
130 // Set Local-Workgroup-Size
Manuel Bottinib56c1752020-11-18 17:56:30 +0000131 kernel.set_lws_hint(p->second.get_lws());
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000132 if(_tuning_info.tune_wbsm)
133 {
134 kernel.set_wbsm_hint(p->second.get_wbsm());
135 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000136 }
137 }
138}
SiCong Li0a486cf2022-04-07 17:41:51 +0100139void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors)
140{
141 DefaultKernelData data{ tensors };
142
143 do_tune_kernel_dynamic(kernel, &data);
144}
145
Manuel Bottinib56c1752020-11-18 17:56:30 +0000146void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params)
147{
148 _tuning_params_table.emplace(kernel_id, optimal_tuning_params);
149}
150
SiCong Li0a486cf2022-04-07 17:41:51 +0100151CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, IKernelData *data)
Anthony Barbier8db83182018-02-27 13:08:00 +0000152{
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000153 // Profiling queue
154 cl::CommandQueue queue_profiler;
155
156 // Extract real OpenCL function to intercept
Anthony Barbier8db83182018-02-27 13:08:00 +0000157 if(real_clEnqueueNDRangeKernel == nullptr)
158 {
159 real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
Gian Marco85e6f512018-02-01 16:57:48 +0000160 }
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000161
162 // Get the default queue
163 cl::CommandQueue default_queue = CLScheduler::get().queue();
164
165 // Check if we can use the OpenCL timer with the default queue
166 cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
167
168 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
169 {
170 // Set the queue for profiling
171 queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
172 }
173 else
174 {
175 queue_profiler = default_queue;
176 }
177
Gian Marco85e6f512018-02-01 16:57:48 +0000178 // Start intercepting enqueues:
Anthony Barbier48c19f12018-04-20 11:31:52 +0100179 auto interceptor = [this](cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, const size_t *gwo, const size_t *gws, const size_t *lws, cl_uint num_events_in_wait_list,
180 const cl_event * event_wait_list, cl_event * event)
181 {
Anthony Barbier48c19f12018-04-20 11:31:52 +0100182 if(this->kernel_event_is_set())
183 {
184 // If the event is already set it means the kernel enqueue is sliced: given that we only time the first slice we can save time by skipping the other enqueues.
185 return CL_SUCCESS;
186 }
187 cl_event tmp;
188 cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
189
190 // Set OpenCL event
191 this->set_cl_kernel_event(tmp);
192
Vidhya Sudhan Loganathanca65af32019-02-07 11:14:42 +0000193 if(event != nullptr)
194 {
195 //return cl_event from the intercepted call
196 clRetainEvent(tmp);
197 *event = tmp;
198 }
Anthony Barbier48c19f12018-04-20 11:31:52 +0100199 return retval;
200 };
201 CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor;
Gian Marcode691f02017-09-08 16:13:11 +0100202
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100203 // Run the kernel with default lws to be used as baseline
SiCong Li0a486cf2022-04-07 17:41:51 +0100204 data->do_run(kernel, queue_profiler);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100205
SiCong Li47f177e2023-02-22 17:24:09 +0000206 /// Get the cached gws used by the kernel
207 /// NOTE: The window configured inside configure() is usually changed in run(). Thus we should not calculate gws
208 /// from this static window. Instead we get the real gws used (and cached) by run() in the previous step.
209 /// This is only a temporary workaround. An ideal solution involves decoupling the execution window from run() / run_op()
210 /// Please see COMPMID-5934
211 cl::NDRange gws = kernel.get_cached_gws();
212 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO,
213 "[CLTuner] Kernel with config_id '%s' uses %s as the upper-bound for lws search",
214 kernel.config_id().c_str(), to_string(gws).c_str());
215
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100216 queue_profiler.finish();
217
218 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
219 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
220 cl_ulong min_exec_time = end - start;
221 _kernel_event = nullptr;
222
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000223 CLTuningParams opt_tuning_params(cl::NullRange, 0);
Gian Marcode691f02017-09-08 16:13:11 +0100224
Manuel Bottinib56c1752020-11-18 17:56:30 +0000225 // Construct the list of tuning parameters values to be tested based on the tuner mode.
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000226 auto tuning_list = cl_tuner::get_tuning_parameters_list(_tuning_info, gws);
227 for(size_t i = 0; i < tuning_list->size(); ++i)
Gian Marco85e6f512018-02-01 16:57:48 +0000228 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000229 CLTuningParams tuning_test = (*tuning_list)[i];
230 // Setting the lws
231 cl::NDRange lws_test = tuning_test.get_lws();
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100232 auto x = lws_test[0];
233 auto y = lws_test[1];
234 auto z = lws_test[2];
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100235 const bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100236
237 if(invalid_lws)
Gian Marcode691f02017-09-08 16:13:11 +0100238 {
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100239 continue;
240 }
Gian Marco85e6f512018-02-01 16:57:48 +0000241
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100242 kernel.set_lws_hint(lws_test);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000243 if(_tuning_info.tune_wbsm && CLKernelLibrary::get().is_wbsm_supported())
244 {
245 cl_int wbsm_test = tuning_test.get_wbsm();
246 kernel.set_wbsm_hint(wbsm_test);
247 }
SiCong Li47f177e2023-02-22 17:24:09 +0000248 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO,
249 "[CLTuner] Trying LWS: %s, WBSM: %d",
250 to_string(kernel.lws_hint()).c_str(), kernel.wbsm_hint());
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000251
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100252 // Run the kernel
SiCong Li0a486cf2022-04-07 17:41:51 +0100253 data->do_run(kernel, queue_profiler);
Gian Marco85e6f512018-02-01 16:57:48 +0000254
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100255 queue_profiler.finish();
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000256
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100257 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
258 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
259 const cl_ulong diff = end - start;
260 _kernel_event = nullptr;
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100261
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100262 // Check the execution time
263 if(diff < min_exec_time)
264 {
265 min_exec_time = diff;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000266 opt_tuning_params.set_lws(tuning_test.get_lws());
267 if(_tuning_info.tune_wbsm)
268 {
269 opt_tuning_params.set_wbsm(tuning_test.get_wbsm());
270 }
Gian Marcode691f02017-09-08 16:13:11 +0100271 }
272 }
273
Gian Marco85e6f512018-02-01 16:57:48 +0000274 // Restore real function
Anthony Barbier8db83182018-02-27 13:08:00 +0000275 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000276 return opt_tuning_params;
Gian Marcode691f02017-09-08 16:13:11 +0100277}
278
Manuel Bottinib56c1752020-11-18 17:56:30 +0000279const std::unordered_map<std::string, CLTuningParams> &CLTuner::tuning_params_table() const
280{
281 return _tuning_params_table;
282}
283
284void CLTuner::import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table)
285{
286 _tuning_params_table.clear();
287 _tuning_params_table = tuning_params_table;
288}
289
Anthony Barbier8b811952018-02-28 13:47:58 +0000290void CLTuner::load_from_file(const std::string &filename)
Gian Marco85e6f512018-02-01 16:57:48 +0000291{
Anthony Barbier8b811952018-02-28 13:47:58 +0000292 std::ifstream fs;
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000293 fs.exceptions(std::ifstream::badbit);
Anthony Barbier8b811952018-02-28 13:47:58 +0000294 fs.open(filename, std::ios::in);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000295 if(!fs.is_open())
Anthony Barbier8db83182018-02-27 13:08:00 +0000296 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100297 ARM_COMPUTE_ERROR_VAR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000298 }
299 std::string line;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000300 bool header_line = true;
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000301 while(!std::getline(fs, line).fail())
302 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000303 if(header_line)
Anthony Barbier8db83182018-02-27 13:08:00 +0000304 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000305 header_line = false;
306 size_t pos_lws = line.find("lws");
307 size_t pos_wbsm = line.find("wbsm");
308 _tuning_info.tune_wbsm = false;
309 if(pos_lws != std::string::npos || pos_wbsm != std::string::npos)
Anthony Barbier8db83182018-02-27 13:08:00 +0000310 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000311 // The file has in the first line the parameters it has been tuned on
312 if(pos_wbsm != std::string::npos)
313 {
314 _tuning_info.tune_wbsm = true;
315 }
316 // Once the line with the tuning parameter is read we can
317 // read the next one to start collecting the values
318 if(std::getline(fs, line).fail())
319 {
320 break;
321 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000322 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000323 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000324
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000325 CLTuningParams tuning_params;
326 size_t pos = line.find(";");
327 if(pos == std::string::npos)
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000328 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000329 ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000330 }
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000331 std::string kernel_id = line.substr(0, pos);
332 line.erase(0, pos + 1);
333 if(!tuning_params.from_string(_tuning_info, line))
334 {
335 ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
336 }
337 add_tuning_params(kernel_id, tuning_params);
Anthony Barbier8db83182018-02-27 13:08:00 +0000338 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000339 fs.close();
Gian Marco85e6f512018-02-01 16:57:48 +0000340}
341
Manuel Bottinib56c1752020-11-18 17:56:30 +0000342bool CLTuner::save_to_file(const std::string &filename) const
Gian Marco85e6f512018-02-01 16:57:48 +0000343{
Manuel Bottinib56c1752020-11-18 17:56:30 +0000344 if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty())
345 {
346 return false;
347 }
Anthony Barbier8b811952018-02-28 13:47:58 +0000348 std::ofstream fs;
349 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
350 fs.open(filename, std::ios::out);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000351 std::string header_string = "";
352 header_string += "lws";
353 if(_tuning_info.tune_wbsm)
354 {
355 if(!header_string.empty())
356 {
357 header_string += " ";
358 }
359 header_string += "wbsm";
360 }
361 fs << header_string << std::endl;
Manuel Bottinib56c1752020-11-18 17:56:30 +0000362 for(auto const &kernel_data : _tuning_params_table)
Anthony Barbier8db83182018-02-27 13:08:00 +0000363 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000364 CLTuningParams tun_pams(kernel_data.second);
365 fs << kernel_data.first << tun_pams.to_string(_tuning_info) << std::endl;
Anthony Barbier8db83182018-02-27 13:08:00 +0000366 }
367 fs.close();
Manuel Bottinib56c1752020-11-18 17:56:30 +0000368 return true;
Anthony Barbier8db83182018-02-27 13:08:00 +0000369}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000370} // namespace arm_compute