Skip to content

Let vendored copies of Transformers tokenizers opt into library-class repairs - #68

Merged
Qubitium merged 2 commits into
mainfrom
vendored-transformers-equivalence
Nov 18, 2026
Merged

Qubitium merged 2 commits into
mainfrom
vendored-transformers-equivalence

Conversation

@Qubitium

Copy link
Copy Markdown
Contributor

Problem

Tokenicer applies two class-specific repairs only to Transformers' own tokenizer classes:

  • normalize_sentencepiece (tokenicer/sentencepiece.py) runs only when type(tokenizer) is transformers.LlamaTokenizer.
  • _resolve_serialized_tokenizer (tokenicer/tokenicer.py) skips any class whose __module__ does not start with transformers..

GPT-QModel now pins the Transformers 5.23 ABI by vendoring exact copies of Transformers' model, config and tokenizer classes (ModelCloud/GPTQModel#4620). A vendored LlamaTokenizer is a different class object in a different module, so both repairs are skipped. For the same checkpoint, it then produces different token IDs than Transformers' own class. InternVL-Chat (Llama/Phi-3 text models) showed this in GPT-QModel's tests.

Change

  • New tokenicer.register_transformers_equivalent(cls, name=None). It is an explicit opt-in, keyed by the class object, that declares cls an exact copy of transformers.<name> (default cls.__name__). It is exported from tokenicer.
  • New tokenicer.equivalence.transformers_class_name(cls). It returns the class name for Transformers' own classes, the registered name for registered copies, and None otherwise.
  • Both gates now use transformers_class_name.
    • A class name alone never grants equivalence, so remote/custom tokenizer code is still left alone.
    • Subclasses of LlamaTokenizer are still not normalized as Llama.
  • The version is bumped to 0.0.21 in pyproject.toml and setup.cfg.

Tests

tests/test_equivalence.py runs offline and covers:

  • name resolution for library classes;
  • an unregistered copy that shares the name staying custom;
  • registration being per class object;
  • invalid registrations being rejected;
  • the SentencePiece gate following registration;
  • a copy registered under a different name not being normalized as Llama.
pytest tests/test_equivalence.py tests/test_sentencepiece.py tests/test_serialized_tokenizer.py tests/test_metaspace.py
91 passed, 1 skipped

ruff check tokenicer tests (0.9.6) passes. tokenicer/__init__.py already had an E402 on main for the import placed after HF cache setup; both late imports are now marked as intentional.

🤖 Generated with Claude Code

Qubitium and others added 2 commits November 18, 2026 08:32
… repairs

Tokenicer applies the SentencePiece pipeline restoration only to
type(tokenizer) is transformers.LlamaTokenizer, and the serialized
tokenizer.json preference only to classes whose module starts with
transformers. A library that vendors exact copies of Transformers tokenizer
classes (GPT-QModel pins the Transformers 5.23 ABI this way) therefore loses
both repairs and gets different token IDs for the same checkpoint.

Add register_transformers_equivalent(cls, name): an explicit, per-class-object
opt-in declaring cls an exact copy of transformers.<name>. Both checks now ask
transformers_class_name(cls), which returns the class name for Transformers'
own classes, the registered name for registered copies, and None otherwise. A
class name alone never grants equivalence, so remote/custom tokenizer code is
still left alone, and subclasses are still not normalized as Llama.

Bump to 0.0.21.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant