Skip to content

[FSDP2] set_gradient_divide_factor(f) divides by f twice when the reduce-scatter has one rank and the reduction forces sums or uses fp16/fp64 #202170

Closed

Description

@Robertboy18

Hi! I was checking FSDP2 runs against one device with a distributed-training checker we're building on TorchLean (a Lean 4 library for neural networks; the checker replays a captured run on exact rationals and compares it with a one-device reference in Lean), and found that the gradient is divided by f**2 instead of f when three things meet: set_gradient_divide_factor(f), a reduce-scatter group of size one (one process, HSDP with shards of one rank, or replicate() from replicate_with_fsdp), and either set_force_sum_reduction_for_comms(True) or a reduce_dtype other than fp32/bf16 (fp16 or fp64). This is on 2.13.0, and main (ffd1d24) has the same code.

If I read foreach_reduce right: _get_gradient_divide_factors returns pre/post divide factors whose product is f in those configurations (pre=None, post=f for forced sums; pre * post = f for fp16/fp64), and foreach_reduce applies both (_div_if_needed(reduce_scatter_input, predivide_factor) and later _div_if_needed(reduce_output, postdivide_factor)). The world-size-one branch then also copies reduce_scatter_input / gradient_divide_factor, which was added in #167178 for the fp32 path, where the pre/post factors are None. So the factor is applied once by the copy and once more by pre/post.

A repro on one process, with no communication at all:

import torch, torch.nn as nn, torch.distributed as dist
from torch.testing._internal.distributed.fake_pg import FakeStore
from torch.distributed.device_mesh import init_device_mesh
from torch.distributed.fsdp import fully_shard, MixedPrecisionPolicy

dist.init_process_group("fake", rank=0, world_size=1, store=FakeStore())
c = torch.tensor([1.0, 2.0])
for label, kwargs, force in (("fp32", {}, False), ("fp32 force-sum", {}, True),
                             ("fp16 reduce", {"mp_policy": MixedPrecisionPolicy(reduce_dtype=torch.float16)}, False),
                             ("fp64 reduce", {"mp_policy": MixedPrecisionPolicy(reduce_dtype=torch.float64)}, False)):
    m = nn.Linear(1, 2, bias=False)
    with torch.no_grad():
        m.weight.zero_()
    fully_shard(m, mesh=init_device_mesh("cpu", (1,)), **kwargs)
    m.set_gradient_divide_factor(2.0)
    if force:
        m.set_force_sum_reduction_for_comms(True)
    (m(torch.ones(1, 1)).reshape(-1) * c).sum().backward()
    print(label, m.weight.grad.to_local().reshape(-1).tolist())
fp32 [0.5, 1.0]
fp32 force-sum [0.25, 0.5]
fp16 reduce [0.25, 0.5]
fp64 reduce [0.25, 0.5]

I also saw it with 2 processes on a (2, 1) HSDP mesh and with replicate() on 2 processes, f = 2, forced sums or fp16 reduce: one SGD step of rate 1 from zero gives -(c_0 + c_1) / 4 where the requested divisor gives -(c_0 + c_1) / 2; a (1, 2) mesh (a real reduce-scatter) and the fp32 path without forced sums give the right step. Those runs used the checker's FileStore-backed process group, not gloo or NCCL, and the checker compared the steps with one device on exact rationals.

If that's the intended reading of set_gradient_divide_factor, one fix would be to keep the division in the copy branch only when both predivide_factor and postdivide_factor are None (the fp32/bf16 path #167178 fixed), and otherwise copy without dividing, since pre/post already carry f. #195732 is about the same line (the temporary the division allocates), so a fix for one could cover both. Happy to send a PR with a test that runs foreach_reduce at world size one for each of the four configurations :)

I used Opus 5.5 and GPT-6 Astra to help write and review this.

cc @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @pragupta @msaroufim @dcci @aditvenk @weifengpy @kapilsh @zhaojuanmao @mrshenli @rohan-varma @chauhang @mori360 @ppwwyyxx

Activity

  1. added
    oncall: distributed
    bot-triaged
    oncall: distributed parallelisms
    triaged
    on Oct 27, 2026
  2. weifengpy commented on Nov 7, 2026

    @weifengpy
    Contributor

    thanks for reporing this! i see 2 PRs fixing this. will land one of them

  3. weifengpy commented on Nov 7, 2026

    @weifengpy
    Contributor

    TorchLean is cool, btw

  4. Robertboy18 commented on Nov 15, 2026

    @Robertboy18
    ContributorAuthor

    Thanks @weifengpy! #203693 landed the fix, so I'll close this. And glad you like TorchLean :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bot-triagedmodule: fsdponcall: distributedoncall: distributed parallelismstriage reviewtriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions