Skip to content

Commit 6cc40fd

Browse files
[QNN EP] Add dynamic option to set HTP performance mode (#26135)
### Description Add a new EP Dynamic option to set HTP performance mode after session creation. --------- Co-authored-by: quic-ashwshan <quic_ashwshan@quicinc.com>
1 parent 0de21a7 commit 6cc40fd

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,10 @@ static const char* const kOrtSessionOptionsDisableModelCompile = "session.disabl
408408
// Note: UNSUPPORTED models always fail regardless of this setting.
409409
static const char* const kOrtSessionOptionsFailOnSuboptimalCompiledModel =
410410
"session.fail_on_suboptimal_compiled_model";
411+
412+
// THIS OPTION IS NOT A REGULAR SESSION OPTION SINCE IT CAN BE MODIFIED AT ANY TIME
413+
// Meant to be used with SetEpDynamicOptions
414+
// options for HTP performance mode: "burst", "balanced", "default", "high_performance",
415+
// "high_power_saver", "low_balanced", "extreme_power_saver", "low_power_saver", "power_saver",
416+
// "sustained_high_performance". Default to "default".
417+
static const char* const kOrtEpDynamicOptionsQnnHtpPerformanceMode = "ep.dynamic.qnn_htp_performance_mode";

onnxruntime/core/providers/qnn/qnn_execution_provider.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,17 @@ Status QNNExecutionProvider::SetEpDynamicOptions(gsl::span<const char* const> ke
15761576
LOGS_DEFAULT(ERROR) << "Invalid EP Workload Type: " << value;
15771577
return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Invalid EP Workload Type.");
15781578
}
1579+
} else if (key == kOrtEpDynamicOptionsQnnHtpPerformanceMode) {
1580+
auto backend_type = qnn_backend_manager_->GetQnnBackendType();
1581+
if (qnn::QnnBackendType::HTP != backend_type && qnn::QnnBackendType::DSP != backend_type) {
1582+
return Status::OK();
1583+
}
1584+
qnn::HtpPerformanceMode htp_performance_mode = qnn::HtpPerformanceMode::kHtpDefault;
1585+
ParseHtpPerformanceMode(value, htp_performance_mode);
1586+
if (GetPerThreadContext().IsHtpPowerConfigIdValid()) {
1587+
ORT_RETURN_IF_ERROR(qnn_backend_manager_->SetHtpPowerConfig(GetPerThreadContext().GetHtpPowerConfigId(),
1588+
htp_performance_mode));
1589+
}
15791590
} else {
15801591
LOGS_DEFAULT(ERROR) << "EP Dynamic Option \"" << key << "\" is not currently supported.";
15811592
return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Unsupported EP Dynamic Option");

onnxruntime/test/providers/qnn/qnn_ep_context_test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,21 @@ TEST_F(QnnHTPBackendTests, QnnEpDynamicOptions) {
20762076
} catch (const std::exception& e) {
20772077
EXPECT_STREQ("Unsupported EP Dynamic Option", e.what());
20782078
}
2079+
2080+
const char* const htp_perf_mode_type[] = {"ep.dynamic.qnn_htp_performance_mode"};
2081+
const char* const eps_type[] = {"extreme_power_saver"};
2082+
const char* const shp_type[] = {"sustained_high_performance"};
2083+
session.SetEpDynamicOptions(htp_perf_mode_type, shp_type, 1);
2084+
ort_output = session.Run(Ort::RunOptions{}, input_names_c.data(), ort_inputs.data(), ort_inputs.size(),
2085+
output_names_c.data(), 1);
2086+
2087+
session.SetEpDynamicOptions(htp_perf_mode_type, eps_type, 1);
2088+
ort_output = session.Run(Ort::RunOptions{}, input_names_c.data(), ort_inputs.data(), ort_inputs.size(),
2089+
output_names_c.data(), 1);
2090+
2091+
session.SetEpDynamicOptions(htp_perf_mode_type, shp_type, 1);
2092+
ort_output = session.Run(Ort::RunOptions{}, input_names_c.data(), ort_inputs.data(), ort_inputs.size(),
2093+
output_names_c.data(), 1);
20792094
}
20802095

20812096
// Implementation of OrtOutStreamWriteFunc that writes the compiled model to a file.

0 commit comments

Comments
 (0)