Skip to content

[Fix] Chunk stacked weights in tilewise FP8 scale precompute - #2337

Merged
ShilohYu merged 5 commits into
mainfrom
fix/fp8-tilewise-scale-chunked-stack
Nov 25, 2026
Merged

ShilohYu merged 5 commits into
mainfrom
fix/fp8-tilewise-scale-chunked-stack

Conversation

@ShilohYu

@ShilohYu ShilohYu commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

precompute_tilewise_float8_scale_for_fsdp stacked every same-shape weight into one tensor to compute per-block scales in a single shot, so the temporary stack peak grew linearly with the shape group. Observed on Qwen3.5-35B-A3B RL training (MoE-only FP8): a 10 GiB torch.stack spike OOMed the critic step when colocated with other memory consumers.

Fix

Per-block amax reduces within each 128x128 block and across reduce_mesh only, never across the stacked dim, so processing the group in chunks bounded by a stack budget is bitwise identical to the single-shot stack. Chunk boundaries derive from deterministic traversal order and padded shapes, so every rank chunks identically; the only cost is extra reduce_mesh collectives (one per chunk).

  • Add max_stack_bytes to precompute_tilewise_float8_scale_for_fsdp and thread it through Float8Config -> Float8Handler, so training configs can lower the budget for tighter colocation. A set value processes each shape group in budget-bounded chunks; None disables chunking and restores the original single-stack behavior. The default stays at the 1 GiB budget.
  • The stack -> roundtrip -> scale kernel -> write-back body is shared by the single-shot and chunked paths via _stack_and_precompute_tilewise_scales.
  • Estimate the per-element transient peak from the actual weight dtype and the RL bf16 roundtrip instead of a flat 4 bytes/elem (bf16 source: 6, fp32 source: 10), consolidated in _rl_bf16_roundtrip / _rl_bf16_roundtrip_peak_bytes so the estimate and the implementation stay next to each other.

Tests

9 CPU/gloo tests cover:

  • chunked vs single-shot bitwise equality: dout>=128, interleaved mixed shape groups, and with XTUNER_RL_FP8_QUANTIZE_IN_BF16=1;
  • max_stack_bytes=None disabling chunking (single kernel call per shape group) while matching the chunked result bitwise;
  • multi-chunk splitting via kernel call counts;
  • a hand-computed block-amax numeric reference;
  • cross-rank chunk-boundary consistency on both collective paths (dout<128 reduce and dout%128==64 divided_64) with 2 gloo ranks — mismatched boundaries would deadlock the all_reduce;
  • max_stack_bytes passthrough from Float8Config through Float8Handler to the precompute call (default 1 GiB, explicit budget, explicit None);
  • a probe pinning the torch semantics the peak factor relies on (.bfloat16() is a no-copy self-return for bf16 sources).

Context

Hit during RL training with a colocated judge service on the same node; the fix removes the transient peak so colocation fits in memory.

precompute_tilewise_float8_scale_for_fsdp stacked every same-shape
weight into one tensor to compute per-block scales in a single shot,
so the stack peak grew linearly with the shape group (observed: a
10 GiB spike OOMing 35B-A3B RL training when colocated with other
memory consumers).

Per-block amax reduces within each 128x128 block and across
reduce_mesh only, never across the stacked dim, so processing the
group in chunks bounded by a 1 GiB stack budget is bitwise identical.
Chunk boundaries derive from deterministic traversal order and padded
shapes, so every rank chunks identically; the only cost is extra
reduce_mesh collectives (one per chunk).

Add max_stack_bytes to precompute_tilewise_float8_scale_for_fsdp and
CPU/gloo regression tests covering chunk invariance, mixed shape
groups, and block-amax correctness.
@ShilohYu
ShilohYu force-pushed the fix/fp8-tilewise-scale-chunked-stack branch from 93e7550 to d53b951 Compare November 24, 2026 07:49
- Thread max_stack_bytes through Float8Config -> Float8Handler ->
  precompute_tilewise_float8_scale_for_fsdp (default unchanged, 1 GiB).
- Add 2-rank gloo tests covering the all_reduce (dout<128) and
  divided_64 (dout%128==64) paths: chunked vs single-shot bitwise
  equality, which also exercises cross-rank chunk-boundary consistency
  (mismatched boundaries would deadlock the collectives).
- Clarify the budget comment: the bf16 roundtrip transiently holds up
  to ~2.5x the estimated bytes.
@ShilohYu
ShilohYu force-pushed the fix/fp8-tilewise-scale-chunked-stack branch from 0b14c9b to 2a539c2 Compare November 24, 2026 08:36
The flat numel*4 estimate undercharges the realistic RL case (bf16
weights with XTUNER_RL_FP8_QUANTIZE_IN_BF16=1 peaks at 6 bytes/elem,
fp32 at 10). Derive the per-element peak from the actual weight dtype
and the roundtrip helper, and consolidate the roundtrip itself into
_rl_bf16_roundtrip (also used by fsdp_pre_all_gather) so the estimate
and the implementation live next to each other.

Add a probe test pinning the torch semantics the factor relies on
(.bfloat16() is a no-copy self-return for bf16 sources) and a chunked
vs single-shot equality test under the env enabled.
@ShilohYu
ShilohYu force-pushed the fix/fp8-tilewise-scale-chunked-stack branch from 2a539c2 to 0718b55 Compare November 24, 2026 08:57
… the stack path

- Extract the stack -> roundtrip -> scale kernel -> write-back body into
  _stack_and_precompute_tilewise_scales, reused by both the single-shot
  and the chunked paths.
- max_stack_bytes=None now disables chunking (one stack per shape group,
  the pre-chunking behavior); a set value enables chunked processing.
  The default stays at the 1 GiB budget.
@ShilohYu
ShilohYu merged commit d0688b8 into main Nov 25, 2026
7 of 8 checks passed
@ShilohYu
ShilohYu deleted the fix/fp8-tilewise-scale-chunked-stack branch November 25, 2026 03:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants