Skip to content

[PP] Expose outstanding send budget - #4662

Merged
fegin merged 29 commits into
mainfrom
gh/fegin/176/head
Oct 2, 2026
Merged

fegin merged 29 commits into
mainfrom
gh/fegin/176/head

Conversation

@fegin

@fegin fegin commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary
Expose the maximum number of pending pipeline send batches. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Unit tests cover forwarding and validation. A deterministic FSDP 2 x PP 2 integration case covers the budget with deferred gradient-reduction waits.

This PR depends on pytorch/pytorch#196727

[ghstack-poisoned]
[ghstack-poisoned]
Comment thread torchtitan/config/configs.py Resolved
fegin added a commit that referenced this pull request Sep 29, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4662
* #4661
* #4660
* #4659
* __->__ #4658

----
**Conflict with #4649, need
to discuss how to incorporate with
#4649
**Depend on pytorch/pytorch#196640 and
pytorch/pytorch#196641

- Add `parallelism.fsdp_defer_gradient_reduction` to reduce gradients
after the final backward of a gradient accumulation group.
- Replay the whole optimizer step during SDC checks.
- Use the public FSDP and pipeline finalization APIs.
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 29, 2026
Expose the maximum number of pending pipeline send batches for Interleaved1F1B. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Unit tests cover forwarding and validation. A deterministic FSDP 2 x PP 2 integration case covers the budget with deferred gradient-reduction waits.

ghstack-source-id: 65c5dc1
Pull-Request: #4662
fegin added a commit that referenced this pull request Sep 29, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4662
* #4661
* #4660
* __->__ #4659

----

- Clear parameter gradients to None in Trainer and FaultTolerantTrainer.
- Keep capture-created gradient buffers alive and restore them after
replay.
- Require capture and replay to start without pending gradients.


**Why this state can reduce memory use:**

1. ``set_to_none=True`` without ``CUDAGraphGradientState`` fails after
the
       first capture.
       Autograd allocates a gradient during capture and assigns it to
``param.grad``. The next ``zero_grad`` clears that Python reference.
CUDA graph replay runs the captured GPU work, but it does not repeat the
``param.grad`` assignment. The optimizer then sees ``param.grad is
None``
       and skips the parameter update.

2. ``set_to_none=False`` avoids that failure, but eager warmup allocates
the
gradient before capture, outside the CUDA graph memory pool. Capture
only
records writes into that existing gradient buffer. Its lifetime overlaps
       the complete forward-backward graph::

time forward backward optimizer
gradient [===============================================]
           forward temp   [==========]
           backward temp                       [=========]

The graph allocator cannot reuse the gradient storage for these
temporary
       tensors because their lifetimes overlap.

3. With ``CUDAGraphGradientState``, ``set_to_none=True`` leaves
gradients
absent when capture starts. Autograd allocates them from the CUDA graph
memory pool during backward, after many forward tensors have expired::

time forward backward optimizer
           forward temp   [==========]
gradient [=========================]
shared block [forward temp] [gradient ]

During capture, the CUDA graph pool may reuse storage released by
forward
tensors for later gradients. This state then keeps those gradient
tensors
alive and restores each ``param.grad`` reference after replay. This
makes
       ``set_to_none=True`` safe.
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 29, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Document the memory and overlap tradeoff. Unit tests cover forwarding. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: d1a7c8d

Pull-Request: #4662
@fegin fegin changed the title [pp] Expose outstanding send budget [PP] Expose outstanding send budget Sep 29, 2026
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 30, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Document the memory and overlap tradeoff. Unit tests cover forwarding. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: b36674f

Pull-Request: #4662
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 30, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Document the memory and overlap tradeoff. Unit tests cover forwarding. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: cb3c0a6

Pull-Request: #4662
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 30, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Document the memory and overlap tradeoff. Unit tests cover forwarding. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: a432bb9

Pull-Request: #4662
[ghstack-poisoned]
[ghstack-poisoned]
fegin added 2 commits October 1, 2026 10:50
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 1, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Document the memory and overlap tradeoff. Unit tests cover forwarding. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: 764acd7

Pull-Request: #4662
@fegin
fegin requested a review from sanketpurandare October 1, 2026 17:51

@sanketpurandare sanketpurandare 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.

Actually, you also need to fix GraphTrainer, check this: #4994

Please also address the GraphPP regression caused by deleting the fwd_cache as shown in above PR and then close it, you can land yours

fegin added 2 commits October 1, 2026 19:23
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 2, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Migrate GraphPP transport ownership to PyTorch forward state while keeping explicit backward-graph values in a separate cache. GraphPP treats WAIT_REDUCE_GRAD as a no-op because explicit reduction tensors carry collective ordering.

Document the memory and overlap tradeoff. Unit tests cover forwarding and GraphPP state retirement. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: 313b214

Pull-Request: #4662

@tianyu-l tianyu-l 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.

overall I haven't internalized this feature

  • I could follow up offline to understand more
  • users may get confused, which I think more extensive notes / diagrams would help much

won't block this PR though

Comment thread torchtitan/config/parallelism.py Resolved
Comment thread docs/composability.md Resolved
Comment thread docs/composability.md Resolved
Comment thread docs/composability.md Resolved
Comment thread docs/composability.md Resolved
fegin added a commit that referenced this pull request Oct 2, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Migrate GraphPP transport ownership to PyTorch forward state while keeping explicit backward-graph values in a separate cache. GraphPP treats WAIT_REDUCE_GRAD as a no-op because explicit reduction tensors carry collective ordering.

Document the memory and overlap tradeoff. Unit tests cover forwarding and GraphPP state retirement. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: 313b214

Pull-Request: #4662
fegin added 2 commits October 1, 2026 23:19
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 2, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Migrate GraphPP transport ownership to PyTorch forward state while keeping explicit backward-graph values in a separate cache. GraphPP treats WAIT_REDUCE_GRAD as a no-op because explicit reduction tensors carry collective ordering.

Document the memory and overlap tradeoff. Unit tests cover forwarding and GraphPP state retirement. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: d2bf75e

Pull-Request: #4662
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 2, 2026
Expose the maximum number of pending pipeline send batches for multi-stage schedules. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Migrate GraphPP transport ownership to PyTorch forward state while keeping explicit backward-graph values in a separate cache. GraphPP treats WAIT_REDUCE_GRAD as a no-op because explicit reduction tensors carry collective ordering.

Document the memory and overlap tradeoff. Unit tests cover forwarding and GraphPP state retirement. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: ab8a3cf

Pull-Request: #4662
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 2, 2026
Expose the maximum number of pending pipeline send actions for multi-stage schedules. Each action is identified by direction, stage, and microbatch and may own several P2P operations. The PyTorch schedule inserts explicit waits and releases sent outputs while autograd keeps the backward edges.

Migrate GraphPP transport ownership to PyTorch forward state while keeping explicit backward-graph values in a separate cache. GraphPP treats WAIT_REDUCE_GRAD as a no-op because explicit reduction tensors carry collective ordering.

Document the memory and overlap tradeoff. Unit tests cover forwarding and GraphPP state retirement. Two-GPU real-PP/fake-SPMD integration tests cover LoopedBFS and Interleaved1F1B.

ghstack-source-id: 412c541

Pull-Request: #4662
fegin added a commit that referenced this pull request Oct 2, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4662
* __->__ #4661

Allow multi-stage pipeline schedules to overlap an FSDP gradient
reduction wait with the next local stage backward. Single-stage
schedules cannot place work between reduction and wait, so they reject
the option.

This uses the `defer_reduce_grad_wait` API from PyTorch PR,
pytorch/pytorch#196963.

