From 42b329aba9f24b833cdb416a80bc31202ad7c698 Mon Sep 17 00:00:00 2001 From: Karol Blaszczak Date: Tue, 24 Dec 2024 08:47:05 +0100 Subject: [PATCH] [PT FE][DOCS] Document conversion of PyTorch models from disk (#28175) (#28187) port: https://github.com/openvinotoolkit/openvino/pull/28175 by R. Kazantsev Signed-off-by: Kazantsev, Roman Co-authored-by: Roman Kazantsev --- .../convert-model-pytorch.rst | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/docs/articles_en/openvino-workflow/model-preparation/convert-model-pytorch.rst b/docs/articles_en/openvino-workflow/model-preparation/convert-model-pytorch.rst index 6ac806daf0cda0..62cfdf05f2b11f 100644 --- a/docs/articles_en/openvino-workflow/model-preparation/convert-model-pytorch.rst +++ b/docs/articles_en/openvino-workflow/model-preparation/convert-model-pytorch.rst @@ -203,6 +203,52 @@ Here is an example of how to convert a model obtained with ``torch.export``: This is an experimental feature. Use it only if you know that you need to. PyTorch version 2.2 is recommended. Dynamic shapes are not supported yet. +Converting a PyTorch Model from Disk +#################################### + +PyTorch provides the capability to save models in two distinct formats: ``torch.jit.ScriptModule`` and ``torch.export.ExportedProgram``. +Both formats can be saved to disk as standalone files, enabling them to be reloaded independently of the original Python code. + +ExportedProgram Format +++++++++++++++++++++++ + +The ``ExportedProgram`` format is saved on disk using `torch.export.save() `__. +Below is an example of how to convert an ``ExportedProgram`` from disk: + +.. tab-set:: + + .. tab-item:: Python + :sync: py + + .. code-block:: py + :force: + + import openvino as ov + ov_model = ov.convert_model('exported_program.pt2') + + .. tab-item:: CLI + :sync: cli + + .. code-block:: sh + + ovc exported_program.pt2 + +ScriptModule Format ++++++++++++++++++++ + +`torch.jit.save() `__ serializes ``ScriptModule`` object on disk. +To convert the serialized ``ScriptModule`` format, run ``convert_model`` function with ``example_input`` parameter as follows: + +.. code-block:: py + :force: + + from openvino import convert_model + import torch + + convert_model(input_model='script_module.pt', example_input=torch.rand(1, 10)) + +``example_input`` is the required parameter for the conversion because ``torch.jit.ScriptModule`` object is always saved in an untraced state on disk. + Exporting a PyTorch Model to ONNX Format ########################################