|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for canonical Gemma 4 attention scaling and weight initialization.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import unittest |
| 19 | +import jax |
| 20 | +import jax.numpy as jnp |
| 21 | +from flax import nnx |
| 22 | + |
| 23 | +from maxtext.configs import pyconfig |
| 24 | +from maxtext.common import common_types |
| 25 | +from maxtext.models import gemma4, gemma4_small |
| 26 | +from maxtext.utils.globals import MAXTEXT_REPO_ROOT |
| 27 | + |
| 28 | + |
| 29 | +class Gemma4CanonicalAttentionTest(unittest.TestCase): |
| 30 | + """Tests that Gemma 4 models follow canonical attention scaling and unscaled weight init.""" |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + super().setUp() |
| 34 | + self.base_config_path = os.path.join(MAXTEXT_REPO_ROOT, "src", "maxtext", "configs", "base.yml") |
| 35 | + |
| 36 | + def test_gemma4_26b_attention_config(self): |
| 37 | + config = pyconfig.initialize( |
| 38 | + ["", self.base_config_path], |
| 39 | + model_name="gemma4-26b", |
| 40 | + enable_dropout=False, |
| 41 | + ) |
| 42 | + self.assertTrue(config.use_qk_norm, "gemma4-26b should enable use_qk_norm in config") |
| 43 | + |
| 44 | + mesh = jax.sharding.Mesh(jax.devices()[:1], ("data",)) |
| 45 | + rngs = nnx.Rngs(0) |
| 46 | + layer = gemma4.Gemma4DecoderLayer( |
| 47 | + config=config, |
| 48 | + mesh=mesh, |
| 49 | + model_mode=common_types.MODEL_MODE_PREFILL, |
| 50 | + rngs=rngs, |
| 51 | + attention_type=gemma4.AttentionType.LOCAL_SLIDING, |
| 52 | + layer_idx=0, |
| 53 | + ) |
| 54 | + # Canonical Gemma 4 attention uses query_pre_attn_scalar = 1.0 (unscaled logits) |
| 55 | + self.assertEqual(layer.self_attention.query_pre_attn_scalar, 1.0) |
| 56 | + self.assertTrue(layer.self_attention.use_qk_norm) |
| 57 | + |
| 58 | + # Initial query weights should NOT be divided by sqrt(head_dim) = 16.0 |
| 59 | + # Expected standard deviation for fan_in=2816 is 1/sqrt(2816) ~= 0.0188 |
| 60 | + q_kernel = layer.self_attention.query.kernel[...] |
| 61 | + std_q = float(jnp.std(q_kernel)) |
| 62 | + self.assertGreater(std_q, 0.01, f"Query kernel std ({std_q}) should not be divided by depth_scaling") |
| 63 | + |
| 64 | + def test_gemma4_small_attention_config(self): |
| 65 | + for model_name in ["gemma4-e2b", "gemma4-e4b"]: |
| 66 | + config = pyconfig.initialize( |
| 67 | + ["", self.base_config_path], |
| 68 | + model_name=model_name, |
| 69 | + enable_dropout=False, |
| 70 | + ) |
| 71 | + self.assertTrue(config.use_qk_norm, f"{model_name} should enable use_qk_norm in config") |
| 72 | + |
| 73 | + mesh = jax.sharding.Mesh(jax.devices()[:1], ("data",)) |
| 74 | + rngs = nnx.Rngs(0) |
| 75 | + layer = gemma4_small.Gemma4SmallDecoderLayer( |
| 76 | + config=config, |
| 77 | + mesh=mesh, |
| 78 | + model_mode=common_types.MODEL_MODE_PREFILL, |
| 79 | + layer_idx=0, |
| 80 | + rngs=rngs, |
| 81 | + ) |
| 82 | + self.assertEqual(layer.self_attention.query_pre_attn_scalar, 1.0) |
| 83 | + self.assertTrue(layer.self_attention.use_qk_norm) |
| 84 | + |
| 85 | + q_kernel = layer.self_attention.query.kernel[...] |
| 86 | + std_q = float(jnp.std(q_kernel)) |
| 87 | + self.assertGreater(std_q, 0.01, f"Query kernel std ({std_q}) should not be divided by depth_scaling") |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + unittest.main() |
0 commit comments