How would you train a custom tokenizer for a genomics LLM on DNA sequences?
You are building a domain-specific LLM for genomics. Your training data consists of DNA sequences (e.g., 'ACGTTTACGCGATCG...'). Describe how you would train a custom tokenizer, including: the algorithm choice, vocabulary size decision, pre-tokenization configuration, and the HuggingFace tokenizers API calls you would use.
Train BPE on a genomics-only corpus with pre-tokenization disabled (DNA has no spaces), set vocab_size to 4k-8k to match the 4^6 k-mer ceiling, and add domain special tokens.
Imagine teaching a reading machine to read DNA, which is one endless string of four letters with no spaces. A normal reading machine looks for spaces to find where words end, but here there are none, so you must tell it to ignore spaces and just hunt for repeated chunks. Then you tell it how big its notebook of chunks should be. Since there are only four letters, the number of useful chunks is small, a few thousand, so a notebook of four to eight thousand entries is plenty. Finally, you train it only on real DNA, never on English, so the chunks it memorizes are the ones that actually show up in genomes.
Detailed answer & concept explanation~6 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.
4 min: BPE vs Unigram + disable pre-tokenization + 4^6 k-mer ceiling sets 4k-8k vocab + genomics-only corpus + domain special tokens + motif-emergence validation.
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
# [UNK] absorbs ambiguous bases (N, Y, R) under character-level BPE
tokenizer = Tokenizer(BPE(unk_token="[UNK]"))
# CRITICAL: DNA has no word boundaries, so disable pre-tokenization.
# Leaving the default on makes the trainer treat a chromosome as one word.
tokenizer.pre_tokenizer = None
# 4^6 = 4,096 hexamers is the productive ceiling; 8192 leaves headroom.
trainer = BpeTrainer(
vocab_size=8192,
special_tokens=["[UNK]", "[PAD]", "[MASK]", "[BOS]", "[EOS]",
"[STRAND+]", "[STRAND-]"],
min_frequency=2, # drop single-occurrence k-mers (often read errors)
show_progress=True,
)
# Train on genomics corpora ONLY, never general text.
tokenizer.train(files=["hg38.txt", "bacterial.txt", "viral.txt"], trainer=trainer)
tokenizer.save("dna_tokenizer.json")
# Sanity check: GAATTC is the EcoRI restriction site, very common in hg38.
out = tokenizer.encode("ATCGTTACGCGATCGGAATTCATCG")
print(out.tokens) # expect GAATTC to surface as a single token
print(len(tokenizer.get_vocab()))Real products, models, and research that use this idea.
- DNABERT-2 swapped DNABERT's fixed 6-mer scheme for BPE trained on multi-species genomes at a roughly 4,096-token vocabulary, learning variable-length motifs from data.
- InstaDeep's Nucleotide Transformer instead enumerates all 4^6 = 4,096 hexamers as a fixed vocabulary, guaranteeing every valid 6-mer is present without training merges.
- Meta's ESM protein models use a 33-token character-level vocabulary, showing the same tiny-alphabet logic applied to 20 amino acids.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you handle reverse-complement strands, and should they map to the same token IDs as the forward strand?
QGiven IUPAC ambiguity codes like N, Y, and R, should you keep them as distinct tokens or normalize to N?
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.
Leaving whitespace pre-tokenization on for DNA, which treats an entire chromosome as one word so the trainer can only learn merges inside that single giant unit.
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.