Skip to content

Add Flash Attention Support to Deepseek v4 - #4488

Open
octatrifan wants to merge 15 commits into
mainfrom
octatrifan-dsv4-flash-attention
Open

Add Flash Attention Support to Deepseek v4#4488
octatrifan wants to merge 15 commits into
mainfrom
octatrifan-dsv4-flash-attention

Conversation

@octatrifan

@octatrifan octatrifan commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

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

  • Enabled compilation and execution of compressed attention kernels on TPUs.
  • This change allows running DSV4 with increased sequence lengths that would OOM on the dot-product path. Performance testing coming soon.

Specific Implementation Details

  • Added indexer_mask support 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)
  • Added block-size padding alignment (sa_block_kv) inside CompressedAttention that 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:

  1. Compressed Attention Compilation Validation:

    • Appended CompressedAttentionTest to tests/unit/attention_test.py to compile and run HCA (ratio 128) and CSA (ratio 4) configurations under both CPU (dot_product) and TPU (flash) attention paths.
    • Command to reproduce: python3 tests/unit/attention_test.py
  2. Correctness check against PyTorch Reference:

    • Executed tests/unit/deepseek_v4_vs_reference_test.py to confirm that output logits match the PyTorch reference implementation within a strict tolerance.
    • Command to reproduce: python3 tests/unit/deepseek_v4_vs_reference_test.py
    • Due to precision change on TPU, we were forced to increase the tolerance for some of the tests to ensure they pass on TPU.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents.

@octatrifan
octatrifan force-pushed the octatrifan-dsv4-flash-attention branch 7 times, most recently from feb7323 to caf292e Compare July 17, 2026 00:42
@octatrifan
octatrifan force-pushed the octatrifan-dsv4-flash-attention branch from caf292e to cf4d118 Compare July 17, 2026 01:46
…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).
… segment IDs, HCA truncation, and segment boundary mask
…igned sequence lengths and clean debug config types
@octatrifan
octatrifan marked this pull request as ready for review July 28, 2026 20:09
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 46.37681% with 37 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/maxtext/layers/attention_compressed.py 35.89% 22 Missing and 3 partials ⚠️
src/maxtext/layers/attention_op.py 60.00% 9 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

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