• perf(trainer): make the FSDP reduce-scatter buffer cap configurable and default it to 1 (#3912)

    carsten released this 2026-10-13 15:32:56 +00:00

    #3638 set a reduce-scatter buffer cap of 2 on the fp32 MoE router's FSDP
    unit. Back then the experts were their own FSDP unit, so with one buffer
    the router's reduce-scatter had to wait for the experts' large
    reduce-scatter to finish, stalling backward. #3753 later moved the
    experts into the block's FSDP unit (for fullgraph compile). With that
    layout and full activation checkpointing, the expert reduce-scatter
    finishes inside the next layer's recompute, so the second buffer no
    longer buys anything. This PR makes the cap configurable
    (model.reduce_scatter_max_input_buffers), sets it once on the root
    FSDP module, and changes the default to 1 (torch's default).

    • Before: fp32-router runs had an effective cap of 2 (torch takes the
      max over all units), bf16-router runs had 1. Now every run uses the
      configured value, 1 by default.
    • A single root-level call is enough: torch keeps one reduce-scatter
      queue and resolves the cap once as the max over all FSDP units.
    • Cap 2 helps only when reduce-scatters are exposed, which in our runs
      needed short sequences without activation checkpointing (last rows and
      the second figure). It is free with the fp32 router but costs ~9 GiB/GPU
      on the bf16 path with ep=1. Cap 3 never beat 2.

    Why one buffer is enough now. The block and the fp32 router are
    still separate FSDP units with separate reduce-scatters. The cap is a
    single global count of reduce-scatters allowed in flight, and at cap 1
    each unit's hook must wait for the previous reduce-scatter to finish
    before launching its own. What #3753 changed is which reduce-scatter
    that is when the router's hook runs in layer L's backward:

    Before #3753 (experts are their own unit), cap 1, backward of layer L:
    
      compute   [ recompute L ][ MoE bwd L ][ router bwd ][ STALL ][ attn bwd L ]
      RS stream                            [====== experts L RS ======][ router L ]
                                           ^ launched just before the router hook,
                                             with no compute in between to hide it
    
    After #3753 (experts in the block's unit), cap 1, backward of layer L:
    
      compute   [ recompute L ][ MoE bwd L ][ router bwd ][ attn bwd L ]
      RS stream [== block L+1 RS ==]                     [ router L ]
                ^ launched at the end of layer L+1, so it has
                  all of layer L's recompute and MoE backward to finish
    

    Before #3753 that big reduce-scatter launched just before the router's
    hook, with no compute in between to hide it, and cap 2 was the fix. Now
    it is the previous layer's, launched a whole layer earlier, so the
    current layer's recompute and MoE backward run while it finishes. It
    only stalls when that compute is shorter than the reduce-scatter (the
    second figure).

    Median s/step and rank-0 peak GiB, GLM-4.5-Air truncated to 24 layers
    (16 for 8k without AC), 2x8 H200, fp32 router, untraced, lower is
    better. Spread is the gap between two same-code runs on different node
    pairs.

    | layout | tokens/GPU, AC | cap 1 (PR) | cap 2 (main) | cap 3 | spread |

    peak GiB, cap 1 / 2 / 3
    -----------------------
    ep=8 16k, full 4.697 4.650 4.693 0.068
    76.1
    ep=1 16k, full 4.535 4.521 4.543 0.041
    98.0
    ep=8 4k, full 1.238 1.234 1.237 0.002
    ep=1 4k, full 2.056 2.064 2.050 0.006
    ep=8 1k, none 0.848 0.841 0.826 0.011
    ep=1 8k, none 1.318 1.279 1.280 0.001
    111.4
    ep=1 4k, none 1.761 1.629 1.627 0.002
    118.3
    ep=1 1k, none 1.651 1.515 1.506 0.005

    In a whole-step trace (ep=8, 16k tokens/GPU, full AC, rank 0), neither
    main nor this PR has a compute-stream gap of 2 ms or more anywhere
    inside the model's backward. The only such gaps are at the start of
    forward, the start of backward, and the end of backward, where the last
    hooks and FSDP's final callback wait on the last reduce-scatters.

    Notation in the figures (RS = reduce-scatter, AG = all-gather):

    • E: the experts' gradient reduce-scatter, over the ranks that hold
      copies of the same experts.
    • D: the reduce-scatter of the block's dense parameters (attention,
      norms, shared experts).
    • R: the fp32 router's reduce-scatter (the router is its own FSDP
      unit).
    • D+E: the whole block's reduce-scatter, used when there is no EP and
      the block has one parameter group.

    per layer, production<br>
shape

    Figure: one layer of backward at a production shape (ep=8, 16k
    tokens/GPU, full activation checkpointing), before and after this PR.
    The expert reduce-scatter (orange, top row) finishes while the next
    layer is still recomputing, well before anything waits on it, so
    dropping to cap 1 adds no stall.

    per layer, exposed<br>
shape

    Figure: what failure looks like: the compute stream waiting on a
    reduce-scatter (red). It shows up at this odd shape we don't run in
    production (one node, 8 layers, ep=2, 2k tokens/GPU, no activation
    checkpointing), where each layer's expert reduce-scatter outlasts the
    compute that would hide it. Raising the cap to 2 (bottom) removes most
    of the wait and makes the step 6.5% faster (0.277 to 0.259 s) at no
    extra memory. We include it to show what to look for in traces and when
    to bump the cap.

    🤖 Generated with Claude Code


    Co-authored-by: Claude Opus 5.5 (1M context) noreply@anthropic.com

    Downloads