diff --git a/onnxruntime/python/onnxruntime_inference_collection.py b/onnxruntime/python/onnxruntime_inference_collection.py index 4c3313046457c..91216473bcad2 100644 --- a/onnxruntime/python/onnxruntime_inference_collection.py +++ b/onnxruntime/python/onnxruntime_inference_collection.py @@ -397,6 +397,16 @@ def run_with_iobinding(self, iobinding, run_options=None): """ self._sess.run_with_iobinding(iobinding._iobinding, run_options) + def set_ep_dynamic_options(self, options: dict[str, str]): + """ + Set dynamic options for execution providers. + + :param options: Dictionary of key-value pairs where both keys and values are strings. + These options will be passed to the execution providers to modify + their runtime behavior. + """ + self._sess.set_ep_dynamic_options(options) + def get_tuning_results(self): return self._sess.get_tuning_results() diff --git a/onnxruntime/python/onnxruntime_pybind_state.cc b/onnxruntime/python/onnxruntime_pybind_state.cc index c548f3df4fb27..c7ff9e4b5433d 100644 --- a/onnxruntime/python/onnxruntime_pybind_state.cc +++ b/onnxruntime/python/onnxruntime_pybind_state.cc @@ -2810,6 +2810,56 @@ including arg name, arg type (contains both type and shape).)pbdoc") ORT_THROW("TunableOp and get_tuning_results are not supported in this build."); #endif }) + .def( + "set_ep_dynamic_options", [](PyInferenceSession* sess, const py::dict& options) -> void { + std::vector keys; + std::vector values; + std::vector key_strings; + std::vector value_strings; + + // Reserve space to avoid reallocations + key_strings.reserve(options.size()); + value_strings.reserve(options.size()); + keys.reserve(options.size()); + values.reserve(options.size()); + + // Convert Python dict to C-style arrays + for (const auto& item : options) { + key_strings.emplace_back(py::str(item.first)); + value_strings.emplace_back(py::str(item.second)); + keys.push_back(key_strings.back().c_str()); + values.push_back(value_strings.back().c_str()); + } + + if (keys.empty()) { + throw std::runtime_error("No options were provided"); + } + + // Call the underlying C++ method + auto status = sess->GetSessionHandle()->SetEpDynamicOptions( + gsl::make_span(keys.data(), keys.size()), + gsl::make_span(values.data(), values.size())); + + if (!status.IsOK()) { + throw std::runtime_error("Failed to set EP dynamic options: " + status.ErrorMessage()); + } + }, + R"pbdoc(Set dynamic options for execution providers. + + Args: + options (dict): Dictionary of key-value pairs where both keys and values are strings. + These options will be passed to the execution providers to modify + their runtime behavior. + + Example: + session.set_ep_dynamic_options({ + "option1": "value1", + "option2": "value2" + }) + + Raises: + RuntimeError: If no options are provided or if setting the options fails. + )pbdoc") .def("set_tuning_results", [](PyInferenceSession* sess, py::list results, bool error_on_invalid) -> void { #if !defined(ORT_MINIMAL_BUILD) std::vector tuning_results;