Repository navigation
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:
Findings (5): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/203693
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit e92b3ba with merge base 780bf8e ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
856db01 to
548296f
Compare
There was a problem hiding this comment.
Automated review of 548296f: nothing blocking; ready for human review.
The PR fixes FSDP2's singleton reduce path (reduce-scatter group of size 1), which divided gradients by gradient_divide_factor a second time when the factor was also applied outside the reduce op (fp16 reduce dtype, force_sum_reduction_for_comms, or MTIA). Recommendation: Approve. Every remaining point is non-blocking.
⚪ 5 non-blocking findings
Non-blocking: the fix depends on a return shape nobody documents. It decides "the factor was folded into the op" from predivide_factor is None and postdivide_factor is None. That holds only because the fp32/bf16 non-forced branch of _get_gradient_divide_factors (:934-945) is the only one that returns (None, None, ...). If that branch ever returns post_factor=1.0 or a pre-factor, the singleton path silently stops applying the factor. Fix: have _get_gradient_divide_factors return the factor it folded into the op and use that here. At minimum, add a comment at :934-945 saying the singleton branch relies on this shape.
Non-blocking: two comments are inaccurate. (1) "For single GPU": the branch runs whenever the reduce-scatter group is None or has size 1, which includes HSDP with shard size 1 and replicate() on N GPUs. (2) Line 650 here and test_fully_shard_comm.py:2840-2841 say the factor is "split into pre/post(-divide) factors". For fp32 forced-sum, :961 returns pre=None, post=factor, so nothing is split. "Applied outside the reduce op" would be accurate.
Non-blocking: no test covers replicate(), which this fix also changes. replicate() builds a DDPMeshInfo (replicate_with_fsdp.py:141), and _fsdp_param_group.py:804-809 then passes reduce_scatter_group=None, so foreach_reduce always takes the singleton branch with the replicate group as all_reduce_group. ReplicateModule inherits both set_gradient_divide_factor and set_force_sum_reduction_for_comms. Before this PR, replicate() with fp16 reduce or forced sum plus a divide factor k divided gradients by k on any number of GPUs. test_singleton_split_divide_factor only runs fully_shard on meshes (1,) and (1,1). A replicate() parametrization would fit in the same world_size == 1 class.
Non-blocking: the release note should spell out the behavior change and its full scope. Users who tuned learning rate, clipping or loss scaling around the old values will now see gradients k times larger. The description says the bug "affects plain FSDP and HSDP with shard size one". It also affects replicate() on any number of GPUs, HSDP with shard size 1 and any replicate size, and every MTIA run that sets a divide factor, because MTIA always forces sum (_fsdp_collectives.py:920-921). The note should list these configurations.
Non-blocking: the PR description is unfinished. "Linked issue or supporting maintainer" and "Summary (human written only)" still say TODO. "Updated documentation" and "Included benchmark results" are ticked but don't apply to this diff; consider unticking them.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
| # apply a gradient_divide_factor that the skipped reduce op would have applied. | ||
| # A factor split into pre/post-divide factors is applied outside the op. | ||
| divide_in_op = predivide_factor is None and postdivide_factor is None | ||
| if ( |
There was a problem hiding this comment.
stack this PR on top of #203556
Compute divide_in_op once in the skipped-reduce-scatter branch and use it for both the copy path and the reuse path.
e490d12 to
c20e26f
Compare
| # A skipped reduce-scatter must still apply a divide factor folded into | ||
| # its op, but not one applied outside the op as pre/post-divide factors. | ||
| divide_in_op = predivide_factor is None and postdivide_factor is None | ||
| if skip_reduce_scatter and all_reduce_group is None: |
There was a problem hiding this comment.
skip_reduce_scatter appears twice in if-else and is hard for human to follow
from ai review
'''
Two points on the singleton branch in foreach_reduce, one readability and one correctness:
-
skip_reduce_scatteris tested twice in the same if/elif chain, so the reader has to re-evaluate the same predicate. Please test it once and split the sub-cases onall_reduce_group, which is the actual difference between them. -
The two sub-cases divide inconsistently: the reuse path divides by the full
gradient_divide_factor, the copy path open-codes its ownis not Nonecheck. The branch only owes the share the collective op would have carried — the full factor when both pre/post factors are None, and nothing otherwise. Dividing unconditionally reintroduces the [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 double division for fp16/fp64 and force_sum, where pre/post already multiply to the full factor. Compute that share once asdivide_in_opand use it for both paths:No collective runs to fold the division into, so the branch owes
exactly what pre/post division does not already cover.
divide_in_op = (
gradient_divide_factor
if predivide_factor is None and postdivide_factor is None
else None
)with device_handle.stream(reduce_scatter_stream):
div_if_needed(reduce_scatter_input, predivide_factor)
if skip_reduce_scatter:
if all_reduce_group is None:
# The packed input already is the reduction output
reduce_output = reduce_scatter_input
else:
# HSDP/replicate keeps a separate output, allocated in the RS
# stream, since its all-reduce and partial accumulation
# outlive the input
reduce_output = torch.empty_like(reduce_scatter_input)
reduce_output.copy(reduce_scatter_input)
_div_if_needed(reduce_output, divide_in_op)
else:
reduce_output = reduce_scatter_comm.allocate(
(reduce_scatter_output_numel,),
dtype=reduce_dtype,
device=device,
)
reduce_scatter_comm(
output_tensor=reduce_output,
input_tensor=reduce_scatter_input,
group=reduce_scatter_group,
op=reduce_scatter_op,
)
reduce_scatter_event = reduce_scatter_stream.record_event()
'''
There was a problem hiding this comment.
you are right, applied and updated.
c20e26f to
f5f51e0
Compare
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.
f5f51e0 to
e92b3ba
Compare
|
@pytorchbot 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 |
) 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
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.spin fixlint)Linked issue or supporting maintainer
supported by @weifengpy
BC-breaking?
No
Agent details (if applicable)