liveBook · Manning

Torch.compile()

As explained in Deep Learning with PyTorch, Second Edition

torch.compile() is a PyTorch API that compiles functions or modules into optimized executable code for execution.

PyTorch normally runs tensor operations as Python reaches them: this is eager execution. For a model that needs faster execution, `torch.compile()` offers a small code change: wrap the model, then call the compiled model as usual. Introduced in PyTorch 2.0, it uses just-in-time compilation to optimize execution. section 17.1.5 section 1.5

Compile a model for inference

Load a model, move it to the chosen device, then wrap it: model = AutoModelForCausalLM.from_pretrained(checkpoint) model = model.to(device) model = torch.compile(model) The same model variable now holds the compiled model. Speedups depend on the model and hardware; compilation can occasionally slow execution down. section 17.1.5

Two optimizations help explain why compilation may be faster:

  • Kernel fusion combines operations so they need fewer separate kernel calls. section 17.1.5
  • Operator reordering can improve memory access and reduce intermediate memory use. section 17.1.5

Compilation also differs from `torch.export`. Export expects a graph it can capture; a branch chosen from an input value can make export fail. `torch.compile()` can instead break execution into smaller graphs around operations it cannot compile. Those graph breaks allow dynamic behavior, though they may cost performance. section 17.3 section 17.3.1

Listing 17.10 A model with dynamic conditional control flow
import torch
import torch.nn as nn
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class ConditionalModel(nn.Module):
    def __init__(self):
        super(ConditionalModel, self).__init__()
        self.fc1 = nn.Linear(10, 5)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(10, 3)
    def forward(self, x):
        if x.sum() > 0:
            x = self.fc1(x)
        else:
            x = self.fc2(x)
        x = self.relu(x)
        return x
model = ConditionalModel()
model = model.to(device)
model.eval()

The model chooses between two layers based on `x.sum()`. This input-dependent branch makes the difference between export and compilation concrete. section 17.3.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains JIT compilation, kernel fusion, operator reordering, and graph breaks at a practical level without detailing compiler internals.
Examples
Shows a compiled causal-language model and an input-dependent conditional model used to contrast compilation with export.
What it assumes
Assumes an existing PyTorch model while explaining eager execution and the basic motivation for just-in-time compilation.
What you can do after
Apply it via PyTorch to wrap a model for inference and recognize when dynamic control flow favors compilation over export.
How it connects
Connects eager execution and JIT compilation to graph breaks, dynamic control flow, and the contrasting behavior of `torch.export`.

Also explained in

Compare how 3 books explain torch.compile() →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.