Skip to content

Commit 5bbcd4f

Browse files
committed
Merge upstream main into PR 5524
Signed-off-by: Chen Cui <chcui@nvidia.com>
2 parents 7bdcc4a + cb3f319 commit 5bbcd4f

87 files changed

Lines changed: 2709 additions & 410 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/config/.secrets.baseline‎

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/training/activation-recomputation.md‎

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ As a rule of thumb:
1717

1818
- start with **selective recomputation** before using full recomputation
1919
- use **full recomputation** only when selective recomputation still does not fit
20-
- for MoE and long-context training, prefer recomputing smaller modules such as
21-
normalization, activation, MoE-side, or model-specific up-projection work
22-
rather than recomputing the whole layer
20+
- choose selective modules from the model and measured peak: `core_attn` is a
21+
common standard-attention candidate, MLA often benefits from `mla_up_proj`,
22+
and grouped MoE often starts with `moe_act` or `layernorm` plus `moe_act`
23+
- with TE fused or Flash Attention, compare `core_attn` against an empty
24+
selective module list because the backend already rematerializes attention
25+
internals
2326
- revisit recomputation after enabling CUDA graphs, because TE-scoped graphs and
2427
full recomputation are not always compatible
2528

@@ -86,7 +89,10 @@ When training with pipeline parallelism:
8689

8790
## Self-attention Recomputation
8891

89-
Megatron Bridge supports selective self-attention recomputation that checkpoints the inputs of each self-attention block and recomputes the intermediate input activations. This cost-efficient method achieves high memory savings with minimal recomputation cost.
92+
Megatron Bridge supports selective self-attention recomputation that checkpoints
93+
the core-attention boundary and recomputes it during backward. This can be a
94+
cost-efficient choice for standard attention, but it is not the universal first
95+
boundary for MLA, MoE, or fused-attention workloads.
9096

9197
The intermediate layers of the self-attention block account for a large share
9298
of activation memory because softmax, dropout, and QKV dot-product attention
@@ -105,36 +111,38 @@ from megatron.bridge.models import GPTModelProvider
105111

106112
model_config = GPTModelProvider(
107113
recompute_granularity="selective", # Enable selective recomputation
108-
recompute_modules=["core_attn"], # Recompute attention modules (default)
114+
recompute_modules=["core_attn"], # Common standard-attention candidate and MCore default
109115
# ... other model parameters
110116
)
111117
```
112118

113119
### Recomputation Modules
114120

115-
Megatron Bridge supports selective recomputation for various modules:
121+
The pinned Megatron Core accepts these selective labels. Do not combine them
122+
blindly: some are architecture-specific and some checkpoint overlapping
123+
regions.
116124

117-
```python
118-
model_config = GPTModelProvider(
119-
recompute_granularity="selective",
120-
recompute_modules=[
121-
"core_attn", # Core attention computation (default)
122-
"mlp", # MLP layers
123-
"layernorm", # Layer normalization
124-
"moe", # Mixture of Experts layers
125-
"moe_act", # MoE activation functions
126-
"shared_experts", # Shared expert layers
127-
"mla_up_proj", # Multi-Latent Attention up projection
128-
],
129-
)
130-
```
125+
| Label | Boundary and typical use |
126+
|---|---|
127+
| `core_attn` | Core attention; a common standard-attention candidate that can replay context-parallel communication |
128+
| `mla_up_proj` | Expanded MLA Q/KV projections and RoPE; often the first MLA candidate |
129+
| `moe_act` | Grouped-expert activation output without replaying dispatch or expert GEMMs |
130+
| `layernorm` | Input and pre-MLP normalization outputs, often paired with a MoE or MLA boundary |
131+
| `mlp` | Whole dense MLP; broader replay and no effect on pure-MoE layers |
132+
| `moe` | Whole MoE forward, including routing, expert compute, and communication |
133+
| `shared_experts` | Non-overlapped shared-expert MLP |
134+
| `gdn_norm_out` | GatedDeltaNet output norm and HP-to-CP communication |
135+
136+
An empty list is valid under selective granularity and is useful as a matched
137+
no-recompute control.
131138

132139
### Flash Attention Integration
133140

134-
Self-attention recomputation is automatically enabled when using Flash Attention
135-
through Transformer Engine. Flash Attention already recovers some memory by
136-
recomputing attention scores rather than storing them, so extra explicit
137-
attention recomputation is often less important than recomputing other modules.
141+
Flash Attention through Transformer Engine already recovers memory by
142+
rematerializing attention internals. That does not automatically make
143+
`recompute_modules=["core_attn"]` the right explicit setting. Compare it with an
144+
empty selective module list; MLA up projections, MoE activations, normalization,
145+
or another boundary may set the actual peak.
138146

139147
## Advanced Recomputation Configuration
140148

@@ -169,29 +177,38 @@ model_config = GPTModelProvider(
169177
# MoE configuration
170178
num_moe_experts=8,
171179
expert_model_parallel_size=2,
180+
moe_grouped_gemm=True,
172181

173182
# MoE recomputation
174183
recompute_granularity="selective",
175-
recompute_modules=["moe", "moe_act"], # Recompute MoE-specific modules
184+
recompute_modules=["moe_act", "layernorm"], # Narrow MoE-side candidates
176185
)
177186
```
178187

179-
For MoE training, it is often better to start with selective recomputation of
180-
MoE-side modules, normalization, activation functions, or model-specific
181-
up-projection modules than to enable blanket full recomputation immediately.
188+
`moe_act` requires grouped-GEMM experts. Whole-`moe` recompute is a broader
189+
alternative that replays routing, dispatch/combine communication, expert
190+
compute, and shared-expert work; it is incompatible with expert-parallel
191+
overlap. `shared_experts` recompute is likewise incompatible with shared-expert
192+
overlap. Prefer the narrow boundary that satisfies the memory target.
182193

183194
## Feature Interactions
184195

185-
- TE-scoped CUDA graphs are usually paired with selective recomputation rather
186-
than full recomputation.
196+
- Full recomputation with CUDA graphs requires
197+
`cuda_graph_impl="full_iteration"` in the pinned Megatron Core. Otherwise use
198+
selective recomputation or disable CUDA graphs.
199+
- A selective checkpoint boundary must lie wholly inside or wholly outside its
200+
CUDA-graph scope.
187201
- MoE communication overlap paths often require recomputation settings that are
188202
more selective than "full."
189203
- At long context, recomputing SDPA-heavy attention internals can cost more than
190204
recomputing smaller supporting modules.
205+
- Advancing from a forward OOM to a gradient-synchronization or optimizer OOM
206+
is useful diagnosis, but not a pass. Validate through optimizer-state
207+
initialization and multiple steady-state iterations.
191208

192209
## Related Docs
193210

194211
- [docs/training/cuda-graphs.md](cuda-graphs.md)
195212
- [docs/training/moe-optimization.md](moe-optimization.md)
196-
- [skills/nemo-mbridge-perf-activation-recompute/SKILL.md](../skills/nemo-mbridge-perf-activation-recompute/SKILL.md) — per-module cost/savings data, measured results
197-
- [skills/nemo-mbridge-perf-memory-tuning/SKILL.md](../skills/nemo-mbridge-perf-memory-tuning/SKILL.md) — expandable segments, parallelism resizing, and other memory reduction strategies
213+
- [skills/nemo-mbridge-perf-activation-recompute/SKILL.md](../../skills/nemo-mbridge-perf-activation-recompute/SKILL.md) — architecture-specific module selection, compatibility, and measurement guidance
214+
- [skills/nemo-mbridge-perf-memory-tuning/SKILL.md](../../skills/nemo-mbridge-perf-memory-tuning/SKILL.md) — expandable segments, parallelism resizing, and other memory reduction strategies

