|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Gemma 3 checkpoints that state only geometry must still build correctly. |
| 5 | +
|
| 6 | +google/gemma-3-12b-it names hidden_size, the layer and head counts, |
| 7 | +intermediate_size, sliding_window and rope_scaling, and nothing else; every |
| 8 | +other value comes from Gemma3TextConfig. Mirrors write the full set out, which |
| 9 | +is why a build against a mirror can pass while the same build against the |
| 10 | +official checkpoint silently produces a different model. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from families.gemma import graph_blocks |
| 18 | +from families.gemma.model import _GEMMA3_CONFIG_DEFAULTS, _apply_gemma3_config_defaults |
| 19 | + |
| 20 | + |
| 21 | +class _Config: |
| 22 | + """Minimal stand-in for the parsed config, matching the sparse official shape.""" |
| 23 | + |
| 24 | + def __init__(self, **raw): |
| 25 | + self.model_type = raw.pop("model_type", "gemma3") |
| 26 | + self.hidden_size = raw.get("hidden_size", 3840) |
| 27 | + self.num_hidden_layers = raw.get("num_hidden_layers", 48) |
| 28 | + self.num_attention_heads = raw.get("num_attention_heads", 16) |
| 29 | + self.num_key_value_heads = raw.get("num_key_value_heads", 8) |
| 30 | + # The parser's own fallback, which is the local base rather than the |
| 31 | + # global one; the defaults must overwrite it. |
| 32 | + self.rope_theta = 10000.0 |
| 33 | + self.rms_norm_eps = None |
| 34 | + self.hidden_act = None |
| 35 | + self.max_position_embeddings = None |
| 36 | + self._head_dim = None |
| 37 | + self.raw = dict(raw) |
| 38 | + |
| 39 | + @property |
| 40 | + def head_dim(self): |
| 41 | + if self._head_dim: |
| 42 | + return self._head_dim |
| 43 | + return self.hidden_size // self.num_attention_heads |
| 44 | + |
| 45 | + |
| 46 | +class _Readers: |
| 47 | + tensor_map: dict = {} |
| 48 | + |
| 49 | + |
| 50 | +def _official_12b() -> _Config: |
| 51 | + return _Config( |
| 52 | + hidden_size=3840, |
| 53 | + intermediate_size=15360, |
| 54 | + num_attention_heads=16, |
| 55 | + num_hidden_layers=48, |
| 56 | + num_key_value_heads=8, |
| 57 | + sliding_window=1024, |
| 58 | + rope_scaling={"factor": 8.0, "rope_type": "linear"}, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def test_sparse_config_resolves_the_gemma3_rope_bases(): |
| 63 | + config = _official_12b() |
| 64 | + _apply_gemma3_config_defaults(config, _Readers(), "model") |
| 65 | + |
| 66 | + assert config.head_dim == 256 |
| 67 | + assert config.rope_theta == 1000000.0, "global layers must not inherit the local base" |
| 68 | + schedule = graph_blocks.gemma3_attention_schedule(config, config.num_hidden_layers) |
| 69 | + assert schedule["window"] == 1024 |
| 70 | + assert schedule["local_theta"] == 10000.0 |
| 71 | + |
| 72 | + |
| 73 | +def test_sparse_config_places_local_attention_five_in_six(): |
| 74 | + config = _official_12b() |
| 75 | + _apply_gemma3_config_defaults(config, _Readers(), "model") |
| 76 | + schedule = graph_blocks.gemma3_attention_schedule(config, config.num_hidden_layers) |
| 77 | + |
| 78 | + # transformers resolves layer_types for this config to full attention at |
| 79 | + # indices 5, 11, 17, 23, 29, 35, 41 and 47. |
| 80 | + assert sum(schedule["is_local"]) == 40 |
| 81 | + assert [i for i, local in enumerate(schedule["is_local"]) if not local] == [ |
| 82 | + 5, |
| 83 | + 11, |
| 84 | + 17, |
| 85 | + 23, |
| 86 | + 29, |
| 87 | + 35, |
| 88 | + 41, |
| 89 | + 47, |
| 90 | + ] |
| 91 | + |
| 92 | + |
| 93 | +def test_gemma3_refuses_to_fall_back_to_all_global(): |
| 94 | + """The old behavior built cleanly and generated fluent, wrong text.""" |
| 95 | + config = _official_12b() |
| 96 | + # Defaults deliberately not applied, so the schedule inputs are missing. |
| 97 | + with pytest.raises(ValueError, match="rope_local_base_freq"): |
| 98 | + graph_blocks.gemma3_attention_schedule(config, config.num_hidden_layers) |
| 99 | + |
| 100 | + |
| 101 | +def test_a_stated_value_still_wins_over_the_default(): |
| 102 | + config = _official_12b() |
| 103 | + config.raw["rope_local_base_freq"] = 12345.0 |
| 104 | + config.raw["sliding_window_pattern"] = 4 |
| 105 | + _apply_gemma3_config_defaults(config, _Readers(), "model") |
| 106 | + schedule = graph_blocks.gemma3_attention_schedule(config, config.num_hidden_layers) |
| 107 | + |
| 108 | + assert schedule["local_theta"] == 12345.0 |
| 109 | + assert [i for i, local in enumerate(schedule["is_local"]) if not local][:3] == [3, 7, 11] |
| 110 | + |
| 111 | + |
| 112 | +def test_gemma2_is_untouched_by_the_gemma3_defaults(): |
| 113 | + config = _Config( |
| 114 | + model_type="gemma2", |
| 115 | + hidden_size=2304, |
| 116 | + num_attention_heads=8, |
| 117 | + num_hidden_layers=26, |
| 118 | + sliding_window=4096, |
| 119 | + ) |
| 120 | + _apply_gemma3_config_defaults(config, _Readers(), "model") |
| 121 | + |
| 122 | + assert config.rope_theta == 10000.0 |
| 123 | + assert "rope_local_base_freq" not in config.raw |
| 124 | + schedule = graph_blocks.gemma3_attention_schedule(config, config.num_hidden_layers) |
| 125 | + assert not any(schedule["is_local"]) |
| 126 | + |
| 127 | + |
| 128 | +def test_defaults_cover_every_field_the_schedule_and_scale_read(): |
| 129 | + for key in ( |
| 130 | + "rope_theta", |
| 131 | + "rope_local_base_freq", |
| 132 | + "sliding_window_pattern", |
| 133 | + "query_pre_attn_scalar", |
| 134 | + "head_dim", |
| 135 | + ): |
| 136 | + assert key in _GEMMA3_CONFIG_DEFAULTS |
0 commit comments