Add Flash Attention Support to Deepseek v4 - #4488
Open
octatrifan wants to merge 15 commits into
Open
Conversation
octatrifan
force-pushed
the
octatrifan-dsv4-flash-attention
branch
7 times, most recently
from
July 17, 2026 00:42
feb7323 to
caf292e
Compare
octatrifan
force-pushed
the
octatrifan-dsv4-flash-attention
branch
from
July 17, 2026 01:46
caf292e to
cf4d118
Compare
…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
octatrifan
marked this pull request as ready for review
July 28, 2026 20:09
octatrifan
requested review from
RissyRan,
jiangjy1982,
parambole,
shuningjin and
suexu1025
as code owners
July 28, 2026 20:09
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
octatrifan
requested review from
A9isha,
NuojCheng,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
darisoy,
dipannita08,
gagika,
hengtaoguo,
huytransformer,
igorts-git,
khatwanimohit,
richjames0,
shralex,
vipannalla and
xibinliu
as code owners
July 28, 2026 20:09
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Description
This PR introduces Flash (Splash) Attention Support for DeepSeek-V4 Heavily Compressed Attention (HCA) and Compressed Sparse Attention (CSA) layer.
Why is this change being made?
DeepSeek-V4's HCA (ratio 128) and CSA (ratio 4) require specialized block-aligned attention masks. This PR integrates these layers with TPU-optimized Pallas/Splash attention kernels (
tokamax_splash_kernel), allowing dynamic block-masked execution directly in the forward loop.Problem Solved
Specific Implementation Details
indexer_masksupport for CSA and HCA that can be passed into the Pallas Splash Kernel (Structured correct causal and sliding window masking sequences for CSA and HCA)sa_block_kv) insideCompressedAttentionthat prepends padding to compressed KV blocks (to make sure the mask is a multiple of the block size, as required by the splash Attention kernel)Tests
The changes were verified using compilation, execution, and numerical parity checks on Cloud TPU VMs:
Compressed Attention Compilation Validation:
CompressedAttentionTesttotests/unit/attention_test.pyto compile and run HCA (ratio 128) and CSA (ratio 4) configurations under both CPU (dot_product) and TPU (flash) attention paths.python3 tests/unit/attention_test.pyCorrectness check against PyTorch Reference:
tests/unit/deepseek_v4_vs_reference_test.pyto confirm that output logits match the PyTorch reference implementation within a strict tolerance.python3 tests/unit/deepseek_v4_vs_reference_test.pyChecklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.