Perf: accelerate DeepSeek V4 Flash prefill sparse attention - #834
Merged
zhangqi-chen merged 3 commits intoJul 28, 2026
Merged
Conversation
added 2 commits
July 25, 2026 14:12
- Pass aligned compressed-row counts through SWA, CSA, and HCA - Bound gather, QK/PV, and merge loops to each token's active blocks Standalone sparse-attention median run mean 635.2 -> 609.6 us (ratio 4, 128 tokens, a2a3 auto device, 3 x 20 rounds).
- Fan score work over 8-token groups while retaining cache-block reuse - Cap the useful top-k prefix at 256 and remove the redundant final merge Standalone indexer latency 952.9 -> 488.7 us (128 tokens, start-pos 896, a2a3, 20 rounds).
📝 WalkthroughWalkthroughThe PR adds per-token compressed-slot counts to packed prefill sparse attention, updates CSA, HCA, and SWA call sites, changes indexer score tiling and top-k sorting, and fuses sparse attention scoring with online-softmax merging. ChangesSparse prefill count propagation
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant PrefillIndexer
participant CSA_HCA_SWA
participant PrefillSparseAttn
PrefillIndexer->>CSA_HCA_SWA: provide sparse indices and visible compressed range
CSA_HCA_SWA->>CSA_HCA_SWA: compute per-token cmp_counts
CSA_HCA_SWA->>PrefillSparseAttn: pass cmp_counts with sparse metadata
PrefillSparseAttn->>PrefillSparseAttn: gather valid compressed KV slots
PrefillSparseAttn->>PrefillSparseAttn: merge attention outputs over active blocks
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
- Keep QK/PV and online merge in separate SPMD tasks - Add an explicit dependency so AIC-produced statistics publish before the vector merge consumes them - Prevent nondeterministic attention output and full-prefill stalls Validated on a2a3 with sparse, CSA, HCA, SWA, EP2 prefill_fwd, and eight-device DeepSeek serving accuracy.
hashiqiqixian
force-pushed
the
perf/dsv4-prefill-sparse-attn
branch
from
July 27, 2026 04:39
0d3c4f1 to
cc78191
Compare
MaxwellF1
added a commit
to MaxwellF1/pypto-lib
that referenced
this pull request
Jul 28, 2026
Rebased onto main after hw-native-sys#834, which independently landed an overlapping optimisation. Where the two differ this keeps whichever measured better: - Sparse-attention block skipping switches from hw-native-sys#834's cmp_counts (a per-token compressed-row count, which can only express "the first N blocks are live" and always treats the whole WIN window as live) to an explicit per-block valid_block_mask. Tokens with abs_pos < WIN can then skip genuinely empty window blocks, which is most of a 128-token prefill. CSA also stops re-filtering the indexer top-k per token and passes cmp_topk_indices straight through. - Indexer: TOPK_TILE 16 -> 4 for a finer top-k SPMD fan-out, and the Q RoPE folds into the Hadamard-quant loop, dropping the qr_rope_out staging tensor and the separate prefill_idx_qr_rope pass. - Keeps hw-native-sys#834's two-stage mrgsort and INDEXER_TOPK_CAP prefix: measured identical to the three-stage/IDX_TOPK variant (221.4 vs 224.4 us min), and since visible_t is capped at INDEXER_SCORE_CAP=256 the extra 256 top-k slots can never select a finite score. - Adds topk_prefix_contract_error, asserting the top-k visible prefix is dense, unique and in range with a -1 tail. a2a3, PYPTO_BENCH 50 rounds (prefill_layer 20, 2 cards), min us: prefill_indexer 305.6 -> 220.0 -28% prefill_attention_csa 1189.9 -> 979.3 -18% prefill_attention_hca 898.3 -> 790.7 -12% prefill_sparse_attn 506.3 -> 444.9 -12% prefill_attention_swa 812.4 -> 785.6 -3% prefill_layer 2812.1 -> 2563.6 -9% All six validate against Golden on a2a3.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Performance
Validation
prefill_fwd.py -p a2a3test passes without scheduler stalls.