Skip to content

Commit 4de6893

Browse files
authored
Add options to onnxruntime_perf_runner to externalize weights (#26441)
### Description Allow the user to externalize weights, set the minimum weight threshold, and save pre-packs when saving an optimized model. ### Motivation and Context Add several options that make the tool more useful. Some models fail to optimize because they are too large. Externalizing initializers makes this possible.
1 parent e719687 commit 4de6893

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

onnxruntime/test/perftest/command_args_parser.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ ABSL_FLAG(size_t, c, DefaultPerformanceTestConfig().run_config.concurrent_sessio
4949
ABSL_FLAG(int, d, DefaultPerformanceTestConfig().run_config.cudnn_conv_algo, "Specifies CUDNN convolution algorithms: 0(benchmark), 1(heuristic), 2(default).");
5050
ABSL_FLAG(int, o, DefaultPerformanceTestConfig().run_config.optimization_level, "Specifies graph optimization level. Default is 99 (all). Valid values are 0 (disable), 1 (basic), 2 (extended), 3 (layout), 99 (all).");
5151
ABSL_FLAG(std::string, u, "", "Specifies the optimized model path for saving.");
52+
ABSL_FLAG(std::string, opt_data, "", "Specifies the data file path (relative to the opt model) for saving weights when -u is in effect");
53+
ABSL_FLAG(int64_t, opt_weight_min_size, -1, "Min initializer size to save to --opt_data when --opt_data is in effect");
54+
ABSL_FLAG(bool, opt_save_prepacks, false, "Saves pre-packs to the file specified by --opt_data along with weights");
5255
ABSL_FLAG(std::string, i, "",
5356
"Specifies EP specific runtime options as key-value pairs.\n Different runtime options available are: \n"
5457
" [Usage]: -e <provider_name> -i '<key1>|<value1> <key2>|<value2>'\n"
@@ -399,7 +402,23 @@ bool CommandLineParser::ParseArguments(PerformanceTestConfig& test_config, int a
399402
// -u
400403
{
401404
const auto& optimized_model_path = absl::GetFlag(FLAGS_u);
402-
if (!optimized_model_path.empty()) test_config.run_config.optimized_model_path = ToPathString(optimized_model_path);
405+
if (!optimized_model_path.empty()) {
406+
test_config.run_config.optimized_model_path = ToPathString(optimized_model_path);
407+
// --opt_data
408+
const auto& opt_data_path = absl::GetFlag(FLAGS_opt_data);
409+
if (!opt_data_path.empty()) {
410+
test_config.run_config.optimized_model_data_path = opt_data_path;
411+
// --opt_weight_min_size
412+
if (absl::GetFlag(FLAGS_opt_weight_min_size) >= 0) {
413+
test_config.run_config.optimized_model_weight_min_size =
414+
std::to_string(absl::GetFlag(FLAGS_opt_weight_min_size));
415+
}
416+
// --opt_save_prepacks
417+
if (absl::GetFlag(FLAGS_opt_save_prepacks)) {
418+
test_config.run_config.optimized_save_optimized_prepacks = true;
419+
}
420+
}
421+
}
403422
}
404423

405424
// -I

onnxruntime/test/perftest/ort_test_session.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,17 @@ select from 'TF8', 'TF16', 'UINT8', 'FLOAT', 'ITENSOR'. \n)");
750750
}
751751
if (!performance_test_config.run_config.optimized_model_path.empty()) {
752752
session_options.SetOptimizedModelFilePath(performance_test_config.run_config.optimized_model_path.c_str());
753+
if (!performance_test_config.run_config.optimized_model_data_path.empty()) {
754+
session_options.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersFileName,
755+
performance_test_config.run_config.optimized_model_data_path.c_str());
756+
if (!performance_test_config.run_config.optimized_model_weight_min_size.empty()) {
757+
session_options.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersMinSizeInBytes,
758+
performance_test_config.run_config.optimized_model_weight_min_size.c_str());
759+
}
760+
if (performance_test_config.run_config.optimized_save_optimized_prepacks) {
761+
session_options.AddConfigEntry(kOrtSessionOptionsSavePrePackedConstantInitializers, "1");
762+
}
763+
}
753764
}
754765
if (performance_test_config.run_config.set_denormal_as_zero) {
755766
warn_dup_config_entry(kOrtSessionOptionsConfigSetDenormalAsZero);

onnxruntime/test/perftest/test_configuration.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ struct RunConfig {
5454
int intra_op_num_threads{0};
5555
int inter_op_num_threads{0};
5656
GraphOptimizationLevel optimization_level{ORT_ENABLE_ALL};
57-
std::basic_string<ORTCHAR_T> optimized_model_path;
57+
PathString optimized_model_path;
58+
std::string optimized_model_data_path; // Always UTF-8
59+
std::string optimized_model_weight_min_size;
60+
bool optimized_save_optimized_prepacks{false};
5861
int cudnn_conv_algo{0};
5962
bool do_cuda_copy_in_separate_stream{false};
6063
bool set_denormal_as_zero{false};

0 commit comments

Comments
 (0)