Skip to content

Commit

Permalink
convert_model() in openvino.runtime. (openvinotoolkit#18080)
Browse files Browse the repository at this point in the history
* Used pip wheel to build OpenVINO wheel

* Added convert_model() to openvino.runtime.

* Removed duplication of InputCutInfo, LayoutMap

* Switched Model Conversion API tests to convert_model from openvino.runtime.

* Small correction.

* Format correction.

* Small correction.

* Removed duplication of moc frontend files.

* Small correction.

* Removed duplication of cli_parser, offline_transformations.

* Code corrections.

* Removed code duplications.

* Removed code duplications.

* Updated codeowners.

* Switched layer tests to convert_model().

* Improvements

* Small correction.

* Caffe parser path fix.

* Added python api properly into deb / rpm packages

* Moved implementation to ovc tool.

* Moved implementation to ovc tool.

* Small correction.

* Use cmake -E variant from cmake 3.13

* Namespace fixes.

* Minor fixes.

* Pylint fixes.

* Fixed BOM file.

* Small corrections.

* Minor corrections.

* Minor fix.

* Error fixes.

* Added telemetry requirement.

* Improvements to fix CI

* Some refactoring

* Don't use developer package for scripts projects

* Added exception in case when MO is not imported.

* Removed exception from init.

* Removed changes from cmake.

* Added unit ovc tests, fixed minor errors.

* Added ovc unit tests to azure.

* Corrected imports.

* Fixed path to tests.

* Added missed files.

* Corrected github labels.

* Removed benchmark app from dev package.

* Small fix.

* Small corrections.

* Comment fixed.

* Removed changes from setup.py

* Removed not needed change.

* Removed duplicating unit tests.

* Removed wrong change.

* Removed not needed change.

* Apply suggestions from code review

Co-authored-by: Roman Kazantsev <[email protected]>

* Added ovc tool test, corrected imports.

* Added legacy TF config test.

* Removed not needed files.

---------

Co-authored-by: Ilya Lavrenov <[email protected]>
Co-authored-by: Roman Kazantsev <[email protected]>
  • Loading branch information
3 people authored Jun 30, 2023
1 parent a2b7d56 commit 5d399fa
Show file tree
Hide file tree
Showing 133 changed files with 11,059 additions and 3,017 deletions.
4 changes: 4 additions & 0 deletions .ci/azure/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ jobs:
python3 -m pytest -s $(INSTALL_TEST_DIR)/mo/unit_tests --junitxml=$(INSTALL_TEST_DIR)/TEST-ModelOptimizer.xml
displayName: 'Model Optimizer UT'
- script: |
python3 -m pytest -s $(REPO_DIR)/tools/ovc/unit_tests --junitxml=$(INSTALL_TEST_DIR)/TEST-OpenVinoConversion.xml
displayName: 'OpenVino Conversion UT'
- script: $(RUN_PREFIX) $(INSTALL_TEST_DIR)/ov_cpu_func_tests --gtest_filter=*smoke* --gtest_print_time=1 --gtest_output=xml:$(INSTALL_TEST_DIR)/TEST-ov_cpu_func_tests.xml
displayName: 'CPU FuncTests'
condition: and(succeeded(), eq(variables['CMAKE_BUILD_SHARED_LIBS'], 'OFF'))
Expand Down
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
/tools/legacy/ @openvinotoolkit/openvino-samples-maintainers
/tools/openvino_dev/ @openvinotoolkit/openvino-tools-maintainers @openvinotoolkit/openvino-ie-python-api-maintainers
/tools/mo/ @openvinotoolkit/openvino-mo-maintainers
/tools/ovc/ @openvinotoolkit/openvino-mo-maintainers
/tools/pot/ @openvinotoolkit/openvino-pot-maintainers
/thirdparty/open_model_zoo/ @openvinotoolkit/omz-maintainers @openvinotoolkit/openvino-pot-maintainers

Expand Down
1 change: 1 addition & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

'category: MO':
- 'tools/mo/**/*'
- 'tools/ovc/**/*'

'category: ONNX FE':
- 'src/frontends/onnx/**/*'
Expand Down
1 change: 1 addition & 0 deletions src/bindings/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy>=1.16.6
singledispatchmethod; python_version<'3.8'
openvino-telemetry>=2023.0.0
6 changes: 3 additions & 3 deletions src/bindings/python/src/openvino/frontend/tensorflow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# mypy: ignore-errors


from openvino.tools.mo.moc_frontend.shape_utils import get_static_shape
from openvino.tools.mo.utils.versions_checker import get_environment_setup # pylint: disable=no-name-in-module
from openvino.tools.mo.utils.error import Error
from openvino.tools.ovc.moc_frontend.shape_utils import get_static_shape
from openvino.tools.ovc.environment_setup_utils import get_environment_setup # pylint: disable=no-name-in-module
from openvino.tools.ovc.error import Error
from distutils.version import LooseVersion
import logging as log

Expand Down
7 changes: 7 additions & 0 deletions src/bindings/python/src/openvino/runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
from openvino.runtime.ie_api import compile_model


# Model Conversion API
try:
from openvino.tools.ovc import convert_model, InputCutInfo, LayoutMap
except ImportError:
pass


# Extend Node class to support binary operators
Node.__add__ = opset11.add
Node.__sub__ = opset11.subtract
Expand Down
24 changes: 12 additions & 12 deletions src/bindings/python/wheel/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@
"install_dir": PY_PACKAGES_DIR,
"binary_dir": OPENVINO_PYTHON_BINARY_DIR,
},
"ovc": {
"entry_point": {
"console_scripts": [
"ovc = openvino.tools.ovc.main:main",
],
},
"name": f"pyopenvino_{PYTHON_VERSION}",
"prefix": f"{BUILD_BASE}/site-packages",
"source_dir": f"{OPENVINO_SOURCE_DIR}/tools/ovc",
"install_dir": PY_PACKAGES_DIR,
"binary_dir": "ovc",
},
# "benchmark_app": { # noqa: E731
# "entry_point": { # noqa: E731
# "console_scripts": [ # noqa: E731
Expand All @@ -187,18 +199,6 @@
# "install_dir": PY_PACKAGES_DIR, # noqa: E731
# "binary_dir": "benchmark_app", # noqa: E731
# }, # noqa: E731
# "model_optimizer": { # noqa: E731
# "entry_point": { # noqa: E731
# "console_scripts": [ # noqa: E731
# "mo = openvino.tools.mo.main:main", # noqa: E731
# ], # noqa: E731
# }, # noqa: E731
# "name": f"pyopenvino_{PYTHON_VERSION}", # noqa: E731
# "prefix": f"{BUILD_BASE}/site-packages", # noqa: E731
# "source_dir": f"{OPENVINO_SOURCE_DIR}/tools/mo", # noqa: E731
# "install_dir": PY_PACKAGES_DIR, # noqa: E731
# "binary_dir": "model_optimizer", # noqa: E731
# }, # noqa: E731
}


