Skip to content

[pipelining] Preconnect both directions of shared-group P2P pairs - #204061

Closed
albmalamd wants to merge 1 commit into
pytorch:mainfrom
ROCm:albmalamd/pp-preconnect-both-directions
Closed

albmalamd wants to merge 1 commit into
pytorch:mainfrom
ROCm:albmalamd/pp-preconnect-both-directions

Conversation

@albmalamd

Copy link
Copy Markdown
Contributor

Linked issue or supporting maintainer

Supported by @jeffdaily

BC-breaking?

No

Summary (human written only)

On ROCm, ScheduleTest.test_unshard_lookahead_fsdp_parity hangs because the shared-group
P2P setup added in #197488 only connects one direction of each pipeline pair (lower to
higher rank). NCCL connects each send direction separately, so the reverse direction is
still connected lazily mid-schedule, which deadlocks in RCCL. This PR makes the setup
exchange in both directions, matching the per-edge path and the behavior before #197488.

Agent details (if applicable)

The following analysis was produced by an AI coding assistant (Cursor) and reviewed by me:

Root cause. _preconnect_shared_p2p_edges sends one tensor per physical pipeline
pair, lower rank to higher rank, assuming the pair channel is bidirectional. The
communicator is, but NCCL connects each send direction separately on first use, so the
higher-to-lower path is connected lazily at the higher rank's first SEND_F inside the
schedule. With NCCL_DEBUG=INFO, PP rank 0 connects Channel 08/1 0->1 during setup,
while PP rank 1 connects Channel 08/1 1->0 only after time_step 4, action 1SEND_F0.

Why it hangs on ROCm (rocgdb, all ranks blocked host-side in dist.isend):

  • PP rank 1: ProcessGroupNCCL::sendImpl -> ncclGroupEnd -> ncclP2PPreconnectFunc -> p2pSendSetup -> ncclProxyCallBlocking.
  • PP rank 1 proxy thread: p2pSendProxySetup -> ncclP2pAllocateShareableBuffer -> ncclCudaCallocDebug -> hipStreamSynchronize, waiting behind the rank's in-flight RECV_F0 kernel on a shared hardware queue.
  • PP rank 0: ncclLaunchPrepare -> ncclRegisterP2pIpcBuffer -> ipcRegisterBuffer -> ncclProxyConnect to rank 1's busy proxy, so its send never launches and rank 1's receive never completes.

It is a race: a pair deadlocks when the higher rank reaches its first send about 5 ms
before the lower rank. With the original code and the lower rank's first send delayed by
200 ms, the test hangs 8/8 at the default GPU_MAX_HW_QUEUES and passes 8/8 with
GPU_MAX_HW_QUEUES=16.

Fix. Exchange the setup tensor in both directions of each pair, in the same order on
both peers. With the fix, no rank creates a P2P connection after the first schedule step.
The per-edge path (_preconnect_p2p_edge_groups) already preconnects both directions,
and the neighbor exchange replaced in #197488 did as well.

Alternatives not taken. Switching the test to per-edge groups or nccl-lazy, or
tuning ROCm hardware queues, would only avoid the hang for one test or platform. The
RCCL proxy behavior (a blocking stream sync in the proxy while RCCL kernels are in
flight) is being reported to ROCm separately, with a two-GPU reproducer.

Validation on 8x MI300X (ROCm) against a release/2.15 build whose _p2p.py is
identical to main:

Case Without fix With fix
test_unshard_lookahead_fsdp_parity 8/17 hang 22/22 pass
Same test, lower rank's first send delayed 200 ms (test-only harness) 8/8 hang 8/8 pass
test_schedule_multiproc.py hangs at this test 66 passed
test_schedule.py not run 155 passed
P2PInitializationTest (updated unit test) fails (2 != 4 P2P calls) passes

Not verified: behavior on CUDA. The lazy reverse-direction connect is generic NCCL
behavior, but a hang has only been observed on ROCm.

_preconnect_shared_p2p_edges exchanges one tensor per physical pipeline pair,
from the lower rank to the higher rank, on the assumption that the pair
channel is bidirectional. The communicator is, but NCCL connects each send
direction of a pair separately on first use. The higher-to-lower path is
therefore still connected lazily inside the schedule, at the first send from
the higher rank. That breaks the setup contract that every P2P path is
connected before pipeline execution or CUDA-graph capture, which the per-edge
path (_preconnect_p2p_edge_groups) and the neighbor exchange replaced in
pytorch#197488 both honor.

On ROCm this lazy connect deadlocks
ScheduleTest.test_unshard_lookahead_fsdp_parity (PP=2 x DP=2 FSDP) in about
half of the runs. The higher rank's RCCL proxy allocates the connection buffer
and calls hipStreamSynchronize, which waits behind that rank's in-flight
receive kernel on a shared hardware queue. The lower rank's matching send
cannot launch because its IPC buffer registration waits on the same busy
proxy, so neither side progresses, and the ProcessGroupNCCL watchdogs are
blocked behind the stuck ncclGroupEnd.

The fix exchanges the setup tensor in both directions of each pair, in the
same order on both peers, so the schedule never creates a P2P connection.
Switching the test to per-edge groups or tuning ROCm hardware queues would
only hide the problem for one test or platform. The RCCL proxy behavior is
being reported to ROCm separately.

Test Plan:

Run on 8x MI300X (ROCm) against a release/2.15 build whose
torch/distributed/pipelining/_p2p.py is identical to main:

```bash
cd test
PYTORCH_TEST_WITH_ROCM=1 python3 distributed/pipelining/test_schedule.py -v -k P2PInitializationTest
PYTORCH_TEST_WITH_ROCM=1 python3 distributed/pipelining/test_schedule.py --use-pytest -q
PYTORCH_TEST_WITH_ROCM=1 python3 distributed/pipelining/test_schedule_multiproc.py --use-pytest -v
PYTORCH_TEST_WITH_ROCM=1 python3 distributed/pipelining/test_schedule_multiproc.py --use-pytest -v -k test_unshard_lookahead_fsdp_parity
```

The updated unit test fails without the fix (2 P2P calls instead of 4) and
passes with it. test_schedule.py passes (155 tests) and
test_schedule_multiproc.py passes (66 tests). The FSDP parity test hung in
8/17 runs before the fix, and in 8/8 runs when the lower rank's first send is
delayed by 200 ms; with the fix it passed 22/22 and 8/8. With NCCL_DEBUG=INFO,
no rank creates a P2P connection after the first schedule step.

This change was authored with assistance from an AI coding assistant.

Co-authored-by: Cursor <[email protected]>
@pytorch-bot

pytorch-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

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

🔗 Helpful Links

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

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

✅ No Failures

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

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

@pytorch-bot pytorch-bot Bot added the ciflow/torchtitan Run TorchTitan integration tests label Nov 9, 2026
@pytorch-bot

pytorch-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@albmalamd

Copy link
Copy Markdown
Contributor Author

@claude Review the changes in this PR

@jithunnair-amd jithunnair-amd added ciflow/trunk Trigger trunk jobs on your pull request ciflow/periodic-rocm-mi350 Trigger "distributed_4gpu" config (only runs distributed tests that need >2GPUs) CI on ROCm MI350 ciflow/periodic-rocm-mi300 Trigger "distributed" config CI on ROCm MI300/MI325 ciflow/periodic-rocm-mi200 Trigger "distributed" config CI on ROCm MI200 labels Nov 9, 2026
@jeffdaily jeffdaily added the release notes: distributed (pipeline) release notes category label Nov 10, 2026
@jeffdaily
jeffdaily marked this pull request as ready for review November 10, 2026 18:15

@jeffdaily jeffdaily left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. @sanketpurandare, can you take a look since this touches the setup from #197488?

@jeffdaily

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Nov 10, 2026
@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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: Auto Request Review

Details for Dev Infra team

Raised by workflow job

@pragupta

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -f "unrelated failures"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

pragupta pushed a commit to ROCm/pytorch that referenced this pull request Nov 10, 2026
… P2P pairs (pytorch#204061) (#3986)

## Linked issue or supporting maintainer

Supported by @jeffdaily

## BC-breaking?

No

## Summary (human written only)

On ROCm, ScheduleTest.test_unshard_lookahead_fsdp_parity hangs because
the shared-group P2P setup added in pytorch#197488 only connects one direction
of each pipeline pair (lower to higher rank). NCCL connects each send
direction separately, so the reverse direction is still connected lazily
mid-schedule, which deadlocks in RCCL. This PR makes the setup exchange
in both directions, matching the per-edge path and the behavior before
pytorch#197488.

## Agent details (if applicable)

The following analysis was produced by an AI coding assistant (Cursor)
and reviewed by me:

> **Root cause.** `_preconnect_shared_p2p_edges` sends one tensor per
physical pipeline
> pair, lower rank to higher rank, assuming the pair channel is
bidirectional. The
> communicator is, but NCCL connects each send direction separately on
first use, so the
> higher-to-lower path is connected lazily at the higher rank's first
`SEND_F` inside the
> schedule. With `NCCL_DEBUG=INFO`, PP rank 0 connects `Channel 08/1
0->1` during setup,
> while PP rank 1 connects `Channel 08/1 1->0` only after `time_step 4,
action 1SEND_F0`.
>
> **Why it hangs on ROCm** (rocgdb, all ranks blocked host-side in
`dist.isend`):
>
> - PP rank 1: `ProcessGroupNCCL::sendImpl -> ncclGroupEnd ->
ncclP2PPreconnectFunc -> p2pSendSetup -> ncclProxyCallBlocking`.
> - PP rank 1 proxy thread: `p2pSendProxySetup ->
ncclP2pAllocateShareableBuffer -> ncclCudaCallocDebug ->
hipStreamSynchronize`, waiting behind the rank's in-flight `RECV_F0`
kernel on a shared hardware queue.
> - PP rank 0: `ncclLaunchPrepare -> ncclRegisterP2pIpcBuffer ->
ipcRegisterBuffer -> ncclProxyConnect` to rank 1's busy proxy, so its
send never launches and rank 1's receive never completes.
>
> It is a race: a pair deadlocks when the higher rank reaches its first
send about 5 ms
> before the lower rank. With the original code and the lower rank's
first send delayed by
> 200 ms, the test hangs 8/8 at the default `GPU_MAX_HW_QUEUES` and
passes 8/8 with
> `GPU_MAX_HW_QUEUES=16`.
>
> **Fix.** Exchange the setup tensor in both directions of each pair, in
the same order on
> both peers. With the fix, no rank creates a P2P connection after the
first schedule step.
> The per-edge path (`_preconnect_p2p_edge_groups`) already preconnects
both directions,
> and the neighbor exchange replaced in pytorch#197488 did as well.
>
> **Alternatives not taken.** Switching the test to per-edge groups or
`nccl-lazy`, or
> tuning ROCm hardware queues, would only avoid the hang for one test or
platform. The
> RCCL proxy behavior (a blocking stream sync in the proxy while RCCL
kernels are in
> flight) is being reported to ROCm separately, with a two-GPU
reproducer.
>
> **Validation** on 8x MI300X (ROCm) against a release/2.15 build whose
`_p2p.py` is
> identical to main:
>
> | Case | Without fix | With fix |
> |---|---|---|
> | `test_unshard_lookahead_fsdp_parity` | 8/17 hang | 22/22 pass |
> | Same test, lower rank's first send delayed 200 ms (test-only
harness) | 8/8 hang | 8/8 pass |
> | `test_schedule_multiproc.py` | hangs at this test | 66 passed |
> | `test_schedule.py` | not run | 155 passed |
> | `P2PInitializationTest` (updated unit test) | fails (2 != 4 P2P
calls) | passes |
>
> **Not verified:** behavior on CUDA. The lazy reverse-direction connect
is generic NCCL
> behavior, but a hang has only been observed on ROCm.
Pull Request resolved: pytorch#204061
Approved by: https://github.com/pragupta, https://github.com/jeffdaily


(cherry picked from commit 1f18fa1)

Co-authored-by: Cursor <[email protected]>

This branch was previously deployed

1 inactive deployment
bedrock — aed4df04 Deployed Nov 10, 2026 by jeffdaily
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/periodic-rocm-mi200 Trigger "distributed" config CI on ROCm MI200 ciflow/periodic-rocm-mi300 Trigger "distributed" config CI on ROCm MI300/MI325 ciflow/periodic-rocm-mi350 Trigger "distributed_4gpu" config (only runs distributed tests that need >2GPUs) CI on ROCm MI350 ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: distributed (pipeline) release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants