-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transformations documentation add python API MatcherPass and ModelPass #24058
Closed
evkotov
wants to merge
2
commits into
openvinotoolkit:master
from
evkotov:cvs_76333_python_transformations
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...xtensibility/openvino-plugin-library/transformation-python-api/matcher-pass.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
.. {#openvino_docs_Extensibility_UG_matcher_pass} | ||
|
||
OpenVINO Model Pass Python API | ||
============================== | ||
|
||
|
||
.. meta:: | ||
:description: Learn how to create a pattern, implement a callback, register | ||
the pattern and Matcher to execute MatcherPass transformation | ||
on a model. | ||
|
||
``MatcherPass`` is used for pattern-based transformations. | ||
To create transformation you need: | ||
|
||
1. Create a pattern | ||
2. Implement a callback | ||
3. Register the pattern and Matcher | ||
|
||
In the next example we define transformation that searches for ``Relu`` layer and inserts after it another | ||
``Relu`` layer. | ||
|
||
.. doxygensnippet:: docs/snippets/ov_matcher_pass.py | ||
:language: py | ||
:fragment: [matcher_pass:ov_matcher_pass_py] | ||
|
||
The next example shows MatcherPass-based transformation usage. | ||
|
||
.. doxygensnippet:: docs/snippets/ov_matcher_pass.py | ||
:language: py | ||
:fragment: [matcher_pass_full_example:ov_matcher_pass_py] | ||
|
||
After running this code you will see the next: text | ||
``` | ||
model ops : | ||
parameter | ||
result | ||
relu | ||
|
||
model ops : | ||
parameter | ||
result | ||
relu | ||
new_relu | ||
``` | ||
|
||
In oder to run this script you need to export PYTHONPATH as the path to binary OpenVINO python models. |
30 changes: 30 additions & 0 deletions
30
...-extensibility/openvino-plugin-library/transformation-python-api/model-pass.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.. {#openvino_docs_Extensibility_UG_model_pass} | ||
|
||
OpenVINO Model Pass Python API | ||
============================== | ||
|
||
|
||
.. meta:: | ||
:description: Learn how to use Model Pass transformation class to take entire | ||
ov::Model as input and process it. | ||
|
||
``ModelPass`` can be used as base class for transformation classes that take entire ``Model`` and proceed it. | ||
To create transformation you need: | ||
|
||
1. Define class with ``ModelPass`` as a parent | ||
2. Redefine run_on_model method that will receive ``Model`` as an argument | ||
|
||
.. doxygensnippet:: docs/snippets/ov_model_pass.py | ||
:language: py | ||
:fragment: [model_pass:ov_model_pass_py] | ||
|
||
In this example we define transformation that prints all model operation names. | ||
|
||
The next example shows ModelPass-based transformation usage. | ||
|
||
.. doxygensnippet:: docs/snippets/ov_model_pass.py | ||
:language: py | ||
:fragment: [model_pass_full_example:ov_model_pass_py] | ||
|
||
We create Model with Relu, Parameter and Result nodes. After running this code you will see names of these three nodes. | ||
In oder to run this script you need to export PYTHONPATH as the path to binary OpenVINO python models. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ! [model_pass:ov_model_pass_py] | ||
from openvino.runtime.passes import ModelPass | ||
|
||
class MyModelPass(ModelPass): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def run_on_model(self, model): | ||
for op in model.get_ops(): | ||
print(op.get_friendly_name()) | ||
// ! [model_pass:ov_model_pass_py] | ||
|
||
// ! [model_pass_full_example:ov_model_pass_py] | ||
from openvino.runtime.passes import Manager, GraphRewrite, BackwardGraphRewrite, Serialize | ||
from openvino import Model, PartialShape | ||
from openvino.runtime import opset13 as ops | ||
from openvino.runtime.passes import ModelPass, Matcher, MatcherPass, WrapType | ||
|
||
|
||
def get_relu_model(): | ||
# Parameter->Relu->Result | ||
param = ops.parameter(PartialShape([1, 3, 22, 22]), name="parameter") | ||
relu = ops.relu(param.output(0)) | ||
res = ops.result(relu.output(0), name="result") | ||
return Model([res], [param], "test") | ||
|
||
|
||
class MyModelPass(ModelPass): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def run_on_model(self, model): | ||
for op in model.get_ops(): | ||
print(op.get_friendly_name()) | ||
|
||
|
||
manager = Manager() | ||
manager.register_pass(MyModelPass()) | ||
manager.run_passes(get_relu_model()) | ||
// ! [model_pass_full_example:ov_model_pass_py] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// ! [matcher_pass:ov_matcher_pass_py] | ||
from openvino.runtime.passes import MatcherPass | ||
|
||
class PatternReplacement(MatcherPass): | ||
def __init__(self): | ||
MatcherPass.__init__(self) | ||
relu = WrapType("opset13::Relu") | ||
|
||
def callback(matcher: Matcher) -> bool: | ||
root = matcher.get_match_root() | ||
new_relu = ops.relu(root.input(0).get_source_output()) | ||
|
||
"""Use new operation for additional matching | ||
self.register_new_node(new_relu) | ||
|
||
Input->Relu->Result => Input->Relu->Relu->Result | ||
""" | ||
root.input(0).replace_source_output(new_relu.output(0)) | ||
return True | ||
|
||
self.register_matcher(Matcher(relu, "PatternReplacement"), callback) | ||
// ! [matcher_pass:ov_matcher_pass_py] | ||
|
||
// ! [matcher_pass_full_example:ov_matcher_pass_py] | ||
from openvino.runtime.passes import Manager, GraphRewrite, BackwardGraphRewrite, Serialize | ||
from openvino import Model, PartialShape | ||
from openvino.runtime import opset13 as ops | ||
from openvino.runtime.passes import ModelPass, Matcher, MatcherPass, WrapType | ||
|
||
class PatternReplacement(MatcherPass): | ||
def __init__(self): | ||
MatcherPass.__init__(self) | ||
relu = WrapType("opset13::Relu") | ||
|
||
def callback(matcher: Matcher) -> bool: | ||
root = matcher.get_match_root() | ||
new_relu = ops.relu(root.input(0).get_source_output()) | ||
new_relu.set_friendly_name('new_relu') | ||
|
||
"""Use new operation for additional matching | ||
self.register_new_node(new_relu) | ||
|
||
Input->Relu->Result => Input->Relu->Relu->Result | ||
""" | ||
root.input(0).replace_source_output(new_relu.output(0)) | ||
return True | ||
|
||
self.register_matcher(Matcher(relu, "PatternReplacement"), callback) | ||
|
||
|
||
def get_relu_model(): | ||
# Parameter->Relu->Result | ||
param = ops.parameter(PartialShape([1, 3, 22, 22]), name="parameter") | ||
relu = ops.relu(param.output(0)) | ||
relu.set_friendly_name('relu') | ||
res = ops.result(relu.output(0), name="result") | ||
return Model([res], [param], "test") | ||
|
||
|
||
def print_model_ops(model): | ||
print('model ops : ') | ||
for op in model.get_ops(): | ||
print(op.get_friendly_name()) | ||
print('') | ||
|
||
|
||
manager = Manager() | ||
manager.register_pass(PatternReplacement()) | ||
|
||
|
||
model = get_relu_model() | ||
print_model_ops(model) | ||
manager.run_passes(model) | ||
print_model_ops(model) | ||
|
||
// ! [matcher_pass_full_example:ov_matcher_pass_py] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a separate page for Python API
I believe it's better to update C++ page with these python examples