Add HCA Static Compilation for Splash Attention [Deepseek v4] - #4924
Add HCA Static Compilation for Splash Attention [Deepseek v4]#4924octatrifan wants to merge 52 commits into
Conversation
…lash Attention - Replace sequence integer-truncation with ceil-padding and -1e9 gate score masking in DeepseekV4HCACompressor. - Guard decoder segment ID sequence slicing with background token padding for unaligned document packing bounds. - Use explicit rotary_embedding.head_dim in CompressedAttention to accommodate non-standard RoPE head dimensions. - Add numerical equivalence testing between dot_product and flash attention kernels and verify non-power-of-two sequence lengths (e.g. L=489).
… PR 4488 config.head_dim
… segment IDs, HCA truncation, and segment boundary mask
…ig types validation conflict
…l sliding block size alignment
…igned sequence lengths and clean debug config types
…runcation of unaligned prompt lengths (e.g. 489 tokens)
…checker.py" This reverts commit 5cdbfc2.
- Add HCAStaticMask with causal masking on compressed tokens - Align KV sequence length to sa_block_kv multiples for static Tokamax Splash attention - Support unaligned sequence lengths in Tokamax SplashConfig using GCD block size calculation - Route HCA (compress_ratio > 4) to static Splash attention with indexer_mask=None
…cal tolerance docstring note
… and test coverage
…ation with adversarial leakage check
…ysses splash conflict
…or in config validation
…rst_window_position in CompressedAttention
- Add autoselected attention support to deepseek4 and indexer config validators. - Set default use_tokamax_splash: true for deepseek4-tiny and deepseek4-284b models. - Separate flash and dot_product attention test methods in DeepSeekV4CompressedAttentionTest with tpu_only markers. - Update reference parity dimensions in DeepSeekV4ConversionMappingTest and DeepSeekV4HyperHeadTest. - Adjust CompressedAttentionTest numerical tolerances for TPU matmul accumulation.
… dynamic jnp.pad in CSA
…and missing comments
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! |
Description
Implements a static Tokamax Splash Attention compilation path for DeepSeek-V4 Heavily Compressed Attention (HCA,
compress_ratio > 4). This replaces the dynamic indexer mask dispatch with a statically compiledHCAStaticMask.Context & Motivation
Previously, both CSA and HCA were routed through
make_dynamic_splash_mha. While CSA requires dynamic boolean masks generated at runtime by the top-k sparse indexer, HCA's compressed attention pattern is completely deterministic and static (local causal window + evenly spaced compressed KV tokens). Running HCA through the dynamic mask path added unnecessary mask materialization and Pallas dynamic grid dispatch overhead.Key Changes
HCAStaticMask: Implemented a computable mask for static Tokamax splash attention that handles the local sliding window, causal boundaries, and compressed token indices without generating runtime boolean masks.AttentionOp.tpu_flash_attentionfor both CSA and HCA. When sequence lengths are not multiples of the block size (e.g., length 3968 with block size 512), query tensors and segment IDs are padded to block boundaries before kernel dispatch, and outputs are sliced back to the original sequence length.jax.vmapbatch dispatch for the static splash kernel, ensuring full compatibility with context parallelism (CP) and per-sequence document packing.Tests
Validated locally and on Cloud TPU v5p8:
All test suites passed on TPU-v5p (including unaligned lengths like 489 and 3968, and packing equivalence checks).
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.