Skip to content

fix: route Granite models to TokenizersBackend to preserve tokenizer.json pre-tokenizer#45813

Merged
itazap merged 4 commits into
huggingface:mainfrom
kndtran:fix/granite-tokenizer-pretokenizer
May 18, 2026
Merged

fix: route Granite models to TokenizersBackend to preserve tokenizer.json pre-tokenizer#45813
itazap merged 4 commits into
huggingface:mainfrom
kndtran:fix/granite-tokenizer-pretokenizer

Conversation

@kndtran

@kndtran kndtran commented May 6, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Change TOKENIZER_MAPPING_NAMES for Granite model types from "GPT2Tokenizer" to "TokenizersBackend" so that AutoTokenizer loads tokenizer.json faithfully instead of routing through GPT2Tokenizer.__init__ which hardcodes a wrong pre-tokenizer.

Fixes #45812

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.

PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.

This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read CONTRIBUTING.md.

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

…json pre-tokenizer

previous fix was not triggering

Fixes huggingface#45812
@kndtran kndtran changed the title fix: add Granite models to MODELS_WITH_INCORRECT_HUB_TOKENIZER_CLASS fix: route Granite models to TokenizersBackend to preserve tokenizer.json pre-tokenizer May 6, 2026
@itazap

itazap commented May 7, 2026

Copy link
Copy Markdown
Collaborator

Thank you @kndtran for finding this and producing a fix! I'm looking into why existing tests didn't find this so we should also add / update a test for granite models. Sorry it slipped through the cracks!

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix is valid tho! as @itazap said we need to probably add tests (integration) to cover that case

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@itazap

itazap commented May 12, 2026

Copy link
Copy Markdown
Collaborator

def test_model_generation(self, device):
EXPECTED_TEXT_COMPLETION = "Simply put, the theory of relativity states that 1) the laws of physics are the same for all observers in uniform motion relative"
prompt = "Simply put, the theory of relativity states that "
tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-4.0-h-tiny")
model = GraniteMoeHybridForCausalLM.from_pretrained("ibm-granite/granite-4.0-h-tiny", device_map=device)
model_inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# greedy generation outputs
generated_ids = model.generate(**model_inputs, max_new_tokens=16, do_sample=False)
text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
self.assertEqual(EXPECTED_TEXT_COMPLETION, text)

this is the only test we had for a granite-4 model and it did no test digit strings 😢 We can add a simple test with digits to this file - let me know if you'd like to add one or I can!

something like

self.assertEqual(tokenizer.encode("650841823", add_special_tokens=False), EXPECTED_OUTPUT)
etc.

@itazap

itazap commented May 18, 2026

Copy link
Copy Markdown
Collaborator

@kndtran please let me if you'd like to add a test otherwise I can!

@kndtran

kndtran commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

@itazap Ah, I thought you were replying to Arthur. I will add a few tests. There were a few other odd strings too.

Verify that AutoTokenizer produces correct token IDs for digit strings, punctuation, and mixed alphanumeric inputs.
@kndtran

kndtran commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

@itazap I added a new test class as the torch gating seemed unnecessary. Please modify as needed.

Here are some tables showing the tokenization effects on the test strings.

Decoded Token Comparison

String v5 (broken) v5 (fixed)
2023 ['20', '23'] ['202', '3']
650841823 ['650', '84', '18', '23'] ['650', '841', '823']
60-138-3818 ['60', '-', '138', '-', '38', '18'] ['60', '-', '138', '-', '381', '8']
d.o.o ['d', '.', 'o', '.', 'o'] ['d', '.o', '.o']
FY2023 ['FY', '20', '23'] ['FY', '202', '3']
ISO 9001:2015 ['ISO', ' ', '9', '001', ':', '201', '5'] ['ISO', ' ', '900', '1', ':', '201', '5']

Token ID Comparison

String v5 IDs (broken) v5 IDs (fixed)
2023 [508, 1419] [2366, 18]
650841823 [13655, 5833, 972, 1419] [13655, 25496, 23848]
60-138-3818 [1399, 12, 10350, 12, 1987, 972] [1399, 12, 10350, 12, 19162, 23]
d.o.o [67, 13, 78, 13, 78] [67, 14778, 14778]
FY2023 [82029, 508, 1419] [82029, 2366, 18]
ISO 9001:2015 [25141, 220, 24, 4119, 25, 679, 20] [25141, 220, 7467, 16, 25, 679, 20]
Reproduce
from transformers import AutoTokenizer, PreTrainedTokenizerFast

MODEL = "ibm-granite/granite-4.0-h-tiny"
STRINGS = ["2023", "650841823", "60-138-3818", "d.o.o", "FY2023", "ISO 9001:2015"]

broken = AutoTokenizer.from_pretrained(MODEL)
fixed = PreTrainedTokenizerFast.from_pretrained(MODEL, use_fast=True)

