-
Notifications
You must be signed in to change notification settings - Fork 46
feat(deepseek/v4): gather-free split-half RoPE for the decode path (draft, decode-only) #564
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
Draft
Hzfengsy
wants to merge
1
commit into
hw-native-sys:main
Choose a base branch
from
Hzfengsy:feat/deepseek-v4-decode-rope-splithalf
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.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -141,6 +141,12 @@ def attention_hca( | |||||||||||||
|
|
||||||||||||||
| rope_cos_t = pl.create_tensor([T, ROPE_HEAD_DIM], dtype=pl.BF16) | ||||||||||||||
| rope_sin_t = pl.create_tensor([T, ROPE_HEAD_DIM], dtype=pl.BF16) | ||||||||||||||
| # Half-width unsigned inverse-RoPE snapshots for the split-half sparse_attn_hca: the first | ||||||||||||||
| # HALF columns of the per-token cos/sin (one value per frequency), cast BF16->FP32 once per | ||||||||||||||
| # token so the per-head rope loop rotates with no in-loop cast and no gather. Same first-HALF | ||||||||||||||
| # columns qkv_proj_rope's forward rope consumes -> a single rope profile, no separate il table. | ||||||||||||||
| rope_cos_half_t = pl.create_tensor([T, ROPE_HEAD_DIM // 2], dtype=pl.FP32) | ||||||||||||||
| rope_sin_half_t = pl.create_tensor([T, ROPE_HEAD_DIM // 2], dtype=pl.FP32) | ||||||||||||||
| cmp_cos = pl.create_tensor([B, ROPE_HEAD_DIM // 2], dtype=pl.FP32) | ||||||||||||||
| cmp_sin = pl.create_tensor([B, ROPE_HEAD_DIM // 2], dtype=pl.FP32) | ||||||||||||||
| with pl.at(level=pl.Level.CORE_GROUP, name_hint="hca_rope"): | ||||||||||||||
|
|
@@ -160,6 +166,10 @@ def attention_hca( | |||||||||||||
| step_sin_row = pl.cast(freqs_sin[pos_b : pos_b + 1, 0 : ROPE_HEAD_DIM], target_type=pl.FP32) | ||||||||||||||
| rope_cos_t[t : t + 1, 0 : ROPE_HEAD_DIM] = pl.cast(step_cos_row, target_type=pl.BF16, mode="rint") | ||||||||||||||
| rope_sin_t[t : t + 1, 0 : ROPE_HEAD_DIM] = pl.cast(step_sin_row, target_type=pl.BF16, mode="rint") | ||||||||||||||
| rope_cos_half_t[t : t + 1, 0 : ROPE_HEAD_DIM // 2] = pl.cast( | ||||||||||||||
| freqs_cos[pos_b : pos_b + 1, 0 : ROPE_HEAD_DIM // 2], target_type=pl.FP32) | ||||||||||||||
| rope_sin_half_t[t : t + 1, 0 : ROPE_HEAD_DIM // 2] = pl.cast( | ||||||||||||||
| freqs_sin[pos_b : pos_b + 1, 0 : ROPE_HEAD_DIM // 2], target_type=pl.FP32) | ||||||||||||||
|
Comment on lines
+169
to
+172
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of performing redundant slicing and casting on
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| q = pl.create_tensor([T, H, HEAD_DIM], dtype=pl.BF16) | ||||||||||||||
| kv = pl.create_tensor([T, HEAD_DIM], dtype=pl.BF16) | ||||||||||||||
|
|
@@ -262,8 +272,8 @@ def attention_hca( | |||||||||||||
| cmp_block_table, | ||||||||||||||
| topk_all, | ||||||||||||||
| attn_sink, | ||||||||||||||
| rope_cos_t, | ||||||||||||||
| rope_sin_t, | ||||||||||||||
| rope_cos_half_t, | ||||||||||||||
| rope_sin_half_t, | ||||||||||||||
| wo_a, | ||||||||||||||
| wo_b, | ||||||||||||||
| wo_b_scale, | ||||||||||||||
|
|
@@ -384,10 +394,15 @@ def golden_attention_hca(tensors): | |||||||||||||
| freqs_sin = tensors["freqs_sin"] | ||||||||||||||
| rope_cos_T = torch.empty(T, rd, dtype=freqs_cos.dtype) | ||||||||||||||
| rope_sin_T = torch.empty(T, rd, dtype=freqs_sin.dtype) | ||||||||||||||
| # Half-width unsigned inverse-RoPE tables for the split-half sparse_attn_hca golden. | ||||||||||||||
| rope_cos_half_T = torch.empty(T, rd // 2, dtype=torch.float32) | ||||||||||||||
| rope_sin_half_T = torch.empty(T, rd // 2, dtype=torch.float32) | ||||||||||||||
| for t in range(T): | ||||||||||||||
| pos = int(position_ids[t].item()) | ||||||||||||||
| rope_cos_T[t] = freqs_cos[pos] | ||||||||||||||
| rope_sin_T[t] = freqs_sin[pos] | ||||||||||||||
| rope_cos_half_T[t] = freqs_cos[pos, : rd // 2].float() | ||||||||||||||
| rope_sin_half_T[t] = freqs_sin[pos, : rd // 2].float() | ||||||||||||||
|
|
||||||||||||||
| # q + win kv (W8A8 q_proj) | ||||||||||||||
| q = torch.zeros(T, H, HEAD_DIM, dtype=torch.bfloat16) | ||||||||||||||
|
|
@@ -480,8 +495,8 @@ def golden_attention_hca(tensors): | |||||||||||||
| "cmp_block_table": cmp_block_table, | ||||||||||||||
| "cmp_sparse_indices": topk_all, | ||||||||||||||
| "attn_sink": tensors["attn_sink"], | ||||||||||||||
| "freqs_cos": rope_cos_T, | ||||||||||||||
| "freqs_sin": rope_sin_T, | ||||||||||||||
| "rope_cos_half": rope_cos_half_T, | ||||||||||||||
| "rope_sin_half": rope_sin_half_T, | ||||||||||||||
| "wo_a": tensors["wo_a"], | ||||||||||||||
| "wo_b": tensors["wo_b"], | ||||||||||||||
| "wo_b_scale": tensors["wo_b_scale"], | ||||||||||||||
|
|
||||||||||||||
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.
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.
Instead of performing redundant
pl.sliceandpl.castoperations onfreqs_cosandfreqs_sinagain, you can slicecos_rowandsin_rowdirectly, as they are already sliced and cast topl.FP32on lines 203-204. This avoids unnecessary overhead and is consistent with the implementation indecode_attention_swa.py.