Torch Inductor JIT Backend Guide

View as Markdown

The Torch Inductor JIT backend uses PyTorch’s built-in compiler (torch.compile with backend="inductor") for model tuning. It provides automatic kernel fusion and optimization without external dependencies.

Overview

  • Pure PyTorch: No external dependencies
  • Automatic Optimization: Kernel fusion and code generation
  • Multiple Modes: Default, reduce-overhead, max-autotune
  • Dynamic Shapes: Configurable dynamic shape support
  • Cross-Platform: Works on CPU and CUDA

Quick Start

1from aitune.torch.backend import TorchInductorJitBackend, TorchInductorJitBackendConfig
2import aitune.torch as ait
3import torch
4
5# Configure backend
6config = TorchInductorJitBackendConfig(mode="max-autotune")
7backend = TorchInductorJitBackend(config)
8
9# Use in tuning
10from aitune.torch.tune_strategy import OneBackendStrategy
11strategy = ait.OneBackendStrategy(backend=backend)
12
13model = ait.Module(model, "my-model", strategy=strategy)
14ait.tune(model, input_data)

Configuration Options

TorchInductorJitBackendConfig

1@dataclass
2class TorchInductorJitBackendConfig(BackendConfig):
3 fullgraph: bool = False
4 dynamic: bool | None = None
5 mode: str | None = None
6 options: dict | None = None
7 autocast_enabled: bool = False
8 autocast_dtype: torch.dtype | None = None

mode

Predefined optimization modes:

1# Default mode (balanced)
2config = TorchInductorJitBackendConfig(mode="default")
3
4# Reduce Python overhead with CUDA graphs
5config = TorchInductorJitBackendConfig(mode="reduce-overhead")
6
7# Maximum auto-tuning
8config = TorchInductorJitBackendConfig(mode="max-autotune")
9
10# Max autotune without CUDA graphs
11config = TorchInductorJitBackendConfig(mode="max-autotune-no-cudagraphs")

Mode Details:

  • default: Good balance, general purpose
  • reduce-overhead: Uses CUDA graphs for small batches, reduces Python overhead
  • max-autotune: Leverages Triton for matmul/conv, enables CUDA graphs
  • max-autotune-no-cudagraphs: Like max-autotune but without CUDA graphs

fullgraph

Require complete graph capture:

1config = TorchInductorJitBackendConfig(
2 fullgraph=True, # Error if graph breaks occur
3 mode="max-autotune",
4)

dynamic

Control dynamic shape behavior:

1# Always generate dynamic kernels
2config = TorchInductorJitBackendConfig(dynamic=True)
3
4# Never generate dynamic kernels (always specialize)
5config = TorchInductorJitBackendConfig(dynamic=False)
6
7# Auto-detect (default)
8config = TorchInductorJitBackendConfig(dynamic=None)

options

Custom inductor options:

1# See all options: torch._inductor.list_options()
2config = TorchInductorJitBackendConfig(
3 options={
4 "triton.cudagraphs": True,
5 "max_autotune": True,
6 "coordinate_descent_tuning": True,
7 }
8)

Note: Cannot use both mode and options.

autocast

Enable automatic mixed precision:

1config = TorchInductorJitBackendConfig(
2 mode="max-autotune",
3 autocast_enabled=True,
4 autocast_dtype=torch.float16,
5)

Debugging

Enable Logging

1# Set environment variables before running
2import os
3os.environ['TORCH_LOGS'] = 'dynamic,perf_hints,graph_breaks'
4
5# Then run tuning
6ait.tune(wrapped_model, input_data)

Check Optimizations

1# See what mode does
2import torch
3print(torch._inductor.list_mode_options())
4
5# See all available options
6print(torch._inductor.list_options())

Best Practices

  1. Start with max-autotune: Best performance for most models
  2. Use reduce-overhead: For latency-critical applications
  3. Enable Autocast: Free performance boost with FP16
  4. Dynamic Shapes: Only when necessary (adds overhead)
  5. Warmup: Run a few iterations before benchmarking

Troubleshooting

Issue: Graph breaks

Check where breaks occur:

$TORCH_LOGS=graph_breaks python your_script.py

Solution: Use fullgraph=False (default) to allow partial compilation.

Issue: Slow compilation

Solution: Reduce auto-tuning:

1config = TorchInductorJitBackendConfig(mode="default")

Issue: Not using CUDA graphs

Check logs:

$TORCH_LOGS=perf_hints python your_script.py

Common causes: Input mutations, unsupported operations

Issue: Variable shape recompilations

Solution: Enable dynamic shapes:

1config = TorchInductorJitBackendConfig(
2 mode="default",
3 dynamic=True,
4)

Comparison with Other Backends

FeatureInductorTensorRTTorchAO
DependenciesNoneTensorRTtorchao
SetupEasyModerateEasy
PerformanceGoodExcellentGood
QuantizationLimitedAdvancedExtensive
PortabilityExcellentNVIDIA onlyGood

Next Steps