Skip to content

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

[OVC][PT FE] Cover leftovers for torch.export.ExportedProgram support (#27042)

Roman Kazantsev2d00d75ab57e
diff --git a/tools/ovc/openvino/tools/ovc/convert.py b/tools/ovc/openvino/tools/ovc/convert.py
index 782fa25ab2d..77693ad4be2 100644
--- a/tools/ovc/openvino/tools/ovc/convert.py
+++ b/tools/ovc/openvino/tools/ovc/convert.py
@@ -27,7 +27,7 @@ def convert_model(
Framework-agnostic parameters:
:param input_model:
- Model object in original framework (PyTorch, Tensorflow) or path to model file.
+ Model object in original framework (PyTorch, TensorFlow) or path to model file.
Supported formats of input model:
@@ -35,6 +35,7 @@ def convert_model(
torch.nn.Module
torch.jit.ScriptModule
torch.jit.ScriptFunction
+ torch.export.ExportedProgram
TF
tf.compat.v1.Graph
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 0119a541494..d3b77c9a61f 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
@@ -89,37 +89,38 @@ def get_pytorch_decoder_for_model_on_disk(argv, args):
else:
input_model = argv.input_model
- if isinstance(input_model, (str, pathlib.Path)):
- # attempt to load scripted model
- try:
- inputs = prepare_torch_inputs(example_inputs)
- model = torch.jit.load(input_model)
- model.eval()
- decoder = TorchScriptPythonDecoder(
- model,
- example_input=inputs,
- shared_memory=args.get("share_weights", True),
- module_extensions=extract_module_extensions(args))
+ if not isinstance(input_model, (str, pathlib.Path)):
+ return False
+
+ # attempt to load scripted model
+ try:
+ inputs = prepare_torch_inputs(example_inputs)
+ model = torch.jit.load(input_model)
+ model.eval()
+ decoder = TorchScriptPythonDecoder(
+ model,
+ example_input=inputs,
+ shared_memory=args.get("share_weights", True),
+ module_extensions=extract_module_extensions(args))
+ argv.input_model = decoder
+ argv.framework = 'pytorch'
+ return True
+ except:
+ pass
+ # attempt to load exported model
+ 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.framework = 'pytorch'
return True
- except:
- pass
- if isinstance(input_model, (str, pathlib.Path)):
- # attempt to load exported model
- 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.framework = 'pytorch'
- return True
- except:
- pass
+ except:
+ pass
return False