forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'itikhono/bug_fix/envvar_typo' of https://github.com/iti…
…khono/openvino into itikhono/bug_fix/envvar_typo
- Loading branch information
Showing
92 changed files
with
46,621 additions
and
688 deletions.
There are no files selected for viewing
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
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
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
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,95 @@ | ||
# ! [matcher_pass:ov_matcher_pass_py] | ||
''' | ||
``MatcherPass`` is used for pattern-based transformations. | ||
To create transformation, you need to: | ||
1. Create a pattern. | ||
2. Implement a callback. | ||
3. Register the pattern and ``Matcher``. | ||
The next example defines transformation that searches for the ``Relu`` layer and inserts after it another | ||
``Relu`` layer. | ||
''' | ||
|
||
from openvino.runtime.passes import MatcherPass | ||
from snippets import get_model | ||
|
||
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) | ||
|
||
|
||
''' | ||
After running this code, you will see the next: | ||
model ops : | ||
parameter | ||
result | ||
relu | ||
model ops : | ||
parameter | ||
result | ||
relu | ||
new_relu | ||
In order to run this script, you need to export PYTHONPATH as the path to binary OpenVINO python models. | ||
''' | ||
from openvino.runtime.passes import Manager | ||
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 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_model() | ||
print_model_ops(model) | ||
manager.run_passes(model) | ||
print_model_ops(model) | ||
|
||
# ! [matcher_pass:ov_matcher_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,47 @@ | ||
# ! [model_pass:ov_model_pass_py] | ||
|
||
''' | ||
``ModelPass`` can be used as a base class for transformation classes that take entire ``Model`` and proceed with it. | ||
To create transformation, you need to: | ||
1. Define a class with ``ModelPass`` as a parent. | ||
2. Redefine the run_on_model method that will receive ``Model`` as an argument. | ||
''' | ||
|
||
from openvino.runtime.passes import ModelPass | ||
from snippets import get_model | ||
|
||
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()) | ||
|
||
|
||
''' | ||
This example defines transformation that prints all model operation names. | ||
The next example shows ModelPass-based transformation usage. | ||
You create ``Model`` with ``Relu``, ``Parameter`` and ``Result`` nodes. After running this code, you will see the names of the nodes. | ||
In order to run this script, you need to export PYTHONPATH as the path to binary OpenVINO python models. | ||
''' | ||
|
||
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 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_model()) | ||
# ! [model_pass: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
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
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
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
Oops, something went wrong.