nemo-mbridge-perf-cuda-graphs
Skill976 starsChanged 22 days agoApache-2.0View on GitHub
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.
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 33
Use local + full_iteration only when you specifically want full-iteration capture and can satisfy the tighter constraints.
- Very specific: line 114
Do not combine moe scope and moe_router scope
- Distinctive: line 131
Only then widen scope or combine with overlap features.
- Very specific: line 275
Delayed wgrad interaction: When delay_wgrad_compute=True and attention or MoE router is in cuda_graph_scope, additional constraints apply: …
- Very specific: line 328
Scope names are structural, not architectural nicknames: use mamba only for real Mamba layers.
- Distinctive: line 356
No NCCL errors or illegal memory access in logs.
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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
cfg.model.cuda_graph_impl = "local"
cfg.model.cuda_graph_scope = ["full_iteration"]
cfg.model.cuda_graph_warmup_steps = 3
cfg.model.use_te_rng_tracker = True
cfg.rng.te_rng_tracker = True
cfg.rerun_state_machine.check_for_nan_in_loss = False
cfg.ddp.check_for_nan_in_grad = FalseTE scoped graph (dense model)
cfg.model.cuda_graph_impl = "transformer_engine"
cfg.model.cuda_graph_scope = ["attn"] # or ["attn", "mlp"]
cfg.model.cuda_graph_warmup_steps = 3
cfg.model.use_te_rng_tracker = True
cfg.rng.te_rng_tracker = TrueTE scoped graph (MoE model)
cfg.model.cuda_graph_impl = "transformer_engine"
cfg.model.cuda_graph_scope = ["moe_router", "moe_preprocess"]
cfg.model.cuda_graph_warmup_steps = 3
cfg.model.use_te_rng_tracker = True
cfg.rng.te_rng_tracker = TruePerformance harness CLI
uv run python scripts/performance/run_script.py \
-m qwen \
-mr qwen3_30b_a3b \
--task pretrain \
-g h100 \
-c bf16 \
-ng 16 \
--cuda_graph_impl transformer_engine \
--cuda_graph_scope moe_router,moe_preprocess \
...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, setNCCL_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
# CUDA graph scope validation: check_for_nan_in_loss must be disabled with full_iteration graph
if self.model.cuda_graph_impl == "local" and CudaGraphScope.full_iteration in self.model.cuda_graph_scope:
assert not self.rerun_state_machine.check_for_nan_in_loss, (
"check_for_nan_in_loss must be disabled when using full_iteration CUDA graph. "
"Set rerun_state_machine.check_for_nan_in_loss=False."
)
if self.model.cuda_graph_impl == "none":
self.model.cuda_graph_scope = []TE RNG tracker requirement
if self.cuda_graph_impl != "none":
assert getattr(self, "use_te_rng_tracker", False), (
"Transformer engine's RNG tracker is required for cudagraphs, it can be "
"enabled with use_te_rng_tracker=True'."Graph creation and capture in training loop
# Capture CUDA Graphs.
cuda_graph_helper = None
if model_config.cuda_graph_impl == "transformer_engine":
cuda_graph_helper = TECudaGraphHelper(...)
# ...
if config.model.cuda_graph_impl == "local" and CudaGraphScope.full_iteration in config.model.cuda_graph_scope:
forward_backward_func = FullCudaGraphWrapper(
forward_backward_func, cuda_graph_warmup_steps=config.model.cuda_graph_warmup_steps
)TE graph capture after warmup
# Capture CUDA Graphs after warmup.
if (
model_config.cuda_graph_impl == "transformer_engine"
and cuda_graph_helper is not None
and not cuda_graph_helper.graphs_created()
and global_state.train_state.step - start_iteration == model_config.cuda_graph_warmup_steps
):
if model_config.cuda_graph_warmup_steps > 0 and should_toggle_forward_pre_hook:
disable_forward_pre_hook(model, param_sync=False)
cuda_graph_helper.create_cudagraphs()
if model_config.cuda_graph_warmup_steps > 0 and should_toggle_forward_pre_hook:
enable_forward_pre_hook(model)
cuda_graph_helper.cuda_graph_set_manual_hooks()RNG initialization
_set_random_seed(
rng_config.seed,
rng_config.data_parallel_random_init,
rng_config.te_rng_tracker,
rng_config.inference_rng_tracker,
use_cudagraphable_rng=(model_config.cuda_graph_impl != "none"),
pg_collection=pg_collection,
)Delayed wgrad + CUDA graph interaction
cuda_graph_scope = getattr(model_cfg, "cuda_graph_scope", []) or []
# ... scope parsing ...
if wgrad_in_graph_scope:
assert is_te_min_version("2.12.0"), ...
assert model_cfg.gradient_accumulation_fusion, ...
if attn_scope_enabled:
assert not model_cfg.add_bias_linear and not model_cfg.add_qkv_bias, ...Perf harness override helper
def _set_cuda_graph_overrides(
recipe, cuda_graph_impl=None, cuda_graph_scope=None
):
# Sets impl, scope, and auto-enables te_rng_trackerGraph cleanup
def _delete_cuda_graphs(cuda_graph_helper):
# Deletes FullCudaGraphWrapper and TE graph objects to free NCCL buffersMCore 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_implwithoutuse_te_rng_tracker=Trueandrng.te_rng_tracker=Truewill assert in the provider. full_iterationrequires 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 graphmoe_routerandmoe_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 > 1compounds overhead since each stage holds its own graph. - Delayed wgrad interaction: When
delay_wgrad_compute=Trueand attention or MoE router is incuda_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=0when usingPYTORCH_CUDA_ALLOC_CONF=expandable_segments:True. Enforced in MCoreCudaGraphManager(cuda_graphs.py:1428) andTECudaGraphHelper(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_routerCUDA graph scope when usingcuda_graph_impl = "transformer_engine". Enforced in MCoretransformer_config.py:1977. - Layer-level recompute requires
full_iterationscope: Usingrecompute_granularity="full"withrecompute_num_layers(recompute N whole transformer layers) is incompatible with TE-scoped graphs. MCore calls this "full" granularity even though you're selecting how many layers — the name refers to recomputing the full layer, not full model. Any TE-scoped scope (attn,mlp,moe_router, etc.) will assert:AssertionError: full recompute is only supported with full iteration CUDA graph.This commonly hits FP8 configs that default to TE-scoped graphs (e.g.LLAMA3_70B_SFT_CONFIG_H100_FP8_CS_V1usescuda_graph_impl= "transformer_engine",cuda_graph_scope="mlp"). Fix: use submodule recompute (recompute_granularity="selective"+recompute_modules), disable CUDA graphs, or switch tolocal+full_iteration. Enforced in MCoretransformer_config.py:2001-2005. See also @skills/nemo-mbridge-perf-activation-recompute/SKILL.md. - Benchmark numbers are workload-specific: graph wins are usually real when host overhead is visible, but the exact gain depends on batch shape, PP depth, recompute, dispatcher backend, and whether the eager baseline was already optimized.
- A successful capture is not a speedup guarantee: On 2026-05-18,
Qwen3 30B A3B H100 BF16 pretrain with the all-to-all dispatcher captured
TE-scoped
attn,moe_router,moe_preprocessgraphs successfully (48graphable layers, about6.9 scapture time on rank 0), but replay iterations 5-8 averaged42.00 sversus41.36 sfor eager. Treat scoped graphs as a bring-up candidate and validate on the target stack. - Scope names are structural, not architectural nicknames: use
mambaonly for real Mamba layers. Hybrid models such as Qwen3.5 place GDN in the attention path, so graph it withattnwhen supported.
Verification
Unit tests
uv run python -m pytest \
tests/unit_tests/training/test_config.py -k "cuda_graph" \
tests/unit_tests/training/test_comm_overlap.py -k "cuda_graph" \
tests/unit_tests/models/test_gpt_full_te_layer_autocast_spec.py -k "cuda_graph" -qFunctional smoke test (requires GPU)
uv run python -m pytest \
tests/functional_tests/test_groups/recipes/test_llama_recipes_pretrain_cuda_graphs.py -qSuccess criteria
- Unit tests pass, covering config validation for both
localandtransformer_engineimplementations. - Functional test completes training steps with both CUDA graph implementations.
- No NCCL errors or illegal memory access in logs.
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 1
- NVIDIA/skills★ 3.8k96% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository35 other files it gives its agents.
- AGENTS.mdAGENTS.md
- adding-model-supportSkill
- build-and-dependencySkill
- bump-dependencySkill
- cicdSkill
- create-model-verification-cardSkill
- linting-and-formattingSkill
- nemo-mbridge-memory-snapshot-analysisSkill
- nemo-mbridge-mlm-bridge-trainingSkill
- nemo-mbridge-multi-node-slurmSkill
- nemo-mbridge-perf-activation-recomputeSkill
- nemo-mbridge-perf-cpu-offloadingSkill
- nemo-mbridge-perf-expert-parallel-overlapSkill
- nemo-mbridge-perf-hierarchical-context-parallelSkill
- nemo-mbridge-perf-megatron-fsdpSkill
- nemo-mbridge-perf-memory-tuningSkill
- nemo-mbridge-perf-moe-comm-overlapSkill
- nemo-mbridge-perf-moe-dispatcher-selectionSkill
- nemo-mbridge-perf-moe-hardware-configsSkill
- nemo-mbridge-perf-moe-long-contextSkill
- nemo-mbridge-perf-moe-optimization-workflowSkill
- nemo-mbridge-perf-moe-vlm-trainingSkill
- nemo-mbridge-perf-nsys-analysisSkill
- nemo-mbridge-perf-parallelism-strategiesSkill
- nemo-mbridge-perf-sequence-packingSkill
- nemo-mbridge-perf-tp-dp-comm-overlapSkill
- nemo-mbridge-perf-vision-dp-over-cpSkill
- nemo-mbridge-recipe-recommenderSkill
- nemo-mbridge-resiliencySkill
- nemo-rl-e2e-testingSkill
- parity-testingSkill
- pr-reviewSkill
- review-prSkill
- testingSkill
- verl-e2e-testingSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

