From a64aa720e951588095dc125eeddc4a5f382b85e1 Mon Sep 17 00:00:00 2001 From: Maxim Vafin Date: Wed, 8 Jan 2025 16:33:50 +0100 Subject: [PATCH 1/4] Disable graph check while tracing --- optimum/exporters/openvino/convert.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index 636bc00f05..08116d5674 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -23,6 +23,7 @@ from transformers.generation import GenerationMixin from transformers.utils import is_tf_available, is_torch_available +from openvino.frontend.pytorch.ts_decoder import TorchScriptPythonDecoder from openvino.runtime import Model, save_model from openvino.runtime.exceptions import OVTypeError from openvino.tools.ovc import convert_model @@ -46,6 +47,7 @@ is_openvino_tokenizers_version, is_tokenizers_version, is_transformers_version, + is_openvino_version, ) from optimum.utils import DEFAULT_DUMMY_SHAPES, is_diffusers_available @@ -427,6 +429,10 @@ def ts_patched_forward(*args, **kwargs): patcher.patched_forward = ts_patched_forward + decoder_kwargs = {} + if library_name == "diffusers" and is_openvino_version(">=", "2025.0"): + decoder_kwargs["trace_kwargs"] = {"check_trace": False} + with patcher: if patch_16bit_model: from openvino.frontend.pytorch.patch_model import __make_16bit_traceable @@ -434,8 +440,9 @@ def ts_patched_forward(*args, **kwargs): __make_16bit_traceable(model) check_dummy_inputs_are_allowed(model, dummy_inputs) input_info = _get_input_info(model, config, dummy_inputs) + decoder = TorchScriptPythonDecoder(model, example_input=dummy_inputs, **decoder_kwargs) ov_model = convert_model( - model, + decoder, example_input=dummy_inputs, input=[(item.shape, item.type) for item in input_info], ) From 57796daa513a6bc33cfa1d6b9e4263f79785d79f Mon Sep 17 00:00:00 2001 From: Maxim Vafin Date: Thu, 9 Jan 2025 11:16:19 +0100 Subject: [PATCH 2/4] Fix style --- optimum/exporters/openvino/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index 08116d5674..0e1a4e1ebe 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -45,9 +45,9 @@ _transformers_version, compare_versions, is_openvino_tokenizers_version, + is_openvino_version, is_tokenizers_version, is_transformers_version, - is_openvino_version, ) from optimum.utils import DEFAULT_DUMMY_SHAPES, is_diffusers_available From 5b17ca37406adec83503f3c45ddc8864c4e284b9 Mon Sep 17 00:00:00 2001 From: Ilyas Moutawwakil <57442720+IlyasMoutawwakil@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:12:48 +0100 Subject: [PATCH 3/4] Apply suggestions from code review --- optimum/exporters/openvino/convert.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index 0e1a4e1ebe..d5803737a7 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -429,9 +429,9 @@ def ts_patched_forward(*args, **kwargs): patcher.patched_forward = ts_patched_forward - decoder_kwargs = {} + ts_decoder_kwargs = {} if library_name == "diffusers" and is_openvino_version(">=", "2025.0"): - decoder_kwargs["trace_kwargs"] = {"check_trace": False} + ts_decoder_kwargs["trace_kwargs"] = {"check_trace": False} with patcher: if patch_16bit_model: @@ -440,9 +440,9 @@ def ts_patched_forward(*args, **kwargs): __make_16bit_traceable(model) check_dummy_inputs_are_allowed(model, dummy_inputs) input_info = _get_input_info(model, config, dummy_inputs) - decoder = TorchScriptPythonDecoder(model, example_input=dummy_inputs, **decoder_kwargs) + ts_decoder = TorchScriptPythonDecoder(model, example_input=dummy_inputs, **ts_decoder_kwargs) ov_model = convert_model( - decoder, + ts_decoder, example_input=dummy_inputs, input=[(item.shape, item.type) for item in input_info], ) From 511855adefec73e2fccc453a43b073d1ed2fd487 Mon Sep 17 00:00:00 2001 From: Maxim Vafin Date: Mon, 13 Jan 2025 11:07:44 +0100 Subject: [PATCH 4/4] Move decoder input closer to the usage. For cases when torch is not available in openvino --- optimum/exporters/openvino/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index d5803737a7..94fe7e8692 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -23,7 +23,6 @@ from transformers.generation import GenerationMixin from transformers.utils import is_tf_available, is_torch_available -from openvino.frontend.pytorch.ts_decoder import TorchScriptPythonDecoder from openvino.runtime import Model, save_model from openvino.runtime.exceptions import OVTypeError from openvino.tools.ovc import convert_model @@ -367,6 +366,7 @@ def export_pytorch( import torch from torch.utils._pytree import tree_map + from openvino.frontend.pytorch.ts_decoder import TorchScriptPythonDecoder from optimum.exporters.utils import check_dummy_inputs_are_allowed logger.info(f"Using framework PyTorch: {torch.__version__}")