Getting Started With Tokenizer Extension#
You will build a tokenizer that adds 30,000 target-language subwords to the Nemotron 3 Nano tokenizer, initialize the matching embedding rows in the base model, and measure the fertility of the result.
This tutorial runs three of the four tokenizer_extension steps in order.
Each step uses its config/default.yaml and CLI dotlist overrides.
What You Will Produce
An extended tokenizer under ./output/tokenizer_extension/add/, a resized Hugging Face checkpoint under ./output/resized_checkpoint/, and a fertility report JSON.
Overview
Install the
tokenizer-extensionextra.Run
tokenizer_extension/extendwithmethod=add.Run
tokenizer_extension/init_embeddingswitharm=add.Run
tokenizer_extension/evaluateon the extended tokenizer.
The extend and evaluate steps run on CPU and stream their corpora.
The init_embeddings step loads the base model and needs a GPU node.
Prerequisites#
See Prerequisites for the full requirements. For this tutorial you need:
A Hugging Face dataset, or a local Parquet or JSON Lines corpus, in the target language. The
extenddefault configuration points atai4bharat/sangrahawithhf_name: verifiedandhf_split: hin.A GPU node for
init_embeddings. The base model,nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-Base-BF16by default, is loaded host-resident for embedding surgery; the GPU serves the auxiliary encoders, and the generated environment profiles request one GPU for this step.A
languagevalue from the registry inlanguages.py. The registered values arehindi,marathi,nepali,sanskrit,bengali,punjabi,gujarati,odia,tamil,telugu,kannada,malayalam,urdu, andvietnamese.
Procedure#
Clone the repository, if you have not already:
$ git clone https://github.com/NVIDIA-NeMo/Nemotron && cd Nemotron
Install the optional dependencies.
indic-nlp-libraryis required whenlanguageselects the Devanagari normalizer (hindi,marathi,nepali, orsanskrit).fasttext-wheelis required only formethod: focusininit_embeddings.$ uv pip install -e '.[tokenizer-extension]'
Set the values that the following commands share.
$ L=hindi $ OUT=./output/tokenizer_extension
Substitute a registered
languagevalue and a corpus that matches it. The commands below keep the corpus fromextend/config/default.yaml; to use a different Hugging Face dataset, addcorpus.hf_dataset=<id> corpus.hf_name=<config> corpus.hf_split=<split>overrides.Build the extended tokenizer.
$ uv run nemotron steps run tokenizer_extension/extend -c default \ language=$L method=add extension_size=30000 output_dir=$OUT
The step writes
$OUT/add/, which contains the tokenizer files andsummary.json. Confirm thattokens_splicedinsummary.jsonequalsextension_size. The step logs a warning when the two values differ, because fertility and BPB comparisons between arms are valid only at a matched budget.Initialize the new embedding rows and write the resized checkpoint.
$ uv run nemotron steps run tokenizer_extension/init_embeddings -c default \ language=$L arm=add extended_tokenizer=$OUT/add \ output_dir=./output/resized_checkpoint
The default
methodissubwordwithinput_averaging: uniform, which initializes each new row from the mean of its base-vocabulary subword rows. Thearmvalue must match howextendbuilt the tokenizer:addformethod=addormethod=expand, andreplaceformethod=replace.The output directory is a Hugging Face checkpoint with weights and tokenizer. Pass it to
pretrain/megatron_bridgeashf_model_pathwhen you start continued pretraining.Measure fertility.
$ uv run nemotron steps run tokenizer_extension/evaluate -c default \ tokenizer=$OUT/add output=./output/eval/fertility_add.json
The default evaluation corpus is
ai4bharat/samanantarwithhf_config: hiandtext_field: tgt. Change thecorpusblock to match your target language. The report recordsfertility,chars_per_token,unique_tokens_used, andvocab_coverage. Lower fertility is better. To compare tokenizers, run the step once per tokenizer with the samecorpusblock.Optional: Print the merged configuration without running a step.
Pass
--dry-runor-dto any of the commands above.$ uv run nemotron steps run tokenizer_extension/extend -d -c default language=$L method=add
Next Steps#
Compare the three extension methods: Add, Replace, and Expand
Choose an initialization engine: Initialize Embeddings
Score the resized checkpoint with bits per byte: Evaluate a Tokenizer
Run the steps on Lepton, DGX Cloud, or Slurm: Run on a Cluster
Field meanings: Extend YAML Reference