feat: add CSA + HCA block-pool attention compression (DeepSeek V4) - #11
Merged
Conversation
Introduces the interleaved Compressed Sparse Attention / Heavily Compressed Attention design from the DeepSeek V4 paper as a self-contained math module plus pipeline strategy. Mild block pooling gives query-dependent top-k selectivity; aggressive block pooling gives a global summary. A single softmax over the union of both views yields the attention output. Validated end-to-end against Qwen2.5-0.5B layer 8 on a 75-token prompt: the degenerate config (csa_m=1, top_k=S, hca_m=S) recovers full SDPA at cosine 1.0000, and 4x compression preserves cosine 0.96. 28 unit tests plus 786 existing tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Records logit cosine, top-1 agreement, perplexity, prefill/decode wall time, decode tokens/sec, and peak RSS delta on Qwen2.5 (0.5B and 3B). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Greedy-decode comparison via mlx_lm.stream_generate of baseline vs tqai 8/8 vs tqai 4/4 with cache_strategy='compressed'. Reports mx.get_peak_memory(), decode tps, and top-1 token agreement. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
tqai.csa_hcamodule: pure-torch math primitives implementing the DeepSeek V4 interleaved attention design (mild block pooling for query-dependent top-k selection + aggressive block pooling for a global summary, joined under a single softmax).CSAHCAStrategyregistered as"csa_hca"in the pipeline registry; compresses a KV tensor into both views and routes both through the configured quantizer.scripts/csa_hca_e2e.py) validating against Qwen2.5-0.5B.Validation
csa_m=1, top_k=S, hca_m=Srecovers full SDPA exactly.Test plan
scripts/csa_hca_e2e.py)tqai.attention_fused)csa_hca_attentioninto a model attention layer and measure generation perplexity🤖 Generated with Claude Code