Skip to content

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

[Py OV] Add runtime/passes to openvino namespace (#27785)

Alicja Miloszewskad2c9c80cf61b
diff --git a/src/bindings/python/src/openvino/passes/__init__.py b/src/bindings/python/src/openvino/passes/__init__.py
new file mode 100644
index 00000000000..037d9774c5b
--- /dev/null
+++ b/src/bindings/python/src/openvino/passes/__init__.py
@@ -0,0 +1,19 @@
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+# type: ignore
+# flake8: noqa
+
+from openvino._pyopenvino.passes import ModelPass, Matcher, MatcherPass, PassBase, WrapType, Or, AnyInput, Optional
+from openvino._pyopenvino.passes import (
+ consumers_count,
+ has_static_dim,
+ has_static_dims,
+ has_static_shape,
+ has_static_rank,
+ rank_equals,
+ type_matches,
+ type_matches_any,
+)
+from openvino._pyopenvino.passes import Serialize, ConstantFolding, VisualizeTree, MakeStateful, LowLatency2, ConvertFP32ToFP16, Version
+from openvino.passes.manager import Manager
+from openvino.passes.graph_rewrite import GraphRewrite, BackwardGraphRewrite
diff --git a/src/bindings/python/src/openvino/runtime/passes/graph_rewrite.py b/src/bindings/python/src/openvino/passes/graph_rewrite.py
similarity index 100%
rename from src/bindings/python/src/openvino/runtime/passes/graph_rewrite.py
rename to src/bindings/python/src/openvino/passes/graph_rewrite.py
diff --git a/src/bindings/python/src/openvino/runtime/passes/manager.py b/src/bindings/python/src/openvino/passes/manager.py
similarity index 100%
rename from src/bindings/python/src/openvino/runtime/passes/manager.py
rename to src/bindings/python/src/openvino/passes/manager.py
diff --git a/src/bindings/python/src/openvino/runtime/passes/__init__.py b/src/bindings/python/src/openvino/runtime/passes/__init__.py
index 19a28c7576d..a74f91fdcfa 100644
--- a/src/bindings/python/src/openvino/runtime/passes/__init__.py
+++ b/src/bindings/python/src/openvino/runtime/passes/__init__.py
@@ -3,8 +3,8 @@
# type: ignore
# flake8: noqa
-from openvino._pyopenvino.passes import ModelPass, Matcher, MatcherPass, PassBase, WrapType, Or, AnyInput, Optional
-from openvino._pyopenvino.passes import (
+from openvino.passes import ModelPass, Matcher, MatcherPass, PassBase, WrapType, Or, AnyInput, Optional
+from openvino.passes import (
consumers_count,
has_static_dim,
has_static_dims,
@@ -14,6 +14,6 @@ from openvino._pyopenvino.passes import (
type_matches,
type_matches_any,
)
-from openvino._pyopenvino.passes import Serialize, ConstantFolding, VisualizeTree, MakeStateful, LowLatency2, ConvertFP32ToFP16, Version
-from openvino.runtime.passes.manager import Manager
-from openvino.runtime.passes.graph_rewrite import GraphRewrite, BackwardGraphRewrite
+from openvino.passes import Serialize, ConstantFolding, VisualizeTree, MakeStateful, LowLatency2, ConvertFP32ToFP16, Version
+from openvino.passes.manager import Manager
+from openvino.passes.graph_rewrite import GraphRewrite, BackwardGraphRewrite
diff --git a/src/bindings/python/src/openvino/runtime/passes/graph_rewrite/__init__.py b/src/bindings/python/src/openvino/runtime/passes/graph_rewrite/__init__.py
new file mode 100644
index 00000000000..a9690e891ff
--- /dev/null
+++ b/src/bindings/python/src/openvino/runtime/passes/graph_rewrite/__init__.py
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+# type: ignore
+from openvino.passes.graph_rewrite import GraphRewrite, BackwardGraphRewrite
diff --git a/src/bindings/python/src/openvino/runtime/passes/manager/__init__.py b/src/bindings/python/src/openvino/runtime/passes/manager/__init__.py
new file mode 100644
index 00000000000..1a2674dd03c
--- /dev/null
+++ b/src/bindings/python/src/openvino/runtime/passes/manager/__init__.py
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+# type: ignore
+from openvino.passes.manager import Manager
diff --git a/src/bindings/python/tests/test_graph/test_manager.py b/src/bindings/python/tests/test_graph/test_manager.py
index ff72ef43158..5101414228c 100644
--- a/src/bindings/python/tests/test_graph/test_manager.py
+++ b/src/bindings/python/tests/test_graph/test_manager.py
@@ -9,7 +9,7 @@ import pytest
import openvino.runtime.opset10 as ops
from openvino import Core, Model
-from openvino.runtime.passes import Manager, Serialize, ConstantFolding, Version
+from openvino.passes import Manager, Serialize, ConstantFolding, Version
from tests.test_graph.util import count_ops_of_type
from tests.utils.helpers import create_filenames_for_ir, compare_models
@@ -48,6 +48,23 @@ def test_constant_folding():
assert np.allclose(values_out, values_expected)
+def test_runtime_passes_manager():
+ import openvino.runtime.passes as rt
+ node_constant = ops.constant(np.array([[0.0, 0.1, -0.1], [-2.5, 2.5, 3.0]], dtype=np.float32))
+ node_ceil = ops.ceiling(node_constant)
+ model = Model(node_ceil, [], "TestModel")
+
+ assert count_ops_of_type(model, node_ceil) == 1
+ assert count_ops_of_type(model, node_constant) == 1
+
+ pass_manager = rt.Manager()
+ pass_manager.register_pass(rt.ConstantFolding())
+ pass_manager.run_passes(model)
+
+ assert count_ops_of_type(model, node_ceil) == 0
+ assert count_ops_of_type(model, node_constant) == 1
+
+
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
@pytest.fixture
def prepare_ir_paths(request, tmp_path):
diff --git a/src/bindings/python/tests/test_transformations/test_graph_rewrite.py b/src/bindings/python/tests/test_transformations/test_graph_rewrite.py
index 4821dad33df..5f5c100597a 100644
--- a/src/bindings/python/tests/test_transformations/test_graph_rewrite.py
+++ b/src/bindings/python/tests/test_transformations/test_graph_rewrite.py
@@ -2,7 +2,7 @@
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from openvino.runtime import opset8
-from openvino.runtime.passes import Manager, GraphRewrite, MatcherPass, WrapType, Matcher
+from openvino.passes import Manager, GraphRewrite, MatcherPass, WrapType, Matcher
from tests.test_transformations.utils.utils import count_ops, get_relu_model, PatternReplacement
@@ -19,6 +19,19 @@ def test_graph_rewrite():
assert count_ops(model, "Relu") == [2]
+def test_runtime_graph_rewrite():
+ import openvino.runtime.passes as rt
+ model = get_relu_model()
+
+ manager = rt.Manager()
+ # check that register pass returns pass instance
+ anchor = manager.register_pass(rt.GraphRewrite())
+ anchor.add_matcher(PatternReplacement())
+ manager.run_passes(model)
+
+ assert count_ops(model, "Relu") == [2]
+
+
def test_register_new_node():
class InsertExp(MatcherPass):
def __init__(self):
diff --git a/src/bindings/python/tests/test_transformations/test_manager.py b/src/bindings/python/tests/test_transformations/test_manager.py
index e78c62d8c1a..e8f113f30b3 100644
--- a/src/bindings/python/tests/test_transformations/test_manager.py
+++ b/src/bindings/python/tests/test_transformations/test_manager.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
-from openvino.runtime.passes import Manager, GraphRewrite, BackwardGraphRewrite, Serialize
+from openvino.passes import Manager, GraphRewrite, BackwardGraphRewrite, Serialize
from tests.test_transformations.utils.utils import MyModelPass, PatternReplacement, expect_exception
diff --git a/src/bindings/python/tests/test_transformations/test_matcher_pass.py b/src/bindings/python/tests/test_transformations/test_matcher_pass.py
index c32483be316..8127e4b7612 100644
--- a/src/bindings/python/tests/test_transformations/test_matcher_pass.py
+++ b/src/bindings/python/tests/test_transformations/test_matcher_pass.py
@@ -2,7 +2,7 @@
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from openvino.runtime import opset8
-from openvino.runtime.passes import Manager, Matcher, MatcherPass, WrapType
+from openvino.passes import Manager, Matcher, MatcherPass, WrapType
from openvino.runtime.utils import replace_node
from tests.test_transformations.utils.utils import count_ops, get_relu_model, PatternReplacement
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 5df3d0a9024..efc797535d8 100644
--- a/src/bindings/python/tests/test_transformations/test_model_pass.py
+++ b/src/bindings/python/tests/test_transformations/test_model_pass.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
-from openvino.runtime.passes import Manager
+from openvino.passes import Manager
from tests.test_transformations.utils.utils import get_relu_model, MyModelPass
diff --git a/src/bindings/python/tests/test_transformations/test_pattern_ops.py b/src/bindings/python/tests/test_transformations/test_pattern_ops.py
index 24b28061582..c445c281e47 100644
--- a/src/bindings/python/tests/test_transformations/test_pattern_ops.py
+++ b/src/bindings/python/tests/test_transformations/test_pattern_ops.py
@@ -6,8 +6,8 @@ import pytest
from openvino import PartialShape
from openvino.runtime import opset13 as ops
-from openvino.runtime.passes import Matcher, WrapType, Or, AnyInput, Optional
-from openvino.runtime.passes import (
+from openvino.passes import Matcher, WrapType, Or, AnyInput, Optional
+from openvino.passes import (
consumers_count,
has_static_dim,
has_static_dims,
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 a10fea786b9..eac356cd134 100644
--- a/src/bindings/python/tests/test_transformations/test_public_transformations.py
+++ b/src/bindings/python/tests/test_transformations/test_public_transformations.py
@@ -7,7 +7,7 @@ import numpy as np
from openvino import Model, PartialShape, Shape, Core
from openvino.runtime import opset13 as ops
-from openvino.runtime.passes import (
+from openvino.passes import (
Manager,
ConstantFolding,
MakeStateful,
diff --git a/src/bindings/python/tests/test_transformations/utils/utils.py b/src/bindings/python/tests/test_transformations/utils/utils.py
index e0239ce05fd..b5f09a68ff1 100644
--- a/src/bindings/python/tests/test_transformations/utils/utils.py
+++ b/src/bindings/python/tests/test_transformations/utils/utils.py
@@ -4,7 +4,7 @@
from openvino import Model, PartialShape
from openvino.runtime import opset13 as ops
-from openvino.runtime.passes import ModelPass, Matcher, MatcherPass, WrapType
+from openvino.passes import ModelPass, Matcher, MatcherPass, WrapType
def get_relu_model():