Repository navigation
fix: use grad div factor when fsdp_degree=1 - #167178
garrett361 wants to merge 11 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/167178
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit a88ea4f with merge base dc00842 ( UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Thanks a lot! Sounds right to me. I'll let @weifengpy review / stamp. Yes, a test would be appreciated. |
7078973 to
0490875
Compare
|
good catch! |
|
triggering CI - waiting for signals |
|
Thanks @weifengpy ! Figuring out CLA stuff with work, then will add a test. |
|
@weifengpy still waiting on CLA stuff, but started looking at where I'd add a test. I'm not seeing Should I add a test here? Seems like I'd have to add a decent bit of infra code to get a proper test set up. |
adding a unit test would be great! I was mentioning CI to make sure it does not break shard world size 2+, but having unit test on 1 is better cc @anshul-si for a bug fix in world size 1 |
337f84f to
8862e11
Compare
|
I found some other issues in the code and tests related to this topic:
CC @anshul-si @weifengpy @tianyu-l Tested locally that everything in |
|
@garrett361 thanks for the detailed unit tests |
|
Thanks @weifengpy , do you want me to make any changes to |
|
Hi @weifengpy , checking in on this. Anything else you need from my end? Thanks! |
@garrett361 just took another look and it's safe. we can land once CI passes. no need to cover _test_set_reduce_scatter_divide_factor_mixed_prevision |
|
@pytorchmergebot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
With a shard group of size one, foreach_reduce skips the reduce-scatter and copies the packed input to the output, dividing by gradient_divide_factor to account for the division the skipped reduce op would have applied (pytorch#167178). That only holds when _get_gradient_divide_factors folds the factor into the reduce op (ReduceOp.PREMUL_SUM or AVG for fp32/bf16). For fp16 reduce dtypes, force_sum_reduction_for_comms, and MTIA, it instead splits the factor into pre/post-divide factors that _div_if_needed applies around the collective, and the singleton branch then divided by the full factor again. For example, a factor of 4 divided gradients by 16. This affects plain FSDP and HSDP with a shard size of one. Only apply the factor in the singleton branch when both pre- and post-divide factors are None, i.e. when the skipped reduce op would have carried it. Test Plan: Run against the 2.16.0.dev20261030+cu130 nightly with this commit's torch/ files overlaid. The new test fails without the fix (gradient 3/16 instead of 3/4) for fp16 reduction and forced sum reduction, with and without HSDP. ```bash python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardReduceOpWorldSize1 python test/distributed/_composable/fsdp/test_fully_shard_comm.py lintrunner -a ``` Authored with assistance from an AI coding assistant.
Stacked on pytorch#203556. With a shard group of size one, foreach_reduce skips the reduce-scatter and applies gradient_divide_factor itself to account for the division the skipped reduce op would have applied (pytorch#167178). After pytorch#203556 this happens in two places: the reused-input path for plain FSDP and the copy path for HSDP/replicate. That only holds when _get_gradient_divide_factors folds the factor into the reduce op (ReduceOp.PREMUL_SUM or AVG for fp32/bf16). For fp16 reduce dtypes, force_sum_reduction_for_comms, and MTIA, it instead applies the factor outside the op as pre/post-divide factors via _div_if_needed, and the singleton path then divided by the full factor again. For example, a factor of 4 divided gradients by 16. This affects any reduce-scatter group of size one, including HSDP with a shard size of one and replicate(). Compute once whether the skipped op would have carried the factor (both pre- and post-divide factors are None) and apply the factor in either singleton path only in that case. Test Plan: Run against the 2.16.0.dev20261030+cu130 nightly with this stack's torch/ files overlaid. The new test fails on pytorch#203556 alone (gradient 3/16 instead of 3/4) for fp16 reduction and forced sum reduction, covering the reused-input path (plain FSDP) and the copy path (HSDP). ```bash python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardReduceOpWorldSize1 python test/distributed/_composable/fsdp/test_fully_shard_comm.py lintrunner -a ``` Authored with assistance from an AI coding assistant.
Stacked on pytorch#203556. With a shard group of size one, foreach_reduce skips the reduce-scatter and applies gradient_divide_factor itself to account for the division the skipped reduce op would have applied (pytorch#167178). After pytorch#203556 this happens in two places: the reused-input path for plain FSDP and the copy path for HSDP/replicate. That only holds when _get_gradient_divide_factors folds the factor into the reduce op (ReduceOp.PREMUL_SUM or AVG for fp32/bf16). For fp16 reduce dtypes, force_sum_reduction_for_comms, and MTIA, it instead applies the factor outside the op as pre/post-divide factors via _div_if_needed, and the singleton path then divided by the full factor again. For example, a factor of 4 divided gradients by 16. This affects any reduce-scatter group of size one, including HSDP with a shard size of one and replicate(). Compute once whether the skipped op would have carried the factor (both pre- and post-divide factors are None) and apply the factor in either singleton path only in that case. Test Plan: Run against the 2.16.0.dev20261030+cu130 nightly with this stack's torch/ files overlaid. The new test fails on pytorch#203556 alone (gradient 3/16 instead of 3/4) for fp16 reduction and forced sum reduction, covering the reused-input path (plain FSDP) and the copy path (HSDP). ```bash python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardReduceOpWorldSize1 python test/distributed/_composable/fsdp/test_fully_shard_comm.py lintrunner -a ``` Authored with assistance from an AI coding assistant.
Stacked on pytorch#203556. With a shard group of size one, foreach_reduce skips the reduce-scatter and applies gradient_divide_factor itself to account for the division the skipped reduce op would have applied (pytorch#167178). After pytorch#203556 this happens in two places: the reused-input path for plain FSDP and the copy path for HSDP/replicate. That only holds when _get_gradient_divide_factors folds the factor into the reduce op (ReduceOp.PREMUL_SUM or AVG for fp32/bf16). For fp16 reduce dtypes, force_sum_reduction_for_comms, and MTIA, it instead applies the factor outside the op as pre/post-divide factors via _div_if_needed, and the singleton path then divided by the full factor again. For example, a factor of 4 divided gradients by 16. This affects any reduce-scatter group of size one, including HSDP with a shard size of one and replicate(). Compute once whether the skipped op would have carried the factor (both pre- and post-divide factors are None) and apply the factor in either singleton path only in that case. Test Plan: Run against the 2.16.0.dev20261030+cu130 nightly with this stack's torch/ files overlaid. The new test fails on pytorch#203556 alone (gradient 3/16 instead of 3/4) for fp16 reduction and forced sum reduction, covering the reused-input path (plain FSDP) and the copy path (HSDP). ```bash python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardReduceOpWorldSize1 python test/distributed/_composable/fsdp/test_fully_shard_comm.py lintrunner -a ``` Authored with assistance from an AI coding assistant.
With a shard group of size one, foreach_reduce skips the reduce-scatter and applies gradient_divide_factor itself to account for the division the skipped reduce op would have applied (pytorch#167178). After pytorch#203556 this happens in two places: the reused-input path for plain FSDP and the copy path for HSDP/replicate. That only holds when _get_gradient_divide_factors folds the factor into the reduce op (ReduceOp.PREMUL_SUM or AVG for fp32/bf16). For fp16 reduce dtypes, force_sum_reduction_for_comms, and MTIA, it instead applies the factor outside the op as pre/post-divide factors via _div_if_needed, and the singleton path then divided by the full factor again. For example, a factor of 4 divided gradients by 16. This affects any reduce-scatter group of size one, including HSDP with a shard size of one and replicate(). Apply the division once for both singleton paths, after reusing or copying the input, and only with a factor the skipped op would have carried (both pre- and post-divide factors are None). The copy path now divides in place instead of allocating a temporary. Test Plan: Run against the 2.16.0.dev20261030+cu130 nightly with this commit's torch/ files overlaid. The new test fails without this fix (gradient 3/16 instead of 3/4) for fp16 reduction and forced sum reduction, covering the reused-input path (plain FSDP) and the copy path (HSDP). ```bash python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardReduceOpWorldSize1 python test/distributed/_composable/fsdp/test_fully_shard_comm.py lintrunner -a ``` Authored with assistance from an AI coding assistant.
human notes: while working on the singleton reduce path for kimi k3's unreplicated expert groups, i found that fsdp2 divides gradients a second time when the divide factor is split into pre/post factors (fp16 reduce dtype or `force_sum_reduction_for_comms`), so gradients come out k times too small. - [x] Passes lint (`spin fixlint`) - [x] Added/updated tests - [x] Updated documentation (if applicable) - [x] Included benchmark results (for PRs impacting perf) ## Linked issue or supporting maintainer supported by @weifengpy ## BC-breaking? No ## Agent details (if applicable) > Drafted with an AI coding assistant and reviewed by the author. > > With a shard group of size one, `foreach_reduce` skips the reduce-scatter and divides by `gradient_divide_factor` to stand in for the skipped reduce op (#167178). That is only correct when `_get_gradient_divide_factors` folds the factor into the op (`PREMUL_SUM`/`AVG` for fp32/bf16). For fp16 reduce dtypes, `force_sum_reduction_for_comms`, and MTIA, the factor is instead split into pre/post-divide factors applied around the collective, so the singleton branch divided by it a second time: a factor of 4 divided gradients by 16. This affects plain FSDP and HSDP with shard size one. > > The fix applies the factor in the singleton branch only when both pre- and post-divide factors are `None`. > > Test plan: `test_singleton_split_divide_factor` covers fp16 reduction and forced sum reduction, with and without HSDP; it fails without the fix (3/16 instead of 3/4). > ``` > python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardReduceOpWorldSize1 > ``` > Tested against the `2.16.0.dev20261030+cu130` nightly with this PR's changed `torch/` files overlaid. This touches the same singleton branch as #203556, so whichever lands second needs a small rebase. Pull Request resolved: #203693 Approved by: https://github.com/weifengpy
) human notes: while working on the singleton reduce path for kimi k3's unreplicated expert groups, i found that fsdp2 divides gradients a second time when the divide factor is split into pre/post factors (fp16 reduce dtype or `force_sum_reduction_for_comms`), so gradients come out k times too small. - [x] Passes lint (`spin fixlint`) - [x] Added/updated tests - [x] Updated documentation (if applicable) - [x] Included benchmark results (for PRs impacting perf) ## Linked issue or supporting maintainer supported by @weifengpy ## BC-breaking? No ## Agent details (if applicable) > Drafted with an AI coding assistant and reviewed by the author. > > With a shard group of size one, `foreach_reduce` skips the reduce-scatter and divides by `gradient_divide_factor` to stand in for the skipped reduce op (pytorch#167178). That is only correct when `_get_gradient_divide_factors` folds the factor into the op (`PREMUL_SUM`/`AVG` for fp32/bf16). For fp16 reduce dtypes, `force_sum_reduction_for_comms`, and MTIA, the factor is instead split into pre/post-divide factors applied around the collective, so the singleton branch divided by it a second time: a factor of 4 divided gradients by 16. This affects plain FSDP and HSDP with shard size one. > > The fix applies the factor in the singleton branch only when both pre- and post-divide factors are `None`. > > Test plan: `test_singleton_split_divide_factor` covers fp16 reduction and forced sum reduction, with and without HSDP; it fails without the fix (3/16 instead of 3/4). > ``` > python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardReduceOpWorldSize1 > ``` > Tested against the `2.16.0.dev20261030+cu130` nightly with this PR's changed `torch/` files overlaid. This touches the same singleton branch as pytorch#203556, so whichever lands second needs a small rebase. Pull Request resolved: pytorch#203693 Approved by: https://github.com/weifengpy
fully_shard'sgradient_divide_factorisn't currently respected when the sharding degree = 1. This PR ensures the division factor applies also in this case.This is a bit of an edge case, but it arises in
torchtitan, e.g. with expert parallelism andep_degree=world_sizewe still wrap the routed experts infully_shardbecause:This PR ensures correctness in the
reduce_scatter_group.size()==1case.Reproducer and sample failures are in the gist here. The net effect is that the EP grads are too-large by a factor of the world size in the case described above. I checked that the proposed fix makes these tests pass.
I guess I should add a test for this, too?
cc @H-Huang @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @pragupta @msaroufim @dcci