Skip to content

Commit b1f7750

Browse files
CVS-175737-[OVEP] Expose kvcache_rewind python api (#831)
* expose rewind api through Python * address PR review --------- Co-authored-by: MayureshV1 <[email protected]>
1 parent eff6cac commit b1f7750

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

onnxruntime/python/onnxruntime_inference_collection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,16 @@ def run_with_iobinding(self, iobinding, run_options=None):
397397
"""
398398
self._sess.run_with_iobinding(iobinding._iobinding, run_options)
399399

400+
def set_ep_dynamic_options(self, options: dict[str, str]):
401+
"""
402+
Set dynamic options for execution providers.
403+
404+
:param options: Dictionary of key-value pairs where both keys and values are strings.
405+
These options will be passed to the execution providers to modify
406+
their runtime behavior.
407+
"""
408+
self._sess.set_ep_dynamic_options(options)
409+
400410
def get_tuning_results(self):
401411
return self._sess.get_tuning_results()
402412

onnxruntime/python/onnxruntime_pybind_state.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,55 @@ including arg name, arg type (contains both type and shape).)pbdoc")
28102810
ORT_THROW("TunableOp and get_tuning_results are not supported in this build.");
28112811
#endif
28122812
})
2813+
.def(
2814+
"set_ep_dynamic_options", [](PyInferenceSession* sess, const py::dict& options) {
2815+
std::vector<const char*> keys;
2816+
std::vector<const char*> values;
2817+
std::vector<std::string> key_strings;
2818+
std::vector<std::string> value_strings;
2819+
2820+
// Reserve space to avoid reallocations
2821+
key_strings.reserve(options.size());
2822+
value_strings.reserve(options.size());
2823+
keys.reserve(options.size());
2824+
values.reserve(options.size());
2825+
2826+
// Convert Python dict to C-style arrays
2827+
for (const auto& item : options) {
2828+
key_strings.emplace_back(py::str(item.first));
2829+
value_strings.emplace_back(py::str(item.second));
2830+
keys.push_back(key_strings.back().c_str());
2831+
values.push_back(value_strings.back().c_str());
2832+
}
2833+
2834+
if (keys.empty()) {
2835+
ORT_THROW("No options were provided");
2836+
}
2837+
2838+
auto status = sess->GetSessionHandle()->SetEpDynamicOptions(
2839+
gsl::make_span(keys.data(), keys.size()),
2840+
gsl::make_span(values.data(), values.size()));
2841+
2842+
if (!status.IsOK()) {
2843+
ORT_THROW("Failed to set EP dynamic options: " + status.ErrorMessage());
2844+
}
2845+
},
2846+
R"pbdoc(Set dynamic options for execution providers.
2847+
2848+
Args:
2849+
options (dict): Dictionary of key-value pairs where both keys and values are strings.
2850+
These options will be passed to the execution providers to modify
2851+
their runtime behavior.
2852+
2853+
Example:
2854+
session.set_ep_dynamic_options({
2855+
"option1": "value1",
2856+
"option2": "value2"
2857+
})
2858+
2859+
Raises:
2860+
RuntimeError: If no options are provided or if setting the options fails.
2861+
)pbdoc")
28132862
.def("set_tuning_results", [](PyInferenceSession* sess, py::list results, bool error_on_invalid) -> void {
28142863
#if !defined(ORT_MINIMAL_BUILD)
28152864
std::vector<TuningResults> tuning_results;

0 commit comments

Comments
 (0)