Repository navigation
[rl] Enable FSDP on the generator - #4838
Conversation
|
|
691cfb6 to
d5d3035
Compare
**Summary:** This commit adds two flags to `FSDPModule.reshard`: - `free_unsharded`: Whether to free the unsharded storage, useful for cudagraphs which requires unsharded addresses to remain the same. Defaults to True. - `force`: Whether to always trigger a reshard, regardless of RAF setting (previously no-op if RAF=false). This is useful for inference where RAF=false. Defaults to False. Enables pytorch/torchtitan#4838. **Test Plan:** ``` python test/distributed/_composable/fsdp/test_fully_shard_training.py \ TestFullyShardRegisteredParams.test_force_reshard_with_raf_disabled_in_forward ```
**Summary:** This commit adds two flags to `FSDPModule.reshard`:
```
free_unsharded (default True):
Whether to free the unsharded storage, useful for cudagraphs
which requires unsharded addresses to remain the same.
force (default False):
Whether to always trigger a reshard, regardless of RAF setting.
Previously, reshard is a no-op if RAF=false, which is always
the case for inference.
```
Enables pytorch/torchtitan#4838.
**Test Plan:**
```
python test/distributed/_composable/fsdp/test_fully_shard_training.py \
TestFullyShardRegisteredParams.test_force_reshard_with_raf_disabled_in_forward
```
**Summary:** This commit adds two flags to `FSDPModule.reshard`:
```
free_unsharded (default True):
Whether to free the unsharded storage, useful for cudagraphs
which requires unsharded addresses to remain the same.
force (default False):
Whether to always trigger a reshard, regardless of RAF setting.
Previously, reshard is a no-op if RAF=false, which is always
the case for inference.
```
Enables pytorch/torchtitan#4838
and preserves existing behavior.
**Test Plan:**
```
python test/distributed/_composable/fsdp/test_fully_shard_training.py \
TestFullyShardRegisteredParams.test_force_reshard_with_raf_disabled_in_forward
```
**Summary:** This commit adds two flags to `FSDPModule.reshard`:
```
free_unsharded (default True):
Whether to free the unsharded storage, useful for cudagraphs
which requires unsharded addresses to remain the same.
force (default False):
Whether to always trigger a reshard, regardless of RAF setting.
Previously, reshard is a no-op if RAF=false, which is always
the case for inference.
```
Enables pytorch/torchtitan#4838
and preserves existing behavior.
**Test Plan:**
```
python test/distributed/_composable/fsdp/test_fully_shard_training.py \
TestFullyShardRegisteredParams.test_force_reshard_with_raf_disabled_in_forward
```
d5d3035 to
ee86dca
Compare
556755b to
d2bd774
Compare
**Summary:** Add a recursive setter that controls whether resharding frees the unsharded parameter storage. This preserves storage addresses without changing the existing reshard API. ```python model = fully_shard(model) model.set_keep_unsharded_storage(True) model.unshard() model.reshard() # Unsharded storage stays resident in memory. ``` Enables pytorch/torchtitan#4838 and preserves existing behavior. **Test Plan:** ``` python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py \ -k keep_unsharded_storage python test/distributed/_composable/fsdp/test_fully_shard_training.py \ -k keep_unsharded_storage_with_post_forward_reshard ``` Pull Request resolved: #198417 Approved by: https://github.com/weifengpy
5489c8b to
e8487e7
Compare
e8487e7 to
98c31e4
Compare
|
@claude could you review the change in vllm_wrapper.py, where |
|
Claude finished @tianyu-l's task in 3m 18s —— View job Review: removing
|
98c31e4 to
6957c35
Compare
|
@claude review the latest changes, is the vllm scale buffer spmd_types annotation issue resolved? See this comment: #4838 (comment) |
6957c35 to
6995bf2
Compare
f5ab7ee to
61d3c41
Compare
| local_sinks = sinks._local_tensor if isinstance(sinks, DTensor) else sinks | ||
| module.inner_attention.vllm_attn.impl.out_transform = partial( | ||
| apply_attention_sink_rescale, sinks=local_sinks | ||
| _apply_attention_sink_rescale, module |
There was a problem hiding this comment.
@tianyu-l latest fix for integration test, tldr is sinks here could be the sharded params which may be freed at this point, so we populate sinks dynamically instead
dfa1246 to
3c76ab8
Compare
**Summary:** Enable FSDP on the generator for quantization, which reuses the FSDP tensor lifecycle mechanism introduced in pytorch#4203 but for inference. We use fsdp=1 to avoid an actual all-gather for now. New lifecycle: ``` bf16 sharded params (_LinearShardedTensorWithMXFP8Compute) | ^ | fetch from TorchStore (new weight sync) | v | updated bf16 sharded params | | | | unshard | v | all-gathered mxfp8 params (_UnshardedFSDPTensor) | | | | | | forward (many decode steps) | | | ---------------------------------------------- reshard ``` Code cleanups enabled by this change: - Removed `skip_dp` flag, since we never skip_dp anymore - Removed spmd <-> DTensor glue code during weight sync, since model state dict always contains DTensors now **Design considerations:** 1. **Cudagraph compatibility:** FSDP frees the unsharded storage today on reshard. However, cudagraph requires the unsharded address be stable across weight syncs. This is enabled through `model.set_keep_unsharded_storage(True)`, added in pytorch/pytorch#198417. 2. **Avoid extra memory during decode:** FSDP never frees the sharded storage today because it's supposed to be the source of truth. However, during decode, we don't actually need the bf16 copy anymore, since forward only needs the mxfp8 operands. At fsdp=1, this incurs an extra bf16 copy of the weights per layer during decode. We work around this by calling private FSDP APIs to free this storage after unshard. Memory lifecycle: ``` 1. prepare_for_state_dict_load: re-allocate previously freed sharded buffers | | 1 bf16 copy + 1 mxfp8 copy v 2. model.state_dict() hook: reshards model (but keep unsharded buffers) | | 1 bf16 copy + 1 mxfp8 copy v 3. ts.get_state_dict: fetch new weights into sharded buffers | | 1 bf16 copy + 1 mxfp8 copy v 4. prepare_for_forward: free sharded buffers | | 1 mxfp8 copy v (prefill/decode, then repeat step 1) ``` Note: In step (2), there is currently a memory spike since FSDP allocates separate buffers for unsharded and sharded buffers, and we need to keep both resident in memory during weight sync. E.g. for bf16 generator and fsdp=1, this currently uses 2x the model memory. This may be fine since it's not the peak memory, but we should revisit in the future.
3c76ab8 to
d09dac2
Compare
) **Summary:** Add a recursive setter that controls whether resharding frees the unsharded parameter storage. This preserves storage addresses without changing the existing reshard API. ```python model = fully_shard(model) model.set_keep_unsharded_storage(True) model.unshard() model.reshard() # Unsharded storage stays resident in memory. ``` Enables pytorch/torchtitan#4838 and preserves existing behavior. **Test Plan:** ``` python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py \ -k keep_unsharded_storage python test/distributed/_composable/fsdp/test_fully_shard_training.py \ -k keep_unsharded_storage_with_post_forward_reshard ``` Pull Request resolved: pytorch#198417 Approved by: https://github.com/weifengpy
Summary: Enable FSDP on the generator for quantization,
which reuses the FSDP tensor lifecycle mechanism introduced in
#4203 but for inference.
We use fsdp=1 to avoid an actual all-gather for now.
New lifecycle:
Code cleanups enabled by this change:
skip_dpflag, since we never skip_dp anymoresince model state dict always contains DTensors now
Design considerations:
Cudagraph compatibility: FSDP frees the unsharded storage
today on reshard. However, cudagraph requires the unsharded
address be stable across weight syncs. This is enabled through
model.set_keep_unsharded_storage(True), added in[fsdp2] Optionally keep unsharded storage across reshard pytorch#198417.
Avoid extra memory during decode: FSDP never frees the
sharded storage today because it's supposed to be the source of
truth. However, during decode, we don't actually need the bf16
copy anymore, since forward only needs the mxfp8 operands.
At fsdp=1, this incurs an extra bf16 copy of the weights per
layer during decode. We work around this by calling private
FSDP APIs to free this storage after unshard.
Memory lifecycle:
Testing: For DAPO math Qwen3-4B, the reward was consistent with before this PR and matches the README reference, with MXFP8 decode 27% faster than before this PR.