|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 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 | +# http://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 | +"""Regression coverage for Gemma 4 recipe checkpoint vocabulary ownership.""" |
| 16 | + |
| 17 | +from unittest.mock import Mock, patch |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from megatron.bridge.models.gemma.gemma4_provider import Gemma4DenseProvider |
| 22 | +from megatron.bridge.recipes.gemma.h100 import gemma4 as gemma4_recipe |
| 23 | +from megatron.bridge.training.setup import _validate_and_set_vocab_size |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.unit |
| 27 | +def test_gemma4_mock_recipe_preserves_checkpoint_embedding_rows() -> None: |
| 28 | + """The mock-data recipe must allocate the converted checkpoint vocabulary.""" |
| 29 | + provider = Gemma4DenseProvider(vocab_size=262143, make_vocab_size_divisible_by=128) |
| 30 | + bridge = Mock() |
| 31 | + bridge.to_megatron_provider.return_value = provider |
| 32 | + |
| 33 | + with patch.object(gemma4_recipe.AutoBridge, "from_hf_pretrained", return_value=bridge): |
| 34 | + config = gemma4_recipe.gemma4_e4b_pretrain_2gpu_h100_bf16_config() |
| 35 | + |
| 36 | + runtime_vocab_size, _ = _validate_and_set_vocab_size( |
| 37 | + model_vocab_size=config.model.vocab_size, |
| 38 | + tokenizer_vocab_size=config.tokenizer.vocab_size, |
| 39 | + use_tokenizer_vocab_size=config.tokenizer.use_tokenizer_vocab_size, |
| 40 | + ) |
| 41 | + checkpoint_rows = 262144 |
| 42 | + runtime_rows = ( |
| 43 | + (runtime_vocab_size + config.model.make_vocab_size_divisible_by - 1) |
| 44 | + // config.model.make_vocab_size_divisible_by |
| 45 | + * config.model.make_vocab_size_divisible_by |
| 46 | + ) |
| 47 | + |
| 48 | + assert runtime_rows == checkpoint_rows |
0 commit comments