‎examples/model_verification_cards/gemma-3-4b-it/card.yaml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ items:
172172
final_loss: null
173173
last_10_steps_step_time_ms_avg: null
174174
last_10_steps_model_tflops_per_gpu_avg: null
175+
last_10_steps_tokens_per_second_per_gpu_avg: null
175176
expected_result: >
176177
Megatron Bridge does not provide a public Gemma 3 VL 4B pretraining
177178
recipe. The available exact-model recipes cover SFT and PEFT, so a
@@ -197,10 +198,11 @@ items:
197198
final_loss: null
198199
last_10_steps_step_time_ms_avg: null
199200
last_10_steps_model_tflops_per_gpu_avg: null
201+
last_10_steps_tokens_per_second_per_gpu_avg: null
200202
expected_result: >
201203
Pending exact-revision CORD-v2 full-SFT verification with the public
202204
Gemma 3 VL 4B H100 recipe. The run must complete at least ten optimizer
203-
steps with finite loss, no skipped or NaN iterations, all four metrics,
205+
steps with finite loss, no skipped or NaN iterations, all five metrics,
204206
and a reloadable final full-model checkpoint.
205207
206208
sft_export_inference:
@@ -228,6 +230,7 @@ items:
228230
final_loss: null
229231
last_10_steps_step_time_ms_avg: null
230232
last_10_steps_model_tflops_per_gpu_avg: null
233+
last_10_steps_tokens_per_second_per_gpu_avg: null
231234
expected_result: >
232235
Gemma 3 VL image-bidirectional attention uses dense local/global
233236
additive biases with FusedAttention. The provider rejects context
@@ -255,10 +258,11 @@ items:
255258
final_loss: null
256259
last_10_steps_step_time_ms_avg: null
257260
last_10_steps_model_tflops_per_gpu_avg: null
261+
last_10_steps_tokens_per_second_per_gpu_avg: null
258262
expected_result: >
259263
Pending exact-revision CORD-v2 LoRA verification with the public Gemma 3
260264
VL 4B H100 recipe. The run must complete at least ten optimizer steps
261-
with finite loss, no skipped or NaN iterations, all four metrics, and a
265+
with finite loss, no skipped or NaN iterations, all five metrics, and a
262266
reloadable adapter checkpoint.
263267
264268
checkpoint_resume:
@@ -273,6 +277,7 @@ items:
273277
final_loss: null
274278
last_10_steps_step_time_ms_avg: null
275279
last_10_steps_model_tflops_per_gpu_avg: null
280+
last_10_steps_tokens_per_second_per_gpu_avg: null
276281
resume_comparison: null
277282
expected_result: >
278283
Checkpoint-resume verification depends on a supported pretraining

‎examples/model_verification_cards/gemma-4-26b-a4b-it/card.yaml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ items:
172172
final_loss: null
173173
last_10_steps_step_time_ms_avg: null
174174
last_10_steps_model_tflops_per_gpu_avg: null
175+
last_10_steps_tokens_per_second_per_gpu_avg: null
175176
expected_result: >
176177
Megatron Bridge does not publish a pretraining recipe for this Gemma 4
177178
conditional-generation variant; the current model package provides SFT
@@ -199,6 +200,7 @@ items:
199200
final_loss: 0.09127883
200201
last_10_steps_step_time_ms_avg: 15936.25
201202
last_10_steps_model_tflops_per_gpu_avg: 32.20
203+
last_10_steps_tokens_per_second_per_gpu_avg: 1028.096
202204
peak_allocated_memory_gib: 65.223
203205
peak_reserved_memory_gib: 72.356
204206
expected_result: >
@@ -244,11 +246,12 @@ items:
244246
final_loss: null
245247
last_10_steps_step_time_ms_avg: null
246248
last_10_steps_model_tflops_per_gpu_avg: null
249+
last_10_steps_tokens_per_second_per_gpu_avg: null
247250
expected_result: >
248251
No Gemma 4 VL long-context recipe or run was verified. A future
249252
workload must establish sequence-packing and context-parallel settings,
250253
complete at least 10 optimizer steps with finite loss and no skipped or
251-
NaN iterations, report all four required metrics, and save a reloadable
254+
NaN iterations, report all five required metrics, and save a reloadable
252255
final checkpoint.
253256
254257
peft:
@@ -273,6 +276,7 @@ items:
273276
final_loss: 0.1072377
274277
last_10_steps_step_time_ms_avg: 14163.25
275278
last_10_steps_model_tflops_per_gpu_avg: 65.01
279+
last_10_steps_tokens_per_second_per_gpu_avg: 2313.593
276280
peak_allocated_memory_gib: 44.252
277281
peak_reserved_memory_gib: 45.637
278282
expected_result: >
@@ -303,6 +307,7 @@ items:
303307
final_loss: null
304308
last_10_steps_step_time_ms_avg: null
305309
last_10_steps_model_tflops_per_gpu_avg: null
310+
last_10_steps_tokens_per_second_per_gpu_avg: null
306311
resume_comparison: null
307312
expected_result: >
308313
Megatron Bridge does not provide a Gemma 4 VLM pretraining recipe, so

