Skip to content

Fix stale _added_tokens_encoder entries in cpmant and wav2vec2 - #47440

Merged
itazap merged 2 commits into
huggingface:mainfrom
ishan-1010:fix/added-tokens-single-source
Sep 30, 2026
Merged

itazap merged 2 commits into
huggingface:mainfrom
ishan-1010:fix/added-tokens-single-source

Conversation

@ishan-1010

@ishan-1010 ishan-1010 commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

CPU CI GPU run-slow

Fixes #47439.

Two tokenizers leave _added_tokens_encoder out of sync with _added_tokens_decoder after mutating it, so convert_tokens_to_ids returns ids that no longer exist.

  • cpmant popped its space/line markers from self.added_tokens_encoder, which is the property. The property builds a fresh dict on every access, so the pop did nothing and the markers kept their stale ids in the real cache. Popping _added_tokens_encoder instead fixes it.
  • wav2vec2 set_target_lang deleted conflicting entries from the decoder only, so a removed added token still encoded to its old id, which now belongs to an unrelated vocabulary token in the new language. The loop now pops both dicts.

One regression test per model; both fail on main and pass here.

Earlier revisions of this PR did this through a base-class sync helper plus a cached sorted view. Reworked per the review to fix the two models directly, no base-class changes. The per-access property rebuild that the cache removed is not a problem in practice: the hot paths that used to read these properties in loops were already fixed to read them once (#46323, #46535, #47425), so nothing user-visible regresses.

Tests run: pytest tests/models/cpmant/test_tokenization_cpmant.py tests/models/wav2vec2/test_tokenization_wav2vec2.py tests/tokenization/test_tokenization_utils.py - 132 passed (with jinja2, tiktoken, sentencepiece installed), 0 failed.

AI-assisted; every line reviewed and run locally.

@Rocketknight1

Copy link
Copy Markdown
Member

cc @itazap

@ishan-1010
ishan-1010 force-pushed the fix/added-tokens-single-source branch from 12fdfb5 to c2c0177 Compare July 26, 2026 05:32
@ishan-1010

Copy link
Copy Markdown
Contributor Author

For whoever picks this up: the red shard is unrelated to this diff. tests_processors shard 2 failed with two httpx.RemoteProtocolError: Server disconnected errors in tests/models/idefics2/test_processing_idefics2.py (911 passed, 2 errors), which is a network drop while fetching a fixture, not a tokenizer failure. The diff here touches only tokenization files and all 8 tests_tokenization shards plus tests_custom_tokenizers are green.

I cannot re-run the job from my side. Happy to rebase to force a fresh run if that is easier than a re-run.

@itazap

itazap commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

I'm not sure if the sync function is what we're looking for here, there should be a direct way to fix the used _added_tokens_decoder / _added_tokens_encoder in the models that touch them

@ishan-1010
ishan-1010 force-pushed the fix/added-tokens-single-source branch from c2c0177 to 23132c9 Compare August 5, 2026 02:59
@ishan-1010 ishan-1010 changed the title Make _added_tokens_decoder the single source of truth for added-token state Fix stale _added_tokens_encoder entries in cpmant and wav2vec2 Aug 5, 2026
@ishan-1010

ishan-1010 commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@itazap pushed the direct version, base class untouched now. cpmant was popping the property instead of the real dict (one underscore in the end), and wav2vec2's set_target_lang pops both dicts now. dropping the cache costs nothing btw, the hot loops already read these once since #46323/#47425.

still think a single mutation point would stop this from recurring, but that can be a follow-up, this PR doesn't depend on it.

@ishan-1010

Copy link
Copy Markdown
Contributor Author

@itazap just checking in on this, it's been about 6 weeks since I pushed the direct fix addressing your feedback. CI is green and it's still mergeable, happy to make any further changes if needed.

@itazap

itazap commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

hey @ishan-1010 , I apologize for the big delay on this! I just left a few comments. happy to merge this asap afterwards!

@ishan-1010
ishan-1010 force-pushed the fix/added-tokens-single-source branch from 23132c9 to 91f508f Compare September 29, 2026 09:21
@ishan-1010

ishan-1010 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@itazap done, pushed. rebased on main too. the new tests fail without the fix.

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: cpmant, wav2vec2

@ishan-1010
ishan-1010 force-pushed the fix/added-tokens-single-source branch from 91f508f to af8618c Compare September 29, 2026 09:41
- wav2vec2: pop the encoder entry straight from the decoder pop
- tests: drop set(), assertNotIn on _added_tokens_encoder and the
  contents of _added_tokens_decoder, compare the dicts directly
@ishan-1010
ishan-1010 force-pushed the fix/added-tokens-single-source branch from af8618c to 32c8cdb Compare September 29, 2026 09:42
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 36548614480:2
Result: success | Jobs: 6 | Tests: 627 | Failures: 0 | Duration: 3m 7s

@itazap
itazap enabled auto-merge September 30, 2026 14:31
@itazap

itazap commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

awesome, thank you ! 🚀

@itazap
itazap disabled auto-merge September 30, 2026 14:38
@itazap

itazap commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

run_slow: wav2vec2, cpmant

@github-actions

Copy link
Copy Markdown
Contributor

AMD CI

Workflow Run ⚙️

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

models: ["models/cpmant", "models/wav2vec2"]

@github-actions

Copy link
Copy Markdown
Contributor

Nvidia CI

Workflow Run ⚙️

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

models: ["models/cpmant", "models/wav2vec2"]
quantizations: []

@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.

@github-actions

Copy link
Copy Markdown
Contributor

CI Results (AMD)

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN b479ea42 workflow commit (merge commit)
PR 32c8cdbe branch commit (from PR)
main b8bea615 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@github-actions

Copy link
Copy Markdown
Contributor

CI Results (Nvidia)

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN b479ea42 workflow commit (merge commit)
PR 32c8cdbe branch commit (from PR)
main b8bea615 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@itazap
itazap added this pull request to the merge queue Sep 30, 2026
Merged via the queue into huggingface:main with commit 5aab642 Sep 30, 2026
43 checks passed
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.

Added-token encoder cache goes stale when tokenizers mutate _added_tokens_decoder directly, giving ids that do not round-trip

4 participants