This Attention & Transformers concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.
Attention & Transformers
BPE vs. Unigram Tokenization
Compare ordered BPE merges with unigram path search on the same string, then predict which tokenizer uses fewer tokens and why.
Concept Structure
BPE vs. Unigram Tokenization
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learner Contract
What this page should let you do.
1 prerequisite listed; refresh them before leaning on the math or code.
Explain the mechanism, trace the main notation, and test one prediction in the live demo.
Read the intuition before the notation; the math should name a mechanism you already felt.
Follow this edge after making one prediction here; the next page should reuse the result, not restart the route.
Claim/source review status
Substantive review recorded
1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
BPE and unigram tokenizers can both produce subword tokens, but they answer different questions.
BPE asks: "Given the merge rules I learned, what happens if I replay those rules on this string?" The order matters. A merge that happens early can create a larger symbol that a later rule can use.
Unigram tokenization asks: "Among all segmentations allowed by my candidate vocabulary, which path has the best score?" It does not replay one ordered merge list. It searches over possible token paths and chooses the segmentation with the highest product of token probabilities, or equivalently the lowest sum of negative log probabilities.
That distinction matters when a string has several plausible pieces. A BPE tokenizer may keep the split created by its historical merge order, while a unigram tokenizer may prefer a different segmentation because one whole piece has a better score.
This page uses a deliberately tiny example so the split is inspectable. It is a teaching model, not a production tokenizer.
Source spine: Sennrich, Haddow, and Birch, Neural Machine Translation of Rare Words with Subword Units; Kudo and Richardson, SentencePiece; Hugging Face course notes on BPE and Unigram tokenization.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
For BPE, start with characters and apply the learned merge rules in order:
Here is the -th learned merge pair. Encoding is deterministic once the vocabulary, merge list, pre-tokenization, and normalization are fixed.
For unigram tokenization, assume a candidate vocabulary and token probabilities for each . A segmentation is a sequence of tokens whose concatenation is the input string :
The best unigram segmentation is
Equivalently, using costs , it minimizes total path cost:
A dynamic program computes this by scanning positions in the string:
The backpointer that achieved the minimum tells us the final segmentation.
SentencePiece-style unigram training starts with many candidate pieces and prunes pieces whose removal hurts likelihood least. The demo below does not train that model. It shows the inference-time choice once a small scored vocabulary is already given.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
This small script compares ordered BPE merges with unigram path search on the same word.
from math import inf
word = "unhugs"
merges = [("u", "g"), ("u", "n"), ("h", "ug"), ("p", "un")]
cost = {
"un": 1.10, "hugs": 1.35, "hug": 1.25, "s": 1.80,
"u": 3.20, "n": 3.20, "h": 3.20, "g": 3.20, "ug": 2.10,
}
def apply_merge(parts, pair):
out, i = [], 0
while i < len(parts):
if i + 1 < len(parts) and (parts[i], parts[i + 1]) == pair:
out.append(parts[i] + parts[i + 1])
i += 2
else:
out.append(parts[i])
i += 1
return out
def bpe_encode(text):
parts = list(text)
for pair in merges:
parts = apply_merge(parts, pair)
return parts
def unigram_encode(text):
dp = [(inf, -1) for _ in range(len(text) + 1)]
dp[0] = (0.0, 0)
for end in range(1, len(text) + 1):
for start in range(end):
token = text[start:end]
if token in cost and dp[start][0] + cost[token] < dp[end][0]:
dp[end] = (dp[start][0] + cost[token], start)
tokens, pos = [], len(text)
while pos:
start = dp[pos][1]
tokens.append(text[start:pos])
pos = start
return list(reversed(tokens)), dp[-1][0]
print("BPE:", bpe_encode(word))
print("Unigram:", unigram_encode(word))
BPE returns ["un", "hug", "s"] because those pieces are created by the ordered merge list. The unigram path search returns ["un", "hugs"] because that two-piece path has lower total cost in the given scored vocabulary.
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Prediction check: inspect the same input string and the candidate unigram pieces, then predict which tokenizer uses fewer tokens and which unigram segmentation has the lowest path cost before revealing the ordered BPE replay and the Viterbi-style path search.
The lab hides the final BPE segmentation, path costs, winning unigram path, token counts, and explanation until you commit. The goal is to feel the difference between a deterministic merge history and a scored segmentation search.
Live Concept Demo
Explore BPE vs. Unigram Tokenization
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what BPE vs. Unigram Tokenization should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
Compare ordered BPE merges with unigram path search on the same string, then predict which tokenizer uses fewer tokens and why.
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change BPE vs. Unigram Tokenization should make visible.
Visual Inquiry
Make the image answer a mathematical question
Compare ordered BPE merges with unigram path search on the same string, then predict which tokenizer uses fewer tokens and why.
Which visible object should carry the first intuition?
Pick the cue that should make BPE vs. Unigram Tokenization easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Primary source for BPE as open-vocabulary subword modeling through repeated frequent symbol-pair merges.
Open sourcePrimary source for SentencePiece, raw-sentence subword model training, and the BPE/unigram tokenizer-family distinction.
Open sourceCourse source for the tiny hug/pug/pun/bun/hugs BPE merge walkthrough that anchors the BPE side of the demo.
Open sourceCourse source for unigram segmentation as a scored path search, including Viterbi-style best-path computation and pruning intuition.
Open sourceClaim Review
Compare ordered BPE merges with unigram path search on the same string, then predict which tokenizer uses fewer tokens and why.
Claims without a substantive review badge still need exact source-support review.
sennrich-2016-bpe-subword, kudo-richardson-2018-sentencepiece, hf-llm-course-bpe, hf-llm-course-unigram
Use equations, runnable code, and demos to check whether the source support is operational.
Sennrich et al. and the Hugging Face BPE walkthrough support the ordered merge-rule view of BPE. Kudo/Richardson and the Hugging Face Unigram page support a candidate-vocabulary subword model where segmentation is chosen by probability/path score rather than by replaying a single merge list.
Sources: Neural Machine Translation of Rare Words with Subword Units, SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing, Hugging Face LLM Course: Byte-Pair Encoding tokenization, Hugging Face LLM Course: Unigram tokenizationThe interactive demo uses a tiny illustrative unigram cost table and a tiny BPE merge list. It is not a production SentencePiece trainer, does not implement subword regularization, and does not model normalization, byte fallback, special tokens, or multilingual coverage.A bounded review summary is present; still check caveats and exact reference scope.Checked ACL Anthology records for Sennrich et al. and Kudo/Richardson plus Hugging Face BPE/Unigram course pages. GPT Pro/Oracle critique remains unavailable because 127.0.0.1:51672 is closed.
Reviewer: codex-local-primary-source-audit; reviewed 2026-07-03Source support candidates
paper 2016Neural Machine Translation of Rare Words with Subword UnitsPrimary source for BPE as open-vocabulary subword modeling through repeated frequent symbol-pair merges.
paper 2018SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text ProcessingPrimary source for SentencePiece, raw-sentence subword model training, and the BPE/unigram tokenizer-family distinction.
course-notes 2026Hugging Face LLM Course: Byte-Pair Encoding tokenizationCourse source for the tiny hug/pug/pun/bun/hugs BPE merge walkthrough that anchors the BPE side of the demo.
course-notes 2026Hugging Face LLM Course: Unigram tokenizationCourse source for unigram segmentation as a scored path search, including Viterbi-style best-path computation and pruning intuition.
Practice Loop
Try the idea before it explains itself
Compare ordered BPE merges with unigram path search on the same string, then predict which tokenizer uses fewer tokens and why.
Before touching the demo, predict one visible change that should happen in BPE vs. Unigram Tokenization.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to a claim, equation, code, or demo
Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.Open the draft below to save one note and next action in this browser.
BPE vs. Unigram Tokenization
What is the smallest example that makes BPE vs. Unigram Tokenization click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays in this browser, attached to the selected learning item.
- References to inspect: attached references on this page.
- Definition, prerequisite, and contrast concept links
- The equation or runnable code that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - BPE vs. Unigram Tokenization Selected item key: recorded for copy. Context: Attention & Transformers Page anchor: recorded for copy. Open question: What is the smallest example that makes BPE vs. Unigram Tokenization click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/attention-transformers/bpe-vs-unigram-tokenization
concept:attention-transformers/bpe-vs-unigram-tokenization