‎examples/model_verification_cards/glm5-2/card.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ items:
177177
final_loss: 6.385243
178178
last_10_steps_step_time_ms_avg: 31204.920
179179
last_10_steps_model_tflops_per_gpu_avg: 54.595
180+
last_10_steps_tokens_per_second_per_gpu_avg: 190.926
180181
expected_result: >
181182
The uninterrupted 352-H100 TP1/PP11/CP1/EP32/ETP1 run completed 100
182183
real-data steps at GBS/MBS 1024/1 with cuDNN DSA, sparse indexer loss
@@ -216,6 +217,7 @@ items:
216217
final_loss: 6.523522
217218
last_10_steps_step_time_ms_avg: 64786.540
218219
last_10_steps_model_tflops_per_gpu_avg: 102.520
220+
last_10_steps_tokens_per_second_per_gpu_avg: 337.189
219221
expected_result: >
220222
The 192-GB200 TP1/PP6/CP1/EP32/ETP1 run completed exactly 100
221223
RP2 head_01 pretraining steps at GBS/MBS 1024/1 with cuDNN DSA, sparse
@@ -254,6 +256,7 @@ items:
254256
final_loss: 0.6634203
255257
last_10_steps_step_time_ms_avg: 4436.290
256258
last_10_steps_model_tflops_per_gpu_avg: 10.154
259+
last_10_steps_tokens_per_second_per_gpu_avg: 35.511
257260
expected_result: >
258261
The immutable-revision 416-H100 run completes exactly 100 full-SFT
259262
steps at TP1/PP13/CP2/EP32/ETP1, GBS/MBS 32/1, with cuDNN DSA, sparse
@@ -286,6 +289,7 @@ items:
286289
final_loss: 0.3682597
287290
last_10_steps_step_time_ms_avg: 8593.990
288291
last_10_steps_model_tflops_per_gpu_avg: 12.446
292+
last_10_steps_tokens_per_second_per_gpu_avg: 39.718
289293
expected_result: >
290294
The immutable-revision 192-GB200 run completed exactly 100 full-SFT
291295
steps at TP1/PP6/CP4/EP32/ETP1, DP8, and GBS/MBS 8/1 without gradient
@@ -363,6 +367,7 @@ items:
363367
final_loss: 2.557671
364368
last_10_steps_step_time_ms_avg: 36603.370
365369
last_10_steps_model_tflops_per_gpu_avg: 39.648
370+
last_10_steps_tokens_per_second_per_gpu_avg: 116.828
366371
expected_result: >
367372
The 608-H100 TP1/PP19/CP32/EP32/ETP1 run completed exactly 20 optimizer
368373
steps over 200,000-token packed rows with cuDNN DSA, sparse indexer loss
@@ -394,6 +399,7 @@ items:
394399
final_loss: 0.000511673
395400
last_10_steps_step_time_ms_avg: 143248.820
396401
last_10_steps_model_tflops_per_gpu_avg: 88.848
402+
last_10_steps_tokens_per_second_per_gpu_avg: 266.874
397403
expected_result: >
398404
The immutable-revision 192-GB200 run completed exactly 20 optimizer
399405
steps at TP1/PP6/CP32/EP32/ETP1, DP1, and GBS/MBS 56/1 with cuDNN DSA,
@@ -427,6 +433,7 @@ items:
427433
final_loss: 0.8835775
428434
last_10_steps_step_time_ms_avg: 4638.550
429435
last_10_steps_model_tflops_per_gpu_avg: 19.423
436+
last_10_steps_tokens_per_second_per_gpu_avg: 67.926
430437
expected_result: >
431438
The immutable-revision 208-H100 run completes exactly 100 LoRA steps at
432439
TP1/PP13/CP1/EP16/ETP1, GBS/MBS 32/1, with cuDNN DSA, sparse indexer
@@ -455,6 +462,7 @@ items:
455462
final_loss: 0.8957523
456463
last_10_steps_step_time_ms_avg: 68588.270
457464
last_10_steps_model_tflops_per_gpu_avg: 1.423
465+
last_10_steps_tokens_per_second_per_gpu_avg: 4.977
458466
expected_result: >
459467
The immutable-revision 192-GB200 run completed exactly 100 LoRA steps
460468
at TP1/PP6/CP1/EP32/ETP1, GBS/MBS 32/1, with offline-packed Tulu 3
@@ -491,6 +499,7 @@ items:
491499
final_loss: 6.372624
492500
last_10_steps_step_time_ms_avg: 32002.290
493501
last_10_steps_model_tflops_per_gpu_avg: 53.235
502+
last_10_steps_tokens_per_second_per_gpu_avg: 186.168
494503
resume_comparison:
495504
reference_item: pretrain
496505
sentinel_steps: [51, 100]
@@ -536,6 +545,7 @@ items:
536545
final_loss: 6.503141
537546
last_10_steps_step_time_ms_avg: 60079.390
538547
last_10_steps_model_tflops_per_gpu_avg: 110.552
548+
last_10_steps_tokens_per_second_per_gpu_avg: 363.608
539549
resume_comparison:
540550
reference_item: pretrain
541551
sentinel_steps: [51, 100]

‎examples/model_verification_cards/glm5/card.yaml‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ items:
139139
final_loss: null
140140
last_10_steps_step_time_ms_avg: null
141141
last_10_steps_model_tflops_per_gpu_avg: null
142+
last_10_steps_tokens_per_second_per_gpu_avg: null
142143
expected_result: >
143144
A public GLM-5 H100 recipe must complete a bounded 100-step run with
144-
finite loss, no skipped or NaN iterations, all four metrics, and a
145+
finite loss, no skipped or NaN iterations, all five metrics, and a
145146
reloadable final checkpoint. Training is deferred by this card.
146147
147148
sft:
@@ -156,9 +157,10 @@ items:
156157
final_loss: null
157158
last_10_steps_step_time_ms_avg: null
158159
last_10_steps_model_tflops_per_gpu_avg: null
160+
last_10_steps_tokens_per_second_per_gpu_avg: null
159161
expected_result: >
160162
A pinned-data 100-step full-SFT run must finish with finite loss, no
161-
skipped or NaN iterations, all four metrics, and a reloadable final
163+
skipped or NaN iterations, all five metrics, and a reloadable final
162164
checkpoint. Training is deferred by this card.
163165
164166
sft_export_inference:
@@ -185,9 +187,10 @@ items:
185187
final_loss: null
186188
last_10_steps_step_time_ms_avg: null
187189
last_10_steps_model_tflops_per_gpu_avg: null
190+
last_10_steps_tokens_per_second_per_gpu_avg: null
188191
expected_result: >
189192
A dedicated packed long-context SFT run must complete a bounded run
190-
with finite loss, no skipped or NaN iterations, all four metrics, and
193+
with finite loss, no skipped or NaN iterations, all five metrics, and
191194
a reloadable checkpoint. Training is deferred by this card.
192195
193196
peft:
@@ -202,9 +205,10 @@ items:
202205
final_loss: null
203206
last_10_steps_step_time_ms_avg: null
204207
last_10_steps_model_tflops_per_gpu_avg: null
208+
last_10_steps_tokens_per_second_per_gpu_avg: null
205209
expected_result: >
206210
A GLM-5 PEFT recipe with an audited adapter target set must complete a
207-
bounded run with finite loss, all four metrics, and a reloadable
211+
bounded run with finite loss, all five metrics, and a reloadable
208212
adapter checkpoint. Training is deferred by this card.
209213
210214
checkpoint_resume:
@@ -219,6 +223,7 @@ items:
219223
final_loss: null
220224
last_10_steps_step_time_ms_avg: null
221225
last_10_steps_model_tflops_per_gpu_avg: null
226+
last_10_steps_tokens_per_second_per_gpu_avg: null
222227
resume_comparison:
223228
reference_item: pretrain
224229
sentinel_steps: [51, 100]

0 commit comments

Comments
 (0)