Skip to content

Fix lossy encode/decode round trip in BPETokenizerSimple (tabs and ids 197/198/201) - #1117

Merged
rasbt merged 3 commits into
rasbt:mainfrom
sohailshk:fix/bpe-tab-whitespace
Nov 26, 2026
Merged

rasbt merged 3 commits into
rasbt:mainfrom
sohailshk:fix/bpe-tab-whitespace

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

BPETokenizerSimple in ch02/05_bpe-from-scratch/bpe-from-scratch.ipynb does not round-trip two kinds of text.

  1. Tabs are dropped. pretokenize_text splits on line breaks and then scans each piece with ( +)|(\S+), which matches neither a tab nor any other non-space whitespace. With the GPT-2 vocab, encode("a\tb") returns [64, 65] (tiktoken: [64, 197, 65]) and decode gives "ab"; "col1\tcol2\n" decodes to "col1col2\n".
  2. In a trained vocab, decode turns ids 198 and 201 into \n and \r, because the checks are tid == 198 or ... and tid == 201 or .... Those ids are the newline/CR glyphs only in the OpenAI vocab. A vocab from train() starts with chr(0..255), where 198 and 201 are Æ and É, so "cafÆ Été" decodes to "caf\n \rté".

Changes:

  • pretokenize_text splits on \r\n and on any single whitespace character other than the plain space, and keeps each as its own token (shared by train() and encode()).
  • load_vocab_and_merges_from_openai aliases "\t" to id 197 (ĉ), like the existing \n -> 198 and \r -> 201 aliases.
  • decode maps the GPT-2 glyphs ĉ, Ċ, č to \t, \n, \r only when the OpenAI vocab is loaded, instead of matching hard-coded ids.
  • Other non-space whitespace (\x0b, \x0c, U+00A0) is not aliased, so with the GPT-2 vocab it now raises the existing "Characters not found in vocab" error instead of being dropped silently.

Tests added to ch02/05_bpe-from-scratch/tests.py: tab cases compared with tiktoken (ids, count of id 197, round trip), and a tokenizer trained on the-verdict round-tripping Å Æ É, \t and \r\n. On main all 6 new test cases fail (6 failed, 12 passed); with the change pytest ch02/05_bpe-from-scratch/tests.py gives 18 passed. ruff check and .github/scripts/check_double_quotes.py are clean.

…97/198/201)

encode() dropped tabs and other non-space whitespace because pretokenize_text only kept runs of spaces and non-whitespace, and decode() mapped ids 198 and 201 to newline and carriage return even for a trained vocab, where they are the characters U+00C6 and U+00C9. Keep each such whitespace character as its own token, alias the GPT-2 tab glyph to id 197 when loading the OpenAI vocab, and only apply the GPT-2 glyph mapping when the OpenAI vocab is loaded.
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@rasbt
rasbt merged commit af16e8a into rasbt:main Nov 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants