torch.compiler — PyTorch 2.7 documentation (original) (raw)

torch.compiler is a namespace through which some of the internal compiler methods are surfaced for user consumption. The main function and the feature in this namespace is torch.compile.

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

torch.compile leverages the following underlying technologies:

Note

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

As mentioned above, to run your workflows faster, torch.compile through TorchDynamo requires a backend that converts the captured graphs into a fast machine code. Different backends can result in various optimization gains. The default backend is called TorchInductor, also known as inductor, TorchDynamo has a list of supported backends developed by our partners, which can be see by running torch.compiler.list_backends() each of which with 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
torch.compile(m, backend="onnxrt") Uses ONNX Runtime for training on CPU/GPU. Read more

Inference-only backends

Backend Description
torch.compile(m, backend="tensorrt") Uses Torch-TensorRT for inference optimizations. Requires import torch_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