.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "recipes/torch_export_aoti_python.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_recipes_torch_export_aoti_python.py: .. meta:: :description: An end-to-end example of how to use AOTInductor for Python runtime. :keywords: torch.export, AOTInductor, torch._inductor.aoti_compile_and_package, aot_compile, torch._export.aoti_load_package ``torch.export`` AOTInductor Tutorial for Python runtime (Beta) =============================================================== **Author:** Ankith Gunapal, Bin Bao, Angela Yi .. GENERATED FROM PYTHON SOURCE LINES 14-33 .. warning:: ``torch._inductor.aoti_compile_and_package`` and ``torch._inductor.aoti_load_package`` are in Beta status and are subject to backwards compatibility breaking changes. This tutorial provides an example of how to use these APIs for model deployment using Python runtime. It has been shown `previously `__ how AOTInductor can be used to do Ahead-of-Time compilation of PyTorch exported models by creating an artifact that can be run in a non-Python environment. In this tutorial, you will learn an end-to-end example of how to use AOTInductor for Python runtime. **Contents** .. contents:: :local: .. GENERATED FROM PYTHON SOURCE LINES 36-41 Prerequisites ------------- * PyTorch 2.6 or later * Basic understanding of ``torch.export`` and AOTInductor * Complete the `AOTInductor: Ahead-Of-Time Compilation for Torch.Export-ed Models `_ tutorial .. GENERATED FROM PYTHON SOURCE LINES 43-49 What you will learn ---------------------- * How to use AOTInductor for Python runtime. * How to use :func:`torch._inductor.aoti_compile_and_package` along with :func:`torch.export.export` to generate a compiled artifact * How to load and run the artifact in a Python runtime using :func:`torch._export.aot_load`. * When to you use AOTInductor with a Python runtime .. GENERATED FROM PYTHON SOURCE LINES 51-72 Model Compilation ----------------- We will use the TorchVision pretrained ``ResNet18`` model as an example. The first step is to export the model to a graph representation using :func:`torch.export.export`. To learn more about using this function, you can check out the `docs `_ or the `tutorial `_. Once we have exported the PyTorch model and obtained an ``ExportedProgram``, we can apply :func:`torch._inductor.aoti_compile_and_package` to AOTInductor to compile the program to a specified device, and save the generated contents into a ".pt2" artifact. .. note:: This API supports the same available options that :func:`torch.compile` has, such as ``mode`` and ``max_autotune`` (for those who want to enable CUDA graphs and leverage Triton based matrix multiplications and convolutions) .. GENERATED FROM PYTHON SOURCE LINES 72-103 .. code-block:: Python import os import torch import torch._inductor from torchvision.models import ResNet18_Weights, resnet18 model = resnet18(weights=ResNet18_Weights.DEFAULT) model.eval() with torch.inference_mode(): inductor_configs = {} if torch.cuda.is_available(): device = "cuda" inductor_configs["max_autotune"] = True else: device = "cpu" model = model.to(device=device) example_inputs = (torch.randn(2, 3, 224, 224, device=device),) exported_program = torch.export.export( model, example_inputs, ) path = torch._inductor.aoti_compile_and_package( exported_program, package_path=os.path.join(os.getcwd(), "resnet18.pt2"), inductor_configs=inductor_configs ) .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /var/lib/ci-user/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth 0%| | 0.00/44.7M [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: torch_export_aoti_python.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: torch_export_aoti_python.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_