1 /*
2 * Copyright (c) 2017-2022 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"
26 #include "tests/framework/DatasetModes.h"
27 #include "tests/framework/Exceptions.h"
28 #include "tests/framework/Framework.h"
29 #include "tests/framework/Macros.h"
30 #include "tests/framework/ParametersLibrary.h"
31 #include "tests/framework/Profiler.h"
32 #include "tests/framework/command_line/CommonOptions.h"
33 #include "tests/framework/instruments/Instruments.h"
34 #include "tests/framework/printers/Printers.h"
35 #include "tests/instruments/Helpers.h"
36 #include "utils/command_line/CommandLineOptions.h"
37 #include "utils/command_line/CommandLineParser.h"
38
39 #ifdef ARM_COMPUTE_CL
40 #include "arm_compute/core/CL/OpenCL.h"
41 #include "arm_compute/runtime/CL/CLGEMMHeuristicsHandle.h"
42 #include "arm_compute/runtime/CL/CLHelpers.h"
43 #include "arm_compute/runtime/CL/CLScheduler.h"
44 #include "arm_compute/runtime/CL/CLTuner.h"
45 #include "utils/TypePrinter.h"
46 #endif /* ARM_COMPUTE_CL */
47 #include "arm_compute/runtime/Scheduler.h"
48 #include "src/common/cpuinfo/CpuModel.h"
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 {
command_line(int argc,char ** argv)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
file_exists(const std::string & filename)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
main(int argc,char ** argv)96 int main(int argc, char **argv)
97 {
98 framework::Framework &framework = framework::Framework::get();
99
100 utils::CommandLineParser parser;
101
102 std::set<framework::DatasetMode> allowed_modes
103 {
104 framework::DatasetMode::DISABLED,
105 framework::DatasetMode::PRECOMMIT,
106 framework::DatasetMode::NIGHTLY,
107 framework::DatasetMode::ALL
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 {
136 CLTunerMode::EXHAUSTIVE,
137 CLTunerMode::NORMAL,
138 CLTunerMode::RAPID
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 {
187 auto ctx_dev_err = create_opencl_context_and_device(CLBackendType::Native);
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
220 if(options.log_level->value() >= framework::LogLevel::CONFIG)
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_sme", support::cpp11::to_string(cpu_info.has_sme()));
241 p->print_entry("cpu_has_sme2", support::cpp11::to_string(cpu_info.has_sme2()));
242 p->print_entry("cpu_has_fp16", support::cpp11::to_string(cpu_info.has_fp16()));
243 p->print_entry("cpu_has_bf16", support::cpp11::to_string(cpu_info.has_bf16()));
244 p->print_entry("cpu_has_dotprod", support::cpp11::to_string(cpu_info.has_dotprod()));
245 p->print_entry("cpu_has_svebf16", support::cpp11::to_string(cpu_info.has_svebf16()));
246
247 for(unsigned int j = 0; j < num_cpus; ++j)
248 {
249 const CPUModel model = cpu_info.get_cpu_model(j);
250 p->print_entry("CPU" + support::cpp11::to_string(j), cpuinfo::cpu_model_to_string(model));
251 }
252 p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
253 p->print_entry("Threads", support::cpp11::to_string(threads->value()));
254 {
255 using support::cpp11::to_string;
256 p->print_entry("Dataset mode", to_string(dataset_mode->value()));
257 }
258 }
259 }
260
261 // Setup instruments meta-data
262 framework::InstrumentsInfo instruments_info;
263 instruments_info._scheduler_users.push_back(cpu_ctx_track.get());
264 framework.set_instruments_info(instruments_info);
265
266 // Initialize framework
267 framework::FrameworkConfig fconfig;
268 fconfig.instruments = options.instruments->value();
269 fconfig.name_filter = filter->value();
270 fconfig.id_filter = filter_id->value();
271 fconfig.num_iterations = options.iterations->value();
272 fconfig.mode = dataset_mode->value();
273 fconfig.log_level = options.log_level->value();
274 fconfig.cooldown_sec = cooldown_sec->value();
275 fconfig.configure_only = configure_only->value();
276 framework.init(fconfig);
277
278 for(auto &p : printers)
279 {
280 framework.add_printer(p.get());
281 }
282 framework.set_throw_errors(options.throw_errors->value());
283 framework.set_stop_on_error(stop_on_error->value());
284 framework.set_error_on_missing_assets(error_on_missing_assets->value());
285
286 bool success = true;
287
288 if(list_tests->value())
289 {
290 for(auto &p : printers)
291 {
292 p->print_list_tests(framework.test_infos());
293 p->print_global_footer();
294 }
295
296 return 0;
297 }
298
299 if(test_instruments->value())
300 {
301 framework::Profiler profiler = framework.get_profiler();
302 profiler.start();
303 profiler.stop();
304 for(auto &p : printers)
305 {
306 p->print_measurements(profiler.measurements());
307 }
308
309 return 0;
310 }
311
312 library = std::make_unique<AssetsLibrary>(assets->value(), seed->value());
313 fixed_library = std::make_unique<AssetsLibrary>(assets->value(), fixed_seed);
314
315 if(!parser.validate())
316 {
317 return 1;
318 }
319
320 success = framework.run();
321
322 if(options.log_level->value() > framework::LogLevel::NONE)
323 {
324 for(auto &p : printers)
325 {
326 p->print_global_footer();
327 }
328 }
329
330 #ifdef ARM_COMPUTE_CL
331 if(opencl_is_available())
332 {
333 CLScheduler::get().sync();
334 if(enable_tuner->is_set() && enable_tuner->value() && tuner_file->is_set())
335 {
336 cl_tuner.save_to_file(tuner_file->value());
337 }
338 }
339 #endif /* ARM_COMPUTE_CL */
340
341 return (success ? 0 : 1);
342 }
343 catch(const std::exception &error)
344 {
345 std::cerr << error.what() << "\n";
346
347 if(options.throw_errors->value())
348 {
349 throw;
350 }
351
352 return 1;
353 }
354 return 0;
355 }
356