Skip to content

[FSDP2] Borrow frozen parameter storage on a single shard rank - #198468

Draft
weifengpy wants to merge 1 commit into
gh/weifengpy/196/basefrom
gh/weifengpy/196/head
Draft

weifengpy wants to merge 1 commit into
gh/weifengpy/196/basefrom
gh/weifengpy/196/head

Conversation

@weifengpy

@weifengpy weifengpy commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

User-requested draft prepared with Codex, pending human code review.

FSDP currently copies parameters into all-gather output storage even when the shard process group has size 1. For an inference parameter that already contains compute-ready weights, this duplicates the full weight and makes graph-preserving weight loading refresh a second allocation.

Let frozen parameters borrow persistent storage when no parameter cast or CPU offload is needed. Ordinary tensors support this directly. A tensor extension can implement fsdp_get_unsharded_view(mesh, module, mp_policy) to return a compute view backed entirely by its persistent storage, or return None to use the existing path. FSDP skips gather staging for borrowed views and never frees their storage on reshard. In-place state-dict loads retain the compute wrapper; replacement loads invalidate it.

This is the FSDP support for a DP=1 follow-up to pytorch/torchtitan#4861, where FP8 data and GEMM-ready scales can be shared by the storage DTensor and the compute parameter. The change does not depend on #198417. Multi-rank gathering, trainable parameters, mixed-precision casts, and CPU offload keep the existing allocation path.

Test Plan

CPU/CUDA view tests: 29 passed, 3 CUDA-only cases skipped on CPU. Tests cover ordinary and subclass parameters, meta initialization, both reshard policies, repeated state-dict updates, storage replacement, fallback cases, and CUDA graph replay after loading without explicit unshard. The existing two-rank extension training-parity test passed. Test discovery includes the new file.

CUDA_VISIBLE_DEVICES=4 python test/distributed/_composable/fsdp/test_fully_shard_unsharded_views.py -v
CUDA_VISIBLE_DEVICES=5,6 python test/distributed/_composable/fsdp/test_fully_shard_extensions.py TestFullyShardAllGatherExtensionsMultiProcess -v
python -m spin quicklint -- -a torch/distributed/fsdp/_fully_shard/_fsdp_param.py torch/distributed/fsdp/_fully_shard/_fsdp_param_group.py test/distributed/_composable/fsdp/test_fully_shard_unsharded_views.py

Runtime checks used the existing locally built PyTorch binary. This PR contains Python-only FSDP changes and is submitted independently on upstream main.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/198468

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (2 Unrelated Failures)

As of commit 52d6ebd with merge base 5613a88 (image):

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests release notes: distributed (sharded) release notes category labels Sep 24, 2026
weifengpy added a commit to pytorch/torchtitan 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
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant