$50K compute budget, 50B tokens of US legal text, 1.5B parameter model. Defend your tokenizer design.
$50K compute budget, 50B tokens of US legal text, 1.5B parameter model. Defend your tokenizer design.
Byte-level BPE, 48K-64K vocab (~262M params at d_model=2048), plus structural [CITATION]/[STATUTE]/[SECTION]/[PARTY]. Train BPE on 5GB stratified sample. Validate fertility vs cl100k_base on 1GB before committing.
Imagine you have a budget to teach a small student a specialized job, reading legal contracts. You decide what shorthand vocabulary they will learn first. Too few shortcuts (only the alphabet), and every common term like 'force majeure' takes many slow letters to read. Too many shortcuts, and the student spends most of their brain power memorizing the shorthand instead of learning to think. The sweet spot is roughly 50,000 shortcuts. Of those, a few are special markers like 'this is a case citation' or 'this is a statute number' that help the student navigate the document structure. Before you spend the whole budget, you spot-check: take a sample of contracts, count how much faster the new shorthand reads them versus the off the shelf shorthand a general-purpose student would use. If your shorthand saves 20 percent of reading time, the budget is worth spending.
Detailed answer & concept explanation~10 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.
Open with the framing that tokenizer design on a constrained budget is parameter-budget plus sequence-length budget optimization with validation before commit. Walk the five axes: byte-level BPE, 48K-64K vocab with explicit param math at d_model=2048, BOS/EOS/PAD plus structural [CITATION]/[STATUTE]/[SECTION]/[PARTY], stratified 5GB training sample, validation against cl100k_base on a 1GB held-out. Cover the budget split (70 percent pretraining / 30 percent inference experiments). Close with the discipline: validate the fertility delta empirically before committing the $50K, and only commit if the saving compounds favorably across 50B pretraining tokens.
from tokenizers import Tokenizer, models, trainers, pre_tokenizers, decoders
# 1. Initialize byte-level BPE
tok = Tokenizer(models.BPE())
tok.pre_tokenizer = pre_tokenizers.ByteLevel()
tok.decoder = decoders.ByteLevel()
# 2. Configure trainer with domain structural tokens
trainer = trainers.BpeTrainer(
vocab_size=64000,
special_tokens=['<|bos|>', '<|eos|>', '<|pad|>',
'[CITATION]', '[STATUTE]', '[SECTION]', '[PARTY]', '[EXHIBIT]'],
min_frequency=5,
)
# 3. Train on stratified 5GB sample (NOT the full 50B)
tok.train(['stratified_legal_5gb.txt'], trainer)
tok.save('legal_bpe_64k.json')
# 4. Validate fertility vs cl100k_base BEFORE pretraining
import tiktoken
enc = tiktoken.get_encoding('cl100k_base')
held_out = open('legal_holdout_1gb.txt').read()
legal_tokens = tok.encode(held_out).ids
general_tokens = enc.encode(held_out)
ratio = len(legal_tokens) / len(general_tokens)
print(f'Legal BPE produces {ratio:.2%} of cl100k_base length')
# Expect 0.75-0.85 (15-25% shorter). Below 0.90, reconsider design.Real products, models, and research that use this idea.
- LegalBERT and Legal-RoBERTa (2020-2022 era) used domain-specific tokenizers tuned for case law, demonstrating the sequence-length wins on legal corpora.
- BloombergGPT (2023) trained a domain-specific 50K BPE for financial text, showing the same domain-tokenizer pattern at scale.
- Galactica (Meta, 2022) used a domain-specific tokenizer for scientific text with special tokens for citations, math, and chemical formulas.
- Code Llama and DeepSeek-Coder use code-aware BPE tokenizers; the legal-domain design parallels their approach but for a different specialty.
- HuggingFace tokenizers and tiktoken-style libraries both support byte-level BPE training on domain corpora with a few lines of code, making the engineering investment small.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does the calculus change if the budget were $500K instead of $50K?
QWhy insert structural tokens via preprocessing rather than learning them from raw text via BPE merges?
QIf your validation experiment showed the domain BPE saves only 8 percent versus cl100k_base, what would you do?
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.
Choosing a vocab size based on what other models use (32K Llama 2, 128K Llama 3) without computing the embedding parameter cost as a fraction of the target model size.
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.