OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[PyOV] Test for export-import via file (#27320)
Anastasia Kuporosovacd15be95e3bb
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.cpp b/src/bindings/python/src/pyopenvino/utils/utils.cppindex e165c2e00b4..c747e2d3b81 100644--- a/src/bindings/python/src/pyopenvino/utils/utils.cpp+++ b/src/bindings/python/src/pyopenvino/utils/utils.cpp@@ -258,20 +258,31 @@ std::map<std::string, ov::Any> properties_to_any_map(const std::map<std::string,if (!py::isinstance<py::list>(property_value)) {OPENVINO_THROW("The value type of ov::cache_encryption_callbacks property is expected list");}+ auto property_list = property_value.cast<py::list>();+ // Wrapped to sp due-to we need to hold GIL upon destruction of python function+ auto py_encrypt = std::shared_ptr<py::function>(new py::function(std::move(property_list[0])),+ [](py::function* py_encrypt) {+ py::gil_scoped_acquire acquire;+ delete py_encrypt;+ });+ auto py_decrypt = std::shared_ptr<py::function>(new py::function(std::move(property_list[1])),+ [](py::function* py_decrypt) {+ py::gil_scoped_acquire acquire;+ delete py_decrypt;+ });+std::function<std::string(const std::string&)> encrypt_func =- [property_value](const std::string& in_str) -> std::string {+ [py_encrypt](const std::string& in_str) -> std::string {// Acquire GIL, execute Python functionpy::gil_scoped_acquire acquire;- auto _list = property_value.cast<py::list>();- return _list[0](in_str).cast<std::string>();+ return (*py_encrypt)(in_str).cast<std::string>();};std::function<std::string(const std::string&)> decrypt_func =- [property_value](const std::string& in_str) -> std::string {+ [py_decrypt](const std::string& in_str) -> std::string {// Acquire GIL, execute Python functionpy::gil_scoped_acquire acquire;- auto _list = property_value.cast<py::list>();- return _list[1](in_str).cast<std::string>();+ return (*py_decrypt)(in_str).cast<std::string>();};ov::EncryptionCallbacks encryption_callbacks{encrypt_func, decrypt_func};properties_to_cpp[property.first] = encryption_callbacks;diff --git a/src/bindings/python/tests/test_graph/test_custom_op.py b/src/bindings/python/tests/test_graph/test_custom_op.pyindex 5a2e33c7ee5..8643844e2c5 100644--- a/src/bindings/python/tests/test_graph/test_custom_op.py+++ b/src/bindings/python/tests/test_graph/test_custom_op.py@@ -12,7 +12,7 @@ from openvino import CompiledModel, Model, Dimension, Shape, Tensor, compile_modfrom openvino.runtime import DiscreteTypeInfoimport openvino.runtime.opset14 as ops-from tests.utils.helpers import create_filename_for_test+from tests.utils.helpers import create_filenames_for_irclass CustomOp(Op):@@ -108,7 +108,7 @@ class CustomOpWithAttribute(Op):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request@pytest.fixturedef prepared_paths(request, tmp_path):- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)yield xml_path, bin_pathdiff --git a/src/bindings/python/tests/test_graph/test_manager.py b/src/bindings/python/tests/test_graph/test_manager.pyindex b5fce8cc09b..ff72ef43158 100644--- a/src/bindings/python/tests/test_graph/test_manager.py+++ b/src/bindings/python/tests/test_graph/test_manager.py@@ -12,7 +12,7 @@ from openvino import Core, Modelfrom openvino.runtime.passes import Manager, Serialize, ConstantFolding, Versionfrom tests.test_graph.util import count_ops_of_type-from tests.utils.helpers import create_filename_for_test, compare_models+from tests.utils.helpers import create_filenames_for_ir, compare_modelsdef create_model():@@ -51,7 +51,7 @@ def test_constant_folding():# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request@pytest.fixturedef prepare_ir_paths(request, tmp_path):- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)yield xml_path, bin_path@@ -138,7 +138,7 @@ def test_serialize_pass_mixed_args_kwargs_v2(prepare_ir_paths):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_serialize_pass_wrong_num_of_args(request, tmp_path):- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)pass_manager = Manager()with pytest.raises(TypeError) as e:diff --git a/src/bindings/python/tests/test_runtime/test_compiled_model.py b/src/bindings/python/tests/test_runtime/test_compiled_model.pyindex d6909fa9471..9e17497fc22 100644--- a/src/bindings/python/tests/test_runtime/test_compiled_model.py+++ b/src/bindings/python/tests/test_runtime/test_compiled_model.py@@ -2,6 +2,7 @@# Copyright (C) 2018-2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0+import osimport pytestimport numpy as np@@ -14,6 +15,7 @@ from tests.utils.helpers import (generate_relu_compiled_model_with_config,encrypt_base64,decrypt_base64,+ create_filenames_for_ir,create_filename_for_test)from openvino import Model, Shape, Core, Tensor, serializefrom openvino.runtime import ConstOutput@@ -97,6 +99,43 @@ def test_export_import_advanced(device):assert np.argmax(res[new_compiled.outputs[0]]) == 531+# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request+def prepare_blob_path(request, tmp_path):+ filename = create_filename_for_test(request.node.name)+ path_to_blob = tmp_path / str(filename + ".blob")+ yield path_to_blob++ os.remove(path_to_blob)+++def test_export_import_via_file(prepare_blob_path, device):+ import io++ core = Core()++ if props.device.Capability.EXPORT_IMPORT not in core.get_property(device, props.device.capabilities):+ pytest.skip(f"{core.get_property(device, props.device.full_name)} plugin due-to export, import model API isn't implemented.")++ compiled_model = generate_relu_compiled_model(device)++ user_stream = io.BytesIO()++ compiled_model.export_model(user_stream)+ path_to_blob = prepare_blob_path++ with open(path_to_blob, "wb") as f_w:+ f_w.write(user_stream.getbuffer())++ with open(path_to_blob, "rb") as f_r:+ new_compiled = core.import_model(f_r.read(), device)++ img = generate_image()+ res = new_compiled.infer_new_request({"data": img})++ assert np.argmax(res[new_compiled.outputs[0]]) == 531++@pytest.mark.parametrize("input_arguments", [[0], ["data"], []])def test_get_input(device, input_arguments):compiled_model = generate_relu_compiled_model(device)@@ -250,7 +289,7 @@ def test_direct_infer(device, shared_flag):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_compiled_model_after_core_destroyed(request, tmp_path, device):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)model = get_relu_model()serialize(model, xml_path, bin_path)with open(bin_path, "rb") as f:@@ -267,7 +306,7 @@ def test_compiled_model_after_core_destroyed(request, tmp_path, device):def test_compiled_model_from_buffer_in_memory(request, tmp_path, device):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)model = get_relu_model()serialize(model, xml_path, bin_path)with open(bin_path, "rb") as f:diff --git a/src/bindings/python/tests/test_runtime/test_core.py b/src/bindings/python/tests/test_runtime/test_core.pyindex 8c15c2a32a0..d147ce2d6bc 100644--- a/src/bindings/python/tests/test_runtime/test_core.py+++ b/src/bindings/python/tests/test_runtime/test_core.py@@ -28,7 +28,7 @@ from tests.utils.helpers import (get_relu_model,plugins_path,compare_models,- create_filename_for_test,+ create_filenames_for_ir,get_model_with_template_extension,)@@ -77,7 +77,7 @@ def test_core_class(device):])def test_compile_model(request, tmp_path, device_name):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(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)@@ -97,7 +97,7 @@ def get_model():@pytest.fixturedef get_model_path(request, tmp_path):- xml_path, _ = create_filename_for_test(request.node.name, tmp_path, True)+ xml_path, _ = create_filenames_for_ir(request.node.name, tmp_path, True)serialize(get_relu_model(), xml_path)return Path(xml_path)@@ -130,7 +130,7 @@ def test_compact_api(model_type, device_name, config, request):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_read_model_from_ir(request, tmp_path):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(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)@@ -143,7 +143,7 @@ def test_read_model_from_ir(request, tmp_path):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_read_model_from_tensor(request, tmp_path):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path, is_xml_path=True, is_bin_path=True)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path, is_xml_path=True, is_bin_path=True)relu_model = get_relu_model()serialize(relu_model, xml_path, bin_path)arr = np.ones(shape=(10), dtype=np.int8)@@ -164,7 +164,7 @@ def test_read_model_with_wrong_input():# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_read_model_as_path(request, tmp_path):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path, True, True)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path, True, True)relu_model = get_relu_model()serialize(relu_model, xml_path, bin_path)@@ -181,7 +181,7 @@ def test_read_model_as_path(request, tmp_path):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_read_model_from_buffer(request, tmp_path):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)relu_model = get_relu_model()serialize(relu_model, xml_path, bin_path)with open(bin_path, "rb") as f:@@ -195,7 +195,7 @@ def test_read_model_from_buffer(request, tmp_path):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_model_from_buffer_valid(request, tmp_path):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)relu_model = get_relu_model()serialize(relu_model, xml_path, bin_path)with open(bin_path, "rb") as f:diff --git a/src/bindings/python/tests/test_runtime/test_model.py b/src/bindings/python/tests/test_runtime/test_model.pyindex 62b1eac9da3..0ae592b2d1d 100644--- a/src/bindings/python/tests/test_runtime/test_model.py+++ b/src/bindings/python/tests/test_runtime/test_model.py@@ -30,7 +30,7 @@ from openvino.runtime.op.util import VariableInfo, Variablefrom tests.utils.helpers import (generate_add_model,generate_model_with_memory,- create_filename_for_test,+ create_filenames_for_ir,)@@ -618,7 +618,7 @@ def test_serialize_rt_info(request, tmp_path):assert model.get_rt_info(["optimization", "test"])core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)input_shape = PartialShape([1])param = ops.parameter(input_shape, dtype=np.float32, name="data")relu1 = ops.relu(param, name="relu1")@@ -701,7 +701,7 @@ def test_serialize_complex_rt_info(request, tmp_path):assert rt_info_val in ["float_empty", "nodes", "type", "directed"]core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)input_shape = PartialShape([1])param = ops.parameter(input_shape, dtype=np.float32, name="data")relu1 = ops.relu(param, name="relu1")diff --git a/src/bindings/python/tests/test_transformations/test_compression.py b/src/bindings/python/tests/test_transformations/test_compression.pyindex fa46b6d227f..d2754fd29d9 100644--- a/src/bindings/python/tests/test_transformations/test_compression.py+++ b/src/bindings/python/tests/test_transformations/test_compression.py@@ -9,7 +9,7 @@ from openvino.runtime.op import Parameter, Constantfrom openvino.runtime.opset13 import add, multiplyimport openvino as ov-from tests.utils.helpers import create_filename_for_test+from tests.utils.helpers import create_filenames_for_irdef make_constant(values, transposed):@@ -38,7 +38,7 @@ def make_model(add_consts, mul_consts):def get_constants(model, request, tmp_path) -> List[Constant]:- model_fname, _ = create_filename_for_test(request.node.name, tmp_path)+ model_fname, _ = create_filenames_for_ir(request.node.name, tmp_path)ov.save_model(model, model_fname)core = ov.Core()restored_model = core.read_model(model_fname)diff --git a/src/bindings/python/tests/test_transformations/test_offline_api.py b/src/bindings/python/tests/test_transformations/test_offline_api.pyindex cd336493b58..e265cef4635 100644--- a/src/bindings/python/tests/test_transformations/test_offline_api.py+++ b/src/bindings/python/tests/test_transformations/test_offline_api.py@@ -18,7 +18,7 @@ from openvino._offline_transformations import (from openvino import Model, PartialShape, Core, serialize, save_modelimport openvino.runtime as ov-from tests.utils.helpers import create_filename_for_test, compare_models, _compare_models+from tests.utils.helpers import create_filenames_for_ir, compare_models, _compare_modelsdef get_relu_model():@@ -171,10 +171,10 @@ def test_fused_names_cleanup():def prepare_test_model_for_serialize(request, tmp_path, is_path_xml, is_path_bin):- xml_path, bin_path = create_filename_for_test(request.node.name,- tmp_path,- is_path_xml,- is_path_bin)+ xml_path, bin_path = create_filenames_for_ir(request.node.name,+ tmp_path,+ is_path_xml,+ is_path_bin)shape = [100, 100, 2]parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A")parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B")@@ -267,10 +267,10 @@ def test_compress_model_transformation():)def test_version_default(request, tmp_path, is_path_xml, is_path_bin):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name,- tmp_path,- is_path_xml,- is_path_bin)+ xml_path, bin_path = create_filenames_for_ir(request.node.name,+ tmp_path,+ is_path_xml,+ is_path_bin)shape = [100, 100, 2]parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A")parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B")@@ -297,10 +297,10 @@ def test_version_default(request, tmp_path, is_path_xml, is_path_bin):],)def test_serialize_default_bin(request, tmp_path, is_path_xml, is_path_bin):- xml_path, bin_path = create_filename_for_test(request.node.name,- tmp_path,- is_path_xml,- is_path_bin)+ xml_path, bin_path = create_filenames_for_ir(request.node.name,+ tmp_path,+ is_path_xml,+ is_path_bin)model = get_relu_model()serialize(model, xml_path)assert os.path.exists(bin_path)@@ -311,7 +311,7 @@ def test_serialize_default_bin(request, tmp_path, is_path_xml, is_path_bin):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_version_ir_v10(request, tmp_path):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)shape = [100, 100, 2]parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A")parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B")@@ -332,7 +332,7 @@ def test_version_ir_v10(request, tmp_path):# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_version_ir_v11(request, tmp_path):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name, tmp_path)+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)shape = [100, 100, 2]parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A")parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B")diff --git a/src/bindings/python/tests/test_transformations/test_public_transformations.py b/src/bindings/python/tests/test_transformations/test_public_transformations.pyindex 429bc6c192a..a10fea786b9 100644--- a/src/bindings/python/tests/test_transformations/test_public_transformations.py+++ b/src/bindings/python/tests/test_transformations/test_public_transformations.py@@ -17,7 +17,7 @@ from openvino.runtime.passes import ()from tests.test_transformations.utils.utils import count_ops, get_relu_model-from tests.utils.helpers import create_filename_for_test, compare_models+from tests.utils.helpers import create_filenames_for_ir, compare_modelsdef get_model():@@ -132,10 +132,10 @@ def test_low_latency2():)def test_serialize_pass(request, tmp_path, is_path_xml, is_path_bin):core = Core()- xml_path, bin_path = create_filename_for_test(request.node.name,- tmp_path,- is_path_xml,- is_path_bin)+ xml_path, bin_path = create_filenames_for_ir(request.node.name,+ tmp_path,+ is_path_xml,+ is_path_bin)model = get_relu_model()diff --git a/src/bindings/python/tests/utils/helpers.py b/src/bindings/python/tests/utils/helpers.pyindex 2ea00484e98..c14bd6e5e77 100644--- a/src/bindings/python/tests/utils/helpers.py+++ b/src/bindings/python/tests/utils/helpers.py@@ -303,7 +303,14 @@ def generate_abs_compiled_model_with_data(device, ov_type, numpy_dtype):return compiled_model, request, tensor1, array1-def create_filename_for_test(test_name, tmp_path, is_xml_path=False, is_bin_path=False):+def create_filename_for_test(test_name):+ python_version = str(sys.version_info.major) + "_" + str(sys.version_info.minor)+ filename = test_name.replace("test_", "").replace("[", "_").replace("]", "_")+ filename = filename + "_" + python_version+ return filename+++def create_filenames_for_ir(test_name, tmp_path, is_xml_path=False, is_bin_path=False):"""Return a tuple with automatically generated paths for xml and bin files.:param test_name: Name used in generating.@@ -311,9 +318,7 @@ def create_filename_for_test(test_name, tmp_path, is_xml_path=False, is_bin_path:param is_bin_path: True if bin file should be pathlib.Path object, otherwise return string.:return: Tuple with two objects representing xml and bin files."""- python_version = str(sys.version_info.major) + "_" + str(sys.version_info.minor)- filename = test_name.replace("test_", "").replace("[", "_").replace("]", "_")- filename = filename + "_" + python_version+ filename = create_filename_for_test(test_name)path_to_xml = tmp_path / Path(filename + ".xml")path_to_bin = tmp_path / Path(filename + ".bin")_xml = path_to_xml if is_xml_path else str(path_to_xml)