Compute Library
 22.05
main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 "support/StringSupport.h"
25 #include "tests/AssetsLibrary.h"
29 #include "tests/framework/Macros.h"
38 
39 #ifdef ARM_COMPUTE_CL
45 #include "utils/TypePrinter.h"
46 #endif /* ARM_COMPUTE_CL */
49 
50 #include <fstream>
51 #include <initializer_list>
52 #include <iostream>
53 #include <memory>
54 #include <random>
55 #include <utility>
56 
57 using namespace arm_compute;
58 using namespace arm_compute::test;
59 
60 namespace
61 {
62 std::string command_line(int argc, char **argv)
63 {
64  std::stringstream ss;
65  for(int i = 0; i < argc; i++)
66  {
67  ss << argv[i] << " ";
68  }
69  return ss.str();
70 }
71 } // namespace
72 namespace arm_compute
73 {
74 namespace test
75 {
76 std::unique_ptr<AssetsLibrary> library;
77 
78 static constexpr uint32_t fixed_seed = 1;
79 std::unique_ptr<AssetsLibrary> fixed_library;
80 
81 extern std::unique_ptr<ParametersLibrary> parameters;
82 } // namespace test
83 } // namespace arm_compute
84 
85 namespace
86 {
87 #ifdef ARM_COMPUTE_CL
88 bool file_exists(const std::string &filename)
89 {
90  std::ifstream file(filename);
91  return file.good();
92 }
93 #endif /* ARM_COMPUTE_CL */
94 } //namespace
95 
96 int main(int argc, char **argv)
97 {
99 
101 
102  std::set<framework::DatasetMode> allowed_modes
103  {
108  };
109 
110  framework::CommonOptions options(parser);
111 
112  auto dataset_mode = parser.add_option<utils::EnumOption<framework::DatasetMode>>("mode", allowed_modes, framework::DatasetMode::PRECOMMIT);
113  dataset_mode->set_help("For managed datasets select which group to use");
114  auto filter = parser.add_option<utils::SimpleOption<std::string>>("filter", ".*");
115  filter->set_help("Regular expression to select test cases");
116  auto filter_id = parser.add_option<utils::SimpleOption<std::string>>("filter-id");
117  filter_id->set_help("List of test ids. ... can be used to define a range.");
118  auto stop_on_error = parser.add_option<utils::ToggleOption>("stop-on-error");
119  stop_on_error->set_help("Stop execution after the first failed test (useful for debugging)");
120  auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
121  seed->set_help("Global seed for random number generation");
122  auto list_tests = parser.add_option<utils::ToggleOption>("list-tests", false);
123  list_tests->set_help("List all test names");
124  auto test_instruments = parser.add_option<utils::ToggleOption>("test-instruments", false);
125  test_instruments->set_help("Test if the instruments work on the platform");
126  auto error_on_missing_assets = parser.add_option<utils::ToggleOption>("error-on-missing-assets", false);
127  error_on_missing_assets->set_help("Mark a test as failed instead of skipping it when assets are missing");
128  auto assets = parser.add_positional_option<utils::SimpleOption<std::string>>("assets");
129  assets->set_help("Path to the assets directory");
130 #ifdef ARM_COMPUTE_CL
131  auto enable_tuner = parser.add_option<utils::ToggleOption>("enable-tuner");
132  enable_tuner->set_help("Enable OpenCL dynamic tuner");
133 
134  const std::set<CLTunerMode> supported_tuner_modes
135  {
139  };
140  auto tuner_mode = parser.add_option<utils::EnumOption<CLTunerMode>>("tuner-mode", supported_tuner_modes, CLTunerMode::NORMAL);
141  tuner_mode->set_help("Configures the time taken by the tuner to tune. Slow tuner produces the most performant LWS configuration");
142 
143  auto tuner_file = parser.add_option<utils::SimpleOption<std::string>>("tuner-file", "");
144  tuner_file->set_help("File to load/save CLTuner values");
145 
146  auto mlgo_file = parser.add_option<utils::SimpleOption<std::string>>("mlgo-file", "");
147  mlgo_file->set_help("File to load MLGO heuristics");
148 #endif /* ARM_COMPUTE_CL */
149  auto threads = parser.add_option<utils::SimpleOption<int>>("threads", 1);
150  threads->set_help("Number of threads to use");
151  auto cooldown_sec = parser.add_option<utils::SimpleOption<float>>("delay", -1.f);
152  cooldown_sec->set_help("Delay to add between test executions in seconds");
153  auto configure_only = parser.add_option<utils::ToggleOption>("configure-only", false);
154  configure_only->set_help("Only configures kernels, without allocating, running or validating. Needed in order to validate OpenCL kernel run-time compilation, without necessarily running or validating the kernels' execution");
155 
156  try
157  {
158  parser.parse(argc, argv);
159 
160  if(options.help->is_set() && options.help->value())
161  {
162  parser.print_help(argv[0]);
163  return 0;
164  }
165 
166  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
167 
168  // Setup CPU Scheduler
169  Scheduler::get().set_num_threads(threads->value());
170 
171  // Create CPU context
172  auto cpu_ctx = std::make_unique<RuntimeContext>();
173  cpu_ctx->set_scheduler(&Scheduler::get());
174 
175  // Track CPU context
176  auto cpu_ctx_track = std::make_unique<ContextSchedulerUser>(cpu_ctx.get());
177 
178  // Create parameters
179  parameters = std::make_unique<ParametersLibrary>();
180  parameters->set_cpu_ctx(std::move(cpu_ctx));
181 
182 #ifdef ARM_COMPUTE_CL
183  CLTuner cl_tuner(false);
184  CLGEMMHeuristicsHandle gemm_heuristics;
185  if(opencl_is_available())
186  {
188  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
189  gemm_heuristics.reload_from_file(mlgo_file->value());
190  CLScheduler::get().default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), &cl_tuner, &gemm_heuristics);
191  }
192 
193  if(enable_tuner->is_set())
194  {
195  cl_tuner.set_tune_new_kernels(enable_tuner->value());
196 
197  //set tuner mode
198  cl_tuner.set_tuner_mode(tuner_mode->value());
199 
200  // If that's the first run then the file won't exist yet
201  if(file_exists(tuner_file->value()))
202  {
203  cl_tuner.load_from_file(tuner_file->value());
204  }
205  }
206  else if(!tuner_file->value().empty())
207  {
208  //If we're not tuning and the file doesn't exist then we should raise an error:
209  cl_tuner.load_from_file(tuner_file->value());
210  }
211 #endif /* ARM_COMPUTE_CL */
212  if(options.log_level->value() > framework::LogLevel::NONE)
213  {
214  for(auto &p : printers)
215  {
216  p->print_global_header();
217  }
218  }
219 
221  {
222  for(auto &p : printers)
223  {
224  p->print_entry("Version", build_information());
225  p->print_entry("CommandLine", command_line(argc, argv));
226  p->print_entry("Seed", support::cpp11::to_string(seed->value()));
227 #ifdef ARM_COMPUTE_CL
228  if(opencl_is_available())
229  {
230  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
231  }
232  else
233  {
234  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
235  }
236 #endif /* ARM_COMPUTE_CL */
237  const arm_compute::CPUInfo &cpu_info = Scheduler::get().cpu_info();
238  const unsigned int num_cpus = cpu_info.get_cpu_num();
239  p->print_entry("cpu_has_sve", support::cpp11::to_string(cpu_info.has_sve()));
240  p->print_entry("cpu_has_fp16", support::cpp11::to_string(cpu_info.has_fp16()));
241  p->print_entry("cpu_has_bf16", support::cpp11::to_string(cpu_info.has_bf16()));
242  p->print_entry("cpu_has_dotprod", support::cpp11::to_string(cpu_info.has_dotprod()));
243 
244  for(unsigned int j = 0; j < num_cpus; ++j)
245  {
246  const CPUModel model = cpu_info.get_cpu_model(j);
247  p->print_entry("CPU" + support::cpp11::to_string(j), cpuinfo::cpu_model_to_string(model));
248  }
249  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
250  p->print_entry("Threads", support::cpp11::to_string(threads->value()));
251  {
253  p->print_entry("Dataset mode", to_string(dataset_mode->value()));
254  }
255  }
256  }
257 
258  // Setup instruments meta-data
260  instruments_info._scheduler_users.push_back(cpu_ctx_track.get());
261  framework.set_instruments_info(instruments_info);
262 
263  // Initialize framework
265  fconfig.instruments = options.instruments->value();
266  fconfig.name_filter = filter->value();
267  fconfig.id_filter = filter_id->value();
268  fconfig.num_iterations = options.iterations->value();
269  fconfig.mode = dataset_mode->value();
270  fconfig.log_level = options.log_level->value();
271  fconfig.cooldown_sec = cooldown_sec->value();
272  fconfig.configure_only = configure_only->value();
273  framework.init(fconfig);
274 
275  for(auto &p : printers)
276  {
277  framework.add_printer(p.get());
278  }
279  framework.set_throw_errors(options.throw_errors->value());
280  framework.set_stop_on_error(stop_on_error->value());
281  framework.set_error_on_missing_assets(error_on_missing_assets->value());
282 
283  bool success = true;
284 
285  if(list_tests->value())
286  {
287  for(auto &p : printers)
288  {
289  p->print_list_tests(framework.test_infos());
290  p->print_global_footer();
291  }
292 
293  return 0;
294  }
295 
296  if(test_instruments->value())
297  {
298  framework::Profiler profiler = framework.get_profiler();
299  profiler.start();
300  profiler.stop();
301  for(auto &p : printers)
302  {
303  p->print_measurements(profiler.measurements());
304  }
305 
306  return 0;
307  }
308 
309  library = std::make_unique<AssetsLibrary>(assets->value(), seed->value());
310  fixed_library = std::make_unique<AssetsLibrary>(assets->value(), fixed_seed);
311 
312  if(!parser.validate())
313  {
314  return 1;
315  }
316 
317  success = framework.run();
318 
319  if(options.log_level->value() > framework::LogLevel::NONE)
320  {
321  for(auto &p : printers)
322  {
323  p->print_global_footer();
324  }
325  }
326 
327 #ifdef ARM_COMPUTE_CL
328  if(opencl_is_available())
329  {
331  if(enable_tuner->is_set() && enable_tuner->value() && tuner_file->is_set())
332  {
333  cl_tuner.save_to_file(tuner_file->value());
334  }
335  }
336 #endif /* ARM_COMPUTE_CL */
337 
338  return (success ? 0 : 1);
339  }
340  catch(const std::exception &error)
341  {
342  std::cerr << error.what() << "\n";
343 
344  if(options.throw_errors->value())
345  {
346  throw;
347  }
348 
349  return 1;
350  }
351  return 0;
352 }
Basic implementation of the OpenCL tuner interface.
Definition: CLTuner.h:40
Framework configuration structure.
Definition: Framework.h:57
void set_tuner_mode(CLTunerMode mode)
Set OpenCL tuner mode.
Definition: CLTuner.cpp:106
bool has_sve() const
Checks if the cpu model supports sve.
Definition: CPPTypes.cpp:94
arm_compute::utils::ToggleOption * help
Show help option.
Definition: CommonOptions.h:74
bool has_bf16() const
Checks if the cpu model supports bf16.
Definition: CPPTypes.cpp:64
int main(int argc, char **argv)
Definition: main.cpp:96
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
static CLScheduler & get()
Access the scheduler singleton.
Profiler class to collect benchmark numbers.
Definition: Profiler.h:45
virtual void set_num_threads(unsigned int num_threads)=0
Sets the number of threads the scheduler will use to run the kernels.
void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr)
Initialises the scheduler with context and device provided by the user.
std::string to_string(T &&value)
Convert integer and float values to string.
void load_from_file(const std::string &filename)
Load the tuning parameters table from file.
Definition: CLTuner.cpp:310
bool has_fp16() const
Checks if the cpu model supports fp16.
Definition: CPPTypes.cpp:59
void set_throw_errors(bool throw_errors)
Set whether errors are caught or thrown by the framework.
Definition: Framework.cpp:265
Implementation of an option that can be either true or false.
Definition: ToggleOption.h:36
const std::vector< T > & value() const
Get the values of the option.
OpenCL native backend.
Searches a subset of LWS configurations while tuning.
void set_instruments_info(InstrumentsInfo instr_info)
Sets instruments info.
Definition: Framework.cpp:703
std::unique_ptr< InstrumentsInfo > instruments_info
Definition: Framework.cpp:50
std::stringstream ss(mlgo_str)
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
bool validate() const
Validates the previously parsed command line arguments.
CPUModel
CPU models types.
Definition: CPPTypes.h:58
CPUInfo & cpu_info()
Get CPU info.
Definition: IScheduler.cpp:41
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device(CLBackendType cl_backend_type)
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:126
void set_error_on_missing_assets(bool error_on_missing_assets)
Set whether a test should be considered as failed if its assets cannot be found.
Definition: Framework.cpp:285
Class to parse command line arguments.
Copyright (c) 2017-2022 Arm Limited.
CPUModel get_cpu_model(unsigned int cpuid) const
Gets the cpu model for a given cpuid.
Definition: CPPTypes.cpp:109
float cooldown_sec
Delay between tests in seconds.
Definition: Framework.h:64
static Framework & get()
Access to the singleton.
Definition: Framework.cpp:124
Searches a minimal subset of LWS configurations while tuning.
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
LogLevel log_level
Verbosity of the output.
Definition: Framework.h:65
bool run()
Run all enabled test cases.
Definition: Framework.cpp:543
void parse(int argc, char **argv)
Parses the command line arguments and updates the options accordingly.
std::vector< framework::InstrumentsDescription > instruments
Instrument types that will be used for benchmarking.
Definition: Framework.h:59
bool save_to_file(const std::string &filename) const
Save the content of the tuning parameters table to file.
Definition: CLTuner.cpp:362
std::vector< std::unique_ptr< Printer > > create_printers()
Create the printers based on parsed command line options.
std::vector< TestInfo > test_infos() const
List of TestInfo&#39;s.
Definition: Framework.cpp:677
std::string name_filter
Regular expression to filter tests by name.
Definition: Framework.h:60
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:76
bool reload_from_file(const std::string &filename)
(Re)Load the heuristics from reading a dotmlgo file
std::string cpu_model_to_string(CpuModel model)
Convert a CPU model value to a string.
Definition: CpuModel.cpp:30
std::unique_ptr< ParametersLibrary > parameters
Definition: Framework.cpp:46
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void set_tune_new_kernels(bool tune_new_kernels)
Setter for tune_new_kernels option.
Definition: CLTuner.cpp:97
bool has_dotprod() const
Checks if the cpu model supports dot product.
Definition: CPPTypes.cpp:74
Profiler get_profiler() const
Factory method to obtain a configured profiler.
Definition: Framework.cpp:643
const T & value() const
Get the option value.
Definition: SimpleOption.h:112
T * add_positional_option(As &&... args)
Function to add a new positional argument to the parser.
const MeasurementsMap & measurements() const
Return measurements for all instruments.
Definition: Profiler.cpp:89
unsigned int get_cpu_num() const
Return the maximum number of CPUs present.
Definition: CPPTypes.cpp:54
arm_compute::utils::SimpleOption< int > * iterations
Number of iterations option.
Definition: CommonOptions.h:76
arm_compute::utils::EnumListOption< InstrumentsDescription > * instruments
Instruments option.
Definition: CommonOptions.h:75
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:74
Handle for loading and retrieving GEMM heuristics.
arm_compute::utils::EnumOption< LogLevel > * log_level
Logging level option.
Definition: CommonOptions.h:79
std::string id_filter
String to match selected test ids.
Definition: Framework.h:61
Implementation of a simple option that accepts a value from a fixed set.
Definition: EnumOption.h:40
void start()
Call start() on all the added instruments.
Definition: Profiler.cpp:48
Wrapper to configure the Khronos OpenCL C++ header.
std::vector< ISchedulerUser * > _scheduler_users
Definition: Instruments.h:65
std::string build_information()
Returns the arm_compute library build information.
bool configure_only
Only configure kernels.
Definition: Framework.h:66
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:672
void stop()
Call stop() on all the added instruments.
Definition: Profiler.cpp:56
bool is_set() const
Has a value been assigned to the option?
Definition: Option.h:135
void print_help(const std::string &program_name) const
Prints a help message for all configured options.
const T & value() const
Get the selected value.
Common command line options used to configure the framework.
Definition: CommonOptions.h:54
Searches all possible LWS configurations while tuning.
std::string to_string(const SimpleTensor< T > &tensor)
void set_stop_on_error(bool stop_on_error)
Set whether to stop execution after the first failed test.
Definition: Framework.cpp:275
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
void init(const FrameworkConfig &config)
Init the framework.
Definition: Framework.cpp:130
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:154
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94
std::unique_ptr< AssetsLibrary > fixed_library
Definition: main.cpp:79
arm_compute::utils::ToggleOption * throw_errors
Throw errors option.
Definition: CommonOptions.h:80