Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PT FE] Unify conversion pipeline for ExportedProgram from memory and disk (#27324)

Roman Kazantsev22c6740f79e8
diff --git a/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py b/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py
index d3b77c9a61f..486f72d87fd 100644
--- a/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py
+++ b/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py
@@ -21,6 +21,22 @@ def extract_module_extensions(args):
return {extension.module: extension for extension in extensions if isinstance(extension, ModuleExtension)}
+def get_decoder_for_exported_program(model):
+ from openvino.frontend.pytorch.fx_decoder import TorchFXPythonDecoder
+ import torch
+
+ from packaging import version
+ if version.parse(torch.__version__) >= version.parse("2.2"):
+ from torch._decomp import get_decompositions
+ from openvino.frontend.pytorch.torchdynamo.decompositions import get_export_decomposition_list
+ decomp = get_decompositions(get_export_decomposition_list())
+ model = model.run_decompositions(decomp_table=decomp)
+ gm = model.module()
+ log.debug(gm.code)
+ decoder = TorchFXPythonDecoder(gm, dynamic_shapes=True)
+ return decoder
+
+
def get_pytorch_decoder(model, example_inputs, args):
try:
from openvino.frontend.pytorch.ts_decoder import TorchScriptPythonDecoder
@@ -49,15 +65,7 @@ def get_pytorch_decoder(model, example_inputs, args):
inputs = prepare_torch_inputs(example_inputs)
if not isinstance(model, (TorchScriptPythonDecoder, TorchFXPythonDecoder)):
if hasattr(torch, "export") and isinstance(model, (torch.export.ExportedProgram)):
- from packaging import version
- if version.parse(torch.__version__) >= version.parse("2.2"):
- from torch._decomp import get_decompositions
- from openvino.frontend.pytorch.torchdynamo.decompositions import get_export_decomposition_list
- decomp = get_decompositions(get_export_decomposition_list())
- model = model.run_decompositions(decomp_table=decomp)
- gm = model.module()
- log.debug(gm.code)
- decoder = TorchFXPythonDecoder(gm, dynamic_shapes=True)
+ decoder = get_decoder_for_exported_program(model)
else:
decoder = TorchScriptPythonDecoder(
model,
@@ -111,12 +119,7 @@ def get_pytorch_decoder_for_model_on_disk(argv, args):
try:
exported_program = torch.export.load(input_model)
if hasattr(torch, "export") and isinstance(exported_program, (torch.export.ExportedProgram)):
- from packaging import version
- if version.parse(torch.__version__) >= version.parse("2.2"):
- exported_program = exported_program.run_decompositions()
- gm = exported_program.module()
- decoder = TorchFXPythonDecoder(gm, dynamic_shapes=True)
- argv.input_model = decoder
+ argv.input_model = get_decoder_for_exported_program(exported_program)
argv.framework = 'pytorch'
return True
except: