Plumbing and core MoE logic for router replay#3881
Open
xuefgu wants to merge 1 commit into
Open
Conversation
11a4a0c to
5877c95
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Collaborator
|
@gemini-cli /investigate |
|
🤖 Hi @shralex, 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 @shralex, but I was unable to process your request. Please see the logs for more details. |
e090d93 to
7c33eb9
Compare
7c33eb9 to
13280fd
Compare
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.
Description
This PR introduces forced expert routing in MaxText’s MoE blocks to support RL training (e.g., GRPO). In this pipeline, completions are generated on vLLM, and their exact expert routing selections must be strictly enforced during training on MaxText to prevent expert selection mismatch and policy gradient divergence.
The externally supplied 4D routing tensor ([bs, seq_len, num_layers, k]) enters via TunixMaxTextAdapter and the top-level Transformer model. It is then sliced layer-by-layer inside decoders.py (Linen) or nnx_decoders.py (NNX) and routed down through model-specific decoder layers (DeepSeek, Qwen3, Qwen3.5, Gemma4, and Mixtral) into the core MoE block (moe.py), where it overrides the model-determined expert selections.
The implementation also implements out-of-bounds safety by mapping vLLM's padding sentinel values (-1) to valid, evenly distributed dummy expert indices before calling JAX bincount in permute(), ensuring hardware load balancing and performance. It also preserves backpropagation to router weights by executing manual gating logit extraction based on the forced selections.
This PR contains the core plumbing, edge case handling, and unscanned decoder loop support. A follow-up PR will support forced expert routing for scan_layers=True.
Tests
Unit tests.
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.