Skip to content

Commit e7f74fa

Browse files
committed
docs(training): clarify vocab checkpoint migration
Signed-off-by: Chen Cui <chcui@nvidia.com>
1 parent b029e15 commit e7f74fa

3 files changed

Lines changed: 52 additions & 10 deletions

File tree

docs/nemo2-migration-guide.md

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,19 +693,25 @@ tokenizer_config = TokenizerConfig(
693693

694694
#### Vocab Size Priority
695695

696-
In Megatron Bridge, vocabulary size can be specified in either the model provider or derived from the tokenizer. The priority order is:
696+
In Megatron Bridge, vocabulary size can be specified in the model provider or derived from the runtime tokenizer. The priority order is:
697697

698-
1. **Model provider `vocab_size` is set**: Uses the model's vocab size
699-
- Must be `>= tokenizer.vocab_size` (raises error if smaller)
700-
- Sets `should_pad_vocab=False` (no automatic padding)
701-
- Useful when you need a specific vocab size (e.g., for checkpoint compatibility)
698+
1. **`TokenizerConfig.use_tokenizer_vocab_size=True`**: Uses the tokenizer's vocab size
699+
- Overrides a preset model-provider `vocab_size`.
700+
- Sets `should_pad_vocab=True` (enables padding for efficient parallelism).
701+
- Intended for from-scratch pretraining, where the dataset tokenizer defines the vocabulary.
702+
- This policy remains active during checkpoint loading; disable it when checkpoint compatibility requires the explicit model vocabulary.
702703

703-
2. **Model provider `vocab_size` is None**: Uses tokenizer's vocab size
704+
2. **Model provider `vocab_size` is set**: Uses the model's vocab size
705+
- Must be `>= tokenizer.vocab_size` (raises an error if smaller).
706+
- Sets `should_pad_vocab=False` (no automatic padding).
707+
- Useful when a specific vocabulary is required for model or checkpoint compatibility.
708+
709+
3. **Model provider `vocab_size` is `None`**: Uses the tokenizer's vocab size
704710
- Automatically derived from `tokenizer.vocab_size` after building the tokenizer.
705-
- Sets `should_pad_vocab=True` (enables padding for efficient parallelism)
711+
- Sets `should_pad_vocab=True`.
706712

707713
```python
708-
# Option 1: Let tokenizer determine vocab size
714+
# Option 1: Let tokenizer determine vocab size when the model has no preset
709715
config = ConfigContainer(
710716
model=GPTModelProvider(
711717
# vocab_size not set - will use tokenizer's vocab size
@@ -717,7 +723,19 @@ config = ConfigContainer(
717723
),
718724
)
719725

720-
# Option 2: Explicitly set vocab size in model
726+
# Option 2: Override a preset model vocab for from-scratch pretraining
727+
config = ConfigContainer(
728+
model=GPTModelProvider(
729+
vocab_size=128256, # Ignored whenever the flag is enabled
730+
),
731+
tokenizer=TokenizerConfig(
732+
tokenizer_type="HuggingFaceTokenizer",
733+
tokenizer_model="my-org/my-pretraining-tokenizer",
734+
use_tokenizer_vocab_size=True,
735+
),
736+
)
737+
738+
# Option 3: Explicitly set vocab size in model
721739
config = ConfigContainer(
722740
model=GPTModelProvider(
723741
vocab_size=128256, # Explicitly set (must be >= tokenizer vocab size)
@@ -726,6 +744,17 @@ config = ConfigContainer(
726744
)
727745
```
728746

747+
Pretraining recipes enable `use_tokenizer_vocab_size` by default. For a new run, use an empty checkpoint directory so the runtime tokenizer defines the model vocabulary. A checkpoint created by that policy can be resumed with the same tokenizer and recipe configuration.
748+
749+
Checkpoints created before a recipe enabled `use_tokenizer_vocab_size` may have used the model provider's larger explicit vocabulary. To preserve their embedding and output tensor shapes, disable the new policy and retain the vocabulary used to create the checkpoint:
750+
751+
```python
752+
config.tokenizer.use_tokenizer_vocab_size = False
753+
config.model.vocab_size = 128256 # The vocabulary used to create the checkpoint
754+
```
755+
756+
Do not change this setting partway through a run. Switching vocabulary policies changes model tensor shapes and is not a checkpoint migration mechanism.
757+
729758

730759
### Parallelism Configuration Migration
731760
In NeMo 2.0, parallelism settings were configured on `MegatronStrategy`. In Megatron Bridge, these are set directly on the model provider:

src/megatron/bridge/training/tokenizers/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class TokenizerConfig(MTrainTokenizerConfig):
3939
Enable this for from-scratch pretraining, where the tokenizer selected for
4040
the dataset defines the embedding and output vocabulary. Keep it disabled
4141
when model or checkpoint compatibility requires an explicitly configured
42-
model vocabulary size.
42+
model vocabulary size. This policy also applies during checkpoint loading;
43+
disable it and configure the checkpoint's original model vocabulary when
44+
resuming a run created with a different vocabulary policy.
4345
"""
4446

4547
hf_tokenizer_kwargs: dict[str, Any] | None = field(default_factory=dict)

tests/unit_tests/training/test_setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ def test_pretraining_uses_tokenizer_vocab_over_larger_model_vocab(self):
206206
assert vocab_size == 32000
207207
assert should_pad_vocab is True
208208

209+
def test_checkpoint_compatibility_override_preserves_model_vocab(self):
210+
"""Disabling the policy preserves the explicit vocabulary used by an existing checkpoint."""
211+
vocab_size, should_pad_vocab = _validate_and_set_vocab_size(
212+
model_vocab_size=248320,
213+
tokenizer_vocab_size=32000,
214+
use_tokenizer_vocab_size=False,
215+
)
216+
217+
assert vocab_size == 248320
218+
assert should_pad_vocab is False
219+
209220
def test_vocab_size_equal_to_tokenizer_returns_same_value(self):
210221
"""Test that vocab_size equal to tokenizer returns the same value and disables padding."""
211222
vocab_size, should_pad_vocab = _validate_and_set_vocab_size(

0 commit comments

Comments
 (0)