Knowledge Sprint SeriesMT24 Learning

Phase 8 · Sprint 23/90 · Tokenization & ParsingGage · Sennrich · Provilkov · Land · Singh

Byte Pair Encoding & Its Edge Cases

A tokenizer is a list of merges. Change the list or its order, and words split differently.

In short. The merge loop, how ties decide the result, rare, capitalised and unseen words, and why numbers split oddly.

Sprint 23 of 90 infographic: Byte Pair Encoding & Its Edge Cases. A tokenizer is a list of merges. Change the list or its order, and words split differently. The full text of the image is on this page.
Full resolution2160 × 2700 PNGPhase summary

The part of the image you are reading lights up. Tap a part to jump to its text.

Fig. 1 in words

Ten merges on a toy corpus · Sennrich's example words · same data as code 05

  1. 1 Merge ladder · most frequent pair wins

    • 01 e + s → es · 9 · tie ×3
    • 02 es + t → est · 9 · tie ×2
    • 03 est + _ → est_ · 9
    • 04 l + o → lo · 7 · tie ×2
    • 05 lo + w → low · 7
    • 06 n + e → ne · 6 · tie ×3
    • 07 ne + w → new · 6 · tie ×2
    • 08 new + est_ → newest_ · 6
    • 09 low + _ → low_ · 5
    • 10 w + i → wi · 3 · tie ×3
  2. 2 16 words · 95 → 28 symbols

    • ×5 low_
    • ×2 low | e | r | _
    • ×6 newest_
    • ×3 wi | d | est_
  3. 3 Unseen words · same 10 merges

    • lowest → low | est_
    • newer → new | e | r | _
    • Lowest → L | o | w | est_
    • L in a dashed red box

01 The Merge Loop Algorithm

  • Gage (1994) compressed files by swapping the most frequent byte pair for an unused byte
  • Sennrich et al. (2016) merge letters instead, plus an end-of-word mark
  • Vocabulary = base symbols + merges, the only setting

Pairs never cross word boundaries. New words replay the merges in the order they were learned.

02 Ties Decide Order

  • Steps 1, 2, 4, 6, 7 and 10 had tied pairs
  • Break ties alphabetically: the corpus still ends at 28, but newer becomes ne|w|e|r|_
  • Different merge list, different pieces, different IDs

The tie rule is part of the tokenizer. Write it down.

03 Rare, Cased, Unseen Edges

  • newer stays in 4 pieces: “er” never won a merge
  • Lowest: L was never seen, so low cannot form
  • Under-trained tokens sit in the vocabulary but were barely seen in training

Land & Bartolo (2024): SolidGoldMagikarp-like tokens, found across many models.

04 Numbers Split Oddly Digits

  • GPT-3.5 and GPT-4 cut numbers into chunks of up to 3 digits, from the left
  • Commas force groups from the right: GPT-3.5 addition went from 68.5% to 95.6%
  • LLaMA and PaLM split numbers into single digits

Singh & Strouse (2024), a preprint: the cut alone moved accuracy, one-shot.

05 Try It Yourself PYTHON 3.11.15

sprint23_byte_pair_encoding_edge_cases.py

import re
from collections import Counter
vocab = {"l o w _": 5, "l o w e r _": 2,
  "n e w e s t _": 6, "w i d e s t _": 3}
def merge(pair, w):            # a b -> ab
    a = re.escape(" ".join(pair))
    return re.sub(rf"(?<!\S){a}(?!\S)",
                  "".join(pair), w)
merges = []
for step in range(10):
    pairs = Counter()
    for w, n in vocab.items():
        s = w.split()
        for p in zip(s, s[1:]):
            pairs[p] += n
    best = max(pairs, key=pairs.get)
    merges.append(best)
    vocab = {merge(best, w): n
             for w, n in vocab.items()}
def bpe(word):
    w = " ".join(word) + " _"
    for pair in merges:
        w = merge(pair, w)
    return w.replace(" ", "|")
print(" ".join("".join(p) for p in merges))
print(sum(len(w.split()) * n
          for w, n in vocab.items()))
for w in ["lowest", "newer", "Lowest"]:
    print(w, bpe(w))

Output, as printed on the image

es est est_ lo low ne new newest_ low_ wi
28
lowest low|est_
newer new|e|r|_
Lowest L|o|w|est_

Run it. sprint23_byte_pair_encoding_edge_cases.py · versions, data and steps

Check: 5×4 + 2×6 + 6×7 + 3×7 = 95 · 5 + 2×4 + 6 + 3×3 = 28 · 11 + 10 = 21

06 General Notes Practice

  • Print the merge list and read it
  • Test capitals, leading spaces, digits, emoji and Arabic
  • BPE gives one fixed split. Dropping merges at random in training gave up to 2.3 BLEU (Provilkov et al. 2020)

Unigram LM tokenization matched or beat BPE in pretraining (Bostrom & Durrett 2020).

07 Mini Project Hands-on

  • Run 14 merges: lower appears, newer stays new|e|r|_
  • Break ties alphabetically: Lowest becomes L|ow|est_
  • Skip each merge with p = 0.1, seeds 1 to 5, and compare splits

Train on one page of my own text and read its first 30 merges.

R Trusted Sources

  1. Gage (1994). A New Algorithm for Data Compression. C Users J. 12(2)
  2. Sennrich et al. (2016). Neural Machine Translation of Rare… ACL
  3. Provilkov et al. (2020). BPE-Dropout. ACL, 1882–1892
  4. Bostrom & Durrett (2020). Byte Pair Encoding is Suboptimal… EMNLP
  5. Land & Bartolo (2024). Fishing for Magikarp. EMNLP, 11631–11646
  6. Singh & Strouse (2024). Tokenization counts… arXiv 2402.14903

Cite this phase

Tahri, M. (2026). MT24 Learning — Knowledge Sprint Series, Phase 8: Tokenization & Parsing (Version 1.0) [Educational resource]. Zenodo. https://doi.org/10.5281/zenodo.23213765

Version DOI 10.5281/zenodo.23213765 · all versions 10.5281/zenodo.23213764 · first published 2 October 2026