Skip to content

[rl] Store generator MXFP8 weights as FP8 data and scales - #4861

Closed
weifengpy wants to merge 2 commits into
gh/weifengpy/76/basefrom
gh/weifengpy/76/head
Closed

weifengpy wants to merge 2 commits into
gh/weifengpy/76/basefrom
gh/weifengpy/76/head

Conversation

@weifengpy

@weifengpy weifengpy commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Generator MXFP8 linears retain BF16 parameter shards during decoding and quantize after FSDP gathers them. Store E4M3 weight data and E8M0 scales instead, and gather those bytes into the existing MXFP8 compute buffers.

BF16 initialization / checkpoint / TorchStore update
  -> quantize each complete local 32x32 weight tile
  -> persistent FP8 data + E8M0 scale shards
  -> FSDP all-gather of a packed data + scale byte payload
  -> MXFP8 compute buffers, refilled at stable addresses

The generator replaces existing MXFP8Linear configs with an inference subclass. It installs quantized storage after TP sharding and before fully_shard. Training continues to use the existing MXFP8Linear. This covers ordinary and stacked projections; grouped experts keep their existing implementation.

State dicts expose temporary BF16 DTensors with the generator's mesh and placements, so checkpoint loading and TorchStore keep the existing BF16 transfer contract. Loading quantizes those receive buffers into the FP8 shards; the generator releases the buffers before unsharding. Data and scales share one flat byte payload per parameter, which also supports FSDP's degree-1 fast path. The existing forward kernels and quantization recipe are reused. Repeated updates refill the same FSDP compute tensors so captured CUDA graphs see the new weights.

Each converted persistent shard uses 33/32 bytes per weight: one E4M3 byte plus a row-major E8M0 scale per 32 columns. Scales repeat across the 32 rows of a quantization tile to preserve FSDP slicing. This is 48.4% less persistent shard storage than BF16, excluding unchanged compute buffers and temporary update buffers. Each TP/FSDP shard must contain complete 32x32 tiles; unsupported layouts fail explicitly.

This stack uses an independent copy of #4838. It does not update the original PR or its branch.

Draft prerequisites

  • Requires [fsdp2] Optionally keep unsharded storage across reshard pytorch#198417 for reshard(free_unsharded=False, force=True), as the base PR does.
  • Multi-rank FSDP with stacked projections also exposes an extension-layout issue: the default nonzero-dimension reassembly assumes every gathered tensor has the full parameter's shape. The packed data and scale payload has a different size and layout. Local validation uses the candidate fix below, which leaves extension-owned layouts to their post-all-gather hook. This needs upstream review before merging; the patch is not part of the TorchTitan runtime changes.
--- a/torch/distributed/fsdp/_fully_shard/_fsdp_collectives.py
+++ b/torch/distributed/fsdp/_fully_shard/_fsdp_collectives.py
@@ -481,7 +481,11 @@
         )
         fsdp_param.alloc_all_gather_outputs()
         param_all_gather_outputs = fsdp_param.all_gather_outputs
-        if fsdp_param.fsdp_placement.dim != 0:
+        # Tensor extensions own the layout of their all-gather inputs/outputs,
+        # which may include smaller tensors such as quantization scales.
+        if fsdp_param.fsdp_placement.dim != 0 and not hasattr(
+            fsdp_param._sharded_local_tensor, "fsdp_post_all_gather"
+        ):
             # Copy to a temporary and then chunk-cat into the final all-gather
             # output tensors
             param_all_gather_outputs = [

Validation

  • pre-commit run --all-files: passed, including type checking.
  • pytest tests/unit_tests/cpu/test_mxfp8_inference.py tests/unit_tests/cpu/test_quantized_fsdp_tensor.py tests/unit_tests/cpu/test_quantization.py -q: 61 passed.
  • pytest tests/unit_tests/gpu/test_mxfp8_inference.py -q: 5 passed, 2 skipped. Two/four-GPU storage, FSDP degree 1/2 and TP placement, mixed FP8/BF16 parameters, initial graph capture, and three weight updates. Tests compare gathered data/scales byte-for-byte and require stable compute addresses and exact graph replay outputs.
  • GPU transport checks use a reference quantizer and BF16 matmul on H100. Real TorchAO MXFP8/GEMM parity tests are included but require two SM100 GPUs; they are skipped locally. No full vLLM/RL training run was performed.
  • Test environment: PyTorch 2.15.0a0+git0757f05, TorchAO e9ad12a, and an isolated Python overlay containing [fsdp2] Optionally keep unsharded storage across reshard pytorch#198417 plus the extension-layout candidate above. The shared PyTorch checkout was not modified.

[ghstack-poisoned]
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 24, 2026
Generator MXFP8 linears use FSDP degree 1, but retaining separate parameter and compute weights wastes memory and complicates CUDA graph weight updates. Store the existing quantizer's FP8 data and GEMM-ready scales as the parameter's persistent buffers, and let FSDP expose a compute view of those same allocations.

```
meta model -> TP -> FP8 storage -> fully_shard -> allocate/init

TorchStore BF16 receive DTensor
  -> existing MXFP8 quantizer
  -> update persistent FP8 data + GEMM-ready scales in place
  -> captured compute reads the same buffers
```

`_MXFP8StorageTensor` reuses `_quantize_mxfp8_weight`, `_MXFP8LinearOperands`, and `_UnshardedFSDPTensor`. There is one persistent storage subclass and one FP8 data allocation per converted weight. Loading BF16 weights preserves the addresses of the data and both existing scale layouts. No FSDP all-gather, packed byte payload, or scale-layout conversion is needed during unshard.

The inference variant is installed after TP and before `fully_shard`, with logical BF16 parameter metadata. State dicts expose temporary BF16 DTensors with the generator's mesh and placements, preserving checkpoint and TorchStore loading. It supports ordinary and stacked projections, requires complete TP-local 32x32 tiles, and explicitly requires FSDP degree 1. Training and grouped-expert implementations are unchanged.

Requires pytorch/pytorch#198468 for FSDP's borrowed-storage path and `fsdp_get_unsharded_view`. Generator parameters are frozen so ordinary BF16 parameters can share storage too. The generic generator prepare/finish callbacks remain for other formats with separate compute buffers and still require pytorch/pytorch#198417. Direct MXFP8 weight-loading tests exercise #198468 independently of #198417.

Validation:

- CPU quantization/storage suite: 61 passed.
- GPU storage and CUDA graph checks: 4 passed; 4 native MXFP8 cases skipped because this machine has H100 GPUs, not SM100.
- `pre-commit run --all-files`: passed, including type checking.
- GPU checks use real DTensor/FSDP with TP=1/2 and a reference quantizer plus BF16 matmul on H100. They verify exact data/scales, shared and stable addresses, and exact replay outputs after three BF16 loads without explicit reshard/unshard. Native TorchAO MXFP8/GEMM cases require SM100 GPUs; full vLLM/RL validation is not included.
- The GPU checks use the independent Python FSDP changes from #198468 on the existing built torch, without the earlier multi-rank extension-layout candidate. The CPU and multi-GPU test files are selected by the repository's existing CI workflows once that dependency is available.

Prepared with Codex as a user-requested draft. Based on the independent #4860 copy of #4838; the original #4838 is untouched.

ghstack-source-id: f959f33
Pull-Request: #4861
@tianyu-l tianyu-l closed this Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants