Skip to content

Optimize pipeline receive buffer allocation - #193872

Closed
SherlockNoMad wants to merge 2 commits into
gh/SherlockNoMad/27/basefrom
gh/SherlockNoMad/27/head
Closed

SherlockNoMad wants to merge 2 commits into
gh/SherlockNoMad/27/basefrom
gh/SherlockNoMad/27/head

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Pipeline receive descriptors allocated activation and gradient buffers for every microbatch during stage initialization and retained them for the whole pipeline step. This reserved memory before communication needed it and required explicit gradient cleanup because the same activation leaves were reused across steps.

Keep only tensor metadata in the receive descriptors, allocate storage immediately before posting an irecv, and transfer the buffer out of the descriptor when microbatch compute consumes it. Same-rank stage handoffs follow the same ownership contract. Fresh receive buffers also make the old input-gradient cleanup and unused non-consuming buffer helper unnecessary.

Test Plan:

python test/distributed/pipelining/test_schedule.py -k test_stage_recv_buffers_allocated_just_in_time
python test/distributed/pipelining/test_schedule_multiproc.py -k test_multi_iter

Both test commands reported OK; the test harness exited with status 134 during teardown, matching the existing environment behavior.

Memory Savings

Configuration: DeepSeek-V3 16B eager, PP4/VPP4, EP2, DP-shard2, microbatch size 1, local batch size 16, sequence length 4096, no activation checkpointing, and 10 training steps on 8 H100 GPUs.

Metric viable/strict pp-lazy-recv-buffers Difference
Peak allocated 66.00 GiB 64.32 GiB -1.68 GiB (-2.5%)
Peak reserved 80.25 GiB 78.59 GiB -1.66 GiB (-2.1%)
GPU headroom 14.83 GiB 16.49 GiB +1.66 GiB

Lazy receive-buffer allocation reduced peak reserved memory on every PP stage by 1.54-1.66 GiB.

Authored with assistance from an AI coding assistant.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 e0a6a27 with merge base 1b5baff (image):

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.

@pytorch-bot pytorch-bot Bot added the ciflow/torchtitan Run TorchTitan integration tests label Aug 18, 2026
@SherlockNoMad SherlockNoMad added the topic: not user facing topic category label Aug 18, 2026
@SherlockNoMad SherlockNoMad changed the title Defer pipeline receive buffer allocation Optimize pipeline receive buffer allocation Aug 18, 2026
[ghstack-poisoned]
SherlockNoMad added a commit that referenced this pull request Aug 18, 2026
Pipeline receive descriptors allocated activation and gradient buffers for every microbatch during stage initialization and retained them for the whole pipeline step. This reserved memory before communication needed it and required explicit gradient cleanup because the same activation leaves were reused across steps.

Keep only tensor metadata in the receive descriptors, allocate storage immediately before posting an irecv, and transfer the buffer out of the descriptor when microbatch compute consumes it. Same-rank stage handoffs follow the same ownership contract. Fresh receive buffers also make the old input-gradient cleanup and unused non-consuming buffer helper unnecessary.

Test Plan:

```
python test/distributed/pipelining/test_schedule.py -k test_stage_recv_buffers_allocated_just_in_time
```

```
python test/distributed/pipelining/test_schedule_multiproc.py -k test_multi_iter
```

Both test commands reported OK; the test harness exited with status 134 during teardown, matching the existing environment behavior.

```
lintrunner -a
```

Lint initialization failed because the environment could not download its pinned CPython runtime through the network tunnel. No patches were applied.

Authored with assistance from an AI coding assistant.

ghstack-source-id: cd67792
Pull-Request: #193872
@sanketpurandare

Copy link
Copy Markdown
Contributor

Superseded by a refreshed current-main ghstack that preserves this implementation and authorship, then adds schedule-derived deterministic receive-buffer pooling. I am taking over maintenance of the replacement stack.

@sanketpurandare

Copy link
Copy Markdown
Contributor

Replacement current-main stack: #196463 carries the preserved lazy-receive implementation, #196464 adds generic schedule-derived resource liveness, and #196465 adds deterministic receive-buffer pooling.

sanketpurandare added a commit that referenced this pull request Sep 15, 2026
… in time"

Summary:
Pipeline stages currently retain one forward and backward receive buffer per microbatch from setup. Allocate a receive buffer immediately before posting its receive, transfer ownership to computation and autograd when consumed, and clear the descriptor reference afterward.

This supersedes #193872 on current main while preserving its implementation and authorship. It establishes explicit ownership for later static reuse, but does not add pooling, polling, schedule rewrites, or send-wait policy. A descriptor that still owns storage fails loudly: an interrupted distributed step may have in-flight P2P work, so callers must recreate the schedule and process group rather than silently reusing that storage.

The peak number of live lazy buffers is determined by the schedule's receive-prefetch depth, not necessarily one. Local V-schedule transfers follow the same ownership contract.

Test Plan:
- `python test/distributed/pipelining/test_schedule.py -k recv_buffer`
- `python test/distributed/pipelining/test_schedule_multiproc.py`

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 15, 2026
Summary:
Pipeline stages currently retain one forward and backward receive buffer per microbatch from setup. Allocate a receive buffer immediately before posting its receive, transfer ownership to computation and autograd when consumed, and clear the descriptor reference afterward.

This supersedes #193872 on current main while preserving its implementation and authorship. It establishes explicit ownership for later static reuse, but does not add pooling, polling, schedule rewrites, or send-wait policy. A descriptor that still owns storage fails loudly: an interrupted distributed step may have in-flight P2P work, so callers must recreate the schedule and process group rather than silently reusing that storage.

The peak number of live lazy buffers is determined by the schedule's receive-prefetch depth, not necessarily one. Local V-schedule transfers follow the same ownership contract.

Test Plan:
- `python test/distributed/pipelining/test_schedule.py -k recv_buffer`
- `python test/distributed/pipelining/test_schedule_multiproc.py`

[ghstack-poisoned]
@github-actions
github-actions Bot deleted the gh/SherlockNoMad/27/head branch November 7, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/torchtitan Run TorchTitan integration tests topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants