torch.compile, mixed precision training, and hardware-specific optimizations. This guide shows you how to enable and configure these features for faster inference and training.
torch.compile
torch.compile is PyTorch’s JIT compiler that optimizes model execution graphs. LeRobot policies include built-in support for compilation.
Enable Compilation
Setcompile_model=True in your policy configuration:
lerobot/policies/pi0/configuration_pi0.py for configuration options.
Compilation Modes
PyTorch offers different compilation modes:str
Balanced compilation with good speedup and compilation time
str
Minimize Python overhead, good for small models
str
Maximum optimization, slower compilation but best performance (recommended for deployment)
What Gets Compiled
Different policies compile different components: Pi0/Pi05 Policies:Compilation Workflow
First inference triggers compilation:Avoiding Graph Breaks
Some operations cause graph breaks and recompilation. Avoid:- Dynamic control flow based on tensor values
- In-place operations on inputs
- Python print statements in hot paths
Hardware Acceleration
CUDA (NVIDIA GPUs)
Use CUDA for NVIDIA GPUs:MPS (Apple Silicon)
Use MPS for Apple Silicon Macs:lerobot/processor/device_processor.py:108 for MPS dtype handling.
CPU Optimization
Optimize CPU inference:Mixed Precision
Use mixed precision (float16/bfloat16) for faster training and inference:Automatic Mixed Precision (AMP)
For training:Manual Precision Control
Control dtype viaDeviceProcessorStep:
lerobot/processor/device_processor.py:34 for dtype options.
Precision Recommendations
Training: Use
bfloat16 for better numerical stabilityInference: Use float16 for maximum speedCPU: Use float32 (CPU doesn’t support bfloat16)Optimization Checklist
For Inference
For Training
Benchmarking
Measure speedup from optimizations:Troubleshooting
Issue: Compilation Takes Too Long
Solution: Use faster compile mode or disable for development:Issue: Out of Memory with Mixed Precision
Solution: Reduce batch size or use gradient accumulation:Issue: Numerical Instability with float16
Solution: Use bfloat16 or gradient scaling:Issue: MPS “float64 not supported” Error
Solution: DeviceProcessorStep handles this automatically:lerobot/processor/device_processor.py:108.
Advanced: Custom Compilation
Compile specific functions:API Reference
torch.compile Settings
bool
default:"false"
Enable torch.compile optimization
str
default:"max-autotune"
Compilation mode: “default”, “reduce-overhead”, or “max-autotune”
DeviceProcessorStep
Seelerobot/processor/device_processor.py:34
str
Target device: “cpu”, “cuda”, “cuda:0”, “mps”
str | None
Target float dtype: “float16”, “bfloat16”, “float32”, “float64”
Hardware Backends
bool
Enable TF32 for matrix multiplication (Ampere+ GPUs)
bool
Enable cuDNN auto-tuner for fixed input sizes
bool
Enable TF32 for cuDNN operations