-
Notifications
You must be signed in to change notification settings - Fork 96
feat: add Stem sparse attention module #286
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
4 commits
Select commit
Hold shift + click to select a range
cacab94
feat: add Stem sparse attention module
696e58c
fix: address review comments - add copyright, rename files, add kerne…
9d8b696
fix: add --prompt-file param to run_stem.sh, update doc usage
b41f74a
fix: update README_cn.md sparse attention table with Stem
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2025 Tencent Inc. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| """Stem — Sparse Token Estimation Module for long-context LLM inference. | ||
|
|
||
| Public API: | ||
| StemInference: Callable that patches a HuggingFace model to use Stem | ||
| sparse attention during the prefill stage. | ||
| """ | ||
|
|
||
| from .stem import StemInference | ||
|
|
||
| __all__ = ["StemInference"] |
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,20 @@ | ||
| # Copyright 2025 Tencent Inc. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| """Stem backend implementations (torch / HPC).""" | ||
|
|
||
| from .dispatcher import stem_forward | ||
|
|
||
| __all__ = ["stem_forward"] |
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,59 @@ | ||
| # Copyright 2025 Tencent Inc. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| """Backend dispatcher: routes Stem prefill to the correct implementation.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import torch | ||
|
|
||
| from .torch_impl import stem_forward_torch | ||
|
|
||
|
|
||
| def stem_forward( | ||
| query_states: torch.Tensor, | ||
| key_states: torch.Tensor, | ||
| value_states: torch.Tensor, | ||
| prefill_kwargs: dict, | ||
| ) -> torch.Tensor: | ||
| """Dispatch a Stem prefill call to the appropriate backend. | ||
|
|
||
| Args: | ||
| query_states: Query tensor of shape ``(B, H_q, L_q, D)``. | ||
| key_states: Key tensor of shape ``(B, H_kv, L_kv, D)``. | ||
| value_states: Value tensor of shape ``(B, H_kv, L_kv, D)``. | ||
| prefill_kwargs: Must contain ``"attn_forward_config"`` (with a | ||
| ``"backend"`` key) and ``"layer_idx"``. | ||
|
|
||
| Returns: | ||
| Attention output tensor of shape ``(B, H_q, L_q, D)``. | ||
|
|
||
| Raises: | ||
| ValueError: If the requested backend is not ``"torch"`` or ``"hpc"``. | ||
| """ | ||
| config = prefill_kwargs["attn_forward_config"] | ||
| backend = config.get("backend", "torch") | ||
|
|
||
| if backend == "torch": | ||
| return stem_forward_torch(query_states, key_states, value_states, prefill_kwargs) | ||
|
|
||
| if backend == "hpc": | ||
| # Lazy import to avoid hard dependency on the ``hpc`` C++ extension | ||
| # when only the pure-torch path is needed. | ||
| from .hpc_impl import stem_forward_hpc | ||
|
|
||
| return stem_forward_hpc(query_states, key_states, value_states, prefill_kwargs) | ||
|
|
||
| raise ValueError(f"Unknown stem backend: {backend!r}") | ||
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.