Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

Add GDN chunkwise CP with recurrent boundary-state propagation

Open ggboooy opened this issue 1 month ago • 3 comments

Is your feature request related to a problem? Please describe.

The current GDN path partitions heads across TP and CP. The existing chunkwise state-summary infrastructure is available for other SSM/linear-attention paths, but GDN cannot use it directly. This limits sequence sharding when the number of heads is small and retains projected-feature all-to-all traffic.

A second concern is numerical parity: native GDN and a single-update GDP formulation are mathematically equivalent, but their BF16 execution can differ. Affine state-summary composition also does not reproduce every rounding step of native recurrence. In a Qwen3.5-35B-A3B prototype, these differences changed MoE routing and token argmax results. Very short CP shards additionally cut through the native 64-token blocks.

Describe the solution you'd like

Add opt-in GDN chunkwise CP while keeping headwise CP as the default:

  • Reuse the shared GDP CP backend with one update for a parallel-summary mode, including packed-sequence metadata and convolution halos.
  • Provide an optional recurrent-boundary mode using native FLA GDN kernels. Preserve global 64-token block boundaries and exchange actual boundary states and backward adjoints between adjacent ranks.
  • Keep GPT layout conversion at the GDN boundary and reuse HybridStack's existing layout ownership.
  • Reject unsupported combinations explicitly. The recurrent mode initially supports unpacked training only.

Boundary state passing has an ordered dependency; local WY preparation, output formation, and remaining gradient computations stay distributed. This is not a claim that the boundary recurrence itself is fully parallel.

Describe alternatives you've considered

  • Retain headwise CP: established behavior, but projected-feature communication and TP-times-CP head partitioning remain.
  • Use only parallel affine summaries: better concurrency, but the tested BF16 path did not preserve headwise argmax results.
  • Relax the argmax criterion: does not address the observed routing differences or short-shard block boundaries.

Additional context

A concrete implementation and real-NCCL tests are prepared for a draft PR. Local experiments used a pretrained 40-layer Qwen3.5-35B-A3B text model, BF16, TP1/PP2/CP4/EP4, and four microbatches of size one on an 8-GPU SM90 node.

The recurrent mode matched headwise next-token argmax at all 147,360 tested positions over lengths 32 through 32768, including unaligned shard boundaries. At 32K, median forward + backward + gradient synchronization was 5825.016 ms for headwise and 5391.698 ms for recurrent CP (1.080x); optimizer updates were excluded. These are bounded local observations, not a universal bitwise guarantee. Whole-model gradients still exhibit measured numerical/replay variation.

The focused suite passes 54 tests on four GPUs and 52 tests with two expected skips on two GPUs. The implementation uses existing FLA kernels rather than introducing new GPU math kernels.

Assisted-by: OpenAI Codex

ggboooy avatar Oct 06 '26 12:10 ggboooy

Data point from a sequence-parallel trainer (verl + mcore 0.20.1, Qwen3.5-35B-A3B, 8×A100), relevant to the "header CP partitions heads across TP and CP" part: with the current headwise-only path there is no error at all when the trainer shards the sequence across CP ranks — the loss is unchanged (12.63023 vs 12.63025) but the gradients are inflated by 300-1500x with output_layer unaffected, i.e. a silent correctness hazard rather than a crash. Filed with measurements, per-parameter breakdown and a 40 s CPU/GPU repro: #9731.

For the chunkwise mode, the two blockers we hit on 0.20.1 were (a) models/hybrid/hybrid_block.py attaches packed_sequence_cp_metadata only when type(layer_config) is MambaLayerConfig, and GDN layers are GDNLayerConfig; (b) gdn.py asserts that metadata is None. Both are touched by this proposal, so +1 from us.

bqgao avatar Nov 24 '26 03:11 bqgao

Correction to a data point we posted here earlier (we referenced #9731 claiming GDN CP=2 inflated gradients 9x-1500x). That claim was wrong — it came from a broken harness on our side (randomly-initialised tiny model, empty loss masks, bad GBS/DP split). With real weights and real data (Qwen3.5-4B text, 24 GDN + 8 full-attention layers, packed 8k blocks, GBS 8, 2 steps):

CP=1 (DP8): dp=8 cp=1 local_mask_sum=8192  n_samples=1 bnt=65536  grad_norm=2.4852783671994336
CP=2 (CP2xDP4): dp=4 cp=2 local_mask_sum=16384 n_samples=2 bnt=65536  grad_norm=2.4877426067491997

ratio 1.000992 on the gradient norm, loss differs by 4e-5 — i.e. equivalent, consistent with the 1.000012 layer-level control reported in #9731.

The chunkwise-CP request in this issue is unaffected: a sequence-sharded trainer still needs chunkwise CP for GDN; headwise CP works but requires every rank to hold the full sequence, which costs the sequence sharding we are after. Sorry for the earlier noise.

bqgao avatar Nov 27 '26 08:11 bqgao

While chunkwise GDN CP is unimplemented, #9735 makes linear_cp_mode="chunkwise" fail loudly at construction for GDN/GDN2 instead of being silently ignored in favour of headwise. When this issue is implemented, that guard needs to be lifted for the new path — flagging it here so the two do not conflict.

YYuJLiu avatar Nov 27 '26 08:11 YYuJLiu