nvidia-nemo/megatron-bridge/nemo-mbridge-perf-cuda-graphs
Claude Code and agent skill by nvidia-nemo/megatron-bridge
- community
Checking that the SKILL.md file is still in the repository.
Profile, validate, and use CUDA graph capture in Megatron Bridge, including local full-iteration graphs and Transformer Engine scoped graphs. Covers replay evidence, matched eager A/Bs, model-specific scopes, regressions, and failures.
Install
npx vskill@latest install nvidia-nemo/megatron-bridge/nemo-mbridge-perf-cuda-graphsSKILL.md
The same SKILL.md was published earlier as nvidia-nemo/megatron-bridge/nemo-mbridge-perf-cuda-graphs.
CUDA Graphs
Stable documentation: @docs/training/cuda-graphs.md Card: @skills/nemo-mbridge-perf-cuda-graphs/card.yaml
What It Is
CUDA graphs capture GPU operations once and replay them with minimal host-driver overhead. Bridge supports two implementations:
cuda_graph_impl | Mechanism | Scope support | |
|---|---|---|---|
"local" | MCore FullCudaGraphWrapper wrapping entire fwd+bwd | full_iteration | |
"transformer_engine" | TE make_graphed_callables() per layer | attn, mlp, moe, moe_router, moe_preprocess, mamba |
Quick Decision
First confirm a profile shows material host/launch gaps. Then test the narrowest TE-scoped graph candidate and verify replay timing against eager on the same dispatcher, layout, routing, precision, and container:
- dense models:
attn, then optionallymlp - dropless MoE:
moe_router moe_preprocess, then optionallyattn - VLMs: the same dropless-MoE scope, but only after the real-data path is stable
Use local + full_iteration only when you specifically want full-iteration capture and can satisfy the tighter constraints.
For recompute-heavy workloads:
- TE-scoped graphs pair naturally with selective recompute
- full recompute usually pushes you toward
localfull-iteration graphs or away
from graphs entirely
Related docs:
- @docs/training/cuda-graphs.md
- @docs/training/activation-recomputation.md
Enablement
Local full-iteration graph
TE scoped graph (dense model)
TE scoped graph (MoE model)
Performance harness CLI
Valid CLI values live in scripts/performance/argument_parser.py:
VALID_CUDA_GRAPH_IMPLS:["none", "local", "transformer_engine"]VALID_CUDA_GRAPH_SCOPES:["full_iteration", "attn", "mlp", "moe", "moe_router", "moe_preprocess", "mamba"]
The performance harness uses a comma-separated --cuda_graph_scope value and auto-enables model.use_te_rng_tracker plus rng.te_rng_tracker when --cuda_graph_impl is not none.
Scope names follow the actual MCore module type. mamba applies to real Mamba layers; do not use it as a generic label for every linear-attention block. Qwen3.5 Gated DeltaNet (GDN) lives under self_attention, so its applicable TE scope is attn, not mamba.
Required constraints
use_te_rng_tracker = True(enforced ingpt_provider.py)full_iterationscope only withcuda_graph_impl = "local"full_iterationscope requirescheck_for_nan_in_loss = False- Do not combine
moescope andmoe_routerscope - Tensor shapes must be static (fixed seq_length, fixed micro_batch_size)
- MoE token-dropless routing limits graphable scope to dense modules
- With
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True, set
NCCL_GRAPH_REGISTER=0 (MCore enforces for local impl on arch < sm_100; TE impl asserts unconditionally)
- CPU offloading is incompatible with CUDA graphs
moe_preprocessscope requiresmoe_routerscope to also be set
Practical bring-up order
- Stabilize the eager run first.
- Fix sequence length and micro-batch size.
- Enable the narrowest useful graph scope.
- Confirm replay is active and memory is still acceptable.
- Compare eager against graph replay iterations after warmup and capture; do
not include the capture step in steady-state timing.
- Only then widen scope or combine with overlap features.
- Re-run the eager/graph A/B on the final dispatcher, overlap, precision, and
recompute stack. An earlier graph win can disappear after other tuning.
Code Anchors
Bridge config and validation
TE RNG tracker requirement
Graph creation and capture in training loop
TE graph capture after warmup
RNG initialization
Delayed wgrad + CUDA graph interaction
Perf harness override helper
Graph cleanup
MCore classes (in 3rdparty/Megatron-LM)
CudaGraphManager:megatron/core/transformer/cuda_graphs.pyTECudaGraphHelper:megatron/core/transformer/cuda_graphs.pyFullCudaGraphWrapper:megatron/core/full_cuda_graph.pyCudaGraphScopeenum:megatron/core/transformer/enums.py
Positive recipe anchors
src/megatron/bridge/perf_recipes/deepseek/gb300/deepseek_v3.pysrc/megatron/bridge/perf_recipes/qwen/gb300/qwen3_moe.pysrc/megatron/bridge/perf_recipes/gpt_oss/gb300/gpt_oss.py
Tests
| File | Coverage | |
|---|---|---|
tests/unit_tests/training/test_config.py | full_iteration NaN-check constraint | |
tests/unit_tests/training/test_comm_overlap.py | delay_wgrad + CUDA graph interaction | |
tests/unit_tests/models/test_gpt_full_te_layer_autocast_spec.py | TE autocast with CUDA graphs | |
tests/functional_tests/test_groups/recipes/test_llama_recipes_pretrain_cuda_graphs.py | End-to-end local and TE graph smoke tests | |
tests/unit_tests/recipes/kimi/test_kimi_k2.py | TE + CUDA graph recipe config | |
tests/unit_tests/recipes/gpt/test_gpt3_175b.py | TE + CUDA graph recipe config | |
tests/unit_tests/recipes/qwen_vl/test_qwen25_vl_recipes.py | VLM CUDA graph settings |
Pitfalls
- TE RNG tracker is mandatory: Setting
cuda_graph_implwithout
use_te_rng_tracker=True and rng.te_rng_tracker=True will assert in the provider.
- `full_iteration` requires NaN checks disabled: The entire fwd+bwd is
captured, so loss-NaN checking cannot inspect intermediate values.
- MoE scope restrictions:
moescope andmoe_routerscope are
mutually exclusive. Token-dropless MoE can only graph moe_router and moe_preprocess, not the full expert dispatch.
- Memory overhead: CUDA graphs pin all intermediate buffers for the
graph's lifetime (no memory reuse). TE scoped graphs add a few GB; full-iteration graphs can increase peak memory by 1.5–2×. PP > 1 compounds overhead since each stage holds its own graph.
- Delayed wgrad interaction: When
delay_wgrad_compute=Trueand
attention or MoE router is in cuda_graph_scope, additional constraints apply: TE >= 2.12.0, gradient_accumulation_fusion=True, and no attention bias.
- Variable-length sequences break graphs: Sequence lengths must be
constant across steps. Use padded packed sequences if packing is needed.
- Graph cleanup is required: CUDA graph objects hold NCCL buffer
references. Bridge handles this in _delete_cuda_graphs() at the end of training, but early exits must call it explicitly.
- Older GPU architectures: On GPUs with compute capability < 10.0
(pre-Blackwell), set NCCL_GRAPH_REGISTER=0 when using PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True. Enforced in MCore CudaGraphManager (cuda_graphs.py:1428) and TECudaGraphHelper (cuda_graphs.py:1697). The TE impl asserts unconditionally regardless of arch.
- CPU offloading incompatible: CUDA graphs cannot be used with CPU
offloading. Enforced in MCore transformer_config.py:1907.
- MoE recompute + moe_router scope: MoE recompute is not supported
with moe_router CUDA graph scope when using cuda_graph_impl = "transformer_engine". Enforced in MCore transformer_config.py:1977.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/nemo-mbridge-perf-cuda-graphs/SKILL.md
Popularity
- Repository stars
- 943
- Repository forks
- 529
- 7-day trend score
- 34
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/nvidia-nemo/megatron-bridge/nemo-mbridge-perf-cuda-graphs)