Skip to content

[c10d] FakeProcessGroup: match NCCL's version-counter effect on collective outputs (#201606) - #201606

Closed
stanley-shi wants to merge 1 commit into
pytorch:mainfrom
stanley-shi:export-D126530628
Closed

stanley-shi wants to merge 1 commit into
pytorch:mainfrom
stanley-shi:export-D126530628

Conversation

@stanley-shi

@stanley-shi stanley-shi commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary:
FakeProcessGroup's collectives fill caller-owned outputs with copy_() under at::AutoDispatchBelowAutograd. That guard skips the Autograd key but not ADInplaceOrView, so every write bumps the output's version counter: all_gather_into_tensor into a fresh tensor goes from version 0 to world_size. On NCCL it stays at 0, because NCCL writes from its own kernels.

This breaks any tensor that autograd saved for backward and a collective then refills. TorchRec's fully sharded 2D embedding (ShardedBatchedFusedEmbeddingBag) reduce-scatters a TBE's weights_dev after forward and all-gathers it back into the same tensor before backward, so under FakePG the TBE backward fails with modified by an inplace operation: ... is at version 2; expected version 0. Every Ads 2D-sparse training run under FakePG dies at its first backward.

The fix matches ProcessGroupNCCL collective by collective (NOTE [FakeProcessGroup output version counters]):

  • Outputs NCCL writes from its kernels (all_gather_into_tensor and its coalesced form, the reduce-scatters, the all-to-alls, the all-reduces) now use AutoDispatchBelowADInplaceOrView and leave the counter alone.
  • Outputs NCCL fills with copy_ (each slot of list all_gather, the root's scatter output, the root's own gather slot) keep AutoDispatchBelowAutograd and still bump. Peers' gather slots arrive by ncclRecv and no longer bump.
  • allgather_coalesced is unchanged; ProcessGroupNCCL does not implement it.

Agent details (AI-generated)

The diagnosis, code change and tests were drafted by an AI coding agent (Claude) working with the author. The per-collective behavior was read from ProcessGroupNCCL.cpp and torch/csrc/cuda/nccl.cpp, and checked against real NCCL on 2 GPUs during development. Per review, the tests were trimmed to one version-counter check per behavior class plus the backward regression.

Test Plan:

buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex 'TestFakePGVersionCounter|TestFakePGUniformRanks|TestFakePG\.'

Pass 43, Fail 0: https://www.internalfb.com/intern/testinfra/testrun/20829148338057493

New TestFakePGVersionCounter (single process, CPU):

  • test_output_versions_match_nccl: all_gather_into_tensor, reduce_scatter_tensor and all_to_all_single leave the output at version 0; list all_gather bumps each slot to 1; gather at the root bumps only its own slot ([1, 0, 0, 0]).
  • test_tensor_saved_for_backward_survives_all_gather_into_it: the 2D-sparse regression; fails on the original header with "modified by an inplace operation".
buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex TestFakePGVersionCounter

https://www.internalfb.com/intern/testinfra/testrun/28710447673116028

The previous revision also verified every collective's version counter against real NCCL on 2 GPUs (TestFakePGNCCLVersionParity, since removed per review): the per-collective table above matched at both ranks.

Reviewed By: ezyang

Differential Revision: D126530628

Pulled By: stanley-shi

@pytorch-bot

pytorch-bot Bot commented Oct 21, 2026

Copy link
Copy Markdown

This appears to be a diff that was exported from phabricator, but the PR author does not have sufficient permissions to run CI. @stanley-shi, please do step 2 of internal wiki to get write access so you do not need to get CI approvals in the future. If you think this is a mistake, please contact the Pytorch Dev Infra team.

@pytorch-bot pytorch-bot Bot added the release notes: distributed (c10d) release notes category label Oct 21, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - Ready for human review:

