Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/196727
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 71fe360 with merge base f239880 ( FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Add no-stall forward-send waits after matching backward actions. Add an optional hard per-rank limit on pending forward and backward send batches. The budget pass walks rank-local and send-to-receive dependencies in causal order. It prefers sends known to have completed, permits waits after a peer receive has been posted, and rejects a limit that cannot be met safely. The limit controls retained send ownership directly. It does not count unrelated schedule actions or bytes. Test Plan: ``` python test/distributed/pipelining/test_schedule.py ``` Authored with assistance from Codex. ghstack-source-id: 955a207 Pull-Request: #196727
Add no-stall forward-send waits after matching backward actions. Add an optional hard per-rank limit on pending forward and backward send batches. The budget pass walks rank-local and send-to-receive dependencies in causal order. It prefers sends known to have completed, permits waits after a peer receive has been posted, and rejects a limit that cannot be met safely. The limit controls retained send ownership directly. It does not count unrelated schedule actions or bytes. Test Plan: ``` python test/distributed/pipelining/test_schedule.py ``` Authored with assistance from Codex. ghstack-source-id: 1496ac7 Pull-Request: #196727
| def _add_wait_send_budget( | ||
| comm_actions: dict[int, list[_Action]], | ||
| stage_to_rank: Callable[[int], int], | ||
| max_outstanding_sends: int, |
There was a problem hiding this comment.
as discussed offline, I feel this feature could be an over engineering. It's unclear how much saving this is bringing.
I would defer landing this, unless it's absolutely needed.
Even if it's needed for dsv3's case, I feel this could be model specific optimization. I wish to decouple the complexity of "schedule generation" and "schedule execution".
Model code, or user, can own the complexity of "schedule generation", and pass the resulting schedule.csv to the executor.
Pipeline infra should just provide a few basic default schedule, and provide the extension point to advance schduel.
There was a problem hiding this comment.
If we don't want this complexity bare minimum we should schedule WAIT_SEND_F after its corresponding B/I action and WAIT_SEND_B just before next SEND_B action. Otherwise we will stall @fegin
There was a problem hiding this comment.
@SherlockNoMad holding all sends till the very end of schedule is completely inappropriate I would not approve
|
|
||
| # Walk actions in causal order. Track pending sends and peer progress, | ||
| # and insert waits before a rank exceeds its send limit. | ||
| queue = deque(sorted(node for node, degree in in_degree.items() if degree == 0)) |
There was a problem hiding this comment.
This single topological traversal is being used as proof that no safe wait placement exists, but its ordering of independent ready nodes is arbitrary. For the existing reordered-send shape rank0=[F0,F1,SEND_F0,SEND_F1,...], rank1=[RECV_F0,RECV_F1,F0,F1,...], a budget of 1 raises here because SEND_F1 is popped just before the independently ready RECV_F0. Inserting WAIT_SEND_F0 before SEND_F1 is causally safe, and _simulate_comms_compute() accepts that transformed schedule. I suggest deriving safety from graph reachability rather than whether one traversal happened to pop the receive first, then adding this reordered-send case as a regression test.
| with self.assertRaisesRegex(ValueError, "non-negative integer"): | ||
| _PipelineScheduleRuntime([], 1, max_outstanding_sends=value) | ||
|
|
||
| def test_max_outstanding_sends_applies_to_interleaved_schedule(self): |
There was a problem hiding this comment.
This proves the generated IR satisfies the counter and the simulator, but both are static models of the same dependency assumptions. This PR changes real Work.wait() placement, so at least one NCCL/XCCL execution is needed to cover the deadlock and ownership risks. I suggest parameterizing the existing multiprocess Interleaved1F1B test with max_outstanding_sends=4 and reusing its step-completion, gradient, and leak checks.
| def _add_wait_send( | ||
| comm_actions: dict[int, list[_Action]], | ||
| ) -> dict[int, list[_Action]]: | ||
| """Wait for a forward send after its matching local backward.""" |
There was a problem hiding this comment.
The current one-line docstring omits both the causal safety argument and the lifetime tradeoff. The placement is deadlock-free because this stage's backward cannot run until the downstream stage has consumed the forward activation and returned its gradient. Choosing the point after backward avoids an exposed send wait, but it retains the send-owned output through backward instead of releasing it at the parent PR's implicit pre-backward wait. I suggest documenting both facts here because they are the core reason for this placement and its memory cost.
| (_stage_index, microbatch_index), () | ||
| ): | ||
| test_case.assertIsNone(storage_ref()) | ||
| if release_before_backward: |
There was a problem hiding this comment.
With release_before_backward=False, this branch stops checking any lifetime at backward and only verifies eventual collection when the context exits. It would still pass if the new WAIT_SEND_F were ignored and the final drain released everything. I suggest reusing or extending assert_explicit_forward_wait_ownership to assert that storage is live through backward and becomes unreachable at the scheduled post-backward wait. That would prove the policy introduced here instead of weakening the parent test.
sanketpurandare
left a comment
There was a problem hiding this comment.
I suggest making every finalized schedule contain exactly one WAIT_SEND_F/B for every SEND_F/B, and making max_outstanding_sends only reposition those existing waits. The default construction policy should put WAIT_SEND_F after the matching local full- or input-backward action: that backward depends on receiving the downstream gradient, which proves the peer consumed the forward activation, so the wait cannot stall. A backward send has no analogous return dependency, so its default WAIT_SEND_B should be placed at the end of the rank's schedule; a forward send with no backward release point should also wait at the end. For compute_only, lowering should create these pairs. For pre-lowered compute_comms, construction should either validate exactly one ordered wait per send or canonicalize missing waits once, rather than relying on runtime implicit waits. With max_outstanding_sends=None, those default no-stall positions remain unchanged. With an integer budget, the pass may move a wait earlier only when graph reachability proves the placement cannot introduce a cycle; it must not infer impossibility from one arbitrary topological traversal. Runtime then becomes mechanical: sends register ownership, waits retire it, and normal completion asserts the tracker is empty. Exception cleanup may drain outstanding work, but that should not be a second normal-path policy. Please document that the budget counts combined forward/backward send batches rather than bytes or individual destinations, and that earlier waits trade possible stalls for lower retained memory. For coverage, I suggest reusing ScheduleWithReorderedB for the tight-budget causal regression and parameterizing the existing multiprocess Interleaved1F1B test with a real budget to verify completion, numerics, forward-output lifetime, backward-gradient lifetime, and leak freedom.
Add an optional hard per-rank limit on pending forward and backward send batches. The budget pass uses reachability over rank-local order, send-to-receive dependencies, and current wait placements. It moves an existing wait only when the move cannot create a cycle. It prefers non-stalling moves, permits safe moves that may stall, and rejects a limit that cannot be met safely. The limit controls retained send ownership. It does not count unrelated schedule actions or bytes. Test Plan: ``` taskset -c 0-382 python test/distributed/pipelining/test_schedule.py taskset -c 0-382 python test/distributed/pipelining/test_schedule_multiproc.py -k max_outstanding_sends_4 taskset -c 0-382 env UV_NO_CONFIG=1 lintrunner -a torch/distributed/pipelining/schedules.py test/distributed/pipelining/test_schedule.py test/distributed/pipelining/test_schedule_multiproc.py ``` Authored with assistance from Codex. ghstack-source-id: 0696cdc Pull-Request: #196727
Add an optional hard per-rank limit on pending forward and backward send batches. The budget pass uses reachability over rank-local order, send-to-receive dependencies, and current wait placements. It moves an existing wait only when the move cannot create a cycle. It prefers non-stalling moves, permits safe moves that may stall, and rejects a limit that cannot be met safely. The limit controls retained send ownership. It does not count unrelated schedule actions or bytes. Test Plan: ``` taskset -c 0-382 python test/distributed/pipelining/test_schedule.py taskset -c 0-382 python test/distributed/pipelining/test_schedule_multiproc.py -k max_outstanding_sends_4 taskset -c 0-382 env UV_NO_CONFIG=1 lintrunner -a torch/distributed/pipelining/schedules.py test/distributed/pipelining/test_schedule.py test/distributed/pipelining/test_schedule_multiproc.py ``` Authored with assistance from Codex. ghstack-source-id: f27ef2b Pull-Request: #196727
|
The new version addresses @sanketpurandare reviews. As whether we should land this PR and export the config to TorchTitan, we can discuss. |
Add an optional hard per-rank limit on pending forward and backward send batches. The budget pass uses reachability over rank-local order, send-to-receive dependencies, and current wait placements. It moves an existing wait only when the move cannot create a cycle. It prefers non-stalling moves, permits safe moves that may stall, and rejects a limit that cannot be met safely. The limit controls retained send ownership. It does not count unrelated schedule actions or bytes. In a DeepSeek-V3 16B run, max_outstanding_sends=8 reduced peak memory by 2.53 GiB, or 3.1%, on the worst rank. Throughput changed by -0.2% +/- 1.1%, which was within measurement noise. Test Plan: ``` taskset -c 0-382 python test/distributed/pipelining/test_schedule.py taskset -c 0-382 python test/distributed/pipelining/test_schedule_multiproc.py ScheduleTest.test_grad_with_manual_interleaved_ScheduleClass1_max_outstanding_sends_4_pre_split_False ScheduleTest.test_grad_with_manual_interleaved_ScheduleClass1_max_outstanding_sends_4_pre_split_True -v taskset -c 0-382 env UV_NO_CONFIG=1 lintrunner -a torch/distributed/pipelining/schedules.py test/distributed/pipelining/test_schedule.py test/distributed/pipelining/test_schedule_multiproc.py ``` Authored with assistance from Codex. ghstack-source-id: 0ad09c1 Pull-Request: #196727
sanketpurandare
left a comment
There was a problem hiding this comment.
If we don't expose this as config in torchtitan atleast we should set a default value for it. It is specifically necessary for backward send. Immediate WAIT_SEND_B is blocking, delayaing it to end of schedule is memory consuming. A basic algorithm should atleast delay the WAIT_SEND_B just before another SEND_B action? @fegin @SherlockNoMad
Add an optional hard per-rank limit on pending forward and backward send batches. Expose it on every runtime multi-stage schedule. The budget pass uses reachability over rank-local order, send-to-receive dependencies, and current wait placements. It moves an existing wait only when the move cannot create a cycle. It prefers non-stalling moves, permits safe moves that may stall, and rejects a limit that cannot be met safely. The limit controls retained send ownership. It does not count unrelated schedule actions or bytes. In a DeepSeek-V3 16B run, max_outstanding_sends=8 reduced peak memory by 2.53 GiB, or 3.1%, on the worst rank. Throughput changed by -0.2% +/- 1.1%, which was within measurement noise. Test Plan: ``` taskset -c 0-382 python test/distributed/pipelining/test_schedule.py taskset -c 0-382 python test/distributed/pipelining/test_schedule_multiproc.py -k max_outstanding_sends_4 -v taskset -c 0-382 env UV_NO_CONFIG=1 lintrunner -a torch/distributed/pipelining/schedules.py test/distributed/pipelining/test_schedule.py test/distributed/pipelining/test_schedule_multiproc.py ``` Authored with assistance from Codex. ghstack-source-id: 0535f71 Pull-Request: #196727
Add an optional hard per-rank limit on pending forward and backward send batches. Expose it on every runtime multi-stage schedule. The budget pass uses reachability over rank-local order, send-to-receive dependencies, and current wait placements. It moves an existing wait only when the move cannot create a cycle. It prefers non-stalling moves, permits safe moves that may stall, and rejects a limit that cannot be met safely. The limit controls retained send ownership. It does not count unrelated schedule actions or bytes. In a DeepSeek-V3 16B run, max_outstanding_sends=8 reduced peak memory by 2.53 GiB, or 3.1%, on the worst rank. Throughput changed by -0.2% +/- 1.1%, which was within measurement noise. Test Plan: ``` taskset -c 0-382 python test/distributed/pipelining/test_schedule.py taskset -c 0-382 python test/distributed/pipelining/test_schedule_multiproc.py -k max_outstanding_sends_4 -v taskset -c 0-382 env UV_NO_CONFIG=1 lintrunner -a torch/distributed/pipelining/schedules.py test/distributed/pipelining/test_schedule.py test/distributed/pipelining/test_schedule_multiproc.py ``` Authored with assistance from Codex. ghstack-source-id: 25e0c99 Pull-Request: #196727
|
I will proceed to land this PR to unblock the performance verification. |
|
@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 |
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0) (oldest at bottom): * __->__ #4662 * #4661 **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
## Summary Add an optional `max_outstanding_sends` limit to runtime pipeline schedules. The schedule moves existing send waits earlier to keep pending forward and backward sends within the per-rank limit. Waits move only when dependency analysis proves the placement cannot create a cycle. Unsafe limits raise an actionable error. The default behavior remains unchanged. Pull Request resolved: pytorch#196727 Approved by: https://github.com/sanketpurandare ghstack dependencies: pytorch#196640, pytorch#196724, pytorch#196725, pytorch#196963, pytorch#196726
Stack from ghstack (oldest at bottom):
Summary
Add an optional
max_outstanding_sendslimit to runtime pipeline schedules. The schedule moves existing send waits earlier to keep pending forward and backward sends within the per-rank limit.Waits move only when dependency analysis proves the placement cannot create a cycle. Unsafe limits raise an actionable error. The default behavior remains unchanged.