[Corrected] GDN + context parallelism: original gradient-inflation report was a harness artifact
[Corrected] GDN + context parallelism: original gradient-inflation report was a harness artifact
Status: retraction / correction. The original version of this issue reported that
context_parallel_size=2silently inflated gradients by 9×–1536× for models withGatedDeltaNetlayers. We could not reproduce that with a corrected harness, and a layer-level control by a maintainer (one BF16 GDN layer, CP1/DP4 vs CP2/DP2, 1,053 masked tokens both sides: grad-norm ratio 1.000012, full-vector relative L2 difference 0.22 %) points the same way. The measurements below supersede the original claim.Related: #7909 (chunkwise CP for GDN) is still the useful feature request.
Environment
| component | version |
|---|---|
| verl | 0.9.0.dev (no git sha; upstream tree + the three local patches listed at the end) |
| megatron-core | 0.20.1 (PyPI cp312 wheel) |
| megatron-bridge | 0.7.1 |
| transformer-engine | 2.11.0 (source build, sm80) |
| flash-attn | 2.8.3 (source build) |
| torch / python | 2.10.0+cu128 / 3.12.14 |
| GPU | 8 × A100-80G, single node |
Corrected end-to-end comparison
Model: Qwen3.5-4B text (24 GDN + 8 full-attention layers, 40 layers total, vocab 248,320). Data: packed 8192-token blocks, every token contributes to the loss (plain LM objective). Batch: GBS 8, 2 optimizer steps, same seed. Only the CP degree changes.
Instrumentation at verl/workers/engine/megatron/transformer_impl.py::forward_backward_batch,
printed per rank after the DP all-reduce of batch_num_tokens:
# CP=1 (TP1 PP1 EP1 DP8)
rank=0 world=8 dp=8 cp=1 tp=1 pp=1 ep=1 local_mask_sum=8192.0 n_samples=1 bnt=65536
# CP=2 (TP1 PP1 EP1 CP2 DP4)
rank=0 world=8 dp=4 cp=2 tp=1 pp=1 ep=1 local_mask_sum=16384.0 n_samples=2 bnt=65536
Global valid tokens reaching gradient finalization: 65,536 in both runs. The loss normalizer is
loss = -sum(logp * mask) / batch_num_tokens * dp_size (verl/workers/utils/losses.py:49), a plain
global-token mean; nothing is rescaled by cp_size.
| loss step 1 | grad_norm step 1 | loss step 2 | grad_norm step 2 | |
|---|---|---|---|---|
| CP=1 (DP8) | 1.7579946517944336 | 2.4852783671994336 | 1.6584860086441040 | 2.7886624944304423 |
| CP=2 (CP2×DP4) | 1.7580680847167969 | 2.4877426067491997 | 1.6584659814834595 | 2.7922002842231026 |
| ratio | 1.000042 | 1.000992 | 0.999988 | 1.001269 |
Agreement is at the 1e-3 level on the gradient norm — the order expected from BF16 reduction-order differences across 40 layers, matching the maintainer's single-layer control (1.000012).
Why the original numbers were wrong
- The same harness reported a 232× grad_norm inflation on a variant with no GDN layers
(
tiny_qwen35_attn, pure full attention: 5.0675 → 1176.18). A GDN-specific cause cannot explain that, so the harness itself was producing the effect. - The tiny-model runs used randomly initialised weights (loss pinned at ≈ ln 248320 = 12.42) and
several of them had
loss_mask_sum = 0/batch_size = 0after an incorrect GBS/DP split. - The per-parameter ratio table came from a gradient dump that aggregated across PP/EP ranks; those numbers are not reproducible with the corrected harness.
What is still unverified
The 35B-A3B @ 32k run (TP2×PP2×EP2, optimizer_cpu_offload_fraction=0.5) showed a 1.9 % step-2
loss divergence. We cannot separate that from our PP/EP gradient-dump aggregation and the
CPU-offload path at the memory limit, so it is not evidence against GDN CP and is reported here
only for completeness.
Local patches in the tree
context_parallel_size>1+experimental_attention_variant=gdn→ fail fast (our own guard, being removed since the premise was wrong);- DCP only: skip the DP all-reduce of
batch_num_tokens, because withdynamic_context_parallel=Trueevery rank holds the whole batch and the all-reduce multiplies the denominator bydp_size(measured 11,792 → 47,168, making loss and grad_norm exactly 1/cp); model.custom_chat_templateaccepts a file path.
Only (2) touches loss normalization, and only for dynamic CP.
Remaining ask
The chunkwise-CP feature request in #7909 is unaffected by this correction and is still what a sequence-sharded trainer needs. We would close this issue; if you prefer to keep a record, the title above can stay as a pointer.
Correction (2026-11-24): The causal explanation below is superseded. GDN/GDN2 headwise CP takes sequence-sharded input and uses CP-to-HP all-to-all internally; callers must not replicate the full sequence on every CP rank. The reported CP=1 versus CP=2 gradient and loss measurements remain observations, but the experiment does not establish that sequence sharding caused them or that the GDN kernel is at fault. The supported input layout, loss normalization, sample identity/packing, and gradient reduction need to be checked in a controlled reproduction. #9735 addresses only the separate silent no-op when linear_cp_mode="chunkwise" is explicitly requested; it does not fix or explain the gradient mismatch. In the text below, statements that headwise CP requires replicated inputs, that its recurrence starts mid-sequence because of sequence sharding, and that switching to replicated inputs is a workaround should be treated as withdrawn.
Thanks for sharing the gradients as well as the loss. I ran a smaller control to see where the two topologies begin to differ.
On four A100s, we ran the same eight packed examples through one BF16 GDN layer with Transformer Engine 2.11 and FLA 0.5.1. We kept the initial weights, token IDs, labels and masks fixed and changed CP1/DP4 to CP2/DP2. Megatron's DDP and finalize_model_grads reduced the gradients in FP32. Both runs counted 1,053 tokens in the masked cross-entropy denominator. Megatron revision
- The gradient norm went from 0.019388647 to 0.019388883, a ratio of 1.000012. The full gradient vectors differed by 0.22% in relative L2.
- On the 0.20.1 PyPI release from your report, a separate two-A100 packed CP1/CP2 layer check gave a norm ratio of 1.000011. On main, deliberately taking a mean on each shard before summing gave 2.06 in the same two-rank example. The shards contained 97 and 163 valid loss tokens.
Our control ends after backward and gradient finalization. Your full run also includes MoE, Bridge, verl and the optimizer. Could you share the verl revision and the global valid-token counts that reached gradient finalization on step one for CP1 and CP2? That would help connect the layer control to the training run, especially given the earlier verl per-token normalization fix.
Reply to NVIDIA/Megatron-LM#9731
Thanks — your layer-level control is very helpful, and it made us re-run the end-to-end case properly. We can no longer reproduce the gradient inflation we reported, and we now believe the report was wrong: the discrepancy came from our own harness, not from GDN headwise CP. Details below, including the numbers you asked for.
Environment / revision
| component | version |
|---|---|
| verl | 0.9.0.dev (verl/version/version; the tree is not a git checkout, so no commit sha is available — it is upstream verl plus three local patches, listed at the end) |
| megatron-core | 0.20.1 (PyPI cp312 wheel) |
| megatron-bridge | 0.7.1 |
| transformer-engine | 2.11.0 (source build, sm80) |
| flash-attn | 2.8.3 (source build) |
| torch / python | 2.10.0+cu128 / 3.12.14 |
| GPUs | 8 × A100-80G, single node |
Step-1 token counts at gradient finalization (what you asked for)
Same model (Qwen3.5-4B text, 24 × GDN + 8 × full attention), same data (packed 8192-token blocks,
every token in the loss), same batch (GBS 8), same seed; only CP changes. Instrumented at
verl/workers/engine/megatron/transformer_impl.py:forward_backward_batch, printing per rank after
the DP all-reduce:
# CP=1, world 8 -> TP1 PP1 EP1 DP8
rank=0 world=8 dp=8 cp=1 tp=1 pp=1 ep=1 local_mask_sum=8192.0 n_samples=1 bnt=65536
# CP=2, world 8 -> TP1 PP1 EP1 CP2 DP4
rank=0 world=8 dp=4 cp=2 tp=1 pp=1 ep=1 local_mask_sum=16384.0 n_samples=2 bnt=65536
- global valid tokens reaching gradient finalization: 65,536 in both cases (8 × 8192).
- CP=1: 1 sample/rank × 8192 tokens, all-reduced over 8 DP ranks.
- CP=2: 2 samples/rank × 8192 tokens (the dataloader shards by DP only, so the CP peers hold the same samples), all-reduced over 4 DP ranks → same 65,536.
- Normalization used by the loss:
loss = -sum(logp * mask) / batch_num_tokens * dp_size(verl/workers/utils/losses.py:49), i.e. a plain global-token mean; no per-CP rescaling.
End-to-end result at that accounting
| loss step 1 | grad_norm step 1 | loss step 2 | grad_norm step 2 | |
|---|---|---|---|---|
| CP=1 (DP8) | 1.7579946517944336 | 2.4852783671994336 | 1.6584860086441040 | 2.7886624944304423 |
| CP=2 (CP2×DP4) | 1.7580680847167969 | 2.4877426067491997 | 1.6584659814834595 | 2.7922002842231026 |
| ratio | 1.000042 | 1.000992 | 0.999988 | 1.001269 |
So: agreement to 1e-3 on the gradient norm, 4e-5 relative on the loss — the same order as your 1.000012 layer control, and consistent with BF16 reduction-order noise accumulated over 40 layers.
Why our original report was wrong
Two pieces of evidence inside our own artifacts should have stopped us:
- The same harness produced a 232× grad_norm inflation on a variant with no GDN layers at all
(
tiny_qwen35_attn, pure full attention: 5.0675 → 1176.18). A GDN-headwise-CP cause cannot explain that. - The tiny-model runs used a randomly initialised model with loss at chance level (≈ ln 248320 =
12.42) and, in several of them,
loss_mask_sum=0/batch_size=0after a bad GBS/DP split. The "per-parameter ratio" table was produced by a gradient dump that aggregated across PP/EP ranks; we have since been unable to reproduce those numbers with the corrected harness.
We are therefore retracting the claim that GDN headwise CP silently corrupts gradients, and will retitle/update the issue accordingly. Sorry for the noise — your control is what closed it.
What we still cannot reproduce (and why we are not claiming it)
Our 35B-A3B @ 32k run (TP2×PP2×EP2, optimizer_cpu_offload_fraction=0.5) showed a 1.9 % step-2
loss divergence and the 300–600× per-parameter ratios. We can no longer separate that from our own
PP/EP gradient-dump aggregation and the CPU-offload path at the memory limit, so we treat it as
unresolved rather than as evidence against GDN CP.
Local patches in the tree (for completeness)
context_parallel_size>1+experimental_attention_variant=gdn→ fail fast (we are removing this now; it was our own guard, not upstream behaviour);- DCP only: skip the DP all-reduce of
batch_num_tokens(with DCP every rank holds the whole batch, so the all-reduce multiplies the denominator bydp_size; measured 11792 → 47168, i.e. loss and grad exactly 1/cp of the CP=1 run); model.custom_chat_templateaccepts a file path.
Only patch 2 touched loss normalization, and it applies to dynamic_context_parallel=True only,
which is not what this issue was about.
Happy to close this if you agree, or to keep it open only as a pointer for the chunkwise-CP feature request (#7909), which we still think is the useful follow-up.
Thanks for the careful rerun and the token accounting. The new end-to-end results line up with our layer control. I agree with closing the gradient-inflation report and keeping the chunkwise CP feature in #7909.