Repository navigation
Fix lossy encode/decode round trip in BPETokenizerSimple (tabs and ids 197/198/201) - #1117
Merged
Merged
Conversation
…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.
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BPETokenizerSimpleinch02/05_bpe-from-scratch/bpe-from-scratch.ipynbdoes not round-trip two kinds of text.pretokenize_textsplits 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]) anddecodegives"ab";"col1\tcol2\n"decodes to"col1col2\n".decodeturns ids 198 and 201 into\nand\r, because the checks aretid == 198 or ...andtid == 201 or .... Those ids are the newline/CR glyphs only in the OpenAI vocab. A vocab fromtrain()starts withchr(0..255), where 198 and 201 areÆandÉ, so"cafÆ Été"decodes to"caf\n \rté".Changes:
pretokenize_textsplits on\r\nand on any single whitespace character other than the plain space, and keeps each as its own token (shared bytrain()andencode()).load_vocab_and_merges_from_openaialiases"\t"to id 197 (ĉ), like the existing\n-> 198 and\r-> 201 aliases.decodemaps the GPT-2 glyphsĉ,Ċ,čto\t,\n,\ronly when the OpenAI vocab is loaded, instead of matching hard-coded ids.\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Å Æ É,\tand\r\n. On main all 6 new test cases fail (6 failed, 12 passed); with the changepytest ch02/05_bpe-from-scratch/tests.pygives 18 passed.ruff checkand.github/scripts/check_double_quotes.pyare clean.