Repository navigation
Fix: Gemma 3 & 4 Base Model and Flax Linen Decoding - #4066
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
from
June 4, 2026 14:21
c54cd4c to
39955c9
Compare
4 tasks
RexBearIU
marked this pull request as ready for review
June 4, 2026 15:01
RexBearIU
requested review from
NicoGrande,
NuojCheng,
RissyRan,
bvandermoon,
gagika,
gobbleturk,
jesselu-google,
jiangjy1982,
parambole,
richjames0,
shralex,
shuningjin and
suexu1025
as code owners
June 4, 2026 15:01
4 tasks done
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
from
June 4, 2026 16:18
39955c9 to
2eb6078
Compare
RexBearIU
requested review from
A9isha,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
darisoy,
dipannita08,
hengtaoguo,
igorts-git,
jacoguzo,
khatwanimohit and
vipannalla
as code owners
June 4, 2026 16:18
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
from
June 8, 2026 12:18
e755816 to
5c2a98d
Compare
RexBearIU
requested review from
Lumosis,
gpolovets1,
jrplatin,
mailvijayasingh,
mitalisi and
patemotter
as code owners
June 8, 2026 12:18
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
3 times, most recently
from
June 12, 2026 08:15
355c904 to
59ca028
Compare
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
3 times, most recently
from
June 18, 2026 09:09
fca7f7c to
338f2fc
Compare
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
6 times, most recently
from
June 30, 2026 08:11
8bf05e7 to
d96b8a6
Compare
NuojCheng
reviewed
Jun 30, 2026
| for i in range(scan_length): | ||
| start_idx = i * block_len | ||
| for offset, updated_item in enumerate(unstacked[i]): | ||
| kv_caches[start_idx + offset] = updated_item |
Collaborator
There was a problem hiding this comment.
is kv_caches a python list, not jax array? jax array is not mutable. If so, use if isinstance(kv_caches) List to make sure kv_caches is a list
Collaborator
Author
There was a problem hiding this comment.
Yes, good catch. kv_caches is indeed a Python list containing JAX PyTrees. Since JAX arrays/PyTrees are immutable but we mutate the outer list structure in update_kv_caches_after_scan, I've added explicit isinstance(kv_caches, list) checks and validation tests to ensure it behaves correctly. I just pushed the fixes.
NuojCheng
approved these changes
Jun 30, 2026
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
from
July 1, 2026 08:10
d96b8a6 to
dbcc192
Compare
khatwanimohit
approved these changes
Jul 2, 2026
…NNX decoding layer KV-cache propagation
RexBearIU
force-pushed
the
jackyf/gemma3_4-base-decoding-fixes
branch
from
July 2, 2026 03:04
dbcc192 to
08239eb
Compare
4 tasks done
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 architectural configurations for Gemma3 and Gemma4, and implements critical decoding layer KV-cache propagation fixes for JAX/NNX
(
nnx_decoders.py) and Flax Linen (decoders.py) pipelines.Before this change, Flax Linen scanned blocks (
_apply_gemma3_scanned_blocksand_apply_gemma4_scanned_blocks) lacked mechanisms to propagateintermediate KV-caches across scanned layers under
nn.scan, causing end-to-end decodes to fail.Key Changes:
- Flax Linen Decoder (
src/maxtext/layers/decoders.py): Addedkv_cachesandattention_metadatapropagation across Gemma3/4 scanned blocksvia a stack/unstack mapping over
nn.scan.- JAX/NNX Decoder (
src/maxtext/layers/nnx_decoders.py): Unified dynamic KV-cache carry updates within scanned block execution.- Model Scaffolding (
gemma3.py,gemma4.py): Addedkv_cacherouting through scannable blocks and layer inputs.Tests
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.