Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PyOV] Fix passing Model as argument (#28896)

Anastasia Kuporosova2e68fd3d3fcf
diff --git a/src/bindings/python/src/pyopenvino/graph/passes/model_pass.cpp b/src/bindings/python/src/pyopenvino/graph/passes/model_pass.cpp
index 8947a4871e5..9adb9399cd0 100644
--- a/src/bindings/python/src/pyopenvino/graph/passes/model_pass.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/passes/model_pass.cpp
@@ -10,6 +10,7 @@
#include <string>
#include "pyopenvino/core/common.hpp"
+#include "pyopenvino/utils/utils.hpp"
namespace py = pybind11;
@@ -34,10 +35,14 @@ void regclass_passes_ModelPass(py::module m) {
"ModelPass");
model_pass.doc() = "openvino.passes.ModelPass wraps ov::pass::ModelPass";
model_pass.def(py::init<>());
- model_pass.def("run_on_model",
- &ov::pass::ModelPass::run_on_model,
- py::arg("model"),
- R"(
+ model_pass.def(
+ "run_on_model",
+ [](ov::pass::ModelPass& self, const py::object& ie_api_model) {
+ const auto model = Common::utils::convert_to_model(ie_api_model);
+ self.run_on_model(model);
+ },
+ py::arg("model"),
+ R"(
run_on_model must be defined in inherited class. This method is used to work with Model directly.
:param model: openvino.Model to be transformed.
diff --git a/src/bindings/python/tests/test_transformations/test_model_pass.py b/src/bindings/python/tests/test_transformations/test_model_pass.py
index a0823112d41..5d061e6d360 100644
--- a/src/bindings/python/tests/test_transformations/test_model_pass.py
+++ b/src/bindings/python/tests/test_transformations/test_model_pass.py
@@ -12,3 +12,10 @@ def test_model_pass():
manager.run_passes(get_relu_model())
assert model_pass.model_changed
+
+
+def test_model_pass_run_on_model():
+ model_pass = MyModelPass()
+ model_pass.run_on_model(get_relu_model())
+
+ assert model_pass.model_changed
diff --git a/src/bindings/python/tests/test_transformations/test_public_transformations.py b/src/bindings/python/tests/test_transformations/test_public_transformations.py
index 903b20158af..89a8bdb26ea 100644
--- a/src/bindings/python/tests/test_transformations/test_public_transformations.py
+++ b/src/bindings/python/tests/test_transformations/test_public_transformations.py
@@ -153,3 +153,14 @@ def test_serialize_pass(request, tmp_path, is_path_xml, is_path_bin):
os.remove(xml_path)
os.remove(bin_path)
+
+
[email protected](("transformation", "arguments"),
+ [(ConstantFolding, []),
+ (MakeStateful, [{"parameter": "result"}]),
+ (ConvertFP32ToFP16, []),
+ (LowLatency2, [])])
+def test_run_on_model_transformations(transformation, arguments):
+ model = get_model()
+ transformation(*arguments).run_on_model(model)
+ assert model is not None