Rate this Page

torch.compiler#

Created On: Jul 28, 2023 | Last Updated On: Jul 28, 2025

torch.compiler is a namespace through which some of the internal compilermethods are surfaced for user consumption. The main function and the feature inthis namespace istorch.compile.

torch.compile is a PyTorch function introduced in PyTorch 2.x that aims tosolve the problem of accurate graph capturing in PyTorch and ultimately enablesoftware engineers to run their PyTorch programs faster.torch.compile iswritten in Python and it marks the transition of PyTorch from C++ to Python.

torch.compile leverages the following underlying technologies:

  • TorchDynamo (torch._dynamo) is an internal API that uses a CPythonfeature called the Frame Evaluation API to safely capture PyTorch graphs.Methods that are available externally for PyTorch users are surfacedthrough thetorch.compiler namespace.

  • TorchInductor is the defaulttorch.compile deep learning compilerthat generates fast code for multiple accelerators and backends. Youneed to use a backend compiler to make speedups throughtorch.compilepossible. For NVIDIA, AMD and Intel GPUs, it leverages OpenAI Triton as the keybuilding block.

  • AOT Autograd captures not only the user-level code, but also backpropagation,which results in capturing the backwards pass “ahead-of-time”. This enablesacceleration of both forwards and backwards pass using TorchInductor.

To better understand howtorch.compile tracing behavior on your code, or tolearn more about the internals oftorch.compile, please refer to thetorch.compile programming model.

Note

In some cases, the termstorch.compile, TorchDynamo,torch.compilermight be used interchangeably in this documentation.

As mentioned above, to run your workflows faster,torch.compile throughTorchDynamo requires a backend that converts the captured graphs into a fastmachine code. Different backends can result in various optimization gains.The default backend is called TorchInductor, also known asinductor,TorchDynamo has a list of supported backends developed by our partners,which can be seen by runningtorch.compiler.list_backends() each of whichwith its optional dependencies.

Some of the most commonly used backends include:

Training & inference backends

Backend

Description

torch.compile(m,backend="inductor")

Uses the TorchInductor backend.Read more

torch.compile(m,backend="cudagraphs")

CUDA graphs with AOT Autograd.Read more

torch.compile(m,backend="ipex")

Uses IPEX on CPU.Read more

Inference-only backends

Backend

Description

torch.compile(m,backend="tensorrt")

Uses Torch-TensorRT for inference optimizations. Requiresimporttorch_tensorrt in the calling script to register backend.Read more

torch.compile(m,backend="ipex")

Uses IPEX for inference on CPU.Read more

torch.compile(m,backend="tvm")

Uses Apache TVM for inference optimizations.Read more

torch.compile(m,backend="openvino")

Uses OpenVINO for inference optimizations.Read more

Read More#

`torch.compile` Programming Model