print(f"{'String':20s} {'v5 (broken)':45s} {'v5 (fixed)'}")
print("─" * 110)
for s in STRINGS:
    b_ids = broken.encode(s, add_special_tokens=False)
    f_ids = fixed.encode(s, add_special_tokens=False)
    b_tok = [broken.decode([i]) for i in b_ids]
    f_tok = [fixed.decode([i]) for i in f_ids]
    print(f"{s:20s} {str(b_tok):45s} {str(f_tok)}")

print()
print(f"{'String':20s} {'v5 IDs (broken)':45s} {'v5 IDs (fixed)'}")
print("─" * 110)
for s in STRINGS:
    b_ids = broken.encode(s, add_special_tokens=False)
    f_ids = fixed.encode(s, add_special_tokens=False)
    print(f"{s:20s} {str(b_ids):45s} {str(f_ids)}")

@itazap itazap added this pull request to the merge queue May 18, 2026
@itazap

itazap commented May 18, 2026

Copy link
Copy Markdown
Collaborator

Thank you @kndtran ! Appreciate your thorough investigation on this 🙌

@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: auto, granitemoehybrid

@itazap

itazap commented May 18, 2026

Copy link
Copy Markdown
Collaborator

run-slow: auto, granitemoehybrid

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/auto", "models/granitemoehybrid"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN a95b663e workflow commit (merge commit)
PR e10d0757 branch commit (from PR)
main 1ae5aaae base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@itazap itazap added this pull request to the merge queue May 18, 2026
Merged via the queue into huggingface:main with commit b8eea41 May 18, 2026
30 checks passed
jp1924 pushed a commit to jp1924/transformers that referenced this pull request May 18, 2026
…json pre-tokenizer (huggingface#45813)

* fix: add Granite models to MODELS_WITH_INCORRECT_HUB_TOKENIZER_CLASS

Fixes huggingface#45812

* fix: route Granite models to TokenizersBackend to preserve tokenizer.json pre-tokenizer

previous fix was not triggering

Fixes huggingface#45812

* test: add tokenizer encoding test for Granite 4+

Verify that AutoTokenizer produces correct token IDs for digit strings, punctuation, and mixed alphanumeric inputs.

---------

Co-authored-by: Khoi-Nguyen Tran <kndtran@ibm.com>
Co-authored-by: Ita Zaporozhets <31893021+itazap@users.noreply.github.com>
yuchenxie4645 pushed a commit to yuchenxie4645/transformers that referenced this pull request May 28, 2026
…json pre-tokenizer (huggingface#45813)

* fix: add Granite models to MODELS_WITH_INCORRECT_HUB_TOKENIZER_CLASS

Fixes huggingface#45812

* fix: route Granite models to TokenizersBackend to preserve tokenizer.json pre-tokenizer

previous fix was not triggering

Fixes huggingface#45812

* test: add tokenizer encoding test for Granite 4+

Verify that AutoTokenizer produces correct token IDs for digit strings, punctuation, and mixed alphanumeric inputs.

---------

Co-authored-by: Khoi-Nguyen Tran <kndtran@ibm.com>
Co-authored-by: Ita Zaporozhets <31893021+itazap@users.noreply.github.com>
kashif pushed a commit to kashif/transformers that referenced this pull request Jun 1, 2026
…json pre-tokenizer (huggingface#45813)

* fix: add Granite models to MODELS_WITH_INCORRECT_HUB_TOKENIZER_CLASS

Fixes huggingface#45812

* fix: route Granite models to TokenizersBackend to preserve tokenizer.json pre-tokenizer

previous fix was not triggering

Fixes huggingface#45812

* test: add tokenizer encoding test for Granite 4+

Verify that AutoTokenizer produces correct token IDs for digit strings, punctuation, and mixed alphanumeric inputs.

---------

Co-authored-by: Khoi-Nguyen Tran <kndtran@ibm.com>
Co-authored-by: Ita Zaporozhets <31893021+itazap@users.noreply.github.com>
khushali9 pushed a commit to khushali9/transformers that referenced this pull request Jun 8, 2026
…json pre-tokenizer (huggingface#45813)

* fix: add Granite models to MODELS_WITH_INCORRECT_HUB_TOKENIZER_CLASS

Fixes huggingface#45812

* fix: route Granite models to TokenizersBackend to preserve tokenizer.json pre-tokenizer

previous fix was not triggering

Fixes huggingface#45812

* test: add tokenizer encoding test for Granite 4+

Verify that AutoTokenizer produces correct token IDs for digit strings, punctuation, and mixed alphanumeric inputs.

---------

Co-authored-by: Khoi-Nguyen Tran <kndtran@ibm.com>
Co-authored-by: Ita Zaporozhets <31893021+itazap@users.noreply.github.com>
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.

AutoTokenizer produces wrong token IDs for all Granite models (silent v4→v5 regression)

5 participants