Advanced
Training your own tokenizer
Tokenizer training fixes a vocabulary every later checkpoint must share; Qwen3.8-27B uses token IDs through 248,076 in matrices with 248,320 rows, an observed alignment whose historical rationale is not published.
Updated
01 · Concept
Concept
Open the pinned Qwen tokenizer and config and two related numbers appear. The tokenizer’s model vocabulary plus added tokens use IDs from 0 through 248,076, while the embedding and output matrices allocate 248,320 rows. That leaves 243 rows the pinned tokenizer never emits. The artifacts establish those counts; they do not publish why the team chose the larger allocation. This distinction is useful because tokenizer design is linguistic, while matrix alignment is an engineering constraint, and evidence should not turn a plausible explanation into history.
The linguistic half comes first. A language model never receives raw text; the tokenizer maps text to integer IDs and the embedding table maps those IDs to 5120-dimensional vectors. Training that tokenizer means choosing a representative sample from the governed corpus of lesson 5.2, with the mixture caps already applied, and then learning a fixed-size inventory of subword pieces from it. Byte-level schemes start from bytes so any input is representable; BPE repeatedly merges the most frequent adjacent pair, producing an ordered merge list; unigram starts from many candidate pieces and prunes those contributing least to a probabilistic objective. SentencePiece can train on raw sentences and treats whitespace explicitly. All of them are compromises between tiny universal units and an impossible full-word dictionary.
Size is the real decision, and it is a direct trade. A larger vocabulary means fewer tokens per sentence, which means shorter sequences, less attention work, and — this matters more than it sounds — fairer treatment of languages that a small English-centric vocabulary would shred into fragments. Lesson 1.4 measured that effect: the same meaning costs more tokens, and therefore more money at USD 0.45 per million input tokens, in Portuguese than in English under a badly balanced tokenizer.
But rows are not free. In this model the embedding and the output head are untied, which the config states plainly as tie_word_embeddings: false. So every row is paid for twice. Compute the marginal cost:
Ten thousand extra vocabulary entries cost about 205 MB of weights. Scale that to the full table: billion parameters in the embedding, another 1.271 billion in the head, roughly 2.54 billion combined — around 9% of a 27-billion-parameter model spent on the mapping between text and vectors, before a single transformer layer exists.
Now inspect the engineering property of the allocated row count without assigning intent. Tensor parallelism can split a vocabulary dimension evenly only when the row count is divisible by the parallel degree. The shipped value factors as:
It divides cleanly by 2, 4, 8, 16, 32, 64, 128, 256, and 512. Any power-of-two tensor-parallel degree through 512 therefore gets an equal slice: at degree 8, each rank owns 31,040 rows; at degree 32, 7,760; at degree 64, 3,880. By contrast, 250,000 divides only through degree 16 among those powers of two. Frameworks can pad incompatible dimensions, so divisibility is a real implementation benefit. The public artifacts do not say that this benefit caused Qwen to select 248,320; that causal account remains an inference.
which divides by 16 and then stops. At tensor-parallel degree 32 you would get 7,812.5 rows per rank — impossible — and the framework would pad it silently or fail loudly at launch. So the tokenizer’s learned inventory is padded up to a highly composite number, and the surplus rows are simply never emitted. They cost a few million parameters and buy freedom in every future parallel layout.
The classic wrong turn follows immediately: treating unused tail rows as safely removable. Trimming the matrix would change checkpoint shapes and may remove useful divisibility, while saving only 243 × 5120 × 2 matrices × 2 bf16 bytes, about 5 MB. The token mapping itself must also remain unchanged. The safe conclusion is contractual: load the matrix and tokenizer shapes the checkpoint publishes; do not invent or delete rows from a trained artifact.
Everything else about tokenizer training follows from the fact that the artifact is permanent. Reserve special tokens — padding, sequence boundaries, chat-role markers, thinking-mode markers, FIM markers — before freezing IDs, because appending later means resizing embeddings and training a cold row, and inserting in the middle corrupts every association after it. Decide normalization deliberately: Unicode can spell visually identical text several ways, and case folding or accent stripping shrinks a vocabulary at the cost of distinctions your product may need. Evaluate on held-out samples, reporting fertility by language and domain rather than one global mean, plus round-trip correctness, adversarial maximum expansion, and encoding throughput. Inspect real examples by hand, because Portuguese morphology, combining accents, emoji sequences, CJK text, right-to-left scripts, indentation, and long digit strings each fail differently.
Then freeze and hash the files, store the training-code version, normalization rules, sample manifest, vocabulary, merges, special-token table, and evaluation report, and test that every data worker produces byte-identical IDs for a golden set. A silent tokenizer-version mismatch will let a run train happily on meaningless input while the loss stays finite and plausible — the most expensive silent failure in this track.
02 · Analogy
Analogy
Before printing a multilingual newspaper, a foundry decides which movable-type pieces to cast. Single letters can print anything but require many pieces per word; whole words are efficient until an unseen word arrives. Subword training studies representative copy and casts recurring fragments. Once the presses ship, changing the type inventory changes every drawer number, so yesterday's printing instructions no longer fit.
03 · Teach it back
Teach it back
Explain how a target vocabulary size is chosen, distinguish Qwen3.8-27B’s documented tokenizer inventory from its matrix row count, and explain what the observed divisibility enables without claiming an unpublished rationale.
Compare with a model answer
Vocabulary size trades fertility against matrix cost: a larger inventory can shorten sequences and improve multilingual coverage, but every row costs parameters in both the embedding and an untied output head. In the pinned Qwen tokenizer, model tokens and added tokens use IDs through 248,076, while config.json allocates 248,320 matrix rows, leaving 243 rows unused by that tokenizer. The allocated size equals 512 × 485, so it can be partitioned evenly across power-of-two tensor-parallel degrees through 512. Padding for parallel alignment is a plausible explanation, but Qwen does not publish why it selected this size. After training, the token-to-ID mapping remains part of the checkpoint contract.
04 · Check your understanding
Check your understanding
Complete the teach-back and answer the quiz correctly to finish this lesson.
◎ · Evidence marker
Sources
- Qwen Team (2026). Qwen3.8-27B config.json.
- Qwen Team (2026). Qwen3.8-27B tokenizer.json.
- Rico Sennrich, Barry Haddow, and Alexandra Birch (2016). Neural Machine Translation of Rare Words with Subword Units.
- Taku Kudo and John Richardson (2018). SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing.