Why the encoder replays the merge list in order

L2WorkingConcept8 minCodingML depth

RolesLLM Product EngineeringAI Data SystemsInference Engineering

The question

At encode time a BPE tokenizer holds the complete merge table. Why does it have to replay the merges in the order they were learned, and what goes wrong when two merges could fire at the same spot?

Answer it first, aloud or on paper. Then open the answer.

The answer

Show short answer

Encoding must reproduce what training did to the same text, and training saw the merges one at a time in that order. If merge one created "th" and merge five created "the" from "th" plus "e", then "the" can only form after "th" exists. When two merges could apply at one position, the one with the lower rank, learned earlier, wins; change that rule and the same text yields different ids, so the model sees a distribution of ids it was never trained on.

Show full answer

The naive encoder

python
def encode_bpe(word, merges):
    symbols = list(word) + ["</w>"]
    for a, b in merges:
        i = 0
        while i < len(symbols) - 1:
            if symbols[i] == a and symbols[i + 1] == b:
                symbols = symbols[:i] + [a + b] + symbols[i + 2:]
            else:
                i += 1
    return symbols

It walks the whole table for every word, so the cost is the word length times the number of merges. Correct for a merge table trained on characters, and slow.

The rank-based encoder

The capstone version starts from the byte split and repeats: find the applicable merge with the lowest rank anywhere in the current sequence, apply it, and scan again, until no merge in the table applies. Production encoders keep the same semantics with a rank lookup and a priority queue of candidate pairs, which runs in close to linear time.

Why the rule is the contract

The training loop and the encoder share the merge table, and that sharing is the whole contract. Training merged the most frequent pair first, so an input that contains both halves of an early merge was always merged that way in the training data. An encoder that prefers a later merge, or the longest piece, produces a segmentation the model never saw. Nothing crashes; the ids are simply different.

What special tokens do here

Reserved strings such as the end-of-text marker are matched exactly before the merge loop runs and mapped to fixed ids. They never take part in a merge, so no table order can split or join them.

Follow-ups

  1. L2WorkingWhy is the naive loop slow?

    Show expected answer

    it scans the full merge table for every word, so the cost grows with the table size times the word length.

  2. L3SeniorHow do production encoders get close to linear time?

    Show expected answer

    a rank lookup for each adjacent pair and a priority queue that always surfaces the lowest-ranked applicable merge.

  3. L3SeniorTwo tokenizers were trained on different corpora to the same vocabulary size. Do they give the same ids?

    Show expected answer

    no; different corpora give different merge tables and different ranks.

  4. L3SeniorWhat test pins the order?

    Show expected answer

    encode a held-out word and compare with recorded ids; the capstone's tests also pin the round trip and the special-token reservation.

Wrong answers

Show the wrong answers that interviewers hear
  • “Greedily take the longest token in the vocabulary that matches.”

    That is a different algorithm and gives a different segmentation from the training run.

  • “Order does not matter because merges commute.”

    A later merge depends on the symbols that earlier merges created, so they do not commute.

  • “Sort the merges by token length.”

    Length is not rank; a long piece may have been learned early and a short one late.

Taught in

The tokenizers lesson explains the ordered table, the BPE capstone implements the rank-based encoder, and the subword lesson gives the naive loop and its cost.