Skip to content

Commit bf6922a

Browse files
committed
[recipe] fix: preserve Gemma 4 checkpoint vocabulary
Signed-off-by: Yu Yao <yaoyu.094@gmail.com>
1 parent 31121a4 commit bf6922a

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎src/megatron/bridge/recipes/gemma/h100/gemma4.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from megatron.bridge import AutoBridge
2323
from megatron.bridge.recipes.common import _pretrain_common
2424
from megatron.bridge.recipes.utils.environment_utils import COMMON_RECIPE_ENV_VARS
25-
from megatron.bridge.recipes.utils.tokenizer_utils import DEFAULT_NULL_TOKENIZER_VOCAB_SIZE
2625
from megatron.bridge.training.config import ConfigContainer
2726

2827

@@ -72,7 +71,7 @@ def gemma4_e4b_pretrain_2gpu_h100_bf16_config() -> ConfigContainer:
7271
# Tokenizer — NullTokenizer for mock pre-training; override for real data
7372
cfg.tokenizer.tokenizer_type = "NullTokenizer"
7473
cfg.tokenizer.tokenizer_model = None
75-
cfg.tokenizer.vocab_size = DEFAULT_NULL_TOKENIZER_VOCAB_SIZE
74+
cfg.tokenizer.vocab_size = cfg.model.vocab_size
7675

7776
# Dataset — mock data by default; override dataset.blend for real data
7877
cfg.dataset.blend = None
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)