Skip to content

fix: handle in-memory tokenizers in get_tokenizer_with_bos - #1962

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
janmenjayap:fix/tokenizer-bos-in-memory
Nov 2, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
janmenjayap:fix/tokenizer-bos-in-memory

Conversation

@janmenjayap

Copy link
Copy Markdown
Contributor

Description

get_tokenizer_with_bos called init_kwargs.pop("name_or_path") with no default. Tokenizers built in memory, such as
PreTrainedTokenizerFast(tokenizer_object=Tokenizer(...)), never get that key, so any of them with a bos_token raised KeyError: 'name_or_path'. This included tokenizers that already prepend BOS. Every Bridge boot runs this function through setup_tokenizer, so TransformerBridge.boot_native(cfg, tokenizer=...) with such a tokenizer crashed before the bridge was built.

Changes:

  • Read the path with a default, falling back to tokenizer.name_or_path.
  • When there is no path to reload from, enable add_bos_token on a copy of a fast tokenizer. This rebuilds the post-processor the same way from_pretrained(add_bos_token=True) does in transformers v5. The original tokenizer is returned when BOS cannot be enabled (slow tokenizers, or BOS tokens that TemplateProcessing rejects). This follows the existing T5 and Seed-OSS fallbacks. The caller's tokenizer is never modified.
  • The from_pretrained path is unchanged, including the padding_side preservation from [Bug Report] Padding side inconsistency with Huggingface Transformers #801.

Tests:

  • tests/unit/utilities/test_tokenize_utils.py: BOS gets enabled on a copy, with padding_side kept 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_native with an in-memory tokenizer boots, records tokenizer_prepends_bos, prepends BOS once, respects prepend_bos=False, and leaves the caller's tokenizer unchanged.

All new tests fail on dev with KeyError: 'name_or_path' and pass with this change. They are fully offline (no hub download).

No new dependencies.

Fixes #1961


Type of change

  • Bug fix (non-breaking change which fixes an issue)

Screenshots

Before (dev) After
get_tokenizer_with_bos(tok) -> KeyError: 'name_or_path' returns a copy; "hello world" -> [1, 3, 4]
boot_native(cfg, tokenizer=tok) -> KeyError: 'name_or_path' boots; to_tokens("hello world") -> [[1, 3, 4]]

Validation

Command Result
uv run pytest tests/unit/utilities/test_tokenize_utils.py tests/unit/model_bridge/test_boot_native.py tests/unit/test_tokenizer_padding_side.py with dev's tokenize_utils.py and the new tests 4 failed (KeyError: 'name_or_path'), 90 passed
same, with the fix 94 passed
make check-format passed, no changes
uv run mypy . passed, no issues in 390 source files
make unit-test passed: 7970 passed, 59 skipped, 3 xfailed on the fix commit; passed again on the final branch as part of make test-pr
make docstring-test passed: 13 passed, 24 skipped
make test-pr 1850 passed, 7 skipped, 2 failed: test_TransformerBridge_gemma2_forward and test_get_params[google/gemma-2-2b-it] could not download the gated google/gemma-2-2b-it (no local HF token). Both are local-only and skipped in CI; neither reaches tokenizer code

Environment: macOS 14.8.9 arm64, Python 3.12.14, torch 2.11.0, transformers 5.13.0, tokenizers 0.22.1.


Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

@jlarson4

jlarson4 commented Nov 2, 2026

Copy link
Copy Markdown
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!

@janmenjayap
janmenjayap marked this pull request as ready for review November 2, 2026 15:19
@jlarson4
jlarson4 merged commit 922b479 into TransformerLensOrg:dev Nov 2, 2026
29 checks passed
@janmenjayap
janmenjayap deleted the fix/tokenizer-bos-in-memory branch November 3, 2026 00:34
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.

[Bug Report] get_tokenizer_with_bos raises KeyError for in-memory tokenizers

2 participants