This PR also changes TorchTitan's CI to use PyTorch cu132 as we no
longer have cu130 wheel.
@fegin
fegin changed the base branch from gh/fegin/176/base to main October 2, 2026 06:58
@fegin
fegin changed the base branch from main to gh/fegin/176/base October 2, 2026 06:59
@fegin
fegin changed the base branch from gh/fegin/176/base to main October 2, 2026 06:59

@sanketpurandare sanketpurandare 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.

Thanks for the fix!

@fegin
fegin merged commit 01f0fcc into main Oct 2, 2026
SherlockNoMad added a commit that referenced this pull request Oct 2, 2026
…tion (#5024)

Replaces #4998, whose stacked base branch was merged and can no longer
be retargeted. Rebased onto `main` now that #5005 (the SPMD gradient
accumulation config) has landed; same single commit.

## Why
`fsdp_param_unshard_mode="schedule"` /
`fsdp_grad_reduce_mode="schedule"` extracted FSDP all-gather /
reduce-scatter out of the PP=1 joint graph into standalone `UNSHARD` /
`REDUCE_GRAD` schedule actions. With PP=1 there is a single stage, so
tracking these collectives as separate schedule actions gives no
scheduling benefit over keeping them inside the first/last (or every)
microbatch graph -- but it required a lot of extra machinery (mixed
REPEAT computation types, extra extracted graphs, merge/compile paths,
runtime branches).

PP>1 is unchanged and still always uses scheduled `UNSHARD` /
`REDUCE_GRAD`.

## What
Remaining PP=1 + GA combinations:
- `(every_microbatch, every_microbatch)`: `FULL_FORWARD_BACKWARD` per
microbatch
- `(first_microbatch, last_microbatch)` (default): `FIRST_WITH_UNSHARD`,
`FORWARD_BACKWARD`, `LAST_WITH_REDUCE_GRAD`, then `RESHARD`
- Mixed combinations are rejected with a `ValueError`.

Removed:
- `FORWARD_BACKWARD_REPEAT_WITH_UNSHARD` /
`FORWARD_BACKWARD_REPEAT_WITH_REDUCE_GRAD`,
`joint_forward_backward_computation_type` and its lookup table
- PP=1 extraction of standalone UNSHARD / REDUCE_GRAD graphs, their
merge passes, annotation/compilation, `_compile_optional_graph`
- `GraphExecutionPlan.requires_graph_extraction` (replaced by
`has_gradient_accumulation`) and a duplicated precompile check
- PP=1 `UNSHARD` / `REDUCE_GRAD` schedule actions (schedule is now
per-microbatch actions + `RESHARD`)
- Runtime branch deciding whether grad reduction ran inside the joint
graph

Net: +99 / -419.

## Tests
- `test_graph_pp_runner.py`, `test_graph_pp_passes.py`,
`test_trace_module.py`: 162 passed, 3 skipped.
- Replaced the three `"schedule"` integration jobs with
`aot_fx_trace_llama3_ga_first_unshard_last_reduce_fsdp` (default FSDP GA
path had no coverage).
- Ran both remaining FSDP=4 + GA configs for 10 steps on 4 GPUs with
`seed=42`, `deterministic=True`; both modes produce identical
loss/grad_norm (stdout precision):

| mode | step 1 loss | step 10 loss | step 10 grad_norm |
|---|---|---|---|
| first/last | 8.14194 | 3.99857 | 1.9983 |
| every/every | 8.14194 | 3.99857 | 1.9983 |

**After rebasing onto `main`:** one import conflict in
`test_graph_pp_runner.py` (kept `WAIT_REDUCE_GRAD` from #4662, dropped
the now-unused `UNSHARD`). pre-commit and pyflakes are clean. The unit
tests above were run before the rebase; they could not be re-run locally
because current `main` imports `WAIT_REDUCE_GRAD` (#4662), which needs a
newer PyTorch than the local build. Relying on CI for the post-rebase
run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants