-
Notifications
You must be signed in to change notification settings - Fork 5.8k
add indexer-topk capture (V3.2 NSA + infra) #24392
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
8 commits
Select commit
Hold shift + click to select a range
7e4a778
consolidate routed-experts capturer onto reusable base
hnyls2002 655411f
add indexer-topk capture (V3.2 NSA + infra)
hnyls2002 331d45b
capture indexer topk on all forward_cuda exits; refuse non-CUDA
hnyls2002 1a4673d
Merge remote-tracking branch 'origin/main' into lsyin/indexer-topk-infra
hnyls2002 92f9933
trim comment redundancy
hnyls2002 6a47062
unify indexer-topk capture via module-level helper
hnyls2002 f360048
add return_indexer_topk e2e test on dsv3.2
hnyls2002 89ed01c
test_return_indexer_topk: add --dp 8 and cap max-total-tokens
hnyls2002 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
103 changes: 103 additions & 0 deletions
103
python/sglang/srt/layers/attention/indexer_topk_capturer.py
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,103 @@ | ||
| import logging | ||
| from typing import Optional | ||
|
|
||
| import numpy as np | ||
| import pybase64 | ||
| import torch | ||
|
|
||
| from sglang.srt.layers.dp_attention import get_attention_tp_size | ||
| from sglang.srt.layers.topk_capturer_base import BaseTopkCapturer | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class IndexerTopkCapturer(BaseTopkCapturer): | ||
| def __init__( | ||
| self, | ||
| num_tokens: int, | ||
| num_indexer_layers: int, | ||
| index_topk: int, | ||
| max_running_requests: int, | ||
| device: str, | ||
| ): | ||
| from sglang.srt.server_args import get_global_server_args | ||
|
|
||
| self.num_indexer_layers = num_indexer_layers | ||
| self.index_topk = index_topk | ||
|
|
||
| attn_tp_size = get_attention_tp_size() | ||
| assert attn_tp_size == 1, "IndexerTopkCapturer now only supports DP attention" | ||
|
|
||
| # DP-attention capture is per-rank-local: each rank writes [:local_batch, ...] | ||
| # to its own device_cache, so the buffer only needs to fit one rank's batch. | ||
| server_args = get_global_server_args() | ||
| max_batch_size = max(server_args.chunked_prefill_size, max_running_requests) | ||
|
|
||
| super().__init__( | ||
| num_tokens=num_tokens, | ||
| max_batch_size=max_batch_size, | ||
| num_layers=self.num_indexer_layers, | ||
| topk_size=self.index_topk, | ||
| device=device, | ||
| name="indexer_topk", | ||
| ) | ||
|
|
||
|
|
||
| _global_indexer_capturer: Optional[IndexerTopkCapturer] = None | ||
|
|
||
|
|
||
| def get_global_indexer_capturer() -> Optional[IndexerTopkCapturer]: | ||
| return _global_indexer_capturer | ||
|
|
||
|
|
||
| def set_global_indexer_capturer(capturer: Optional[IndexerTopkCapturer]): | ||
| global _global_indexer_capturer | ||
| _global_indexer_capturer = capturer | ||
|
|
||
|
|
||
| def maybe_capture_indexer_topk( | ||
| layer_id: int, topk_indices: Optional[torch.Tensor] | ||
| ) -> Optional[torch.Tensor]: | ||
| """Capture topk for layer_id if a capturer is set; pass through unchanged. | ||
|
|
||
| Works in both expression context (`return maybe_capture_indexer_topk(...)`) | ||
| and statement context (call for side-effect, ignore return). | ||
| """ | ||
| if topk_indices is None: | ||
| return None | ||
| if (cap := get_global_indexer_capturer()) is not None: | ||
| cap.capture(layer_id=layer_id, topk_indices=topk_indices) | ||
| return topk_indices | ||
|
|
||
|
|
||
| def extract_indexer_topk_from_meta_info(data): | ||
| # Mirrors extract_routed_experts_from_meta_info: indices are returned as | ||
| # base64-encoded int32 bytes. Caller reshapes to (seqlen-1, num_indexer_layers, | ||
| # index_topk). | ||
| indexer_topk_base64 = data["meta_info"].get("indexer_topk", None) | ||
| indexer_topk = np.frombuffer( | ||
| pybase64.b64decode(indexer_topk_base64.encode("utf-8")), dtype=np.int32 | ||
| ) | ||
| return indexer_topk | ||
|
|
||
|
|
||
| def create_indexer_capturer( | ||
| enable: bool, | ||
| num_indexer_layers: int, | ||
| index_topk: int, | ||
| num_tokens: int, | ||
| max_running_requests: int, | ||
| device: str, | ||
| ) -> Optional[IndexerTopkCapturer]: | ||
| if not enable: | ||
| return None | ||
| if num_indexer_layers == 0: | ||
| logger.warning("No indexer layers found, IndexerTopkCapturer disabled") | ||
| return None | ||
| return IndexerTopkCapturer( | ||
| num_tokens=num_tokens, | ||
| num_indexer_layers=num_indexer_layers, | ||
| index_topk=index_topk, | ||
| max_running_requests=max_running_requests, | ||
| device=device, | ||
| ) | ||
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
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.