From a622710fe171fab399bee41c8c3cd1dc16dd8f62 Mon Sep 17 00:00:00 2001 From: Hector Li Date: Mon, 12 Feb 2024 19:11:40 -0800 Subject: [PATCH] Add option to skip session run in perf_test tool (#19501) Enable a option to exit after session creation so that user can measure session creation time to measure impact of enabling any initialization optimizations. --- onnxruntime/test/perftest/command_args_parser.cc | 6 +++++- onnxruntime/test/perftest/main.cc | 7 +++++++ onnxruntime/test/perftest/performance_runner.cc | 5 +++++ onnxruntime/test/perftest/performance_runner.h | 2 ++ onnxruntime/test/perftest/test_configuration.h | 1 + 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/onnxruntime/test/perftest/command_args_parser.cc b/onnxruntime/test/perftest/command_args_parser.cc index 7cfbe0a84e..3874901f86 100644 --- a/onnxruntime/test/perftest/command_args_parser.cc +++ b/onnxruntime/test/perftest/command_args_parser.cc @@ -128,6 +128,7 @@ namespace perftest { "\t\t The number of affinities must be equal to intra_op_num_threads - 1\n\n" "\t-D [Disable thread spinning]: disable spinning entirely for thread owned by onnxruntime intra-op thread pool.\n" "\t-Z [Force thread to stop spinning between runs]: disallow thread from spinning during runs to reduce cpu usage.\n" + "\t-n [Exit after session creation]: allow user to measure session creation time to measure impact of enabling any initialization optimizations.\n" "\t-h: help\n"); } #ifdef _WIN32 @@ -190,7 +191,7 @@ static bool ParseSessionConfigs(const std::string& configs_string, /*static*/ bool CommandLineParser::ParseArguments(PerformanceTestConfig& test_config, int argc, ORTCHAR_T* argv[]) { int ch; - while ((ch = getopt(argc, argv, ORT_TSTR("b:m:e:r:t:p:x:y:c:d:o:u:i:f:F:S:T:C:AMPIDZvhsqz"))) != -1) { + while ((ch = getopt(argc, argv, ORT_TSTR("b:m:e:r:t:p:x:y:c:d:o:u:i:f:F:S:T:C:AMPIDZvhsqzn"))) != -1) { switch (ch) { case 'f': { std::basic_string dim_name; @@ -373,6 +374,9 @@ static bool ParseSessionConfigs(const std::string& configs_string, case 'Z': test_config.run_config.disable_spinning_between_run = true; break; + case 'n': + test_config.run_config.exit_after_session_creation = true; + break; case '?': case 'h': default: diff --git a/onnxruntime/test/perftest/main.cc b/onnxruntime/test/perftest/main.cc index 36f08167c2..43bf54963c 100644 --- a/onnxruntime/test/perftest/main.cc +++ b/onnxruntime/test/perftest/main.cc @@ -43,6 +43,13 @@ int real_main(int argc, char* argv[]) { } std::random_device rd; perftest::PerformanceRunner perf_runner(env, test_config, rd); + + // Exit if user enabled -n option so that user can measure session creation time + if (test_config.run_config.exit_after_session_creation) { + perf_runner.LogSessionCreationTime(); + return 0; + } + auto status = perf_runner.Run(); if (!status.IsOK()) { printf("Run failed:%s\n", status.ErrorMessage().c_str()); diff --git a/onnxruntime/test/perftest/performance_runner.cc b/onnxruntime/test/perftest/performance_runner.cc index 9f2cbcf6a2..37bf80c80e 100644 --- a/onnxruntime/test/perftest/performance_runner.cc +++ b/onnxruntime/test/perftest/performance_runner.cc @@ -115,6 +115,11 @@ void PerformanceResult::DumpToFile(const std::basic_string& path, boo } } +void PerformanceRunner::LogSessionCreationTime() { + std::chrono::duration session_create_duration = session_create_end_ - session_create_start_; + std::cout << "\nSession creation time cost: " << session_create_duration.count() << " s\n"; +} + Status PerformanceRunner::Run() { if (!Initialize()) { return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "failed to initialize."); diff --git a/onnxruntime/test/perftest/performance_runner.h b/onnxruntime/test/perftest/performance_runner.h index da2df9c39f..cb1cb66155 100644 --- a/onnxruntime/test/perftest/performance_runner.h +++ b/onnxruntime/test/perftest/performance_runner.h @@ -46,6 +46,8 @@ class PerformanceRunner { ~PerformanceRunner(); Status Run(); + void LogSessionCreationTime(); + inline const PerformanceResult& GetResult() const { return performance_result_; } inline void SerializeResult() const { diff --git a/onnxruntime/test/perftest/test_configuration.h b/onnxruntime/test/perftest/test_configuration.h index 5a49414a49..74c8eb472c 100644 --- a/onnxruntime/test/perftest/test_configuration.h +++ b/onnxruntime/test/perftest/test_configuration.h @@ -63,6 +63,7 @@ struct RunConfig { std::string intra_op_thread_affinities; bool disable_spinning = false; bool disable_spinning_between_run = false; + bool exit_after_session_creation = false; }; struct PerformanceTestConfig {