`DualPipeV` optimisations #8705
Please document the activation-lifetime restriction introduced by this optimization. DualPipeV now clears each sent output's data after communication, so a stage that both returns a tensor and consumes that same tensor in another local operation can produce an incorrect backward pass. Without this warning, the public tutorial presents the feature as generally safe for existing layers even though this valid stage topology is unsupported.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
The public documentation omits a correctness-critical restriction caused by freeing sent activation data.
Review effort: Balanced
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Adds zero-bubble weight-gradient deferral and activation-memory optimizations to DualPipeV.
Changes:
WeightPass execution.| File | Description |
| ---- | ----------- |
| deepspeed/runtime/pipe/dualpipev.py | Implements gradient deferral and activation freeing. |
| deepspeed/runtime/pipe/schedule.py | Adds zero-bubble scheduling and WeightPass. |
| deepspeed/pipe/__init__.py | Exports WeightGradStore. |
| tests/unit/runtime/pipe/test_pipe.py | Tests deferred gradients end to end. |
| tests/unit/runtime/pipe/test_pipe_schedule.py | Validates deferred-pass scheduling. |
| docs/_tutorials/pipeline.md | Documents zero-bubble layer integration. |
</details>
---
💡 <a href="/deepspeedai/DeepSpeed/new/master?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
I still need to add the overlapping forward-backward support. I am working on it - a bit confused how to add that. Will post the issue if I stay stuck for a longer time. Thanks!
will add it in the next commit
Thanks!
Added in the latest commit. (a warning in docstring and to the docs)
Hi @delock @hwchen2017
The PR is ready for review. Thanks!
This weight-gradient formula fails for a valid 1-D torch.nn.functional.linear input: transposing a 1-D grad_output is a no-op, so the matrix product cannot produce the [out_features, in_features] weight gradient. Reshape both operands to 2-D before multiplying so the public example also handles unbatched inputs.
This callback executes the complete forward pass before starting backward, exactly matching the no-callback fallback, so it demonstrates no overlap and provides no optimization. Please show forward work split around the backward(...) call (or explicitly label this as an interface-only scaffold) so users can implement the feature described by this section.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
The public examples contain an invalid unbatched gradient formula and demonstrate no actual forward/backward overlap.
Review effort: Balanced
Findings: 2 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (2)</strong></summary>
</details>
<details>
<summary><strong>Resolved since last review (1)</strong></summary>
</details>
Hi @hwchen2017, I have addressed the copliot reviews. Thanks!
@phoeenniixx Thanks for the PR. Can you explain what's the motivation of submitting this PR given that PyTorch has natively support DualPipeV: https://docs.pytorch.org/docs/2.14/distributed.pipelining.html, also do you have any benchmarking results on modeling training here as well?
Thanks for the comment @PKUWZP! TBH i didnt know torch has also released its pp version! Thanks for sharing it.
DualPipeV was added from this issue: #7100
Also, this PR is made to setup for Bidirectional DualPipe which i think is not present in torch. DeepSeek's DualPipe uses the same zero-bubble and overlapped forward/backward pieces this PR adds.
Also, I think we cannot use torch implementation our of the box in deepspeed, we would need to actually create a wrapper around it. Otherwise the user would have to go down a layer (from deepspeed to torch) to use this. Also:
torch.distributed directly (stage.py), so pipeline traffic would bypass deepspeed.comm and its communication logging. We would have to monkeypatch torch to handle this (or some better way?). The current implementation sends through deepspeed.comm.
Please let me know what your preference here - keeping the current implementation, a new torch wrapper or should we drop the idea of having DualpipeV at all (This would mean reverting #8576). Happy to do whatever you prefer.
Thanks!
---
Regarding benchmarks, I actually didnt have much compute, so tried the benchmarks on 2 T4s from kaggle (notebook). Though having just 2 ranks i think are not enough for for a meaningful comparison. The results:
ranks=2 micro_batches=8 micro_batch_size=4 seq=256 hidden=512 heads=8 blocks=8 dtype=fp32 zero=0 measured steps=10 torch=2.10.0+cu128 device=cuda
| schedule | step time (s) | samples/s | vs 1F1B | peak mem rank 0 (MB) | peak mem rank 1 (MB) | final loss | max loss diff vs first |
|---|---|---|---|---|---|---|---|
| 1F1B | 0.214 | 149.2 | 1.00x | 402 | 294 | 2.373949 | 0.00e+00 |
| DualPipeV | 0.212 | 151.2 | 1.01x | 469 | 458 | 2.373949 | 2.38e-07 |
| DualPipeV + zero bubble | 0.208 | 154.2 | 1.03x | 469 | 461 | 2.373949 | 2.38e-07 |
| torch DualPipeV | 0.207 | 154.9 | 1.04x | 531 | 531 | 2.373949 | 2.38e-07 |
ranks=2 micro_batches=8 micro_batch_size=4 seq=256 hidden=512 heads=8 blocks=8 dtype=fp16 zero=1 measured steps=10 torch=2.10.0+cu128 device=cuda
| schedule | step time (s) | samples/s | vs 1F1B | peak mem rank 0 (MB) | peak mem rank 1 (MB) | final loss | max loss diff vs first |
|---|---|---|---|---|---|---|---|
| 1F1B | 0.094 | 341.6 | 1.00x | 362 | 323 | 2.378906 | 0.00e+00 |
| DualPipeV | 0.094 | 342.1 | 1.00x | 388 | 381 | 2.378906 | 0.00e+00 |
| DualPipeV + zero bubble | 0.109 | 292.7 | 0.86x | 388 | 385 | 2.378906 | 0.00e+00 |
| torch DualPipeV | 0.094 | 340.4 | 1.00x | 421 | 421 | 2.377197 | 1.71e-03 |
ranks=2 micro_batches=16 micro_batch_size=4 seq=256 hidden=512 heads=8 blocks=8 dtype=fp32 zero=0 measured steps=10 torch=2.10.0+cu128 device=cuda
| schedule | step time (s) | samples/s | vs 1F1B | peak mem rank 0 (MB) | peak mem rank 1 (MB) | final loss | max loss diff vs first |
|---|---|---|---|---|---|---|---|
| 1F1B | 0.422 | 151.5 | 1.00x | 400 | 310 | 2.372787 | 0.00e+00 |
| DualPipeV | 0.424 | 150.9 | 1.00x | 486 | 458 | 2.372787 | 2.38e-07 |
| DualPipeV + zero bubble | 0.423 | 151.3 | 1.00x | 489 | 462 | 2.372787 | 4.77e-07 |
| torch DualPipeV | 0.425 | 150.7 | 0.99x | 628 | 631 | 2.372787 | 4.77e-07 |
phoeenniixx
Summary
Follow-up to #8576. This adds two parts of the DualPipe reference that the first DualPipeV PR left out.
Changes
WeightPassinstruction: The weight chunks of the schedule now run the deferred weight gradients. Before, they only committed pending communication.WeightGradStore: Layers opt in through this class, exported fromdeepspeed.pipe. A layer needs a customtorch.autograd.Functionto defer its weight gradient. Other layers behave as before.DualPipeVModuletakes an optionaloverlapped_forward_backwardfunction. The engine calls it where the schedule runs a forward pass and the backward pass of another micro-batch back to back. Without it the forward pass runs first, as in the reference.Limitations
compiled_autograd.Tests
test_pipe_schedule.pychecks that everyWeightPasshas a deferred backward to serve. It also checks that none is left at the optimizer step.TestDualPipeVcompares loss and weights against the unpartitioned model. It runs withnn.Linear, with a linear layer that defers its weight gradients, and with an overlap function. Each runs with and without activation checkpointing.