Skip to content

fix: use _added_tokens_encoder cache instead of property in convert_tokens_to_string and get_vocab#46464

Closed
Achyuthan-S wants to merge 4 commits into
huggingface:mainfrom
Achyuthan-S:fix/added-tokens-encoder-per-token-rebuild
Closed

fix: use _added_tokens_encoder cache instead of property in convert_tokens_to_string and get_vocab#46464
Achyuthan-S wants to merge 4 commits into
huggingface:mainfrom
Achyuthan-S:fix/added-tokens-encoder-per-token-rebuild

Conversation

@Achyuthan-S

@Achyuthan-S Achyuthan-S commented Jun 6, 2026

Copy link
Copy Markdown

Fixes #46396

Follow-up to #46323, which fixed _convert_token_to_id_with_added_voc.

The added_tokens_encoder property rebuilds and re-sorts the full token
map on every access. This PR replaces remaining per-token calls to the
property with direct reads from the maintained _added_tokens_encoder
cache in convert_tokens_to_string() and get_vocab() across four
slow tokenizers: byt5, myt5, dia, and perceiver.

9 replacements across 4 files. The property itself and
tokenization_cpmant.py are intentionally untouched (Option B from
the issue).

Tests run:

  • tests/models/byt5/test_tokenization_byt5.py
  • tests/models/myt5/test_tokenization_myt5.py
  • tests/models/dia/test_tokenization_dia.py
  • tests/models/perceiver/test_tokenization_perceiver.py
  • tests/test_tokenization_common.py

All passing (160 + 18 passed, 0 failures).

cc @itazap

Copilot AI review requested due to automatic review settings June 6, 2026 15:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds dtype propagation when loading sub-model configs from composite configs, and aligns several tokenizers to use the internal added-tokens encoder mapping.

Changes:

  • Propagate dtype from parent (composite) config into derived config during AutoModel* loading.
  • Add a regression test to ensure dtype is preserved when loading a CausalLM from a saved ConditionalGeneration checkpoint.
  • Update multiple tokenizers to reference _added_tokens_encoder when building vocab / converting tokens.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/models/qwen3_5/test_modeling_qwen3_5.py Adds a test intended to validate dtype propagation through save/load for AutoModel.
src/transformers/models/auto/auto_factory.py Implements dtype propagation from composite config to sub-config in from_config and from_pretrained.
src/transformers/models/perceiver/tokenization_perceiver.py Switches vocab + token-to-string logic to use _added_tokens_encoder.
src/transformers/models/myt5/tokenization_myt5.py Switches vocab + token-to-string logic to use _added_tokens_encoder.
src/transformers/models/dia/tokenization_dia.py Switches vocab + token-to-string logic to use _added_tokens_encoder.
src/transformers/models/byt5/tokenization_byt5.py Switches vocab + token-to-string logic to use _added_tokens_encoder.

Comment thread tests/models/qwen3_5/test_modeling_qwen3_5.py Outdated
Comment thread tests/models/qwen3_5/test_modeling_qwen3_5.py Outdated
Comment thread src/transformers/models/auto/auto_factory.py
Comment thread src/transformers/models/byt5/tokenization_byt5.py
Comment thread src/transformers/models/byt5/tokenization_byt5.py
@github-actions

github-actions Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: auto, byt5, dia, myt5, perceiver, qwen3_5

@Achyuthan-S Achyuthan-S deleted the fix/added-tokens-encoder-per-token-rebuild branch June 8, 2026 15:01
@Achyuthan-S Achyuthan-S restored the fix/added-tokens-encoder-per-token-rebuild branch June 8, 2026 15:02
@Achyuthan-S Achyuthan-S deleted the fix/added-tokens-encoder-per-token-rebuild branch July 5, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

added_tokens_encoder rebuilds the entire added-token map on every access

3 participants