fix: resolve Gemma 3/4 RL rollout gibberish by unrolling scanned weights for vLLM adapter#4536
Merged
Merged
Conversation
mesakhcienet
force-pushed
the
fix/train_rl_issues
branch
from
July 22, 2026 04:15
5c0e8dc to
ce15699
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
mesakhcienet
marked this pull request as ready for review
July 22, 2026 08:18
mesakhcienet
requested review from
RissyRan,
abhinavclemson,
bvandermoon,
gagika,
gobbleturk,
khatwanimohit,
richjames0 and
vipannalla
as code owners
July 22, 2026 08:18
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
mesakhcienet
requested review from
A9isha,
NuojCheng,
SurbhiJainUSC,
aireenmei,
darisoy,
dipannita08,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
shralex,
suexu1025 and
xibinliu
as code owners
July 22, 2026 08:18
mesakhcienet
force-pushed
the
fix/train_rl_issues
branch
from
July 22, 2026 08:31
bd9a668 to
5244251
Compare
mesakh123
reviewed
Jul 22, 2026
hengtaoguo
approved these changes
Jul 22, 2026
Collaborator
|
Could you also take a look at the failing CPU tests and see if relevant? Thanks! |
SurbhiJainUSC
approved these changes
Jul 22, 2026
mesakhcienet
force-pushed
the
fix/train_rl_issues
branch
from
July 22, 2026 23:33
5244251 to
3d244ae
Compare
Collaborator
Author
Looks like it was from main branch, i have rebased our branch and the old failing CPU tests are passed. Thank you! |
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
Problem
When running
train_rl.pyRL rollouts for Gemma 3 and Gemma 4 withscan_layers=true, the vLLM integration generates pure gibberish (e.g.,"enenenen...").The root cause is a structural mismatch: Gemma 3/4's cyclic attention forces MaxText to split scanned layers into nested blocks (
layers.layers_0andlayers_remainder). Because Gemma relies on theMaxTextForCausalLMadapter (which expects flat, integer-keyed layers like standard Hugging Face models), the weight mapping silently failed. As a result, vLLM initialized with random weights, leading to the gibberish output.Solution
unroll_gemma_scanned_weightsinmaxtext_vllm_rollout.py. This function detects thelayers.layers_0structure and dynamically unrolls it into a flat, integer-keyed dictionary, ensuring exact compatibility with theMaxTextForCausalLMvLLM adapter.intkeys instead ofstr(e.g.,0instead of"0") to comply with the adapter'snnx.Listtarget structure.tests/end_to_end/tpu/gemma3/4b/test_gemma3_rl.shto explicitly include the requireduse_standalone_converter=trueflag.Why
use_standalone_converter=trueis required for Gemma 3/4Unlike Llama 3 or Qwen 3, Gemma 3 and 4 are bleeding-edge architectures that are not yet natively supported by upstream vLLM. To run them, MaxText overrides the vLLM model registry using a custom Python adapter (
MaxTextForCausalLM).Because of this custom adapter and Gemma's complex cyclic attention (
layers_0andlayers_remainder), the defaulttunixfast-mapping path (transfer_state_with_mappings) completely fails to match the tensor structures. Whentunixfails, it silently falls back to random weights (resulting in gibberish).Setting
use_standalone_converter=trueexplicitly bypassestunixand routes the weights through our custom conversion script—triggering the newunroll_gemma_scanned_weightslogic and ensuring the adapter receives a properly formatted dictionary.If you look at the exact configuration passed to the vLLM rollout adapter inside
src/maxtext/trainers/post_train/rl/train_rl.py (around line 499), you will see this explicitly definedflag:
The Optimization Hack: To save memory and time during the RL loop, MaxText does not ask vLLM to load real weights from disk. Because MaxText is about to stream the Actor's trained weights directly from its own memory over to vLLM, it tells vLLM to just boot up instantly using
jax.random(pure garbage initialization). This is whatrollout_vllm_init_with_random_weights=Truedoes.The tunix Failure Mode: tunix maps weights by matching dictionary keys. If a key from MaxText (e.g.
layers.layers_0) doesn't perfectly match a key in the vLLM model registry (e.g.layers.0), tunix simply skips that tensor. It does not crash or throw a fatal error.The Resulting State: Because tunix skipped those tensors, it never overwrites the garbage memory. vLLM proceeds to execute the decoding rollout using those skipped, randomly initialized weights.
When you feed random weights through a Transformer's attention layers, the math collapses into a repeating feedback loop of garbage tokens, which is exactly why it spit out
"enenenenenenenen".This single line of configuration (
rollout_vllm_init_with_random_weights=True) combined with tunix's silent failure mode is the definitive proof of why it generates gibberish!Tests
gemma3-4b unscan
gemma3-4b scan
use_standalone_converterandscan_layersshould be set totrueChecklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.