-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Add DeepSeek-V4 CSA attention kernels #13
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
aws-zifan-he
wants to merge
7
commits into
main
Choose a base branch
from
zifan/deepseek_v4_csa
base: main
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ca48fd
create experimental kernel for deepseek v4 compressed sparse attentio…
aws-zifan-he d5e9467
style: apply ruff format and trim verbose comments in deepseek v4 csa
aws-zifan-he 0fb4176
test: add integration test for the deepseek v4 csa attention block
aws-zifan-he ec45b00
feat: add sparse prefill and sequence-parallel prefill to deepseek v4…
aws-zifan-he d1bd880
feat: multi-head layout support in the csa prefill rms+rope kernel
aws-zifan-he 8e649d4
feat: sparse prefill dispatch, hadamard indexer compressor, and 2-cor…
aws-zifan-he 2cbd299
feat: fuse rope+hadamard into the indexer q-projection and add a pack…
aws-zifan-he 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
src/nkilib_src/nkilib/experimental/deepseek_v4_csa/__init__.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 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. 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. | ||
|
|
||
| """DeepSeek-V4 Compressed Sparse Attention (CSA) kernels for Trainium3. | ||
|
|
||
| CSA replaces attention's full comparison against every past token with a | ||
| selection. The model keeps a compressed KV cache, a small "lightning indexer" | ||
| scores every compressed position, and the attention reads only the | ||
| ``index_topk`` highest-scoring positions plus a local sliding window. The compute | ||
| cost of the attention body is therefore O(``window_size`` + ``index_topk``) and | ||
| does not grow with the context length -- at a 32K context one decode block takes | ||
| 0.337 ms on Trainium3 in BF16. | ||
|
|
||
| Layout | ||
| ------ | ||
| ``csa_common`` | ||
| Config dataclasses and the host-side tables (RoPE, window bias) the kernels | ||
| take as inputs. | ||
| ``csa_decode_attention`` | ||
| Decode kernels, headlined by ``nki_indexer_score_topk_gather_2core`` -- the | ||
| fused megakernel that runs the indexer score, the GpSimd top-k and the O(k) | ||
| sparse attention in a single ``[2]``-grid launch. | ||
| ``csa_prefill_attention`` | ||
| Prefill kernels: the fused RMS+RoPE projection tail, the compressor, the | ||
| indexer's bisection top-k mask, and the two sparse-attention variants. | ||
| ``csa_tp_all_reduce`` | ||
| The 2-LNC ``ncc.all_reduce`` that sums the head-parallel output partials | ||
| across tensor-parallel ranks. | ||
| ``csa_block`` | ||
| The composition layer: complete prefill and decode attention blocks, plus a | ||
| runnable driver that grades them against ``csa_block_torch``. | ||
|
|
||
| These kernels use ``priority=`` DMA class-of-service hints, which are | ||
| NeuronCore-v4 only, so they target trn3. | ||
|
|
||
| Each module's own docstring carries the design rationale for what it holds: why the | ||
| sequence rather than the head axis is split below the rank boundary, why | ||
| ``name=`` on a ``shared_hbm`` allocation is load-bearing on a ``[2]``-grid kernel, | ||
| and how the snake layout that ``nisa.topk`` requires is assembled. | ||
| """ | ||
|
|
||
| from .csa_common import ( | ||
| CSAConfig, | ||
| CSAConfigFull, | ||
| precompute_freqs_cos_sin, | ||
| precompute_win_bias_parts, | ||
| shard_for_tp, | ||
| ) | ||
| from .csa_decode_attention import ( | ||
| nisa_topk_snake_kernel, | ||
| nki_decode_gather_ok_kernel, | ||
| nki_indexer_qproj_gemv, | ||
| nki_indexer_score_2core, | ||
| nki_indexer_score_kernel, | ||
| nki_indexer_score_topk_2core, | ||
| nki_indexer_score_topk_gather_2core, | ||
| nki_indexer_score_topk_kernel, | ||
| nki_qkv_rms_rope_kernel, | ||
| ) | ||
| from .csa_prefill_attention import ( | ||
| nki_compressor_core_kernel, | ||
| nki_fused_csa_attn_kernel, | ||
| nki_gather_csa_attn_kernel, | ||
| nki_indexer_score_mask_kernel, | ||
| nki_rms_rope_kernel, | ||
| ) | ||
| from .csa_tp_all_reduce import TPAllReduceNKI, nki_tp_all_reduce_kernel, tp_all_reduce | ||
|
|
||
| __all__ = [ | ||
| "CSAConfig", | ||
| "CSAConfigFull", | ||
| "TPAllReduceNKI", | ||
| "nisa_topk_snake_kernel", | ||
| "nki_compressor_core_kernel", | ||
| "nki_decode_gather_ok_kernel", | ||
| "nki_fused_csa_attn_kernel", | ||
| "nki_gather_csa_attn_kernel", | ||
| "nki_indexer_qproj_gemv", | ||
| "nki_indexer_score_2core", | ||
| "nki_indexer_score_kernel", | ||
| "nki_indexer_score_mask_kernel", | ||
| "nki_indexer_score_topk_2core", | ||
| "nki_indexer_score_topk_gather_2core", | ||
| "nki_indexer_score_topk_kernel", | ||
| "nki_qkv_rms_rope_kernel", | ||
| "nki_rms_rope_kernel", | ||
| "nki_tp_all_reduce_kernel", | ||
| "precompute_freqs_cos_sin", | ||
| "precompute_win_bias_parts", | ||
| "shard_for_tp", | ||
| "tp_all_reduce", | ||
| ] | ||
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.
[General comment] Can you add profiles with the PR. Decode and Prefill Profiles with few seqlen and some target sharding configs.