Skip to content

Commit

Permalink
[Py OV] Add runtime/passes to openvino namespace (#27785)
Browse files Browse the repository at this point in the history
### Details:
- copy _openvino/runtime/passes_ to _openvino/_
- `passes` are not initialized in _openvino/runtime/__init__.py_ so no
changes to _openvino/__init__.py_
- update imports for Python API tests (_src/bindings/python/tests_) 
- add possibility to import `passes` from `openvino` module
```python
from openvino.passes import Manager

manager = Manager()
```

Part of `openvino.runtime` deprecation

### Tickets:
 - *ticket-id*
  • Loading branch information
almilosz authored Dec 3, 2024
1 parent 340a3d4 commit d2c9c80
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 14 deletions.
19 changes: 19 additions & 0 deletions src/bindings/python/src/openvino/passes/__init__.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions src/bindings/python/src/openvino/runtime/passes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,6 +14,6 @@
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
19 changes: 18 additions & 1 deletion src/bindings/python/tests/test_graph/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit d2c9c80

Please sign in to comment.