xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/CL/CLTuner.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2022 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTuner.h"
25*c217d954SCole Faust #include "arm_compute/runtime/CL/tuners/CLTuningParametersList.h"
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/Error.h"
28*c217d954SCole Faust #include "arm_compute/runtime/CL/CLScheduler.h"
29*c217d954SCole Faust #include "src/core/CL/ICLKernel.h"
30*c217d954SCole Faust #include "support/StringSupport.h"
31*c217d954SCole Faust 
32*c217d954SCole Faust #include <cerrno>
33*c217d954SCole Faust #include <fstream>
34*c217d954SCole Faust #include <limits>
35*c217d954SCole Faust 
36*c217d954SCole Faust namespace arm_compute
37*c217d954SCole Faust {
CLTuner(bool tune_new_kernels,CLTuningInfo tuning_info)38*c217d954SCole Faust CLTuner::CLTuner(bool tune_new_kernels, CLTuningInfo tuning_info)
39*c217d954SCole Faust     : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info)
40*c217d954SCole Faust {
41*c217d954SCole Faust }
42*c217d954SCole Faust 
43*c217d954SCole Faust struct CLTuner::IKernelData
44*c217d954SCole Faust {
45*c217d954SCole Faust     virtual ~IKernelData() = default;
46*c217d954SCole Faust     virtual void do_run(ICLKernel &kernel, cl::CommandQueue &queue) = 0;
47*c217d954SCole Faust };
48*c217d954SCole Faust struct DefaultKernelData : public CLTuner::IKernelData
49*c217d954SCole Faust {
DefaultKernelDataarm_compute::DefaultKernelData50*c217d954SCole Faust     DefaultKernelData(ITensorPack &tensors)
51*c217d954SCole Faust         : _tensors{ tensors }
52*c217d954SCole Faust     {
53*c217d954SCole Faust     }
54*c217d954SCole Faust     ~DefaultKernelData() override = default;
do_runarm_compute::DefaultKernelData55*c217d954SCole Faust     void do_run(ICLKernel &kernel, cl::CommandQueue &queue) override
56*c217d954SCole Faust     {
57*c217d954SCole Faust         const bool inject_memory = !_tensors.empty();
58*c217d954SCole Faust         inject_memory ? kernel.run_op(_tensors, kernel.window(), queue) : kernel.run(kernel.window(), queue);
59*c217d954SCole Faust     }
60*c217d954SCole Faust 
61*c217d954SCole Faust private:
62*c217d954SCole Faust     ITensorPack &_tensors;
63*c217d954SCole Faust };
64*c217d954SCole Faust 
kernel_event_is_set() const65*c217d954SCole Faust bool CLTuner::kernel_event_is_set() const
66*c217d954SCole Faust {
67*c217d954SCole Faust     return _kernel_event() != nullptr;
68*c217d954SCole Faust }
set_cl_kernel_event(cl_event kernel_event)69*c217d954SCole Faust void CLTuner::set_cl_kernel_event(cl_event kernel_event)
70*c217d954SCole Faust {
71*c217d954SCole Faust     _kernel_event = kernel_event;
72*c217d954SCole Faust }
73*c217d954SCole Faust 
set_tune_new_kernels(bool tune_new_kernels)74*c217d954SCole Faust void CLTuner::set_tune_new_kernels(bool tune_new_kernels)
75*c217d954SCole Faust {
76*c217d954SCole Faust     _tune_new_kernels = tune_new_kernels;
77*c217d954SCole Faust }
tune_new_kernels() const78*c217d954SCole Faust bool CLTuner::tune_new_kernels() const
79*c217d954SCole Faust {
80*c217d954SCole Faust     return _tune_new_kernels;
81*c217d954SCole Faust }
82*c217d954SCole Faust 
set_tuner_mode(CLTunerMode mode)83*c217d954SCole Faust void CLTuner::set_tuner_mode(CLTunerMode mode)
84*c217d954SCole Faust {
85*c217d954SCole Faust     _tuning_info.tuner_mode = mode;
86*c217d954SCole Faust }
87*c217d954SCole Faust 
tune_kernel_static(ICLKernel & kernel)88*c217d954SCole Faust void CLTuner::tune_kernel_static(ICLKernel &kernel)
89*c217d954SCole Faust {
90*c217d954SCole Faust     ARM_COMPUTE_UNUSED(kernel);
91*c217d954SCole Faust }
92*c217d954SCole Faust 
tune_kernel_dynamic(ICLKernel & kernel)93*c217d954SCole Faust void CLTuner::tune_kernel_dynamic(ICLKernel &kernel)
94*c217d954SCole Faust {
95*c217d954SCole Faust     ITensorPack pack;
96*c217d954SCole Faust     tune_kernel_dynamic(kernel, pack);
97*c217d954SCole Faust }
98*c217d954SCole Faust 
do_tune_kernel_dynamic(ICLKernel & kernel,IKernelData * data)99*c217d954SCole Faust void CLTuner::do_tune_kernel_dynamic(ICLKernel &kernel, IKernelData *data)
100*c217d954SCole Faust {
101*c217d954SCole Faust     // Get the configuration ID from the kernel and append GPU target name and number of available compute units
102*c217d954SCole Faust     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());
103*c217d954SCole Faust 
104*c217d954SCole Faust     // 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
105*c217d954SCole Faust     if(kernel.config_id() != arm_compute::default_config_id)
106*c217d954SCole Faust     {
107*c217d954SCole Faust         auto p = _tuning_params_table.find(config_id);
108*c217d954SCole Faust 
109*c217d954SCole Faust         if(p == _tuning_params_table.end())
110*c217d954SCole Faust         {
111*c217d954SCole Faust             if(_tune_new_kernels)
112*c217d954SCole Faust             {
113*c217d954SCole Faust                 // Find the optimal LWS for the kernel
114*c217d954SCole Faust                 CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, data);
115*c217d954SCole Faust 
116*c217d954SCole Faust                 // Insert the optimal LWS in the table
117*c217d954SCole Faust                 add_tuning_params(config_id, opt_tuning_params);
118*c217d954SCole Faust 
119*c217d954SCole Faust                 // Set Local-Workgroup-Size
120*c217d954SCole Faust                 kernel.set_lws_hint(opt_tuning_params.get_lws());
121*c217d954SCole Faust                 if(_tuning_info.tune_wbsm)
122*c217d954SCole Faust                 {
123*c217d954SCole Faust                     kernel.set_wbsm_hint(opt_tuning_params.get_wbsm());
124*c217d954SCole Faust                 }
125*c217d954SCole Faust             }
126*c217d954SCole Faust         }
127*c217d954SCole Faust         else
128*c217d954SCole Faust         {
129*c217d954SCole Faust             // Set Local-Workgroup-Size
130*c217d954SCole Faust             kernel.set_lws_hint(p->second.get_lws());
131*c217d954SCole Faust             if(_tuning_info.tune_wbsm)
132*c217d954SCole Faust             {
133*c217d954SCole Faust                 kernel.set_wbsm_hint(p->second.get_wbsm());
134*c217d954SCole Faust             }
135*c217d954SCole Faust         }
136*c217d954SCole Faust     }
137*c217d954SCole Faust }
tune_kernel_dynamic(ICLKernel & kernel,ITensorPack & tensors)138*c217d954SCole Faust void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors)
139*c217d954SCole Faust {
140*c217d954SCole Faust     DefaultKernelData data{ tensors };
141*c217d954SCole Faust 
142*c217d954SCole Faust     do_tune_kernel_dynamic(kernel, &data);
143*c217d954SCole Faust }
144*c217d954SCole Faust 
add_tuning_params(const std::string & kernel_id,CLTuningParams optimal_tuning_params)145*c217d954SCole Faust void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params)
146*c217d954SCole Faust {
147*c217d954SCole Faust     _tuning_params_table.emplace(kernel_id, optimal_tuning_params);
148*c217d954SCole Faust }
149*c217d954SCole Faust 
find_optimal_tuning_params(ICLKernel & kernel,IKernelData * data)150*c217d954SCole Faust CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, IKernelData *data)
151*c217d954SCole Faust {
152*c217d954SCole Faust     // Profiling queue
153*c217d954SCole Faust     cl::CommandQueue queue_profiler;
154*c217d954SCole Faust 
155*c217d954SCole Faust     // Extract real OpenCL function to intercept
156*c217d954SCole Faust     if(real_clEnqueueNDRangeKernel == nullptr)
157*c217d954SCole Faust     {
158*c217d954SCole Faust         real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
159*c217d954SCole Faust     }
160*c217d954SCole Faust 
161*c217d954SCole Faust     // Get the default queue
162*c217d954SCole Faust     cl::CommandQueue default_queue = CLScheduler::get().queue();
163*c217d954SCole Faust 
164*c217d954SCole Faust     // Check if we can use the OpenCL timer with the default queue
165*c217d954SCole Faust     cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
166*c217d954SCole Faust 
167*c217d954SCole Faust     if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
168*c217d954SCole Faust     {
169*c217d954SCole Faust         // Set the queue for profiling
170*c217d954SCole Faust         queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
171*c217d954SCole Faust     }
172*c217d954SCole Faust     else
173*c217d954SCole Faust     {
174*c217d954SCole Faust         queue_profiler = default_queue;
175*c217d954SCole Faust     }
176*c217d954SCole Faust 
177*c217d954SCole Faust     // Start intercepting enqueues:
178*c217d954SCole Faust     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,
179*c217d954SCole Faust                               const cl_event * event_wait_list, cl_event * event)
180*c217d954SCole Faust     {
181*c217d954SCole Faust         if(this->kernel_event_is_set())
182*c217d954SCole Faust         {
183*c217d954SCole Faust             // 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.
184*c217d954SCole Faust             return CL_SUCCESS;
185*c217d954SCole Faust         }
186*c217d954SCole Faust         cl_event tmp;
187*c217d954SCole Faust         cl_int   retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
188*c217d954SCole Faust 
189*c217d954SCole Faust         // Set OpenCL event
190*c217d954SCole Faust         this->set_cl_kernel_event(tmp);
191*c217d954SCole Faust 
192*c217d954SCole Faust         if(event != nullptr)
193*c217d954SCole Faust         {
194*c217d954SCole Faust             //return cl_event from the intercepted call
195*c217d954SCole Faust             clRetainEvent(tmp);
196*c217d954SCole Faust             *event = tmp;
197*c217d954SCole Faust         }
198*c217d954SCole Faust         return retval;
199*c217d954SCole Faust     };
200*c217d954SCole Faust     CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor;
201*c217d954SCole Faust 
202*c217d954SCole Faust     cl::NDRange gws = ICLKernel::gws_from_window(kernel.window());
203*c217d954SCole Faust 
204*c217d954SCole Faust     // Run the kernel with default lws to be used as baseline
205*c217d954SCole Faust     data->do_run(kernel, queue_profiler);
206*c217d954SCole Faust 
207*c217d954SCole Faust     queue_profiler.finish();
208*c217d954SCole Faust 
209*c217d954SCole Faust     const cl_ulong start         = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
210*c217d954SCole Faust     const cl_ulong end           = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
211*c217d954SCole Faust     cl_ulong       min_exec_time = end - start;
212*c217d954SCole Faust     _kernel_event                = nullptr;
213*c217d954SCole Faust 
214*c217d954SCole Faust     CLTuningParams opt_tuning_params(cl::NullRange, 0);
215*c217d954SCole Faust 
216*c217d954SCole Faust     // Construct the list of tuning parameters values to be tested based on the tuner mode.
217*c217d954SCole Faust     auto tuning_list = cl_tuner::get_tuning_parameters_list(_tuning_info, gws);
218*c217d954SCole Faust     for(size_t i = 0; i < tuning_list->size(); ++i)
219*c217d954SCole Faust     {
220*c217d954SCole Faust         CLTuningParams tuning_test = (*tuning_list)[i];
221*c217d954SCole Faust         // Setting the lws
222*c217d954SCole Faust         cl::NDRange lws_test    = tuning_test.get_lws();
223*c217d954SCole Faust         auto        x           = lws_test[0];
224*c217d954SCole Faust         auto        y           = lws_test[1];
225*c217d954SCole Faust         auto        z           = lws_test[2];
226*c217d954SCole Faust         const bool  invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1);
227*c217d954SCole Faust 
228*c217d954SCole Faust         if(invalid_lws)
229*c217d954SCole Faust         {
230*c217d954SCole Faust             continue;
231*c217d954SCole Faust         }
232*c217d954SCole Faust 
233*c217d954SCole Faust         kernel.set_lws_hint(lws_test);
234*c217d954SCole Faust         if(_tuning_info.tune_wbsm && CLKernelLibrary::get().is_wbsm_supported())
235*c217d954SCole Faust         {
236*c217d954SCole Faust             cl_int wbsm_test = tuning_test.get_wbsm();
237*c217d954SCole Faust             kernel.set_wbsm_hint(wbsm_test);
238*c217d954SCole Faust         }
239*c217d954SCole Faust 
240*c217d954SCole Faust         // Run the kernel
241*c217d954SCole Faust         data->do_run(kernel, queue_profiler);
242*c217d954SCole Faust 
243*c217d954SCole Faust         queue_profiler.finish();
244*c217d954SCole Faust 
245*c217d954SCole Faust         const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
246*c217d954SCole Faust         const cl_ulong end   = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
247*c217d954SCole Faust         const cl_ulong diff  = end - start;
248*c217d954SCole Faust         _kernel_event        = nullptr;
249*c217d954SCole Faust 
250*c217d954SCole Faust         // Check the execution time
251*c217d954SCole Faust         if(diff < min_exec_time)
252*c217d954SCole Faust         {
253*c217d954SCole Faust             min_exec_time = diff;
254*c217d954SCole Faust             opt_tuning_params.set_lws(tuning_test.get_lws());
255*c217d954SCole Faust             if(_tuning_info.tune_wbsm)
256*c217d954SCole Faust             {
257*c217d954SCole Faust                 opt_tuning_params.set_wbsm(tuning_test.get_wbsm());
258*c217d954SCole Faust             }
259*c217d954SCole Faust         }
260*c217d954SCole Faust     }
261*c217d954SCole Faust 
262*c217d954SCole Faust     // Restore real function
263*c217d954SCole Faust     CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel;
264*c217d954SCole Faust     return opt_tuning_params;
265*c217d954SCole Faust }
266*c217d954SCole Faust 
tuning_params_table() const267*c217d954SCole Faust const std::unordered_map<std::string, CLTuningParams> &CLTuner::tuning_params_table() const
268*c217d954SCole Faust {
269*c217d954SCole Faust     return _tuning_params_table;
270*c217d954SCole Faust }
271*c217d954SCole Faust 
import_tuning_params(const std::unordered_map<std::string,CLTuningParams> & tuning_params_table)272*c217d954SCole Faust void CLTuner::import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table)
273*c217d954SCole Faust {
274*c217d954SCole Faust     _tuning_params_table.clear();
275*c217d954SCole Faust     _tuning_params_table = tuning_params_table;
276*c217d954SCole Faust }
277*c217d954SCole Faust 
load_from_file(const std::string & filename)278*c217d954SCole Faust void CLTuner::load_from_file(const std::string &filename)
279*c217d954SCole Faust {
280*c217d954SCole Faust     std::ifstream fs;
281*c217d954SCole Faust     fs.exceptions(std::ifstream::badbit);
282*c217d954SCole Faust     fs.open(filename, std::ios::in);
283*c217d954SCole Faust     if(!fs.is_open())
284*c217d954SCole Faust     {
285*c217d954SCole Faust         ARM_COMPUTE_ERROR_VAR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno);
286*c217d954SCole Faust     }
287*c217d954SCole Faust     std::string line;
288*c217d954SCole Faust     bool        header_line = true;
289*c217d954SCole Faust     while(!std::getline(fs, line).fail())
290*c217d954SCole Faust     {
291*c217d954SCole Faust         if(header_line)
292*c217d954SCole Faust         {
293*c217d954SCole Faust             header_line            = false;
294*c217d954SCole Faust             size_t pos_lws         = line.find("lws");
295*c217d954SCole Faust             size_t pos_wbsm        = line.find("wbsm");
296*c217d954SCole Faust             _tuning_info.tune_wbsm = false;
297*c217d954SCole Faust             if(pos_lws != std::string::npos || pos_wbsm != std::string::npos)
298*c217d954SCole Faust             {
299*c217d954SCole Faust                 // The file has in the first line the parameters it has been tuned on
300*c217d954SCole Faust                 if(pos_wbsm != std::string::npos)
301*c217d954SCole Faust                 {
302*c217d954SCole Faust                     _tuning_info.tune_wbsm = true;
303*c217d954SCole Faust                 }
304*c217d954SCole Faust                 // Once the line with the tuning parameter is read we can
305*c217d954SCole Faust                 // read the next one to start collecting the values
306*c217d954SCole Faust                 if(std::getline(fs, line).fail())
307*c217d954SCole Faust                 {
308*c217d954SCole Faust                     break;
309*c217d954SCole Faust                 }
310*c217d954SCole Faust             }
311*c217d954SCole Faust         }
312*c217d954SCole Faust 
313*c217d954SCole Faust         CLTuningParams tuning_params;
314*c217d954SCole Faust         size_t         pos = line.find(";");
315*c217d954SCole Faust         if(pos == std::string::npos)
316*c217d954SCole Faust         {
317*c217d954SCole Faust             ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
318*c217d954SCole Faust         }
319*c217d954SCole Faust         std::string kernel_id = line.substr(0, pos);
320*c217d954SCole Faust         line.erase(0, pos + 1);
321*c217d954SCole Faust         if(!tuning_params.from_string(_tuning_info, line))
322*c217d954SCole Faust         {
323*c217d954SCole Faust             ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
324*c217d954SCole Faust         }
325*c217d954SCole Faust         add_tuning_params(kernel_id, tuning_params);
326*c217d954SCole Faust     }
327*c217d954SCole Faust     fs.close();
328*c217d954SCole Faust }
329*c217d954SCole Faust 
save_to_file(const std::string & filename) const330*c217d954SCole Faust bool CLTuner::save_to_file(const std::string &filename) const
331*c217d954SCole Faust {
332*c217d954SCole Faust     if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty())
333*c217d954SCole Faust     {
334*c217d954SCole Faust         return false;
335*c217d954SCole Faust     }
336*c217d954SCole Faust     std::ofstream fs;
337*c217d954SCole Faust     fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
338*c217d954SCole Faust     fs.open(filename, std::ios::out);
339*c217d954SCole Faust     std::string header_string = "";
340*c217d954SCole Faust     header_string += "lws";
341*c217d954SCole Faust     if(_tuning_info.tune_wbsm)
342*c217d954SCole Faust     {
343*c217d954SCole Faust         if(!header_string.empty())
344*c217d954SCole Faust         {
345*c217d954SCole Faust             header_string += " ";
346*c217d954SCole Faust         }
347*c217d954SCole Faust         header_string += "wbsm";
348*c217d954SCole Faust     }
349*c217d954SCole Faust     fs << header_string << std::endl;
350*c217d954SCole Faust     for(auto const &kernel_data : _tuning_params_table)
351*c217d954SCole Faust     {
352*c217d954SCole Faust         CLTuningParams tun_pams(kernel_data.second);
353*c217d954SCole Faust         fs << kernel_data.first << tun_pams.to_string(_tuning_info) << std::endl;
354*c217d954SCole Faust     }
355*c217d954SCole Faust     fs.close();
356*c217d954SCole Faust     return true;
357*c217d954SCole Faust }
358*c217d954SCole Faust } // namespace arm_compute
359