Add support for Qwen3-Next Model Architecture #2487
+1,741
−14
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.
TL;DR
What: This PR integrates the Qwen3-Next architecture into MaxText. Key features include a hybrid attention mechanism combining a Gated Delta Net (a form of linear attention) with standard attention, and an ultra-sparse MoE block that includes a shared expert.
How: By implementing several new, highly-optimized JAX layers (
Qwen3NextGatedDeltaNet
,Qwen3NextSparseMoeBlock
,Qwen3NextRMSNorm
), creating a hybridQwen3NextDecoderLayer
that alternates between attention types based on thefull_attention_interval
, and validating each new component against a PyTorch reference in a robust new test suite.Detailed Description
This pull request introduces an implementation of the Qwen3-Next architecture, as described in the official Qwen3-Next blog post.
Architectural Implementation (
src/MaxText/layers/qwen3.py
)Hybrid Attention: The core of this architecture is its hybrid attention system. This PR implements:
Qwen3NextGatedDeltaNet
: A full JAX implementation of the Gated Delta Net for efficient linear attention. The core recurrence is handled by the pure JAXjax_chunk_gated_delta_rule
, a parallel scan algorithm.Qwen3NextFullAttention
: A placeholder for the model's standard attention layers.Custom Normalization: A custom
Qwen3NextRMSNorm
layer is included where the learnable weight is initialized to zeros and applied as(1.0 + weight)
, matching the reference implementation's specific behavior.Decoder and Model Integration
Qwen3NextDecoderLayer
: This new decoder layer acts as the hybrid controller. Based on itslayer_idx
and thefull_attention_interval
config parameter, it conditionally invokes either theQwen3NextGatedDeltaNet
or theQwen3NextFullAttention
layer before passing the result to theQwen3NextSparseMoeBlock
.Scanned Layers: The implementation also introduces a
Qwen3NextScannableBlock
.Configuration: A new model config
qwen3-next-80b-a3b.yml
is added, along with the necessary parameters inbase.yml
to control the new architectural features.Testing and Validation
This PR includes a new test file,
tests/check_qwen3_next_vs_reference.py
.It directly copies the reference PyTorch implementations for each new component.
It provides granular unit tests that validate the numerical output of each JAX module (
Qwen3NextGatedDeltaNet
,Qwen3NextSparseMoeBlock
, etc.) against its corresponding PyTorch reference.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-review
label.