Skip to content

feat: add CSA + HCA block-pool attention compression (DeepSeek V4) - #11

Merged
pbertsch merged 8 commits into
mainfrom
feat/csa-hca
Jun 30, 2026
Merged

feat: add CSA + HCA block-pool attention compression (DeepSeek V4)#11
pbertsch merged 8 commits into
mainfrom
feat/csa-hca

Conversation

@pbertsch

@pbertsch pbertsch commented May 1, 2026

Copy link
Copy Markdown
Member

Summary

  • New tqai.csa_hca module: 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).
  • New CSAHCAStrategy registered as "csa_hca" in the pipeline registry; compresses a KV tensor into both views and routes both through the configured quantizer.
  • 28 unit tests + a real-model E2E script (scripts/csa_hca_e2e.py) validating against Qwen2.5-0.5B.

Validation

  • Sanity (cos = 1.0000): degenerate config csa_m=1, top_k=S, hca_m=S recovers full SDPA exactly.
  • Real Qwen2.5-0.5B layer 8, 75-token prompt: at 4× compression cos = 0.96, at 5.8× cos = 0.92.
  • Tests: 28 new tests + 786 existing tests all passing.

Test plan

  • Hand-calculated reference values for 8-token toy KV
  • Block pool / score / top-k / gather primitives covered with edge cases (partial remainder block, block_size > S, GQA)
  • End-to-end equivalence with full SDPA in degenerate config
  • Strategy compress / decompress roundtrip preserves shape
  • Strategy registry lookup
  • Real-model Q/K/V capture and cosine comparison (scripts/csa_hca_e2e.py)
  • Follow-up: replace mean pooling with learned softmax-gated pooling (matches DeepSeek paper more closely)
  • Follow-up: fused MLX/Metal kernel path for the combined attention (tqai.attention_fused)
  • Follow-up: substitute csa_hca_attention into a model attention layer and measure generation perplexity

🤖 Generated with Claude Code

pbertsch and others added 8 commits May 1, 2026 09:27
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>
@pbertsch
pbertsch merged commit e1c4a56 into main Jun 30, 2026
6 checks passed
@pbertsch
pbertsch deleted the feat/csa-hca branch June 30, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant