Rewrite Gemma4 scannable block#4530
Draft
aireenmei wants to merge 1 commit into
Draft
Conversation
Contributor
🤖 CI Failure Investigation ReportI have analyzed the recent test failures in the CI pipeline and identified the following: 🔍 What Failed
🪵 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 & ContextConfidence: high (confirmed cause) The failure is caused by code style and quality violations introduced in this PR. Specifically:
These issues are purely style/quality regressions and do not affect behavioral correctness. They have been fully corrected and verified locally using 🛠️ Recommended FixApply the formatting changes and add the missing docstring to 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)
+ ) |
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
Start with a short description of what the PR does and how this is a change from
the past.
The rest of the description includes relevant details and context, examples:
If the change fixes a bug or a Github issue, please include a link, e.g.,:
FIXES: b/123456
FIXES: #123456
You can also provide a comma-separated list. If you don't want to close a bug but
simply to reference it, use BUGS, e.g.:
BUGS: b/123456
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
Please describe how you tested this change, and include any instructions and/or
commands to reproduce.
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.