-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Opt tp: tp attn support tp reduce scattered input #10568
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a162eea
Opt tp: reduce-scattered input for attn input
xu-yfei cd3d937
add global attn tp context
xu-yfei 9319d22
pad for tokens
xu-yfei b702157
disable when enable_piecewise_cuda_graph
xu-yfei fede5e7
add switch --enable-attn-tp-input-scattered
xu-yfei b7bc56c
update arg enable-attn-tp-input-scattered decription
xu-yfei 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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Critical: reduce_scatter with tensor_split breaks for L % tp_size != 0
NCCL reduce_scatter requires equal chunk sizes; tensor_split yields uneven chunks when total_tokens is not divisible by tp_size, causing runtime errors.
Apply a pad-to-equal-chunk fallback (or use reduce_scatterv if available):
def _tp_reduce_scatter( self, hidden_states: torch.Tensor, residual: torch.Tensor, ) -> Tuple[torch.Tensor, torch.Tensor]: - if hidden_states.shape[0] == 0: - return hidden_states, hidden_states - - inputs = list(hidden_states.tensor_split(self._context.tp_size)) - scattered_local_tokens = inputs[self._context.tp_rank] - hidden_states = get_tp_group().reduce_scatter(scattered_local_tokens, inputs) - - if residual is not None: - residual = residual.tensor_split(self._context.tp_size)[ - self._context.tp_rank - ] - return hidden_states, residual + total = hidden_states.shape[0] + if total == 0: + return hidden_states, hidden_states + tp_size = self._context.tp_size + rank = self._context.tp_rank + # Equal-size path + if total % tp_size == 0: + chunk = total // tp_size + inputs = list(hidden_states.split(chunk, dim=0)) + out = torch.empty_like(inputs[rank]) + get_tp_group().reduce_scatter(out, inputs) + hidden_states = out + if residual is not None: + residual = residual.split(chunk, dim=0)[rank] + return hidden_states, residual + # Fallback: pad to equal chunks, then slice local + max_chunk = (total + tp_size - 1) // tp_size + pad = max_chunk * tp_size - total + if pad: + pad_shape = (pad,) + hidden_states.shape[1:] + hidden_states_padded = torch.cat( + [hidden_states, hidden_states.new_zeros(pad_shape)], dim=0 + ) + else: + hidden_states_padded = hidden_states + inputs = list(hidden_states_padded.split(max_chunk, dim=0)) + out = torch.empty_like(inputs[rank]) + get_tp_group().reduce_scatter(out, inputs) + local_len = total // tp_size + (1 if rank < (total % tp_size) else 0) + hidden_states = out[:local_len] + if residual is not None: + residual = residual.tensor_split(tp_size)[rank] + return hidden_states, residualIf reduce_scatterv is available in GroupCoordinator, prefer it; otherwise keep this padding path.
📝 Committable suggestion
🤖 Prompt for AI Agents
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.
torch.distributed.reduce_scatter(output, input_list, op=<RedOpType.SUM: 0>, group=None, async_op=False)
input_list ([list] List of tensors to reduce and scatter. Uneven sized tensors are supported. Self-verification on local datasets such as GSM8K and MMLU shows normal results.
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.
🧩 Analysis chain
🌐 Web query:
💡 Result:
Short answer: No — reduce_scatter (and reduce_scatter_multigpu / reduce_scatter_tensor) expects the input pieces to be sized consistently for the scatter and the output tensor size must be the same across ranks; uneven-sized tensors in the input_list are not supported. See the PyTorch docs stating the input must be the concatenation/stack of equal-sized per-rank outputs and that list lengths/sizes must match across processes. [1][2]
Sources:
🌐 Web query:
💡 Result:
Short answer: reduce_scatter requires each rank's output (the scattered piece) to have the same shape/size across ranks, so you cannot directly use torch.tensor_split to create uneven chunks and feed them into torch.distributed.reduce_scatter (or reduce_scatter_tensor) — the input must be a concatenation/stack of equal-sized output pieces. For uneven-shard use-cases you must either (a) pad chunks to equal size before reduce_scatter and then trim after, or (b) use higher-level DTensor/Shard abstractions (uneven sharding is experimental) or a custom collective that supports per-rank sizes. [1][2]
References
Sources
[1] PyTorch distributed reduce_scatter / reduce_scatter_tensor documentation. (pytorch.cadn.net.cn)
[2] PyTorch DTensor (redistribute / sharding notes about uneven sharding being experimental). (docs.pytorch.org)
Citations:
@xu-yfei, I appreciate you sharing the documentation reference. However, according to the official PyTorch documentation [1],
reduce_scatterrequires that the output tensor size must be the same across all ranks, meaning the input chunks must be equal-sized. The documentation states: "output must have the same size across ranks" and "input is the concatenation/stack of those outputs".Your tests on GSM8K and MMLU may be passing because:
tp_size=8(e.g., 1024, 2048, 4096)To verify this, could you please test the specific failing case mentioned by @miter6:
tp_size=8With
tensor_split, this creates uneven chunks:[128, 128, 128, 128, 128, 128, 128, 127], which violatesreduce_scatter's requirement. You can verify this with:Recommendation: Implement the padding approach suggested in the original review comment to handle cases where
total_tokens % tp_size != 0, or verify that yourget_tp_group().reduce_scatter()wrapper provides additional handling for uneven chunks (which the standard PyTorch API does not).Could you confirm by running a test with exactly 1023 tokens on TP8?
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.
The bench_serving verification is normal. One of the logs shows an input length of 1021:
Prefill batch. #new-seq: 1, #new-token: 1021, #cached-token: 3, token usage: 0.00, #running-req: 0, #queue-req: 0
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.