Repository navigation
fix: handle in-memory tokenizers in get_tokenizer_with_bos - #1962
Merged
jlarson4 merged 2 commits intoNov 2, 2026
Merged
Conversation
This was referenced Nov 1, 2026
1 task done
Collaborator
|
This looks good to my eyes @janmenjayap, excellent work on this bug fix. If you're happy with it as well, mark it ready for review and I will merge! |
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.
Description
get_tokenizer_with_boscalledinit_kwargs.pop("name_or_path")with no default. Tokenizers built in memory, such asPreTrainedTokenizerFast(tokenizer_object=Tokenizer(...)), never get that key, so any of them with abos_tokenraisedKeyError: 'name_or_path'. This included tokenizers that already prepend BOS. Every Bridge boot runs this function throughsetup_tokenizer, soTransformerBridge.boot_native(cfg, tokenizer=...)with such a tokenizer crashed before the bridge was built.Changes:
tokenizer.name_or_path.add_bos_tokenon a copy of a fast tokenizer. This rebuilds the post-processor the same wayfrom_pretrained(add_bos_token=True)does in transformers v5. The original tokenizer is returned when BOS cannot be enabled (slow tokenizers, or BOS tokens thatTemplateProcessingrejects). This follows the existing T5 and Seed-OSS fallbacks. The caller's tokenizer is never modified.from_pretrainedpath is unchanged, including thepadding_sidepreservation from [Bug Report] Padding side inconsistency with Huggingface Transformers #801.Tests:
tests/unit/utilities/test_tokenize_utils.py: BOS gets enabled on a copy, withpadding_sidekept and the original unchanged; an in-memory tokenizer that already adds BOS is returned as is; a BOS that cannot be expressed falls back to the original.tests/unit/model_bridge/test_boot_native.py:boot_nativewith an in-memory tokenizer boots, recordstokenizer_prepends_bos, prepends BOS once, respectsprepend_bos=False, and leaves the caller's tokenizer unchanged.All new tests fail on
devwithKeyError: 'name_or_path'and pass with this change. They are fully offline (no hub download).No new dependencies.
Fixes #1961
Type of change
Screenshots
dev)get_tokenizer_with_bos(tok)->KeyError: 'name_or_path'"hello world"->[1, 3, 4]boot_native(cfg, tokenizer=tok)->KeyError: 'name_or_path'to_tokens("hello world")->[[1, 3, 4]]Validation
uv run pytest tests/unit/utilities/test_tokenize_utils.py tests/unit/model_bridge/test_boot_native.py tests/unit/test_tokenizer_padding_side.pywithdev'stokenize_utils.pyand the new testsKeyError: 'name_or_path'), 90 passedmake check-formatuv run mypy .make unit-testmake test-prmake docstring-testmake test-prtest_TransformerBridge_gemma2_forwardandtest_get_params[google/gemma-2-2b-it]could not download the gatedgoogle/gemma-2-2b-it(no local HF token). Both are local-only and skipped in CI; neither reaches tokenizer codeEnvironment: macOS 14.8.9 arm64, Python 3.12.14, torch 2.11.0, transformers 5.13.0, tokenizers 0.22.1.
Checklist: