Repository navigation
fix: flush byte fallback runs at end of input #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pcuenca
merged 5 commits into
huggingface:main
from
TheTom:codex/bytefallback-end-of-input
Oct 28, 2026
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d91813
test: cover terminal byte fallback and downstream tokenizer cleanup
TheTom 95d0ab6
fix: flush terminal bytes inside the byte fallback decoder
TheTom 863f785
style: format the byte fallback regression fixtures
TheTom 8e9e58b
DecoderFactory throws
pcuenca 0789de4
Remove misleading (imo) test
pcuenca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import Foundation | ||
| import Hub | ||
| import Testing | ||
|
|
||
| @testable import Tokenizers | ||
|
|
||
| @Suite("Byte fallback end-of-input") | ||
| struct ByteFallbackEndOfInputTests { | ||
| @Test("Terminal UTF-8 bytes decode without a following ordinary token") | ||
| func terminalEmoji() { | ||
| let decoder = ByteFallbackDecoder(config: Config(["type": "ByteFallback"] as [NSString: Any])) | ||
| #expect(decoder.decode(tokens: ["<0xF0>", "<0x9F>", "<0x98>", "<0x80>"]) == ["😀"]) | ||
| } | ||
|
|
||
| @Test("Incomplete terminal UTF-8 uses the same replacement behavior as an interior run") | ||
| func incompleteTerminalRun() { | ||
| let decoder = ByteFallbackDecoder(config: Config(["type": "ByteFallback"] as [NSString: Any])) | ||
| #expect(decoder.decode(tokens: ["<0xF0>", "<0x9F>"]) == ["�"]) | ||
| #expect(decoder.decode(tokens: ["<0xF0>", "<0x9F>", "a"]) == ["�", "a"]) | ||
| } | ||
|
|
||
| @Test("Interior and terminal byte runs retain their order") | ||
| func mixedRuns() { | ||
| let decoder = ByteFallbackDecoder(config: Config(["type": "ByteFallback"] as [NSString: Any])) | ||
| #expect(decoder.decode(tokens: ["a", "<0x62>", "c", "<0x20>"]) == ["a", "b", "c", " "]) | ||
| #expect(decoder.decode(tokens: []) == []) | ||
| #expect(decoder.decode(tokens: ["ordinary"]) == ["ordinary"]) | ||
| } | ||
|
|
||
| private func pipeline() -> Config { | ||
| Config( | ||
| [ | ||
| "type": "Sequence", | ||
| "decoders": [ | ||
| ["type": "Replace", "pattern": ["String": "▁"], "content": " "], | ||
| ["type": "ByteFallback"], | ||
| ["type": "Fuse"], | ||
| ["type": "Strip", "content": " ", "start": 0, "stop": 1], | ||
| ], | ||
| ] as [NSString: Any]) | ||
| } | ||
|
|
||
| @Test("A terminal byte-spelled space still passes through downstream Fuse and Strip") | ||
| func downstreamCleanup() throws { | ||
| let decoder = try #require(DecoderFactory.fromConfig(config: pipeline())) | ||
|
pcuenca marked this conversation as resolved.
Resolved
|
||
| #expect(decoder.decode(tokens: ["<0x20>"]) == [""]) | ||
| #expect(decoder.decode(tokens: ["a", "<0x20>"]) == ["a"]) | ||
| #expect(decoder.decode(tokens: ["▁"]) == [""]) | ||
| } | ||
|
|
||
| @Test("PreTrainedTokenizer preserves terminal bytes, special-token filtering and configured cleanup") | ||
| func configuredTokenizer() throws { | ||
| let config = Config( | ||
| [ | ||
| "tokenizer_class": "PreTrainedTokenizerFast", | ||
| "unk_token": "<unk>", | ||
| "eos_token": "</s>", | ||
| "clean_up_tokenization_spaces": false, | ||
| ] as [NSString: Any]) | ||
| let data = Config( | ||
| [ | ||
| "added_tokens": [ | ||
| ["id": 0, "content": "<unk>", "special": true], | ||
| ["id": 1, "content": "</s>", "special": true], | ||
| ], | ||
| "decoder": pipeline(), | ||
| "model": [ | ||
| "type": "BPE", "unk_token": "<unk>", "byte_fallback": true, | ||
| "vocab": [ | ||
| "<unk>": 0, "</s>": 1, "a": 2, "▁": 3, | ||
| "<0x20>": 4, "<0xF0>": 5, "<0x9F>": 6, "<0x98>": 7, "<0x80>": 8, | ||
| ], | ||
| "merges": [String](), | ||
| ], | ||
| ] as [NSString: Any]) | ||
| let tokenizer = try PreTrainedTokenizer(tokenizerConfig: config, tokenizerData: data) | ||
| #expect(tokenizer.decode(tokens: [5, 6, 7, 8]) == "😀") | ||
| #expect(tokenizer.decode(tokens: [4]) == tokenizer.decode(tokens: [3])) | ||
| #expect(tokenizer.decode(tokens: [2, 4]) == "a") | ||
| #expect(tokenizer.decode(tokens: [1, 5, 6, 7, 8], skipSpecialTokens: true) == "😀") | ||
| #expect(tokenizer.decode(tokens: [1, 5, 6, 7, 8], skipSpecialTokens: false) == "</s>😀") | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.