Add HCA Static Compilation for Splash Attention [Deepseek v4] - #4924
Add HCA Static Compilation for Splash Attention [Deepseek v4]#4924octatrifan wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for DeepSeek-V4's Compressed Sparse Attention (CSA) and Heavily Compressed Attention (HCA) mechanisms, including overlapping window pooling, document-packing-aware masking, and sequence padding for Splash kernel alignment. It also adds extensive unit and parity tests. The reviewer feedback identifies a potential IndexError when decoder_segment_ids is None due to hardcoded indexing of the indexer mask, which can be resolved by dynamically squeezing size-1 dimensions. Additionally, the reviewer points out that compress_ratio is incorrectly inferred in HCAStaticMask for unaligned sequence lengths, suggesting explicitly passing compress_ratio through the attention pipeline. Finally, simplifying the pattern matching in configuration validation to a standard if statement is recommended for improved readability.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
🤖 Hi @octatrifan, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
There was a problem hiding this comment.
This PR introduces compile-time static mask generation (HCAStaticMask) for DeepSeek-V4 Heavily Compressed Attention (HCA), bypassing expensive runtime HBM mask allocation and dispatching directly to static Splash MQA kernels. The overall implementation is exceptionally clean, robust, and performs highly precise index/tile calculations to enable efficient static compilation and document packing.
🔍 General Feedback
- Math and Alignment Precision: The coordinate-based block sparsity computation in
HCAStaticMaskperfectly matches the mathematical window boundaries of the HCA compressor. - Performance & Scalability: Setting
dq_reduction_steps=3for Compressed Attention is a major highlight, drastically reducing unreduced gradient write traffic to HBM during backward pass. - Comprehensive Testing: The unit testing suite is highly comprehensive, covering unaligned sequence lengths, document packing boundaries, static mask parity, and error routing for unsupported Context Parallelism configurations.
parambole
left a comment
There was a problem hiding this comment.
Nice work on this — the HCAStaticMask approach and the upfront block padding.
A few questions from reading through it. Caveat up front: this is a static read, I
haven't run anything on TPU, so the first two may well be non-issues if the configs
that would trigger them can't occur in practice.
| # | Kind | Location | Question |
|---|---|---|---|
| 1 | Please check | attention_op.py — HCAStaticMask.__getitem__ |
Can pad_kv_total be smaller than pad_q? |
| 2 | Please check | attention_op.py — COMPRESSED mask branch |
Is use_tokamax_splash=False reachable here? |
| 3 | Question | attention_compressed.py — flash early return |
Does the non-TPU AR fallback matter for this model? |
| 4 | Suggestion | attention_test.py — HCAStaticMaskTest |
Test cases don't seem to reach the pad_m branch |
shuningjin
left a comment
There was a problem hiding this comment.
Thanks for the careful design to eliminate dense mask materialization in device memory! Some comments on fix and cleanup.
Add static Tokamax Splash Attention compilation for DeepSeek-V4 Heavily Compressed Attention (HCA). - Subclass splash Mask with HCAStaticMask implementing __getitem__ for compile-time sparse attention layout without runtime VPU partial block evaluation. - Route HCA compressed splash attention to make_splash_mqa for native MQA execution. - Handle unaligned sequences with upfront block padding and 1-to-1 dummy query row mapping to eliminate NaN gradients. - Decouple seq_len == 1 decode check from compressed_len == 0 for flash bypass. - Address all review comments (20-25) from @shuningjin and @parambole. - Add comprehensive forward and backward equivalence tests vs dot-product attention.
89bd9a5 to
678cfaa
Compare
Description
Add static Tokamax Splash Attention compilation for DeepSeek-V4 Heavily Compressed Attention (HCA).
Previously, both Compressed Sparse Attention (CSA,
compress_ratio == 4) and HCA were dispatched through dynamic Splash Attention (make_dynamic_splash_mqa), requiringDeepseekV4HCACompressorto materialize dense(B, 1, S, S_comp)float mask arrays in HBM and build runtime indexer masks.Because HCA attention patterns are strictly deterministic (local sliding window + completed preceding compression windows) and do not rely on the indexer (unlike CSA, which requires a top-k step), we can construct the block mask analytically on CPU during AOT tracing. This PR introduces
HCAStaticMask, bypassing HBM mask allocation and dispatching directly to static Splash MQA kernels (make_splash_mqa).Key changes:
HCAStaticMask: Subclassessplash_attention_mask.Maskto evaluate coordinate-based block sparsity on CPU during tracing without runtime HBM mask materialization.DeepseekV4HCACompressorreturnsNonefor masks whenattention_kernel="flash".generate_attention_maskis now restricted strictly to CSA.num_kv_heads == 1tomake_splash_mqa, stripping singleton KV head dimensions for 2D Tokamax input requirements.dq_reduction_steps = 3forAttentionType.COMPRESSEDincreate_sa_configto enable in-SRAM circular ring buffer accumulation fordQ. (Without this change, the backward pass is much slower than the dynamic path)AttentionOp.tpu_flash_attentionfor unaligned sequence lengths (e.g.ValueErrorchecks blocking Context Parallelism (cp_size > 1) and Ulysses/USP whenAttentionType.COMPRESSEDis active, pending future asymmetric communication support.Details in b/537346777
Performance
We conduct microbenchmarks (just on the attention layer) on a v5p TPU for DeepSeek-V4 Flash HCA.
Tests
Tested on Cloud TPU v5p:
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.