Skip to content

Rewrite Gemma4 scannable block - #4530

Merged
copybara-service[bot] merged 1 commit into
mainfrom
aireen/pr-gemma4-scan-rewrite
Jul 30, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
aireen/pr-gemma4-scan-rewrite

Conversation

@aireenmei

@aireenmei aireenmei commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Currently the implementation of Gemma4ScannableBlock makes XLA treat the whole 6-layer block as a "big layer". The all-gathers for all 6 layers are run at the beginning of each block. Similarly, in backward pass, remat runs the whole block. This causes elevated HBM usage and more exposed collectives. This PR builds on top of my previous PRs #4506 and #4507, which allows a more modular and clean implementation.

After this rewrite:

  • 5 local layers via jax.lax.scan, each per-layer jax.checkpoint
  • length-1 scan for global layer, also wrapped in jax.checkpoint
  • remove block-level remat and unroll outer block scan

Added after revision:

  • Refactor in gemma4.py
  • Fix ckpt conversion: update checkpoint conversion code for both to_maxtext and to_hf directions.
  • Fix gemma4-26b: support sowing of moe_lb_loss from layer intermediates to fix a bug breaking 26b model
  • Fix decode: add "local_layer" to logical rules to fix a bug breaking vllm decode introduced in Fix: Gemma 3 & 4 Base Model and Flax Linen Decoding  #4066

Tests

  • added unit test
  • Before rewrite: xprof for pdbs=4. xprof for pdbs=8
  • After rewrite: xprof for pdbs=4. xprof for pdbs=8 + vocab_tiling=8 (OOM without vocab_tiling).
  • Before rewrite, the HBM peak is not on vocab peak(screenshot). After rewrite, vocab peak becomes the highest (screenshot) due to other peaks become smaller, also by having remat boundary at layer level adds HBM usage and overlap with the vocab peak. Therefore, without vocab tiling, the peak HBM usage doesn't change much compared with original. But when combined with vocab_tiling, we can effectively lower the HBM peak a lot (70 GB -> 40 GB for pdbs=4).

Added after revision

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 CI Failure Investigation Report

I have analyzed the recent test failures in the CI pipeline and identified the following:

🔍 What Failed

  • Job/Matrix: code_quality_check / Static code-quality checkers
  • Failing Test: pre-commit checks on just the files that have changed
  • Error: Pylint and pyink code style violations

🪵 Error Details & Stack Trace

# Missing function docstring in Gemma4ScannableBlock helper method
src/maxtext/models/gemma4.py:496:2: C0116: Missing function or method docstring (missing-function-docstring)

# Line too long violations in model_test.py
tests/unit/model_test.py:238:0: C0301: Line too long (131/125) (line-too-long)
tests/unit/model_test.py:283:0: C0301: Line too long (131/125) (line-too-long)

# Formatting non-compliance reported by pyink
would reformat tests/unit/model_test.py
would reformat src/maxtext/models/gemma4.py
would reformat tests/unit/nnx_scan_test.py
would reformat tests/unit/nnx_decoders_test.py

💡 Root Cause Analysis & Context

Confidence: high (confirmed cause)

The failure is caused by code style and quality violations introduced in this PR. Specifically:

  1. In src/maxtext/models/gemma4.py, the new helper method _apply_local_layers was added without a docstring, triggering the pylint missing-function-docstring check.
  2. In tests/unit/model_test.py, the lines asserting logits shapes exceed the maximum length of 125 characters configured for pylint (each was 131 characters).
  3. The files tests/unit/model_test.py, src/maxtext/models/gemma4.py, tests/unit/nnx_scan_test.py, and tests/unit/nnx_decoders_test.py had spacing and line wraps that did not comply with the pyink formatting rules (indentation 2, line length 122).

These issues are purely style/quality regressions and do not affect behavioral correctness. They have been fully corrected and verified locally using pre-commit run.

🛠️ Recommended Fix

Apply the formatting changes and add the missing docstring to src/maxtext/models/gemma4.py. The required diffs are:

diff --git a/src/maxtext/models/gemma4.py b/src/maxtext/models/gemma4.py
index b974ec5..f626b8e 100644
--- a/src/maxtext/models/gemma4.py
+++ b/src/maxtext/models/gemma4.py
@@ -453,9 +453,7 @@ class Gemma4ScannableBlock(nnx.Module):
 
     pattern_length = len(GEMMA4_ATTENTION_PATTERN)
     if not 0 <= num_of_layers <= pattern_length:
-      raise ValueError(
-          f"Gemma4ScannableBlock must contain between 0 and {pattern_length} layers; got {num_of_layers}."
-      )
+      raise ValueError(f"Gemma4ScannableBlock must contain between 0 and {pattern_length} layers; got {num_of_layers}.")
 
     # Pattern is 5 local, 1 global.
     self.num_local = min(5, num_of_layers)
@@ -505,6 +503,8 @@ class Gemma4ScannableBlock(nnx.Module):
       bidirectional_mask=None,
       attention_metadata=None,
   ):
+    """Applies local attention layers sequentially using scan."""
+
     def apply_layer(layer, carry):
       layer_out = layer(
           carry,
@@ -654,9 +652,7 @@ class Gemma4ScannableBlock(nnx.Module):
         offload_names = maxtext_utils.get_save_and_offload_names(cfg)
         if offload_names[0] or offload_names[1]:
           save_names, offload_to_device = offload_names
-          global_remat_policy = jax.checkpoint_policies.save_only_these_names(
-              *(save_names + offload_to_device)
-          )
+          global_remat_policy = jax.checkpoint_policies.save_only_these_names(*(save_names + offload_to_device))
 
         if self.apply_internal_remat and self.config.remat_policy != "none":
           prevent_cse = maxtext_utils.should_prevent_cse_in_remat(self.config)
@@ -669,9 +667,7 @@ class Gemma4ScannableBlock(nnx.Module):
         # Carry state through the loop instead of returning a stacked [1, ...]
         # scan result: slicing that result previously introduced a bitcast
         # between device and pinned-host memory under offload remat.
-        with xla_metadata.set_xla_metadata(
-            **{"skip-simplify-while-loops_trip-count-one": "true"}
-        ):
+        with xla_metadata.set_xla_metadata(**{"skip-simplify-while-loops_trip-count-one": "true"}):
           (y, global_state), _ = jax.lax.scan(
               scan_global_layer,
               (y, state_g),
diff --git a/tests/unit/model_test.py b/tests/unit/model_test.py
index 7c3d4ea..dd679ff 100644
--- a/tests/unit/model_test.py
+++ b/tests/unit/model_test.py
@@ -235,7 +235,9 @@ class TestModel(unittest.TestCase):
         enable_dropout=False,
         model_mode=MODEL_MODE_TRAIN,
     )
-    self.assertEqual(logits.shape, (new_config.global_batch_size_to_train_on, new_config.max_target_length, new_config.vocab_size))
+    self.assertEqual(
+        logits.shape, (new_config.global_batch_size_to_train_on, new_config.max_target_length, new_config.vocab_size)
+    )
 
   def test_gemma4_model_linen(self):
     """Test the shared Gemma4 scannable block on the linen (ToLinen) path.
@@ -280,7 +282,9 @@ class TestModel(unittest.TestCase):
         rngs={"aqt": self.rng},
     )
     logits = logits[0] if isinstance(logits, tuple) else logits
-    self.assertEqual(logits.shape, (new_config.global_batch_size_to_train_on, new_config.max_target_length, new_config.vocab_size))
+    self.assertEqual(
+        logits.shape, (new_config.global_batch_size_to_train_on, new_config.max_target_length, new_config.vocab_size)
+    )

@codecov

codecov Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

@aireenmei

Copy link
Copy Markdown
Collaborator Author

I add pull ready to generate internal cl for testing. Won't submit until PR approved

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @RissyRan, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 I'm sorry @RissyRan, but I was unable to process your request. Please see the logs for more details.

@RissyRan RissyRan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change! Overall it looks good. I am a little bit lost in gemma4.py if/else branches, wondering if we could simplify a little bit?

Comment thread src/maxtext/models/gemma4.py Outdated
Comment thread src/maxtext/models/gemma4.py
Comment thread src/maxtext/models/gemma4.py
Comment thread src/maxtext/models/gemma4.py Outdated
Comment thread src/maxtext/layers/nnx_scan.py
Comment thread src/maxtext/models/gemma4.py Outdated
Comment thread src/maxtext/models/gemma4.py Outdated
Comment thread src/maxtext/models/gemma4.py Outdated
@aireenmei
aireenmei force-pushed the aireen/pr-gemma4-scan-rewrite branch 2 times, most recently from 5cd61be to e6b68f2 Compare July 29, 2026 19:09
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @RissyRan, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This pull request presents an exceptionally high-quality rewrite of the Gemma4ScannableBlock attention layers, refactoring the previous monolithic 6-layer block into a scan over 5 local layers and a length-1 scan for the global layer. By removing the block-level remat and unrolling the outer block scan, this change allows XLA to optimize activations and collectives, substantially reducing peak HBM usage and eliminating OOM issues when scaling.

🔍 General Feedback

  • Exceptional Architectural Rigor: The design decisions are well-founded, particularly wrapping the global layer in a length-1 scan to prevent co-scheduling of backward working sets under offload remat.
  • Flawless State Management: Sowing and tracking intermediate states (like moe_lb_loss in MoE blocks) via the scan carry's ys path is extremely clean and prevents pytree/tracing mismatches.
  • Outstanding Test Coverage: The unit tests added (nnx_decoders_test.py, nnx_scan_test.py, param_mapping_test.py) are highly thorough, covering state updates, external-kv compatibility, and stacking/unstacking identity roundtrips.

Comment thread src/maxtext/checkpoint_conversion/to_maxtext.py
@aireenmei
aireenmei force-pushed the aireen/pr-gemma4-scan-rewrite branch from e6b68f2 to e978c4c Compare July 29, 2026 23:24
@aireenmei
aireenmei force-pushed the aireen/pr-gemma4-scan-rewrite branch from e978c4c to b4e8103 Compare July 30, 2026 00:26
@copybara-service
copybara-service Bot merged commit ca42063 into main Jul 30, 2026
124 of 127 checks passed
@copybara-service
copybara-service Bot deleted the aireen/pr-gemma4-scan-rewrite branch July 30, 2026 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants