DevTools Logo

BPE Tokenizer Trainer

BPE Tokenizer Trainer

Train a byte-level BPE vocabulary on your own text and replay every merge, right in the browser.

Runs in your browser
Try an example

Corpus and configuration

31 lines · 2,394 bytes
Paste a corpus or load an example, then train.

Training results

0 merges · 0/256 target

No vocabulary yet

Train a corpus to see merges, the vocabulary and step-by-step replay.

Examples

Repeated pairs merge first

Input
ab ab ab ab cd cd · target vocab 260
Output
0: a + b -> ab (count 8); 1: c + d -> cd (count 4)

Counts are weighted by corpus frequency, and ties break by first occurrence in corpus order.

Replay after merge 0

Input
Sample 'ab cd', slider at 0
Output
⎪a⎪b⎪ ⎪c⎪d (decodes back to 'ab cd')

At step zero only the mandatory byte vocabulary exists; each later merge shortens the token list without changing the decoded text.

Corpus exhausted

Input
abcdef with a large target vocabulary
Output
Stopped with 'Corpus exhausted'; no pair occurs twice.

The trainer stops honestly instead of inventing merges that have no evidence in the corpus.

About this tool

BPE Tokenizer Trainer trains a real byte-level byte-pair-encoding vocabulary from text you paste into the browser. It pre-tokenizes the corpus with a GPT-2-style regex (or whitespace/character modes), counts word frequencies, then repeatedly merges the most frequent adjacent pair until the target vocabulary size is reached or the corpus is exhausted.

Unlike static explainers and token counters with frozen vocabularies, this tool exposes the learning process: merges stream into an ordered table with their counts, the 256 byte tokens are inspectable, and any sample can be replayed after merge k with explicit boundary glyphs. Round-trip decoding proves that the learned encoding is reversible.

The trainer runs in short browser ticks with a cancel button and a documented 64 KB corpus budget, keeping the page responsive. Statistic include vocabulary size, token count and bytes per token; exports are HF-compatible vocab.json and merges.txt files. No corpus is uploaded or stored.

What this tool does not do

  • Training is capped at 64 KB in the browser so it stays responsive; larger corpora need an offline trainer.
  • The vocabulary is byte-level GPT-2-style and is not compatible with any vendor tokenizer's ids.
  • Merge counting uses unique pre-tokens weighted by frequency, which is faithful for the displayed corpus but may differ from a production trainer's optimizations.
  • No Web Worker is used; long training runs are sliced into short ticks and can be cancelled at any time.

How to use this tool

  1. Load or paste a corpus

    Choose one of the three original presets or paste your own text; the byte count and budget warning update live.

  2. Set training options

    Choose a target vocabulary between 260 and 4096, a pre-tokenization mode and whether to lowercase, then press Train.

  3. Inspect the learning process

    Read the ordered merge table, search the vocabulary or switch to Step-through to replay a sample at any merge index.

  4. Export the model

    Download vocab.json and merges.txt and use them with a tokenizers-compatible pipeline.

Use cases

Learn BPE visually

Watch merge order form on a small corpus instead of reading a diagram in a blog post.

Prototype a domain tokenizer

Train on code or domain text to see which patterns a vocabulary captures before a full offline run.

Teaching tokenization

Drag the merge slider in front of a class to show how boundaries change while the text stays the same.

Inspect encoding reversibility

Use the decode line in Step-through to verify that merging never loses information.

Outputs and their meanings

ViewWhat it shows
Mergesrank, left + right symbols, merged token and corpus-weighted count
Vocabulary256 base byte symbols plus every token learned by a merge
Step-throughsample tokenization after merge k with ⎪ boundary glyphs and decoded text
Statsvocabulary size, corpus bytes, tokens produced, bytes/token and early-stop reason
vocab.jsonsymbol → id mapping in the Hugging Face format
merges.txtone 'left right' pair per line, with a version comment header

The Ġ marker is the GPT-2 rendering of the space byte so whitespace is visible inside token symbols.

Common mistakes

Mistake:Expecting the vocabulary to match GPT-2 or tiktoken exactly.

Fix:This trains a new GPT-2-style byte-level vocabulary from your text; vendor encodings are a different tool.

Mistake:Pasting a multi-megabyte corpus and wondering why training is blocked.

Fix:The browser budget is 64 KB so the main thread stays responsive; sample the text or train offline for larger corpora.

Mistake:Interpreting Ġ as a literal character in the source text.

Fix:Ġ is the GPT-2 rendering of byte 32 (space); decoding restores the real whitespace.

Mistake:Assuming more merges always improve compression.

Fix:Watch bytes/token in Stats and replay early steps; diminishing returns are the point of the visualization.

Frequently asked questions

What is a merge in BPE?

A merge is learned from the corpus: the trainer finds the adjacent pair of symbols that occurs most often, replaces every occurrence with a new symbol, and records the rule with its rank and count. Repeating this builds a vocabulary in which frequent words or word pieces become single tokens.

Why start from 256 byte tokens?

Byte-level BPE starts with one token for each possible byte, so every Unicode string can be encoded without an unknown-token fallback. The GPT-2 bytes_to_unicode mapping keeps printable bytes readable and gives the rest distinct code points; the space byte renders as Ġ in token displays.

How does this compare with GPT-2's 50,257-token vocabulary?

This tool trains a new vocabulary from your own corpus, typically a few hundred tokens for the browser budget. GPT-2's ids belong to its own fixed vocabulary trained on a much larger corpus, so the two are structurally similar but not interchangeable.

Does whitespace matter (the Ġ convention)?

Yes. The GPT-2 pre-tokenizer keeps a leading space attached to the following word, and that space is displayed as Ġ so the token boundary is visible. Decoding reverses the mapping and restores real whitespace byte for byte.

How large a corpus can it train on?

The in-browser budget is 64 KB of text, enforced with a byte count and a clear warning. That keeps training responsive without a Web Worker and covers the learning and prototyping use cases; use an offline trainer for production vocabularies.

Can I cancel or replay training?

Yes. Training runs in short ticks and the Cancel button stops it while keeping the merges learned so far. The Step-through tab replays a sample after any merge index, with a decode-back line to verify reversibility.

References & standards