Skip to content

Commit

Permalink
Align openvino.compile_model and openvino.Core.compile_model functions (
Browse files Browse the repository at this point in the history
openvinotoolkit#19778)

Co-Authored-By: Anastasia Kuporosova <anastasia.kuporosova@intel.com>
  • Loading branch information
2 people authored and alvoron committed Nov 6, 2023
1 parent cf825ec commit aa8f3ac
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
21 changes: 16 additions & 5 deletions src/bindings/python/src/openvino/runtime/ie_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,25 @@ def import_model(
)


def compile_model(model_path: Union[str, Path]) -> CompiledModel:
def compile_model(
model: Union[Model, str, Path],
device_name: Optional[str] = "AUTO",
config: Optional[dict] = None,
) -> CompiledModel:
"""Compact method to compile model with AUTO plugin.
:param model_path: Path to file with model.
:type model_path: str, pathlib.Path
:return: A compiled model
:param model: Model acquired from read_model function or a path to a model in IR / ONNX / PDPD /
TF and TFLite format.
:type model: Union[openvino.runtime.Model, str, pathlib.Path]
:param device_name: Optional. Name of the device to load the model to. If not specified,
the default OpenVINO device will be selected by AUTO plugin.
:type device_name: str
:param config: Optional dict of pairs:
(property name, property value) relevant only for this load operation.
:type config: dict, optional
:return: A compiled model.
:rtype: openvino.runtime.CompiledModel
"""
core = Core()
return core.compile_model(model_path, "AUTO")
return core.compile_model(model, device_name, {} if config is None else config)
64 changes: 45 additions & 19 deletions src/bindings/python/tests/test_runtime/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
serialize,
)

import openvino.properties.hint as hints
from openvino.runtime import Extension
from tests.utils.helpers import (
generate_image,
Expand All @@ -40,15 +41,6 @@ def test_compact_api_xml():
assert np.argmax(results[list(results)[0]]) == 531


# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
def test_compact_api_xml_posix_path(request, tmp_path):
xml_path, _ = create_filename_for_test(request.node.name, tmp_path, True)
model = get_relu_model()
serialize(model, xml_path)
compiled_model = compile_model(Path(xml_path))
assert isinstance(compiled_model, CompiledModel)


def test_compact_api_wrong_path():
# as inner method takes py::object as an input and turns it into string
# it is necessary to assure that provided argument is either
Expand Down Expand Up @@ -77,24 +69,58 @@ def test_core_class(device):


# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
def test_compile_model(request, tmp_path, device):
@pytest.mark.parametrize("device_name", [
None,
"CPU",
])
def test_compile_model(request, tmp_path, device_name):
core = Core()
xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)
relu_model = get_relu_model()
serialize(relu_model, xml_path, bin_path)
model = core.read_model(model=xml_path, weights=bin_path)
compiled_model = core.compile_model(model, device)
compiled_model = None
if device_name is None:
compiled_model = core.compile_model(model)
else:
compiled_model = core.compile_model(model, device_name)

assert isinstance(compiled_model, CompiledModel)


# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
def test_compile_model_without_device(request, tmp_path):
core = Core()
xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)
relu_model = get_relu_model()
serialize(relu_model, xml_path, bin_path)
model = core.read_model(model=xml_path, weights=bin_path)
compiled_model = core.compile_model(model)
@pytest.fixture()
def get_model():
return get_relu_model()


@pytest.fixture()
def get_model_path(request, tmp_path):
xml_path, _ = create_filename_for_test(request.node.name, tmp_path, True)
serialize(get_relu_model(), xml_path)
return Path(xml_path)


@pytest.mark.parametrize("model_type", [
"get_model",
"get_model_path",
])
@pytest.mark.parametrize("device_name", [
None,
"CPU",
])
@pytest.mark.parametrize("config", [
None,
{hints.performance_mode(): hints.PerformanceMode.THROUGHPUT},
])
def test_compact_api(model_type, device_name, config, request):
compiled_model = None

model = request.getfixturevalue(model_type)
if device_name is not None:
compiled_model = compile_model(model=model, device_name=device_name, config=config)
else:
compiled_model = compile_model(model=model, config=config)

assert isinstance(compiled_model, CompiledModel)


Expand Down

0 comments on commit aa8f3ac

Please sign in to comment.