Expand Down
5 changes: 2 additions & 3 deletions tests/layer_tests/common/layer_test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import numpy as np
from common.constants import test_device, test_precision
from common.layer_utils import IEInfer, InferAPI20
from common.utils.common_utils import generate_ir
from common.utils.parsers import mapping_parser
from common.utils.common_utils import generate_ir_python_api


class CommonLayerTest:
Expand Down Expand Up @@ -60,7 +59,7 @@ def _test(self, framework_model, ref_net, ie_device, precision, ir_version, temp
else:
mo_params["use_legacy_frontend"] = True

exit_code, stderr = generate_ir(**mo_params)
exit_code, stderr = generate_ir_python_api(**mo_params)

del os.environ['MO_ENABLED_TRANSFORMS']
del os.environ['MO_DISABLED_TRANSFORMS']
Expand Down
9 changes: 6 additions & 3 deletions tests/layer_tests/common/mo_convert_test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from pathlib import Path

from openvino.runtime import serialize
from openvino.runtime import serialize, convert_model
from openvino.tools.mo import convert_model as legacy_convert_model
from openvino.test_utils import compare_functions
from openvino.tools.mo import convert_model

from common.utils.common_utils import generate_ir

Expand All @@ -16,7 +16,10 @@ def generate_ir_python_api(**kwargs):
output_dir = kwargs['output_dir']
model_name = kwargs['model_name']
del kwargs['output_dir']
model = convert_model(**kwargs)
if 'use_legacy_frontend' in kwargs:
model = legacy_convert_model(**kwargs)
else:
model = convert_model(**kwargs)
serialize(model, str(Path(output_dir, model_name + '.xml')))

def _test(self, temp_dir, test_params, ref_params):
Expand Down
15 changes: 15 additions & 0 deletions tests/layer_tests/common/utils/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

import logging
import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -46,6 +47,20 @@ def generate_ir(coverage=False, **kwargs):
return exit_code, stderr


def generate_ir_python_api(coverage=False, **kwargs):
from openvino.runtime import convert_model, serialize
from openvino.tools.mo import convert_model as legacy_convert_model

if "use_legacy_frontend" in kwargs and kwargs['use_legacy_frontend']:
ov_model = legacy_convert_model(**kwargs)
else:
ov_model = convert_model(**kwargs)

out_dir = kwargs['output_dir'] + os.sep + kwargs['model_name'] + ".xml"
serialize(ov_model, out_dir)

return 0, ""

def shell(cmd, env=None, cwd=None, out_format="plain"):
"""
Run command execution in specified environment
Expand Down
2 changes: 1 addition & 1 deletion tests/layer_tests/mo_python_api_tests/mo_convert_help.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from openvino.tools.mo import convert_model
from openvino.runtime import convert_model

if __name__ == "__main__":
convert_model(help=True)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import numpy as np
import os
import pytest
from openvino.runtime import Model, Layout, PartialShape, Shape, layout_helpers, Type, Dimension
from openvino.tools.mo.convert import InputCutInfo, LayoutMap
from openvino.runtime import Model, Layout, PartialShape, Shape, layout_helpers, Type, Dimension, InputCutInfo, LayoutMap

from common.mo_convert_test_class import CommonMOConvertTest
from common.tf_layer_test_class import save_to_pb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import pytest
import torch
import unittest
from openvino.runtime import PartialShape, Dimension, Model, Type
from openvino.tools.mo import InputCutInfo
from openvino.runtime import PartialShape, Dimension, Model, Type, InputCutInfo

from common.mo_convert_test_class import CommonMOConvertTest

Expand Down Expand Up @@ -747,7 +746,7 @@ def forward(self, x):

class ConvertRaises(unittest.TestCase):
def test_example_inputs(self):
from openvino.tools.mo import convert_model
from openvino.runtime import convert_model
pytorch_model = create_pt_model_with_custom_op()

# Check that mo raises error message of wrong argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ class TFConvertTest(unittest.TestCase):
@pytest.mark.precommit
def test_tf_function_no_signature(self):
import tensorflow as tf
from openvino.tools.mo import convert_model
from openvino.runtime import convert_model

@tf.function()
def function(x1, x2):
Expand Down
2 changes: 1 addition & 1 deletion tests/layer_tests/mo_python_api_tests/test_mo_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import unittest
from openvino.tools.mo import mo
from openvino.tools.mo.utils.cli_parser import get_mo_convert_params
from openvino.tools.ovc.cli_parser import get_mo_convert_params
from pathlib import Path

from common.utils.common_utils import shell
Expand Down
89 changes: 89 additions & 0 deletions tests/layer_tests/mo_python_api_tests/test_ovc_cli_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import os
import sys
from pathlib import Path

import numpy as np
import openvino.runtime as ov
from openvino.runtime import PartialShape, Model
from openvino.test_utils import compare_functions
from openvino.tools.ovc import ovc

from common.mo_convert_test_class import CommonMOConvertTest
from common.tf_layer_test_class import save_to_pb
from common.utils.common_utils import shell


def generate_ir_ovc(coverage=False, **kwargs):
# Get OVC file directory
ovc_path = Path(ovc.__file__).parent

ovc_runner = ovc_path.joinpath('main.py').as_posix()
if coverage:
params = [sys.executable, '-m', 'coverage', 'run', '-p', '--source={}'.format(ovc_runner.parent),
'--omit=*_test.py', ovc_runner]
else:
params = [sys.executable, ovc_runner]
for key, value in kwargs.items():
if key == "batch":
params.extend(("-b", str(value)))
elif key == "k":
params.extend(("-k", str(value)))
# for FP32 set explicitly compress_to_fp16=False,
# if we omit this argument for FP32, it will be set implicitly to True as the default
elif key == 'compress_to_fp16':
params.append("--{}={}".format(key, value))
elif isinstance(value, bool) and value:
params.append("--{}".format(key))
elif isinstance(value, bool) and not value:
continue
elif (isinstance(value, tuple) and value) or (isinstance(value, str)):
params.extend(("--{}".format(key), str('"{}"'.format(value))))
elif key == "mean_values" and (' ' in value or '(' in value):
params.extend(("--{}".format(key), str('"{}"'.format(value))))
else:
params.extend(("--{}".format(key), str(value)))
exit_code, stdout, stderr = shell(params)
return exit_code, stderr

def create_ref_graph():
shape = PartialShape([1, 3, 2, 2])
param = ov.opset8.parameter(shape, dtype=np.float32)
relu = ov.opset8.relu(param)
sigm = ov.opset8.sigmoid(relu)

return Model([sigm], [param], "test")

class TestOVCTool(CommonMOConvertTest):
def create_tf_model(self, tmp_dir):
import tensorflow as tf

tf.compat.v1.reset_default_graph()

with tf.compat.v1.Session() as sess:
inp = tf.compat.v1.placeholder(tf.float32, [1, 3, 2, 2], 'Input')
relu = tf.nn.relu(inp, name='Relu')
output = tf.nn.sigmoid(relu, name='Sigmoid')
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def

# save model to .pb and return path to the model
return save_to_pb(tf_net, tmp_dir)


def test_ovc_tool(self, ie_device, precision, ir_version, temp_dir, use_new_frontend, use_old_api):
from openvino.runtime import Core

model_path = self.create_tf_model(temp_dir)

core = Core()

# tests for MO cli tool
exit_code, stderr = generate_ir_ovc(coverage=False, **{"input_model": model_path, "output_dir": temp_dir})
assert not exit_code

ov_model = core.read_model(os.path.join(temp_dir, "model.xml"))
flag, msg = compare_functions(ov_model, create_ref_graph(), False)
assert flag, msg
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _prepare_input(self):

def convert_via_mo(self, model, example_input, trace_model, dynamic_shapes, ov_inputs):
import torch
from openvino.tools.mo import convert_model
from openvino.runtime import convert_model
kwargs = {"example_input": example_input if len(
example_input) > 1 else example_input[0], "compress_to_fp16": False}
with torch.no_grad():
Expand Down
7 changes: 6 additions & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ add_subdirectory(mo)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pot/openvino/tools/pot/version.txt.in"
"${CMAKE_CURRENT_SOURCE_DIR}/pot/openvino/tools/pot/version.txt" @ONLY)

# Benchmark Tool

if(ENABLE_PYTHON)

# Benchmark Tool
add_subdirectory(benchmark_tool)

# OpenVino Conversion Tool
add_subdirectory(ovc)

endif()

# wheel openvino-dev
Expand Down
17 changes: 1 addition & 16 deletions tools/mo/automation/package_BOM.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ openvino/tools/mo/back/MatMulNormalizer.py
openvino/tools/mo/back/MaxPool.py
openvino/tools/mo/back/names_uniqueness_check.py
openvino/tools/mo/back/NormalizeToNormalizeL2.py
openvino/tools/mo/back/offline_transformations.py
openvino/tools/mo/back/op_versioning.py
openvino/tools/mo/back/OptimizeTransposeReshapeSequence.py
openvino/tools/mo/back/PackBinaryWeights.py
openvino/tools/mo/back/pass_separator.py
openvino/tools/mo/back/preprocessing.py
openvino/tools/mo/back/priorbox_mutation.py
openvino/tools/mo/back/ProposalMutation.py
openvino/tools/mo/back/ReduceMerge.py
Expand Down Expand Up @@ -831,16 +829,6 @@ openvino/tools/mo/mo_mxnet.py
openvino/tools/mo/mo_onnx.py
openvino/tools/mo/mo_paddle.py
openvino/tools/mo/mo_tf.py
openvino/tools/mo/moc_frontend/__init__.py
openvino/tools/mo/moc_frontend/analysis.py
openvino/tools/mo/moc_frontend/check_config.py
openvino/tools/mo/moc_frontend/extractor.py
openvino/tools/mo/moc_frontend/layout_utils.py
openvino/tools/mo/moc_frontend/paddle_frontend_utils.py
openvino/tools/mo/moc_frontend/pipeline.py
openvino/tools/mo/moc_frontend/pytorch_frontend_utils.py
openvino/tools/mo/moc_frontend/serialize.py
openvino/tools/mo/moc_frontend/shape_utils.py
openvino/tools/mo/ops/__init__.py
openvino/tools/mo/ops/activation.py
openvino/tools/mo/ops/activation_ops.py
Expand Down Expand Up @@ -1038,7 +1026,6 @@ openvino/tools/mo/utils/find_inputs.py
openvino/tools/mo/utils/get_ov_update_message.py
openvino/tools/mo/utils/graph.py
openvino/tools/mo/utils/guess_framework.py
openvino/tools/mo/utils/help.py
openvino/tools/mo/utils/ie_version.py
openvino/tools/mo/utils/import_extensions.py
openvino/tools/mo/utils/ir_engine/__init__.py
Expand Down Expand Up @@ -1098,12 +1085,10 @@ openvino/tools/mo/utils/shape.py
openvino/tools/mo/utils/simple_proto_parser.py
openvino/tools/mo/utils/str_to.py
openvino/tools/mo/utils/summarize_graph.py
openvino/tools/mo/utils/telemetry_params.py
openvino/tools/mo/utils/telemetry_stub.py
openvino/tools/mo/utils/telemetry_utils.py
openvino/tools/mo/utils/tensorboard_util.py
openvino/tools/mo/utils/type_utils.py
openvino/tools/mo/utils/unsupported_ops.py
openvino/tools/mo/utils/utils.py
openvino/tools/mo/utils/version.py
openvino/tools/mo/utils/versions_checker.py
openvino/tools/mo/utils/version.py
Loading

0 comments on commit 5d399fa

Please sign in to comment.