- name
- perf-torch-cuda-graphs
- description
- Apply CUDA Graphs to PyTorch workloads — API selection (torch.compile, PyTorch make_graphed_callables, TE make_graphed_callables, MCore CudaGraphManager, FullCudaGraphWrapper, manual torch.cuda.graph), code compatibility, capture workflows, dynamic pattern handling, and troubleshooting. Triggers: CUDA graph, torch.cuda.graph, make_graphed_callables, reduce-overhead, graph capture, graph replay, kernel launch overhead, CudaGraphManager, FullCudaGraphWrapper, full-iteration graph, stream capture.
- tags
- ["cuda-graph","optimization","pytorch"]
- license
- Apache-2.0
- metadata
- {"author":"NVIDIA Corporation"}
# CUDA Graphs for PyTorch
CUDA Graphs capture a sequence of GPU operations once and replay them with
minimal CPU overhead. This skill guides applying CUDA Graphs to PyTorch
training and inference workloads using native PyTorch APIs, Transformer
Engine, and Megatron-LM.
## When to Use
Reach for this skill when you encounter:
- **Triggers**: User wants to optimize with CUDA Graphs, reduce kernel launch
overhead, or speed up training/inference loops
- **Symptoms**: Low GPU utilization (<80%), many small kernel launches (<50 us
each), CPU-bound training, high kernel launch latency visible in Nsight
Systems profiles
- **Keywords**: "CUDA graph", "torch.cuda.graph", "make_graphed_callables",
"reduce-overhead", "graph capture", "graph replay", "kernel launch overhead",
"CudaGraphManager", "FullCudaGraphWrapper", "full-iteration graph", "stream
capture"
Do NOT use this skill for:
- General PyTorch performance tuning unrelated to kernel launch overhead
- CUDA kernel development or custom CUDA C++ code
- Host-device sync elimination only (use **perf-torch-sync-free** skill instead)
- Nsight Systems profiling (use **perf-nsight-systems** skill)
- TensorFlow/JAX graph compilation (different APIs entirely)
## Requirements
| Dependency | Version | Notes |
|------------|---------|-------|
| PyTorch | >= 1.10 | `torch.cuda.graph()` available |
| CUDA | >= 11.0 | Graph update APIs |
| GPU | NVIDIA (any) | Required for CUDA |
| Nsight Systems | any | Optional, for profiling |
| APEX | any | Optional, for capturable optimizers |
| Transformer Engine | >= 2.2 | Optional, for FP8-aware graphing |
| Megatron-LM | core >= 0.14.0 | Optional, for CudaGraphManager / FullCudaGraphWrapper |
## API Selection Guide
Choose the API based on your framework and performance needs.
| Situation | API | Workflow |
|-----------|-----|---------|
| Quick experiment, unknown graph boundaries | `torch.compile(mode="reduce-overhead")` | Workflow 2 |
| Training, need autograd, no FP8/PP | `torch.cuda.make_graphed_callables()` | Workflow 3 |
| Any PyTorch model, FP8 or PP support | TE `make_graphed_callables` | Workflow 4 |
| Megatron-LM, per-layer, automatic | MCore `CudaGraphManager` | Workflow 5 |
| Maximum perf, full-iteration capture | MCore `FullCudaGraphWrapper` | Workflow 6 |
| Full manual control, custom pipelines | `torch.cuda.graph()` | Workflow 7 |
**Decision flowchart:**
1. Using Megatron-LM with FP8/PP?
- Yes, want maximum perf with static workload --> Workflow 6 (FullCudaGraphWrapper)
- Yes, want per-layer automatic graphing --> Workflow 5 (CudaGraphManager)
- Yes, want manual control over what gets graphed --> Workflow 4 (TE make_graphed_callables)
2. Using Transformer Engine without Megatron?
- Yes, need FP8 or PP --> Workflow 4 (TE make_graphed_callables)
3. General PyTorch?
- Want zero effort, okay with fragmented graphs --> Workflow 2 (torch.compile)
- Want autograd support, training loop --> Workflow 3 (PyTorch make_graphed_callables)
- Want full manual control --> Workflow 7 (torch.cuda.graph)
**Strategy:** Start with the highest-level API available for your framework.
Move to lower-level APIs only if you need more control, hit limitations, or
do not achieve the expected performance improvement.
## Workflows
### Workflow 1: Profile and Decide Whether Graphs Help
Goal: Determine if CUDA Graphs will benefit your workload before investing
effort.
1. Profile with Nsight Systems:
```bash
nsys profile --cuda-graph-trace=graph python train.py
```
2. Check GPU utilization -- if already >95%, graphs won't help much.
3. Look for gaps between kernel launches (CPU overhead) and many small kernels
(<50 us each). These are the targets for graphing.
4. Annotate regions of interest to correlate idle GPU time with code:
```python
with torch.cuda.nvtx.range("forward"):
output = model(input)
```
5. Estimate benefit: count kernels per iteration. Workloads with hundreds of
small kernels and <80% GPU utilization are strong candidates.
Expected result: Identified bottleneck regions with low GPU occupancy between
kernels. Proceed to the appropriate workflow from the API Selection Guide.
### Workflow 2: torch.compile(mode="reduce-overhead")
Goal: Automatic CUDA Graph capture with zero manual effort.
When to use: Quick experiment, unknown graph boundaries, already using
`torch.compile`.
Steps:
1. Decorate the training step with `@torch.compile(mode="reduce-overhead")`:
```python
@torch.compile(mode="reduce-overhead")
def train_step(model, x, target, criterion):
output = model(x)
loss = criterion(output, target)
loss.backward()
return loss
```
2. Run the training loop normally -- graphs are captured automatically.
3. Profile with Nsight Systems to see captured graphs:
```bash
nsys profile --cuda-graph-trace=graph python train.py
```
4. If you see too many small graphs (graph fragmentation), check for graph
breaks: `.item()`, `print()`, data-dependent control flow. Fix these or
escalate to Workflow 3+.
Trade-offs:
- Zero effort, but may create fragmented small graphs.
- Limited control over what gets graphed.
- Graph fragmentation limits performance gains compared to manual approaches.
### Workflow 3: torch.cuda.make_graphed_callables()
Goal: Training with autograd support. Separate forward/backward graphs.
When to use: Training with custom loops, non-FP8, need autograd.
Steps:
1. Prepare sample inputs matching training batch shape:
```python
sample_input = torch.randn(batch_size, seq_len, hidden_size, device="cuda")
```
2. Create the graphed model:
```python
graphed_model = torch.cuda.make_graphed_callables(
model, (sample_input,), num_warmup_iters=3
)
```
3. Use `graphed_model` as a drop-in replacement in the training loop:
```python
for data, target in dataloader:
optimizer.zero_grad()
output = graphed_model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
```
4. If using AMP, set `cache_enabled=False`:
```python
for data, target in dataloader:
optimizer.zero_grad()
with torch.amp.autocast("cuda", cache_enabled=False):
output = graphed_model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
```
5. If using DDP, construct DDP on a side stream and use 11 warmup iters:
```python
os.environ["TORCH_NCCL_ASYNC_ERROR_HANDLING"] = "0"
s = torch.cuda.Stream()
with torch.cuda.stream(s):
model = DistributedDataParallel(model)
torch.cuda.current_stream().wait_stream(s)
graphed_model = torch.cuda.make_graphed_callables(
model, (sample_input,), num_warmup_iters=11
)
```
Limitations:
- No double backward (higher-order gradients).
- No module hooks during capture.
- Module structure is frozen after graphing (no add/remove parameters).
- Argument signature must match `sample_args` exactly.
### Workflow 4: TE make_graphed_callables
Goal: Per-callable graphing with FP8 support and pipeline parallelism.
When to use: FP8 training, PP with manual scheduling, non-Megatron models
needing FP8, or any PyTorch model that needs FP8-aware CUDA Graphs.
Steps:
1. Import and configure:
```python
from transformer_engine.pytorch.graph import make_graphed_callables
from transformer_engine.pytorch.fp8 import fp8_autocast
```
2. Prepare sample inputs (one per callable per microbatch per chunk):
```python
sample_args = tuple(
(torch.randn(batch_size, seq_len, hidden_size, device="cuda"),)
for _ in range(num_callables * num_microbatches)
)
```
3. Define pipeline schedule if using PP (1-indexed chunk IDs, positive=fwd,
negative=bwd):
```python
# Example: 2 chunks, 3 microbatches
layer_order = [1, 2, 1, 2, 1, 2, -2, -1, -2, -1, -2, -1]
```
4. Wrap layers in CUDA Graphs:
```python
graphed_layers = make_graphed_callables(
tuple(layers),
sample_args=sample_args,
fp8_enabled=True,
fp8_recipe=fp8_recipe,
fp8_weight_caching=True,
_order=layer_order, # None for no PP
)
```
5. Training loop -- wrap with `fp8_autocast` during replay:
```python
with fp8_autocast(enabled=True, fp8_recipe=fp8_recipe):
for layer in graphed_layers[start:end]:
x = layer(x, is_first_microbatch=(mb_idx == 0))
# FP8 scaling auto-updated on fp8_autocast exit
optimizer.step()
```
Key points:
- **AOT capture**: Graphs captured before the training loop when you call
`make_graphed_callables()`.
- **Replay order must match `_order`**: The training loop must execute graphs
in the same interleaved order as specified during capture.
- **`fp8_autocast` required during replay**: Without it, FP8 state is not
properly configured.
- **Weight caching**: `fp8_weight_caching=True` caches FP8 weight
quantization across microbatches; pass `is_first_microbatch` kwarg to
control when weights are requantized.
For full API details, see `references/api-te-megatron.md`.
### Workflow 5: MCore CudaGraphManager (Per-Layer)
Goal: Automatic per-layer graphing for Megatron-LM training.
When to use: Megatron-LM training, especially with PP > 1. Default choice
for Megatron users.
Steps:
1. Enable via CLI flags (no code changes needed):
```bash
python pretrain_gpt.py \
--enable-cuda-graph \
--cuda-graph-num-warmup-steps 3
```
2. Or enable via Python config:
```python
config = TransformerConfig(
enable_cuda_graph=True,
cuda_graph_num_warmup_steps=3,
)
```
3. Training loop is unchanged -- graphs are captured automatically after
warmup iterations.
Key points:
- **Megatron layers only**: Works with `TransformerLayer` and `MambaLayer`.
- **JIT capture**: Records execution order during warmup, captures graphs
after warmup completes, then replays on subsequent iterations.
- **Automatic FP8 handling**: Uses `fp8_autocast(..., _graph=True)` to skip
per-layer amax reduction; reduction happens once after all backward graphs.
- **Automatic PP support**: Handles microbatch interleaving automatically.
- **Memory savings**: Set `cuda_graph_share_io_buffers=True` to share I/O
buffers between layers (requires no operations between layers).
- **Memory pool strategy**: Default uses separate pools per microbatch for
graph reuse. Set `cuda_graph_use_single_mempool=True` for shared pool
(higher graph count but may reduce fragmentation).
### Workflow 6: MCore FullCudaGraphWrapper (Full-Iteration)
Goal: Maximum performance. Captures forward+backward for all microbatches
as a single graph.
When to use: Maximum performance priority, static workloads, Megatron-LM
training.
Steps:
1. Enable via CLI flags:
```bash
python pretrain_gpt.py \
--enable-cuda-graph \
--cuda-graph-scope full_iteration \
--cuda-graph-warmup-steps 1 \
--te-rng-tracker \
--no-check-for-nan-in-loss-and-grad
```
2. Ensure all forward+backward code is capturable (no `.item()`, no NaN
check, no dynamic control flow).
3. Optimizer remains in eager mode by default (outside the graph). Can be
included inside the graph for maximum performance.
Key points:
- **Only 2 graphs total**: One for training, one for validation.
- **`--te-rng-tracker` required**: Standard RNG uses CPU scalars that cannot
be captured; TE RNG uses device tensors compatible with graphs.
- **`--no-check-for-nan-in-loss-and-grad` mandatory**: NaN checking uses
`.item()` which requires CPU-GPU sync, forbidden during capture.
- **StaticBufferLoader**: Pre-allocates input buffers for all microbatches
during warmup.
- **Optimizer in/out of graph**: Inside = maximum performance (all optimizer
kernels captured). Outside = more flexible (can change optimizer/LR without
recapture).
- **JIT capture**: Graph captured during training at iteration
`warmup_steps + 1`.
### Workflow 7: torch.cuda.graph() (Manual)
Goal: Full control over capture and replay. Custom pipelines, full-iteration
capture without Megatron.
When to use: Need fine-grained control, non-Megatron full-iteration capture,
custom pipelines.
**Inference pattern:**
1. Pre-allocate static input/output tensors:
```python
static_input = torch.randn(batch_size, *shape, device="cuda")
```
2. Warmup on a side stream (3 iterations, 11 for DDP):
```python
s = torch.cuda.Stream()
with torch.cuda.stream(s):
for _ in range(3):
_ = model(static_input)
torch.cuda.current_stream().wait_stream(s)
```
3. Capture the graph:
```python
g = torch.cuda.CUDAGraph()
with torch.cuda.graph(g):
static_output = model(static_input)
```
4. Replay loop -- update inputs via `.copy_()`, clone outputs:
```python
for data in loader:
static_input.copy_(data)
g.replay()
result = static_output.clone()
```
**Full training pattern (fwd+bwd+optimizer in one graph):**
```python
model = MyModel().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
criterion = torch.nn.CrossEntropyLoss()
static_input = torch.randn(batch_size, *shape, device="cuda")
static_target = torch.randint(0, num_classes, (batch_size,), device="cuda")
# Warmup
s = torch.cuda.Stream()
with torch.cuda.stream(s):
for _ in range(3):
optimizer.zero_grad()
with torch.amp.autocast("cuda", cache_enabled=False):
out = model(static_input)
loss = criterion(out, static_target)
loss.backward()
torch.cuda.current_stream().wait_stream(s)
# Capture
g = torch.cuda.CUDAGraph()
with torch.cuda.graph(g):
optimizer.zero_grad()
GitHubで見る