Skip to content

Commit c98a34e

Browse files
committed
feat: implement DeepSeek-V4 compressed attention layers (HCA, CSA, LightningIndexer)
Implement compressed attention mechanisms and indexer modules for DeepSeek-V4 integration into MaxText: - CSACompressor & HCACompressor: Long-range attention compressors supporting causal block bias and YaRN frequency scaling decoupling. - LightningIndexer: Memory-efficient indexer module implementing sentinel masking and dynamic RoPE scaling. - Configuration: Register attention compression hyperparameters (compress_ratios, index_head_dim, sliding_window) in types.py and base.yml. - Parity verification: Extended unit test suite (deepseek_v4_vs_reference_test.py) validating attention compression parity against PyTorch reference implementations at atol=1e-5, rtol=1e-5.
1 parent c92f2e0 commit c98a34e

4 files changed

Lines changed: 1501 additions & 26 deletions

File tree

src/maxtext/configs/base.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ qk_clip_threshold: 100.0 # Threshold for clipping (tau in the paper)
405405
fused_qkv: False
406406
fused_mlp: False
407407

408+
# DeepSeek-V4 Compressed Attention parameters
409+
compress_rope_theta: 160000.0
410+
compress_ratios: []
411+
index_head_dim: 128
412+
index_n_heads: 64
413+
index_topk: 512
414+
o_groups: 8
415+
o_lora_rank: 1024
416+
sliding_window: 128
417+
408418
record_internal_nn_metrics: 0
409419

410420
# Output directory

src/maxtext/configs/types.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,22 @@ class AttentionIndexer(BaseModel):
618618
indexer_loss_scaling_factor: float = Field(0.0, description="Multiplier for the indexer KL divergence loss.")
619619

620620

621+
class DeepSeekV4AttentionConfig(BaseModel):
622+
"""Configuration specific to DeepSeek-V4 stateless compressed attention layers."""
623+
624+
compress_rope_theta: float = Field(160000.0, description="Theta base frequency for long-range compressor layers.")
625+
compress_ratios: list[int] = Field(
626+
default_factory=list,
627+
description="Layer-by-layer compressor rates (0: standard, 4: CSA, 128: HCA).",
628+
)
629+
index_head_dim: int = Field(128, description="Head dim for indexer query and key.")
630+
index_n_heads: int = Field(64, description="Number of query heads in indexer.")
631+
index_topk: int = Field(512, description="Number of tokens selected by indexer.")
632+
o_groups: int = Field(8, description="Number of group partitions for grouped linear output projection.")
633+
o_lora_rank: int = Field(1024, description="Low-rank output dimension prior to grouped mix projection.")
634+
sliding_window: int = Field(128, description="Sliding window size for attention.")
635+
636+
621637
class Llama4Attention(BaseModel):
622638
"""Configuration specific to Llama4-style models."""
623639

@@ -2224,6 +2240,7 @@ class MaxTextConfig(
22242240
MlaAttention,
22252241
MoBa,
22262242
AttentionIndexer,
2243+
DeepSeekV4AttentionConfig,
22272244
Llama4Attention,
22282245
SplashAttention,
22292246
PagedAttention,

0 commit comments

Comments
 (0)