Repository navigation
[DeepSeek-V4] Implement Compressed Attention Layers - #3866
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
5f54827 to
07eb3e2
Compare
37ee811 to
31329c5
Compare
07eb3e2 to
4520166
Compare
31329c5 to
22a57ff
Compare
4520166 to
10ca4f6
Compare
22a57ff to
32869e5
Compare
10ca4f6 to
31a5932
Compare
32869e5 to
c92f2e0
Compare
31a5932 to
c98a34e
Compare
|
🤖 Hi @parambole, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
d39e334 to
86e251f
Compare
fb88300 to
42ad560
Compare
5a4f487 to
7555c61
Compare
42ad560 to
b9900b8
Compare
Thanks for the review! Here is how we addressed these: nnx.Linear replaced: Fixed. All occurrences of nnx.Linear across the compressors, indexer, and projections have been replaced with MaxText's native DenseGeneral. Indexer reuse vs Base class: While I strongly considered a unified BaseIndexer, DeepSeek-V4's indexer requires complex 2m-overlapping window compression logic to pool KV states before scoring them. V3.2's indexer doesn't do this compression. Merging them into a single base class created too much structural friction. Instead, to remove redundancy, we extracted the overlapping compression math into a shared standalone function (csa_overlap_pooling) which is now cleanly reused by both CSACompressor and DeepseekV4Indexer. "Additional Branch of Sliding Window Attention": This simply means that the recent sliding window tokens are preserved uncompressed alongside the sparse compressed blocks. We implement this by appending the uncompressed KV entries and passing them down to AttentionOp, relying on generate_attention_mask to construct a banded causal sliding window mask over those recent entries. |
|
🤖 Hi @parambole, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
|
🤖 I'm sorry @parambole, but I was unable to process your request. Please see the logs for more details. |
|
🤖 Hi @RissyRan, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
|
🤖 I'm sorry @RissyRan, but I was unable to process your request. Please see the logs for more details. |
|
🤖 I'm sorry @RissyRan, but I was unable to process your request. Please see the logs for more details. |
RissyRan
left a comment
There was a problem hiding this comment.
Thanks for the change! From the conversation, I guess it's still non-trivial to re-use existing codebase (which was my hope). Left a few comments, and happy to chat more!
| num_kv_heads=1, | ||
| max_target_length=128, | ||
| mesh=None, | ||
| attention_kernel="dot_product", |
There was a problem hiding this comment.
Will you support flash attention in next step in the bringup?
There was a problem hiding this comment.
Yes. It will be supported as part of Phase 2
Hmm.. |
| # DeepSeek-V4 uses a mathematical attention sink (a learnable scalar per-head added to the | ||
| # attention logits prior to softmax, rather than a physical key/value token). We unconditionally | ||
| # initialize it here, overriding the base Attention class which disables it by default. | ||
| self.sinks = nnx.data(nnx.Param(jnp.zeros((self.num_query_heads,), dtype=self.dtype))) |
There was a problem hiding this comment.
Maybe can remove nnx.data, and directly use self.sinks=nnx.Param(...). Should use dtype=self.weight_dtype.
Actually, why not hard-code attention_sink=True in super().__init__ to use this:
maxtext/src/maxtext/layers/attentions.py
Lines 472 to 478 in 2e6cd11
There was a problem hiding this comment.
@shuningjin I originally tried removing nnx.data(...), but it actually causes the tests to fail with a ValueError. Because self.sinks = None in the base Attention class, Flax NNX strictly infers it as a static attribute. When we try to dynamically assign an nnx.Param to it in this subclass, Flax throws an exception for assigning a dynamic data value to a static attribute. Wrapping it in nnx.data(...) is currently the only way to explicitly override that static status! We'll need to keep the wrapper for now.
| return final_out | ||
|
|
||
|
|
||
| def compressed_attention( |
There was a problem hiding this comment.
For new modules, do we need to_linen wrapper? can we remove these? @bvandermoon
There was a problem hiding this comment.
We still need the compressed_attention free function and its to_linen wrapper for backwards compatibility with the legacy pipeline in our internal testing infrastructure. Even though we are actively migrating to NNX, we can't safely deprecate the Linen wrappers on this module just yet. Let's leave them in for now!
cc: @bvandermoon please correct me if I am wrong
There was a problem hiding this comment.
Thanks for the refactor! Approve to unblock. Please address the other comments.
Agree with @RissyRan for reuse when possible. Ideally we hope to overlap these as much as possible: (1) hca & csa, (2) dsv4_indexer & dsv32_indexer, (3) attention_compressed & attention. For (1), the initialization seems very similar; see inline comment.
Description
Implement compressed attention mechanisms and indexer modules required for DeepSeek-V4 integration into MaxText.
csa_overlap_pooling.compressed_maskto enforce sparsity natively insideAttentionOp.compress_ratios,compressed_rope_max_timescale,o_lora_rank, etc.) directly into the baseConfigclass intypes.py.Tests
Tested on CPU
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.