Gemma4e2b jax.checkpoint & remat fix - #4847
Open
mattdonati wants to merge 1 commit into
Open
Conversation
mattdonati
requested review from
NuojCheng,
RissyRan,
aireenmei,
bvandermoon,
gagika,
gobbleturk,
huytransformer,
igorts-git,
jiangjy1982,
parambole,
richjames0,
shralex,
shuningjin and
xibinliu
as code owners
August 12, 2026 01:30
There was a problem hiding this comment.
Code Review
This pull request refactors the layer application in _apply_gemma4_small_layers to use jax.lax.scan and jax.checkpoint for rematerialization. Feedback on these changes includes removing an unused variable extract_donor_kv, moving the inline import of xla_metadata outside of the loop, and hoisting loop-invariant computations (global_remat_policy and prevent_cse) outside of the decoder layers loop to avoid redundant overhead.
mattdonati
force-pushed
the
gemma4e2b_checkpoint_fix
branch
from
August 12, 2026 15:29
6799293 to
fc24032
Compare
mattdonati
force-pushed
the
gemma4e2b_checkpoint_fix
branch
from
August 12, 2026 18:13
fc24032 to
46e7797
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
When training the Gemma 4 Small (E2B/E4B) model with NEW_MODEL_DESIGN=1, XLA LICM was co-scheduling the unrolled 35 decoder layers concurrently, causing the peak HBM overhead during the backward pass to spike past 400GB+ for per device for max_target_length=24576 & per_device_batch_size=8
Before: max_target_length=24576 per_device_batch_size=8
Peak HBM: 421.33GiB
Before: max_target_length=8192 per_device_batch_size=1
Xprof: https://xprof.corp.google.com/memory_profile/mattdonati-7708346007755985034
Peak HBM: 46.61 GiB
Fix:
Modified _apply_gemma4_small_layers in src/maxtext/layers/nnx_decoders.py to securely wrap both the layer's forward pass and its KV extraction (compute_shared_kv) within a jax.checkpoint boundary.
Plumbed this boundary through a native jax.lax.scan(length=1) block armed with skip-simplify-while-loops_trip-count-one, isolating the compiler graph identically to how the standard _scan_global_layer operates in gemma4.py.
This optimization safely forces peak memory back down to ~75-90GB.
Notice 1: Once all tests pass, the "pull ready" label will automatically be assigned.
This label is used for administrative purposes. Please do not add it manually.
Notice 2: For external contributions, our settings currently require an approval from a MaxText maintainer to trigger CI tests.
Tests
After: max_target_length=24576 per_device_batch_size=8
Peak HBM: 82.94 GiB
Xprof: http://xprof.corp.google.com/trace_viewer/mattdonati-17081265437688384004
After: max_target_length=8192 per_device_batch_size=1
Peak HBM: 17.73 GiB
Xprof: https://xprof.corp.google.com/memory_profile/mattdonati-3052316813571309926
logits check
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.