Skip to content

FSDP2 chunked-loss ROCm race: stream-level wait_event at post_backward exit - #181218

Closed
weifengpy wants to merge 10 commits into
gh/weifengpy/133/basefrom
gh/weifengpy/133/head
Closed

weifengpy wants to merge 10 commits into
gh/weifengpy/133/basefrom
gh/weifengpy/133/head

Conversation

@weifengpy

@weifengpy weifengpy commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Nature of the bug (refined 2026-04-23 after CUDA + ROCm experiments):
The production failure as observed is ROCm-specific; the underlying
stream-ordering hazard is cross-platform. Two race windows, both
closed by the same stream-level wait:

  1. rs_input block reuse. Active in the standalone repro on both CUDA
    (2/3 baseline, 3/3 with _sleep on H100) and ROCm (32/200 baseline,
    49-50/50 with --ag etc). Inert in FSDP on both platforms because
    reduce_scatter_states.append(...) ref-holds rs_input. Verified by
    adding --hold-refs input to the standalone repro on ROCm: 0/200.

  2. Allocator-internal buffer (HIP caching allocator; not
    Python-reachable; FSDP-code-path specific). Active in FSDP on
    ROCm. Not reproducible in the standalone repro on ROCm by any
    combination of --hold-refs + --sleep + --alloc-pressure, which
    implies the racing buffer is something the standalone loop
    doesn't allocate — most plausibly an autograd backward
    intermediate, reshard buffer, or RCCL workspace from the
    all_gather + reduce_scatter sequence FSDP issues per param
    group. CUDA FSDP status of vector 2 is indeterminate from
    FSDP-path data alone (either absent or timing-masked).

The conditional gating on in_chunked_path is currently kept to
minimize blast radius; making the fix unconditional is the right
long-term decision but requires a real-model perf check first.

Also adds investigation artifacts:

  • fsdp2_chunked_loss_rocm_race.md — full writeup with all experiments
    (row O/O' bisect, CUDA H100 discriminator, ROCm MI350X ref-hold
    verification, vector 2 FSDP-specificity proof).
  • rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
    repro. --hold-refs {off,input,both} toggles the FSDP-style
    reduce_scatter_states ref-hold.
  • rccl_chunked_loss_repro_sleep.py — _sleep variant for CUDA
    discriminator + ROCm vector-2 isolation. Adds --hold-refs and
    --alloc-pressure N (+ --alloc-pressure-numel).
  • hip_allocator_cross_stream_repro.py — pure-allocator attempt that
    did NOT reproduce; rules out a generic HIP allocator
    cross-stream-reuse bug.
  • analyze_memory_snapshot.py — summarizer for torch.cuda.memory
    snapshot pickles.

Test Plan:

  • ROCm MI350X (gfx950, ROCm 7.0.51831): full subtest sweep of
    TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone
    passes 5/5 with fix, fails bit-exactly (11.481... at (0,10), 2048/4096
    elements, rank-1 shard) without.
  • Standalone RCCL repro on ROCm: 0/500 with any stream-level sync
    (rs_event_stream, post_accum_event_stream, device), 500/500 with
    none. --hold-refs input: 0/200 (confirms vector 1 is rs_input).
  • _sleep stress on ROCm: 1e9-cycle sleep at rs_after_accum or
    rs_between_rs_and_accum races 50/50 without fix, 0/3 with fix.
  • CUDA H100 + NCCL 2.29.7: baseline 2/3 races, sleep 1e9 = 3/3
    races without fix; sleep 1e9 + post_accum_event_stream = 0/3.
    Confirms vector 1 is cross-platform.
  • ROCm vector-2 isolation: --hold-refs both × --sleep-cycles 1e9 ×
    --alloc-pressure 1000 (numel 256/2048/4096) = 0/50 across the
    matrix. Confirms vector 2 is FSDP-code-path-specific, not a
    generic "RCCL + allocator + chunked loop" hazard.

Authored with Claude.

Reviewers:

Subscribers:

Tasks:

Tags:

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Apr 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure

As of commit 6174b7e with merge base 12c7b75 (image):

NEW FAILURE - The following job has failed:

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

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/rocm-mi300 Trigger "default" config CI on ROCm MI300 ciflow/torchtitan Run TorchTitan integration tests module: rocm AMD GPU support for Pytorch release notes: distributed (fsdp) release notes category labels Apr 23, 2026
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: 19e29f6
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: a1b3024
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: 32ab8f0
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: fafa061
Pull-Request: #181218
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Nature of the bug (refined 2026-04-23 after CUDA + ROCm experiments):
The production failure as observed is ROCm-specific; the underlying
stream-ordering hazard is cross-platform. Two race windows, both
closed by the same stream-level wait:

1. rs_input block reuse. Active in the standalone repro on both CUDA
   (2/3 baseline, 3/3 with _sleep on H100) and ROCm (32/200 baseline,
   49-50/50 with --ag etc). Inert in FSDP on both platforms because
   reduce_scatter_states.append(...) ref-holds rs_input. Verified by
   adding --hold-refs input to the standalone repro on ROCm: 0/200.

2. Allocator-internal buffer (HIP caching allocator; not
   Python-reachable; FSDP-code-path specific). Active in FSDP on
   ROCm. Not reproducible in the standalone repro on ROCm by any
   combination of --hold-refs + --sleep + --alloc-pressure, which
   implies the racing buffer is something the standalone loop
   doesn't allocate — most plausibly an autograd backward
   intermediate, reshard buffer, or RCCL workspace from the
   all_gather + reduce_scatter sequence FSDP issues per param
   group. CUDA FSDP status of vector 2 is indeterminate from
   FSDP-path data alone (either absent or timing-masked).

The conditional gating on in_chunked_path is currently kept to
minimize blast radius; making the fix unconditional is the right
long-term decision but requires a real-model perf check first.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup with all experiments
  (row O/O' bisect, CUDA H100 discriminator, ROCm MI350X ref-hold
  verification, vector 2 FSDP-specificity proof).
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. --hold-refs {off,input,both} toggles the FSDP-style
  reduce_scatter_states ref-hold.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for CUDA
  discriminator + ROCm vector-2 isolation. Adds --hold-refs and
  --alloc-pressure N (+ --alloc-pressure-numel).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- ROCm MI350X (gfx950, ROCm 7.0.51831): full subtest sweep of
  TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone
  passes 5/5 with fix, fails bit-exactly (11.481... at (0,10), 2048/4096
  elements, rank-1 shard) without.
- Standalone RCCL repro on ROCm: 0/500 with any stream-level sync
  (rs_event_stream, post_accum_event_stream, device), 500/500 with
  none. --hold-refs input: 0/200 (confirms vector 1 is rs_input).
- _sleep stress on ROCm: 1e9-cycle sleep at rs_after_accum or
  rs_between_rs_and_accum races 50/50 without fix, 0/3 with fix.
- CUDA H100 + NCCL 2.29.7: baseline 2/3 races, sleep 1e9 = 3/3
  races without fix; sleep 1e9 + post_accum_event_stream = 0/3.
  Confirms vector 1 is cross-platform.
- ROCm vector-2 isolation: --hold-refs both × --sleep-cycles 1e9 ×
  --alloc-pressure 1000 (numel 256/2048/4096) = 0/50 across the
  matrix. Confirms vector 2 is FSDP-code-path-specific, not a
  generic "RCCL + allocator + chunked loop" hazard.

Authored with Claude.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: fafa061
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: fd60a47
Pull-Request: #181218
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: fd60a47
Pull-Request: #181218
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: fd60a47
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: d6e0915
Pull-Request: #181218
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: d6e0915
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: d6e0915
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: 94af962
Pull-Request: #181218
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Apr 23, 2026
…d exit

Summary:
Closes the reduce-scatter race in the grouped chunked-loss path on
ROCm (MI350X, RCCL). When a standalone per-chunk head call triggers
post_backward, chunk N+1's autograd backward runs on the default
stream concurrently with chunk N's accumulate on the RS stream. The
caching allocator can hand an autograd kernel a block that is a live
input/output of chunk N's accumulate, corrupting head.weight.grad
on non-zero ranks.

Fix: at post_backward exit, detect the chunked path via
TrainingState.FORWARD entry state (group post-hook never fires full
path, so pre_backward never runs) and call
current_stream().wait_event(self._post_reduce_event). Pure
stream-level wait, no CPU sync. Gated on in_chunked_path so normal
FSDP (non-grouped or grouped with full forward) is unaffected.

Why post-accumulate event at exit (not pre-accumulate at entry):
waiting on reduce_scatter_event at post_backward exit FAILS; waiting
on _post_reduce_event at post_backward exit PASSES. The accumulate
kernel itself is load-bearing, and autograd queues chunk N+1's
backward between this chunk's post_backward exit and the next one's
entry — a wait at entry is too late.

Also adds investigation artifacts:
- fsdp2_chunked_loss_rocm_race.md — full writeup (experiments O/O'
  bisect, root-cause, minimal repro, open question for CUDA)
- rccl_chunked_loss_repro.py — standalone 2-process RCCL-in-loop
  repro. 500/500 races with none, 0/500 with any stream-level sync.
- rccl_chunked_loss_repro_sleep.py — _sleep variant for the CUDA
  discriminator (pure stream-ordering-bug vs allocator
  event-gating-bug).
- hip_allocator_cross_stream_repro.py — pure-allocator attempt that
  did NOT reproduce; rules out a generic HIP allocator
  cross-stream-reuse bug.
- analyze_memory_snapshot.py — summarizer for torch.cuda.memory
  snapshot pickles.

Test Plan:
- Full subtest sweep on MI350X (gfx950, ROCm 7.0.51831): 5/5 clean
  on TestFullyShard1DTrainingCompose.test_partial_group_forward_then_standalone.
- Standalone RCCL repro: 0/500 races with rs_event_stream or
  post_accum_event_stream sync modes, vs 500/500 with none.
- _sleep stress on ROCm: 1e9-cycle sleep injected on RS stream
  after accumulate; fix holds (0/3 races). Without fix: 3/3 races.
- CUDA discriminator run still pending — see
  fsdp2_chunked_loss_rocm_race.md "Open question" section.

Reviewers:

Subscribers:

Tasks:

Tags:
ghstack-source-id: 3d281c0
Pull-Request: #181218
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Jun 23, 2026
@github-actions github-actions Bot closed this Jul 23, 2026
@github-actions
github-actions Bot deleted the gh/weifengpy/133/head branch August 23, 2026 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/rocm-mi300 Trigger "default" config CI on ROCm MI300 ciflow/torchtitan Run TorchTitan integration tests module: rocm AMD GPU support for Pytorch release notes: distributed (fsdp) release notes category Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant