Repository navigation
Row-shard Kimi attention residual and latent MoE tail #3587
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
whx-sjtu
wants to merge
22
commits into
task-3312-inproj-cpu-20261119
Choose a base branch
from
task-3312-row-tail-cpu-20261119
base: task-3312-inproj-cpu-20261119
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.
+888
−41
Open
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
92c31ae
Split Kimi attention residual and latent MoE tail by rows
whx-sjtu 1533280
Merge updated KDA projection parent into row-tail stack
whx-sjtu b6099cd
Merge reviewed Linear row-view parent into row-tail stack
whx-sjtu 0277eff
Merge refreshed KDA projection parent into row-tail stack
whx-sjtu 35fa940
Merge commit '3e8527a1d2fb6f9c7e4329c01054fe66abcc4d1b' into maint-35…
whx-sjtu d3f31f5
Preserve configured DCP size during pipeline initialization
whx-sjtu 7989461
Merge main and retain Engram owned-copy tests
whx-sjtu 0915e4a
Fuse closing residual bank writes into attention residual kernel
whx-sjtu 27e7369
Read required DCP width directly and clarify residual closes
whx-sjtu 7698b4a
Merge main and preserve DCP and preload initialization options
whx-sjtu 8839a3a
Merge main initialization policy and complete required test headers
whx-sjtu 6aba732
Cache Kimi row-tail environment thresholds [skip ci]
whx-sjtu e5a5962
Merge branch 'main' into task-3312-bank-cpu-20261119
whx-sjtu e03f22b
Keep row-sharding policy internal and model-local [skip ci]
whx-sjtu fe41a32
Clarify the fixed row-tail floor comment [skip ci]
whx-sjtu 0b6e5ba
Remove unreachable Kimi residual bank copy fallback
whx-sjtu c26e607
Merge branch 'main' into task-3312-bank-cpu-20261119
whx-sjtu 5029bf6
Merge branch 'main' into task-3312-bank-cpu-20261119
whx-sjtu 65793d2
Merge branch 'main' into task-3312-bank-cpu-20261119
whx-sjtu a9263ea
Merge current Kimi parent while preserving row-tail integration [skip…
whx-sjtu 03223ea
Merge the updated projection parent into Kimi row-tail work
whx-sjtu 1636423
Complete headers for the Kimi row-sharding source and tests
whx-sjtu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # SPDX-License-Identifier: MIT | ||
| """TP row sharding shared by residual and model-tail paths.""" | ||
|
|
||
| import torch | ||
| from aiter.dist.parallel_state import ( | ||
| get_tensor_model_parallel_rank, | ||
| get_tensor_model_parallel_world_size, | ||
| get_tp_group, | ||
| ) | ||
| from torch import nn | ||
|
|
||
| from atom.utils import envs | ||
|
|
||
|
|
||
| def _pad_rows(x: torch.Tensor, missing: int) -> torch.Tensor: | ||
| """`x` with `missing` zero rows appended on dim 0, for any rank.""" | ||
| if not missing: | ||
| return x | ||
| return nn.functional.pad(x, (0, 0) * (x.dim() - 1) + (0, missing)) | ||
|
|
||
|
|
||
| class SequenceParallel: | ||
| """Shard rows across TP ranks, gather rows, or scatter partial sums. | ||
|
|
||
| Pad to a multiple of the TP size and drop padded rows on gather. | ||
| Per-token work runs locally; consumers needing every row gather first. | ||
| """ | ||
|
|
||
| def __init__(self, num_tokens: int, tp_size: int, tp_rank: int) -> None: | ||
| self.num_tokens = num_tokens | ||
| self.chunk = -(-num_tokens // tp_size) | ||
| self.pad = self.chunk * tp_size - num_tokens | ||
| self.start = tp_rank * self.chunk | ||
| self.group = get_tp_group() | ||
|
|
||
| def shard(self, x: torch.Tensor) -> torch.Tensor: | ||
| """Replicated `[T, ...]` -> this rank's rows (a view unless padded). | ||
|
|
||
| Only the local rows are padded, never the whole replicated tensor.""" | ||
| rows = x[self.start : self.start + self.chunk] | ||
| return _pad_rows(rows, self.chunk - rows.shape[0]) | ||
|
|
||
| def gather(self, x: torch.Tensor) -> torch.Tensor: | ||
| """This rank's rows -> all `[T, ...]` rows.""" | ||
| out = self.group.all_gather(x.contiguous(), use_custom=False, dim=0) | ||
| return out[: self.num_tokens] if self.pad else out | ||
|
|
||
| def scatter(self, x: torch.Tensor) -> torch.Tensor: | ||
| """Per-rank partial sums over all `[T, ...]` rows -> summed local rows.""" | ||
| return self.group.reduce_scatter_tensor( | ||
| _pad_rows(x.contiguous(), self.pad), use_custom=False, dim=0 | ||
| ) | ||
|
|
||
| @classmethod | ||
| def maybe(cls, num_tokens: int) -> "SequenceParallel | None": | ||
| """Sharding for a forward of `num_tokens`, or None to stay replicated: | ||
| no TP, below `ATOM_QWEN4_EXP_SP_MIN_TOKENS`, or under graph capture. | ||
| The target model adds its own fused-quant floor | ||
| (`Qwen4ExpModel._sequence_parallel`); the MTP drafter, whose unfused | ||
| hyper-connections never quantize, uses this gate as is.""" | ||
| min_tokens = envs.ATOM_QWEN4_EXP_SP_MIN_TOKENS | ||
| if ( | ||
| min_tokens <= 0 | ||
| or num_tokens < min_tokens | ||
| or torch.cuda.is_current_stream_capturing() | ||
| ): | ||
| return None | ||
| tp_size = get_tensor_model_parallel_world_size() | ||
| if tp_size == 1: | ||
| return None | ||
| return cls(num_tokens, tp_size, get_tensor_model_parallel_rank()) |
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.
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.
env read + os.getenv on every decode step? cache both in init like _dual_stream_token_threshold..