Add caching for GatedDeltaNetCache (linear attention models)#2971
Open
Rohan-Bierneni wants to merge 1 commit intomainfrom
Open
Add caching for GatedDeltaNetCache (linear attention models)#2971Rohan-Bierneni wants to merge 1 commit intomainfrom
Rohan-Bierneni wants to merge 1 commit intomainfrom
Conversation
15f871b to
7ec8a31
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
parambole
reviewed
Jan 28, 2026
parambole
reviewed
Jan 28, 2026
parambole
reviewed
Jan 28, 2026
parambole
reviewed
Jan 28, 2026
Collaborator
parambole
left a comment
There was a problem hiding this comment.
Thanks for making these changes. I have left a few comments. Additionally can you add a note on what scan layer settings are supported ?
6f9ef90 to
b49d574
Compare
32514bb to
5d03505
Compare
4 tasks
5d03505 to
b4f5fdf
Compare
Added support for GDN to maxengine but NNX linen incompatible Merged code from other branch Qwen3-next Modified to accept dynamic model mode and work with maxengine changes Fix GDN init with model_mode Do same cache update during packed prefill as normal prefill Convert batch to int in init for state remove new_cache and resolve comments from pr fix merge conflicts use maxtext instead of MaxText typo in import removed testcases remove circular import Add support for decoding with pdb > 1 Fix slicing bug when using batch_size > 1 Fix linter issues Fix linter issues and flatten conditionals for pylint uncommit pre-commit check
b4f5fdf to
15c1aa1
Compare
RissyRan
reviewed
Feb 21, 2026
Collaborator
There was a problem hiding this comment.
For this one, let's add one unit test like
maxtext/tests/unit/attention_test.py
Lines 1195 to 1241 in ec712bf
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.
PR: Add Caching Support for Qwen3-Next (Gated Delta Network)
Problem
The Qwen3-Next architecture utilizes a hybrid approach consisting of standard Self-Attention layers and Gated Delta Network (GDN) layers (a form of Linear Attention).
While MaxText’s existing KVCache handles standard attention (growing Key/Value history), it does not support the state management required for GDN. GDN layers require:
A Fixed-size Recurrent State: Compresses history into a constant-size matrix, rather than growing linearly with sequence length.
A Convolution State: A sliding window buffer for the short 1D convolution preceding the delta rule.
Without these, Qwen3-Next could not perform efficient autoregressive decoding; it would either need to recompute the entire history at every step or fail to capture temporal dependencies correctly.
This PR adds caching support to the Gated Delta Net implementation for Qwen3-Next. This is the first non-standard attention caching support in MaxText and required changes to both the MaxEngine code and the Qwen3-Next implementation.
Note: Decoding is currently ONLY supported in unscanned checkpoint format (scan_layers=False and stack_prefill_result_cache=False).
Solution
This PR implements specific caching infrastructure for Qwen3-Next's linear attention mechanism and integrates it natively into the MaxEngine workflow.
1. New Cache Implementation (src/MaxText/inference/kvcache.py)
2. Layer Logic & State Management (src/MaxText/layers/qwen3.py)
Conv & Recurrent Updates: Modified Qwen3NextGatedDeltaNet to properly slide the convolution window and update the recurrent state during MODEL_MODE_AUTOREGRESSIVE.
Chunked Prefill Support: Re-wrote MODEL_MODE_PREFILL logic to read from self.cache rather than defaulting to zeros. This allows the GDN state to seamlessly carry over between chunks for long prompts.
Dynamic Batch Alignment: Added jnp.broadcast_to and [:batch] slicing during cache retrieval. This dynamically bridges the gap between MaxEngine's global cache allocation (e.g., batch=64) and the active compute shape (e.g., batch=1 during prefill), preventing concatenation shape errors during XLA dummy-tracing passes.
3. MaxEngine Integration (src/MaxText/maxengine.py)
Non-sequence-growing Insertions: Updated _insert_jit and bulk_insert to handle fixed-size states. For GDN variables (recurrent_state, conv_state), the prefill step produces a "final state" which is directly copied into the decode cache slot (bypassing the sequence-length slicing logic used for standard KV caches).
Packed Prefill Guard: Added a NotImplementedError in insert_partial to block packed prefill for GDN states, preventing sequential memory from bleeding across independent packed prompts.
Logical Axes Fix: Updated init_decode_state to properly check for both .logical_axes and .names when unpacking LogicallyPartitioned variables, fixing a bug where empty tuples caused batch index crashes.
If the change fixes a bug or a Github issue, please include a link, e.g.,:
FIXES: b/448407748
Tests
Ran the MaxText.decode command and got meaningful output (v5p-64 & pdb=1): https://paste.googleplex.com/6411536458448896
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.