Skip to content

fix: use grad div factor when fsdp_degree=1 - #167178

Closed
garrett361 wants to merge 11 commits into
pytorch:mainfrom
garrett361:fully-shard-divide-fix
Closed

garrett361 wants to merge 11 commits into
pytorch:mainfrom
garrett361:fully-shard-divide-fix

Conversation

@garrett361

@garrett361 garrett361 commented Nov 6, 2025 •

Copy link
Copy Markdown
Contributor

fully_shard's gradient_divide_factor isn'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 and ep_degree=world_size we still wrap the routed experts in fully_shard because:

  1. It lets us take advantage of its mixed-precision mechanisms.
  2. A specific gradient_divide_factor is needed for correctness

This PR ensures correctness in the reduce_scatter_group.size()==1 case.

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

@pytorch-bot

pytorch-bot Bot commented Nov 6, 2025 •

Copy link
Copy Markdown

🔗 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 (image):

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.

@pytorch-bot pytorch-bot Bot added oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (fsdp) release notes category labels Nov 6, 2025
@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 6, 2025 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@tianyu-l

tianyu-l commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

Thanks a lot! Sounds right to me. I'll let @weifengpy review / stamp.

Yes, a test would be appreciated.

@garrett361
garrett361 force-pushed the fully-shard-divide-fix branch from 7078973 to 0490875 Compare November 6, 2025 03:45
@weifengpy

Copy link
Copy Markdown
Contributor

good catch!

@weifengpy

Copy link
Copy Markdown
Contributor

triggering CI - waiting for signals

@garrett361

Copy link
Copy Markdown
Contributor Author

Thanks @weifengpy ! Figuring out CLA stuff with work, then will add a test.

@garrett361

Copy link
Copy Markdown
Contributor Author

@weifengpy still waiting on CLA stuff, but started looking at where I'd add a test. I'm not seeing gradient_divide_factor tested anywhere in the code base. Am I missing a test somewhere? Closest I can find is in TestFullyShardCollectiveOps where it's mentioned but left as None.

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.

@weifengpy

Copy link
Copy Markdown
Contributor

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

@garrett361
garrett361 force-pushed the fully-shard-divide-fix branch from 337f84f to 8862e11 Compare November 11, 2025 15:26
@garrett361

Copy link
Copy Markdown
Contributor Author

I found some other issues in the code and tests related to this topic:

  1. There is a current edge case where if the user calls model.set_gradient_divide_factor(factor) and factor happens to equal the data parallel size, then a (I believe) unintended code path is taken: we end up dividing grads by factor and then averaging over the allreduce group, rather than summing. From the code below:

    if not overflow_risk and not force_sum_reduction_for_comms:
    if factor == data_parallel_size:
    # Warning: NCCL ReduceOp.AVG may produce incorrect results with
    # world size 1.
    if data_parallel_size == 1:
    return None, None, ReduceOp.SUM, ReduceOp.SUM
    return None, None, ReduceOp.AVG, ReduceOp.AVG
    else:
    reduce_scatter_op = torch.distributed._make_nccl_premul_sum(1 / factor)
    return None, None, reduce_scatter_op, ReduceOp.SUM
    if factor != data_parallel_size then we end up at the final line and return None, None, reduce_scatter_op, ReduceOp.SUM. But if we happen to set factor = data_parallel_size, then we enter the first if statement and return None, None, ReduceOp.AVG, ReduceOp.AVG, assuming data_parallel_size > 1. This is a change in semantics due to swapping the final ReduceOp.SUM return value for a ReduceOp.AVG in the latter case. I believe we should always want ReduceOp.SUM if a custom division factor is provided and changed the code to reflect that, but let me know if that is not desired.

  2. I found the relevant _test_set_reduce_scatter_divide_factor test, and as written it wasn't sensitive enough to catch some of these issues. I updated this PR to increase the sensitivity of this test and cover more cases. This is how I caught the above edge case.

  3. There's also the test _test_set_reduce_scatter_divide_factor_mixed_prevision, but this doesn't seem to actually test anything about custom division factors because we apply the division factor only to the outermost fully_shard wrapped module (the whole model) which doesn't seem to own any parameters itself, since it's a Sequential:

    model = nn.Sequential(*[MLP(16) for _ in range(3)])
    ref_model = copy.deepcopy(model).to(device_type)
    ref_model_bf16 = copy.deepcopy(ref_model).to(param_dtype)
    ref_optim = torch.optim.AdamW(ref_model.parameters(), lr=1e-2)
    for mlp in model:
    fully_shard(mlp, mp_policy=mp_policy)
    model = fully_shard(model, mp_policy=mp_policy)
    optim = torch.optim.AdamW(model.parameters(), lr=1e-2)
    model.set_reduce_scatter_divide_factor(divide_factor)
    Sound right? I didn't touch this test yet, but can if desired. Trying to keep the PR small.

CC @anshul-si @weifengpy @tianyu-l

Tested locally that everything in test/distributed/_composable/fsdp/test_fully_shard_comm.py passes.

@weifengpy

Copy link
Copy Markdown
Contributor

@garrett361 thanks for the detailed unit tests

@garrett361

Copy link
Copy Markdown
Contributor Author

Thanks @weifengpy , do you want me to make any changes to _test_set_reduce_scatter_divide_factor_mixed_prevision as well?

@garrett361

Copy link
Copy Markdown
Contributor Author

Hi @weifengpy , checking in on this. Anything else you need from my end? Thanks!

@weifengpy

Copy link
Copy Markdown
Contributor

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

@weifengpy

Copy link
Copy Markdown
Contributor

@pytorchmergebot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 19, 2025
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

shuhuayu added a commit to shuhuayu/pytorch that referenced this pull request Nov 6, 2026
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.
shuhuayu added a commit to shuhuayu/pytorch that referenced this pull request Nov 7, 2026
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.
shuhuayu added a commit to shuhuayu/pytorch that referenced this pull request Nov 7, 2026
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.
shuhuayu added a commit to shuhuayu/pytorch that referenced this pull request Nov 9, 2026
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.
shuhuayu added a commit to shuhuayu/pytorch that referenced this pull request Nov 10, 2026
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.
pytorchmergebot pushed a commit that referenced this pull request Nov 13, 2026
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
taras-janea pushed a commit to taras-janea/pytorch that referenced this pull request Nov 13, 2026
)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged oncall: distributed Add this issue/PR to distributed oncall triage queue open source release notes: distributed (fsdp) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants