Repository navigation
Fix lost TP all-reduce results for non-contiguous tensors #8837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jinyouzhi
wants to merge
3
commits into
deepspeedai:master
Choose a base branch
from
jinyouzhi:fix-tp-contiguous-allreduce
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import torch | ||
|
|
||
| import deepspeed.comm as dist | ||
| from deepspeed.accelerator import get_accelerator | ||
| from deepspeed.module_inject.layers import ColumnParallel, RowParallel | ||
|
|
||
| from unit.common import DistributedTest | ||
|
|
||
|
|
||
| class TestTensorParallelNonContiguousReduce(DistributedTest): | ||
| """The TP all-reduce must reach the returned tensor even when it is not contiguous. | ||
|
|
||
| Reducing a temporary from ``x.contiguous()`` silently drops the result for non-contiguous ``x``. | ||
| """ | ||
| world_size = 2 | ||
|
|
||
| def test_row_parallel_forward(self): | ||
| rank = dist.get_rank() | ||
| x = torch.full((3, 2), rank + 1.0, device=get_accelerator().device_name()).t() | ||
| assert not x.is_contiguous() | ||
|
|
||
| out = RowParallel.apply(dist.new_group([0, 1]), x, False) | ||
|
|
||
| assert torch.all(out == 3.0) | ||
|
|
||
| def test_column_parallel_backward(self): | ||
| group = dist.new_group([0, 1]) | ||
| rank = dist.get_rank() | ||
| x = torch.ones(3, 2, device=get_accelerator().device_name(), requires_grad=True) | ||
|
|
||
| # The transpose makes the incoming gradient non-contiguous. | ||
| out = ColumnParallel.apply(group, x).t() | ||
| (out * (rank + 1)).sum().backward() | ||
|
|
||
| assert torch.all(x.grad == 3.0) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_column_parallel_backwardcallsColumnParallel.applydirectly, but actual AutoTP training reaches this reduction throughLinearLayer.forward's process-group, overlap, and deferred-collective routing, so this test can pass without showing that a transposed downstream loss receives the fix through the user-facing path. Add a minimal injected-model forward/backward test whose layer output is transposed before the loss; training-loop contract changes require integration coverage rather than only primitive unit coverage.AGENTS.md reference: AGENTS.md:L76-L81
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I tried this, but an AutoTP-level test cannot reach the bug, so it would not guard the fix.
In-tree, these primitives only receive freshly computed tensors:
LinearLayer/SubParamLinearLayerwrap their input inColumnParallel, whose backward gradient comes frommatmul's backward, andLinearAllreduce/VocabParallelEmbedding/SubParamLinearAllreducefeedRowParallelamatmul/ embedding output. Those are contiguous whatever the caller does with the layer's input or output. I checked that transposed, expanded and permuted inputs and a transposed loss all still deliver a contiguous gradient toColumnParallel.backward.To confirm, I ran the suggested test (a
LinearLayer->LinearAllreducestack over a 2-rank TP group, output transposed before the loss, input gradient compared with an unsharded reference) against the code before this fix:test_tp_noncontiguous_reduce.py: both tests fail (each rank keeps only its own value)So the bug is latent for AutoTP layers today. It is reachable whenever these autograd functions get a non-contiguous tensor, e.g. a custom TP layer or a future caller, which is exactly the contract the primitive tests pin down. I kept the regression tests at that level, since a training-loop test would pass with or without the fix.