forked from openvino-dev-samples/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.
Offline transformations exposed to python with pybind11 (openvinotool…
…kit#7987) * New approach to offline transformations * Include paths fix * Imports fix * offline_transformations target dependency simplification * MakeStatefulTransformation exposed to python * Python bindings build configuration fix * Test variable name refactor * Stop crying (snow)flake * Snake cased offline transformations API * Removal of old offline transformations from python * Imports adaptation to new code style * offline_transformations as a part of the common wheel * Cmake simplification and refactor * Correct transform invocation * CI fix * Proper dependency check in MO * _pyngraph as a dependency of MO in cmake * IR serialization fix in MO * POT adaptation to the new API * Revert "Removal of old offline transformations from python" This reverts commit f9a0551. * Merge of old& new bindings for offline_transformations * Revert "POT adaptation to the new API" This reverts commit 499554e. * Obsolete cmake line removal * Missing comma and merge conflict fix * Offline transformations tests fix * IE imports removal from check_ie_bindings * Installation of opevino/__init__.py fix * Obsolete line removal * MO serialization switched to the new API * Revert of preliminary MO adaptation to the new API * Another magic spell that will hopefully make CI pass * Python api cmake dependencies reorg * Temporary solution for the CI/cpack errors * Installation fix and code formatting * ie_api & pyopenvino dependency removal * Explicit cpack configuration for the new API * cpack configuration adaptation * Revert of obsolete cpack changes Co-authored-by: Alexander Zhogov <[email protected]>
- Loading branch information
Tomasz Dołbniak
and
Alexander Zhogov
authored
Nov 3, 2021
1 parent
db27dcc
commit b5f0ca5
Showing
10 changed files
with
192 additions
and
6 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
10 changes: 10 additions & 0 deletions
10
runtime/bindings/python/src/openvino/offline_transformations_pybind/__init__.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,10 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2021 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from openvino.pyopenvino.offline_transformations_pybind import apply_moc_transformations | ||
from openvino.pyopenvino.offline_transformations_pybind import apply_pot_transformations | ||
from openvino.pyopenvino.offline_transformations_pybind import apply_low_latency_transformation | ||
from openvino.pyopenvino.offline_transformations_pybind import apply_pruning_transformation | ||
from openvino.pyopenvino.offline_transformations_pybind import generate_mapping_file | ||
from openvino.pyopenvino.offline_transformations_pybind import apply_make_stateful_transformation |
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
84 changes: 84 additions & 0 deletions
84
runtime/bindings/python/src/pyopenvino/core/offline_transformations.cpp
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,84 @@ | ||
// Copyright (C) 2021 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "pyopenvino/core/offline_transformations.hpp" | ||
|
||
#include <pybind11/stl.h> | ||
|
||
#include <generate_mapping_file.hpp> | ||
#include <openvino/pass/make_stateful.hpp> | ||
#include <pot_transformations.hpp> | ||
#include <pruning.hpp> | ||
#include <transformations/common_optimizations/moc_transformations.hpp> | ||
|
||
#include "openvino/pass/low_latency.hpp" | ||
#include "openvino/pass/manager.hpp" | ||
|
||
namespace py = pybind11; | ||
|
||
void regmodule_offline_transformations(py::module m) { | ||
// TODO: change the submodule name according to the description in 69196 | ||
py::module m_offline_transformations = | ||
m.def_submodule("offline_transformations_pybind", "Offline transformations module"); | ||
|
||
m_offline_transformations.def( | ||
"apply_moc_transformations", | ||
[](std::shared_ptr<ov::Function> function, bool cf) { | ||
ov::pass::Manager manager; | ||
manager.register_pass<ngraph::pass::MOCTransformations>(cf); | ||
manager.run_passes(function); | ||
}, | ||
py::arg("function"), | ||
py::arg("cf")); | ||
|
||
m_offline_transformations.def( | ||
"apply_pot_transformations", | ||
[](std::shared_ptr<ov::Function> function, std::string device) { | ||
ov::pass::Manager manager; | ||
manager.register_pass<ngraph::pass::POTTransformations>(std::move(device)); | ||
manager.run_passes(function); | ||
}, | ||
py::arg("function"), | ||
py::arg("device")); | ||
|
||
m_offline_transformations.def( | ||
"apply_low_latency_transformation", | ||
[](std::shared_ptr<ov::Function> function, bool use_const_initializer = true) { | ||
ov::pass::Manager manager; | ||
manager.register_pass<ov::pass::LowLatency2>(use_const_initializer); | ||
manager.run_passes(function); | ||
}, | ||
py::arg("function"), | ||
py::arg("use_const_initializer") = true); | ||
|
||
m_offline_transformations.def( | ||
"apply_pruning_transformation", | ||
[](std::shared_ptr<ngraph::Function> function) { | ||
ov::pass::Manager manager; | ||
manager.register_pass<ngraph::pass::Pruning>(); | ||
manager.run_passes(function); | ||
}, | ||
py::arg("function")); | ||
|
||
m_offline_transformations.def( | ||
"generate_mapping_file", | ||
[](std::shared_ptr<ov::Function> function, std::string path, bool extract_names) { | ||
ov::pass::Manager manager; | ||
manager.register_pass<ngraph::pass::GenerateMappingFile>(path, extract_names); | ||
manager.run_passes(function); | ||
}, | ||
py::arg("function"), | ||
py::arg("path"), | ||
py::arg("extract_names")); | ||
|
||
m_offline_transformations.def( | ||
"apply_make_stateful_transformation", | ||
[](std::shared_ptr<ov::Function> function, const std::map<std::string, std::string>& param_res_names) { | ||
ngraph::pass::Manager manager; | ||
manager.register_pass<ov::pass::MakeStateful>(param_res_names); | ||
manager.run_passes(function); | ||
}, | ||
py::arg("function"), | ||
py::arg("param_res_names")); | ||
} |
11 changes: 11 additions & 0 deletions
11
runtime/bindings/python/src/pyopenvino/core/offline_transformations.hpp
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,11 @@ | ||
// Copyright (C) 2021 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace py = pybind11; | ||
|
||
void regmodule_offline_transformations(py::module m); |
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
62 changes: 62 additions & 0 deletions
62
runtime/bindings/python/tests/test_transformations/test_offline_api.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,62 @@ | ||
# Copyright (C) 2018-2021 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# TODO: change the module name according to the description in 69196 | ||
from openvino.offline_transformations_pybind import apply_moc_transformations, apply_pot_transformations, \ | ||
apply_low_latency_transformation, apply_pruning_transformation, apply_make_stateful_transformation | ||
|
||
from openvino import Function, PartialShape | ||
import openvino as ov | ||
|
||
|
||
def get_test_function(): | ||
param = ov.opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") | ||
relu = ov.opset8.relu(param) | ||
res = ov.opset8.result(relu, name="result") | ||
return Function([res], [param], "test") | ||
|
||
|
||
def test_moc_transformations(): | ||
function = get_test_function() | ||
|
||
apply_moc_transformations(function, False) | ||
|
||
assert function is not None | ||
assert len(function.get_ops()) == 3 | ||
|
||
|
||
def test_pot_transformations(): | ||
function = get_test_function() | ||
|
||
apply_pot_transformations(function, "GNA") | ||
|
||
assert function is not None | ||
assert len(function.get_ops()) == 3 | ||
|
||
|
||
def test_low_latency_transformation(): | ||
function = get_test_function() | ||
|
||
apply_low_latency_transformation(function, True) | ||
|
||
assert function is not None | ||
assert len(function.get_ops()) == 3 | ||
|
||
|
||
def test_pruning_transformation(): | ||
function = get_test_function() | ||
|
||
apply_pruning_transformation(function) | ||
|
||
assert function is not None | ||
assert len(function.get_ops()) == 3 | ||
|
||
|
||
def test_make_stateful_transformations(): | ||
function = get_test_function() | ||
|
||
apply_make_stateful_transformation(function, {"parameter": "result"}) | ||
|
||
assert function is not None | ||
assert len(function.get_parameters()) == 0 | ||
assert len(function.get_results()) == 0 |