perf(pytorch): add opt-in torch.compile for decode CUDA graphs - #4808
perf(pytorch): add opt-in torch.compile for decode CUDA graphs#4808grimoire wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an experimental, opt-in torch.compile path that is only used during decode CUDA graph capture on CUDA backends, aiming to fuse compiler-visible PyTorch regions while keeping existing custom-kernel behavior within the outer CUDA graph capture.
Changes:
- Introduces lazy construction of a compiled decode-only model callable and routes decode graph capture through it.
- Adds a new environment toggle
LMDEPLOY_ENABLE_DECODE_TORCH_COMPILE(default off). - Adds unit tests covering decode-only behavior, lazy compilation, non-CUDA behavior, and one-time Dynamo configuration.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/pytorch/engine/test_decode_torch_compile.py | Adds unit tests for decode-only torch.compile integration and Dynamo configuration behavior. |
| lmdeploy/pytorch/envs.py | Adds env var parsing for LMDEPLOY_ENABLE_DECODE_TORCH_COMPILE. |
| lmdeploy/pytorch/backends/cuda/graph_runner.py | Implements decode-only compile configuration + lazy compiled callable usage during decode CUDA graph capture. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| fullgraph=False, | ||
| dynamic=False, | ||
| options={ | ||
| 'emulate_divison_rounding': True, |
There was a problem hiding this comment.
Make the compile options compatible with supported PyTorch versions —
emulate_divison_rounding exists in PyTorch 2.10, but is rejected by both PyTorch 2.8 and 2.11; newer PyTorch uses eager_numerics.division_rounding. Since current main supports Torch 2.0–2.12.1, enabling LMDEPLOY_ENABLE_DECODE_TORCH_COMPILE=1 causes the first decode capture to fail immediately on common supported installations.
Motivation
LMDeploy currently captures CUDA graphs directly on the raw model. Tuned custom kernels are captured correctly, but compiler-visible PyTorch operations cannot be fused by Inductor.
This PR adds an experimental decode-only path that runs
torch.compilebefore the existing CUDA graph capture. The goal is to fuse small PyTorch regions while preserving tuned attention, RMSNorm, fused-MoE, Triton, and other custom kernel launches inside the outer CUDA graph.Changes
fullgraph=Falseanddynamic=False; graph breaks are allowed.Enable it with:
No model-specific fusion patterns or model code changes are introduced.
Limitations
torch._dynamo.trace_rulesAPIs and has primarily been validated with PyTorch 2.10.TORCHINDUCTOR_CACHE_DIRis recommended.