You want to add 5,000 Hindi medical terms to Llama 3 8B. Walk through the procedure and the risks.
Train BPE on Hindi, add_tokens, resize_token_embeddings (input AND lm_head), mean-init new rows, fine-tune with English mixed in, save the tokenizer. Watch forgetting and under-trained tokens.
Imagine teaching a friend who reads English to read Hindi medical reports. You decide to give them index cards for 5,000 common Hindi medical words so they do not have to spell each one letter by letter. The procedure has five parts. Pick the right 5,000 words by looking at lots of real Hindi medical text, not by guessing. Add the index cards to their card box and make sure their brain has space for the new words on both the 'reading' side and the 'speaking' side. Write a sensible first guess on each new card based on the spelling parts your friend already knows. Have them practice with enough Hindi medical text to actually learn the cards. Finally, give them the updated card box every time they go to read, not the old one. Skip any step and you get a friend who has forgotten some English, makes up nonsense for the new words, or cannot find the new cards at all.
Detailed answer & concept explanation~8 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Walk the five steps in order: domain-BPE selection, add_tokens, resize_token_embeddings (input plus lm_head), embedding-mean init, then fine-tune and save. Name catastrophic forgetting and under-trained embeddings as the two main failure modes. Cover the parameter cost (about 80M params for 5K tokens on 8B model) and the tokenizer.save_pretrained discipline. Close with the data-volume heuristic: at least 100 occurrences per new token for it to learn well.
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tok = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B')
model = AutoModelForCausalLM.from_pretrained('meta-llama/Meta-Llama-3-8B')
# 1. Select via domain BPE (sketch)
new_tokens = train_hindi_medical_bpe(corpus_path)[:5000]
new_tokens = [t for t in new_tokens if t not in tok.get_vocab()]
# 2. Add to tokenizer
num_added = tok.add_tokens(new_tokens)
# 3. Resize model (handles both input embedding and lm_head)
model.resize_token_embeddings(len(tok), pad_to_multiple_of=8)
# 4. Embedding-mean init for new rows
emb = model.get_input_embeddings().weight
lm_head = model.get_output_embeddings().weight
for i, tok_str in enumerate(new_tokens):
sub_ids = tok.encode(tok_str, add_special_tokens=False)
# Use the OLD subword decomposition for init
new_id = len(tok) - num_added + i
with torch.no_grad():
emb[new_id] = emb[sub_ids].mean(dim=0)
lm_head[new_id] = lm_head[sub_ids].mean(dim=0)
# 5. Fine-tune, then save tokenizer alongside model
model.save_pretrained(out_path)
tok.save_pretrained(out_path) # criticalReal products, models, and research that use this idea.
- OpenHathi (Sarvam AI) and Project Indus extended Llama 2 with Hindi tokens before fine-tuning on Hindi corpora; the published procedure uses BPE-trained additions plus mean init and explicitly warns about catastrophic forgetting.
- HuggingFace transformers ships resize_token_embeddings with a pad_to_multiple_of parameter to keep new embedding matrices tensor-core aligned.
- Meta's Code Llama and Code Llama Python were trained from Llama 2 with extended code-aware tokenization, using the same pattern of BPE-trained additions on top of the base vocab.
- Mistral and Qwen 3.5 multilingual variants ship with extended vocabularies for Devanagari, CJK, and Arabic, reducing fertility on those scripts compared to the base.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow many fine-tune tokens do you need before the new embedding rows are competitive with byte-fallback through the old tokenizer?
QShould you use LoRA or full fine-tune when extending vocab?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Calling add_tokens without resize_token_embeddings, or resizing only the input embedding while leaving the lm_head unchanged, so the model cannot predict the new ids.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.