The PR changes `FakeProcessGroup` so a collective's output version counter moves
the way it does on ProcessGroupNCCL: outputs NCCL writes directly stay at
version 0, while outputs filled via `copy_` (list `all_gather` slots, the root's
own `scatter`/`gather` slot) still get bumped. The review found no issues.
Recommendation: Approve.

Reviewed commit: f3f3103

Review run

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/201606

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit f3f3103 with merge base 289f49d (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: stanley-shi / name: Stanley Shi (354a3fe)

@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown

@stanley-shi has exported this pull request. If you are a Meta employee, you can view the originating Diff in D126530628.

@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 21, 2026
@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown

@stanley-shi has imported this pull request. If you are a Meta employee, you can view this in D126530628.

github-actions[bot]

This comment was marked as outdated.

@stanley-shi

Copy link
Copy Markdown
Contributor Author

/easycla

@ezyang ezyang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 23, 2026
@meta-codesync meta-codesync Bot changed the title [c10d] FakeProcessGroup: leave output version counters alone, as real backends do [c10d] FakeProcessGroup: match NCCL's version-counter effect on collective outputs (#201606) Oct 26, 2026
stanley-shi added a commit to stanley-shi/pytorch that referenced this pull request Oct 26, 2026
…ctive outputs (pytorch#201606)

Summary:
`FakeProcessGroup`'s collectives fill caller-owned outputs with `copy_()` under `at::AutoDispatchBelowAutograd`. That guard skips the Autograd key but not ADInplaceOrView, so every write bumps the output's version counter: `all_gather_into_tensor` into a fresh tensor goes from version 0 to `world_size`. On NCCL it stays at 0, because NCCL writes from its own kernels.

This breaks any tensor that autograd saved for backward and a collective then refills. TorchRec's fully sharded 2D embedding (`ShardedBatchedFusedEmbeddingBag`) reduce-scatters a TBE's `weights_dev` after forward and all-gathers it back into the same tensor before backward, so under FakePG the TBE backward fails with `modified by an inplace operation: ... is at version 2; expected version 0`. Every Ads 2D-sparse training run under FakePG dies at its first backward.

The fix matches ProcessGroupNCCL collective by collective (NOTE [FakeProcessGroup output version counters]):
- Outputs NCCL writes from its kernels (`all_gather_into_tensor` and its coalesced form, the reduce-scatters, the all-to-alls, the all-reduces) now use `AutoDispatchBelowADInplaceOrView` and leave the counter alone.
- Outputs NCCL fills with `copy_` (each slot of list `all_gather`, the root's `scatter` output, the root's own `gather` slot) keep `AutoDispatchBelowAutograd` and still bump. Peers' `gather` slots arrive by `ncclRecv` and no longer bump.
- `allgather_coalesced` is unchanged; ProcessGroupNCCL does not implement it.

## Agent details (AI-generated)
The diagnosis, code change and tests were drafted by an AI coding agent (Claude) working with the author. The per-collective behavior was read from `ProcessGroupNCCL.cpp` and `torch/csrc/cuda/nccl.cpp`, and checked against real NCCL on 2 GPUs during development. Per review, the tests were trimmed to one version-counter check per behavior class plus the backward regression.


Test Plan:
```
buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex 'TestFakePGVersionCounter|TestFakePGUniformRanks|TestFakePG\.'
```
Pass 43, Fail 0: https://www.internalfb.com/intern/testinfra/testrun/20829148338057493

New `TestFakePGVersionCounter` (single process, CPU):
- `test_output_versions_match_nccl`: `all_gather_into_tensor`, `reduce_scatter_tensor` and `all_to_all_single` leave the output at version 0; list `all_gather` bumps each slot to 1; `gather` at the root bumps only its own slot (`[1, 0, 0, 0]`).
- `test_tensor_saved_for_backward_survives_all_gather_into_it`: the 2D-sparse regression; fails on the original header with "modified by an inplace operation".

```
buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex TestFakePGVersionCounter
```
https://www.internalfb.com/intern/testinfra/testrun/28710447673116028

The previous revision also verified every collective's version counter against real NCCL on 2 GPUs (`TestFakePGNCCLVersionParity`, since removed per review): the per-collective table above matched at both ranks.

Reviewed By: ezyang

Differential Revision: D126530628

Pulled By: stanley-shi
github-actions[bot]

This comment was marked as outdated.

…ctive outputs (pytorch#201606)

Summary:
`FakeProcessGroup`'s collectives fill caller-owned outputs with `copy_()` under `at::AutoDispatchBelowAutograd`. That guard skips the Autograd key but not ADInplaceOrView, so every write bumps the output's version counter: `all_gather_into_tensor` into a fresh tensor goes from version 0 to `world_size`. On NCCL it stays at 0, because NCCL writes from its own kernels.

This breaks any tensor that autograd saved for backward and a collective then refills. TorchRec's fully sharded 2D embedding (`ShardedBatchedFusedEmbeddingBag`) reduce-scatters a TBE's `weights_dev` after forward and all-gathers it back into the same tensor before backward, so under FakePG the TBE backward fails with `modified by an inplace operation: ... is at version 2; expected version 0`. Every Ads 2D-sparse training run under FakePG dies at its first backward.

The fix matches ProcessGroupNCCL collective by collective (NOTE [FakeProcessGroup output version counters]):
- Outputs NCCL writes from its kernels (`all_gather_into_tensor` and its coalesced form, the reduce-scatters, the all-to-alls, the all-reduces) now use `AutoDispatchBelowADInplaceOrView` and leave the counter alone.
- Outputs NCCL fills with `copy_` (each slot of list `all_gather`, the root's `scatter` output, the root's own `gather` slot) keep `AutoDispatchBelowAutograd` and still bump. Peers' `gather` slots arrive by `ncclRecv` and no longer bump.
- `allgather_coalesced` is unchanged; ProcessGroupNCCL does not implement it.

## Agent details (AI-generated)
The diagnosis, code change and tests were drafted by an AI coding agent (Claude) working with the author. The per-collective behavior was read from `ProcessGroupNCCL.cpp` and `torch/csrc/cuda/nccl.cpp`, and checked against real NCCL on 2 GPUs during development. Per review, the tests were trimmed to one version-counter check per behavior class plus the backward regression.


Test Plan:
```
buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex 'TestFakePGVersionCounter|TestFakePGUniformRanks|TestFakePG\.'
```
Pass 43, Fail 0: https://www.internalfb.com/intern/testinfra/testrun/20829148338057493

New `TestFakePGVersionCounter` (single process, CPU):
- `test_output_versions_match_nccl`: `all_gather_into_tensor`, `reduce_scatter_tensor` and `all_to_all_single` leave the output at version 0; list `all_gather` bumps each slot to 1; `gather` at the root bumps only its own slot (`[1, 0, 0, 0]`).
- `test_tensor_saved_for_backward_survives_all_gather_into_it`: the 2D-sparse regression; fails on the original header with "modified by an inplace operation".

```
buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex TestFakePGVersionCounter
```
https://www.internalfb.com/intern/testinfra/testrun/28710447673116028

The previous revision also verified every collective's version counter against real NCCL on 2 GPUs (`TestFakePGNCCLVersionParity`, since removed per review): the per-collective table above matched at both ranks.

Reviewed By: ezyang

Differential Revision: D126530628

Pulled By: stanley-shi

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of f3f3103: nothing blocking; ready for human review.

The PR changes FakeProcessGroup so a collective's output version counter moves the way it does on ProcessGroupNCCL: outputs NCCL writes directly stay at version 0, while outputs filled via copy_ (list all_gather slots, the root's own scatter/gather slot) still get bumped. The review found no issues. Recommendation: Approve.

This review is AI-generated and advisory. Each new automated review replaces the previous one.

@github-actions
github-actions Bot dismissed stale reviews from themself October 26, 2026 19:29

Withdrawn: the automated review of f3f3103 found nothing blocking.

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Oct 26, 2026
@bobrenjc93

Copy link
Copy Markdown
Contributor

@pytorchbot merge

@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

d4l3k added a commit that referenced this pull request Oct 27, 2026
…oc; fake registration fallback

[ghstack-poisoned]
d4l3k added a commit that referenced this pull request Oct 27, 2026
…oc; fake registration fallback

[ghstack-poisoned]
d4l3k added a commit that referenced this pull request Oct 27, 2026
…oc; fake registration fallback

[ghstack-poisoned]
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…ctive outputs (pytorch#201606) (pytorch#201606)

Summary:
`FakeProcessGroup`'s collectives fill caller-owned outputs with `copy_()` under `at::AutoDispatchBelowAutograd`. That guard skips the Autograd key but not ADInplaceOrView, so every write bumps the output's version counter: `all_gather_into_tensor` into a fresh tensor goes from version 0 to `world_size`. On NCCL it stays at 0, because NCCL writes from its own kernels.

This breaks any tensor that autograd saved for backward and a collective then refills. TorchRec's fully sharded 2D embedding (`ShardedBatchedFusedEmbeddingBag`) reduce-scatters a TBE's `weights_dev` after forward and all-gathers it back into the same tensor before backward, so under FakePG the TBE backward fails with `modified by an inplace operation: ... is at version 2; expected version 0`. Every Ads 2D-sparse training run under FakePG dies at its first backward.

The fix matches ProcessGroupNCCL collective by collective (NOTE [FakeProcessGroup output version counters]):
- Outputs NCCL writes from its kernels (`all_gather_into_tensor` and its coalesced form, the reduce-scatters, the all-to-alls, the all-reduces) now use `AutoDispatchBelowADInplaceOrView` and leave the counter alone.
- Outputs NCCL fills with `copy_` (each slot of list `all_gather`, the root's `scatter` output, the root's own `gather` slot) keep `AutoDispatchBelowAutograd` and still bump. Peers' `gather` slots arrive by `ncclRecv` and no longer bump.
- `allgather_coalesced` is unchanged; ProcessGroupNCCL does not implement it.

## Agent details (AI-generated)
The diagnosis, code change and tests were drafted by an AI coding agent (Claude) working with the author. The per-collective behavior was read from `ProcessGroupNCCL.cpp` and `torch/csrc/cuda/nccl.cpp`, and checked against real NCCL on 2 GPUs during development. Per review, the tests were trimmed to one version-counter check per behavior class plus the backward regression.

Test Plan:
```
buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex 'TestFakePGVersionCounter|TestFakePGUniformRanks|TestFakePG\.'
```
Pass 43, Fail 0: https://www.internalfb.com/intern/testinfra/testrun/20829148338057493

New `TestFakePGVersionCounter` (single process, CPU):
- `test_output_versions_match_nccl`: `all_gather_into_tensor`, `reduce_scatter_tensor` and `all_to_all_single` leave the output at version 0; list `all_gather` bumps each slot to 1; `gather` at the root bumps only its own slot (`[1, 0, 0, 0]`).
- `test_tensor_saved_for_backward_survives_all_gather_into_it`: the 2D-sparse regression; fails on the original header with "modified by an inplace operation".

```
buck2 test fbcode//mode/opt fbcode//caffe2/test/distributed:test_fake_pg -- --regex TestFakePGVersionCounter
```
https://www.internalfb.com/intern/testinfra/testrun/28710447673116028

The previous revision also verified every collective's version counter against real NCCL on 2 GPUs (`TestFakePGNCCLVersionParity`, since removed per review): the per-collective table above matched at both ranks.

Reviewed By: ezyang

Differential Revision: D126530628

Pulled By: stanley-shi

Pull Request resolved: pytorch#201606
Approved by: https://github.com/ezyang, https://github.com/d4l3k, https://github.com/kapilsh
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 meta-exported ready for review Automated review passed, ready for a final review by a maintainer. release notes: distributed (c10d) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants