Skip to content

Improve Transformers v4/v5 compatibility in tokenizers and processors#34768

Closed
cccat6 wants to merge 2 commits intovllm-project:mainfrom
cccat6:pr/transformers-v4-v5-runtime
Closed

Improve Transformers v4/v5 compatibility in tokenizers and processors#34768
cccat6 wants to merge 2 commits intovllm-project:mainfrom
cccat6:pr/transformers-v4-v5-runtime

Conversation

@cccat6
Copy link

@cccat6 cccat6 commented Feb 18, 2026

Summary

  • Improve tokenizer compatibility across Transformers v4/v5 without global monkey patches.
  • Add processor compatibility retry for remote processors that still pass optional_attributes.
  • Make Ovis2.5 special-token handling robust across Transformers v4/v5.
  • Support huggingface-hub v1 transfer flags while preserving older hub behavior.
  • Update targeted multimodal test input normalization for GLM-ASR minimum audio length.

Validation

  • transformers==4.56.2
    • pytest -q -rs tests/model_executor/test_weight_utils.py
    • pytest -q -rs tests/models/multimodal/processing/test_common.py::test_processing_correctness -k 'AIDC-AI/Ovis2.5-2B or allenai/Molmo2-8B or allenai/Molmo2-O-7B or zai-org/GLM-ASR-Nano-2512'
  • transformers==4.57.4
    • Same commands, pass.
  • transformers==5.1.0
    • Same commands, pass.

Transformers 5.2.0 validation

  • Updated dependency cap: transformers >= 4.56.0, <= 5.2.0.
  • Re-tested locally in a fresh GPU-enabled environment with transformers==5.2.0:
    • pytest -q -rs tests/model_executor/test_weight_utils.py (2 passed)
    • pytest -q -rs tests/models/multimodal/processing/test_common.py::test_processing_correctness -k 'AIDC-AI/Ovis2.5-2B or allenai/Molmo2-8B or allenai/Molmo2-O-7B or zai-org/GLM-ASR-Nano-2512' (12 passed, 351 deselected)

@mergify mergify bot added the multi-modality Related to multi-modality (#4194) label Feb 18, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces several improvements to enhance compatibility with different versions of transformers and huggingface-hub.

  • The enable_hf_transfer function is now compatible with both old and new APIs for transfer acceleration.
  • A robust compatibility layer has been added to handle older custom processors and tokenizers with newer versions of the transformers library, using temporary patches instead of global monkey-patching.
  • Special token handling for Ovis2.5 has been improved to be more robust.
  • Tests have been updated to be more comprehensive and to properly clean up global state modifications.

The changes are well-implemented and address important compatibility issues. The code quality is high, and the solutions are pragmatic and well-designed. I have no major concerns and approve of these changes.

@mergify
Copy link

mergify bot commented Feb 18, 2026

Hi @cccat6, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

Signed-off-by: Codex Bot <codex-bot@users.noreply.github.com>
@cccat6 cccat6 force-pushed the pr/transformers-v4-v5-runtime branch from b602bd0 to 54cc897 Compare February 18, 2026 02:42
@cccat6
Copy link
Author

cccat6 commented Feb 18, 2026

Addressed CI feedback in force-pushed commit 54cc897e2: ran pre-commit fixes (ruff check/format) and added DCO sign-off.

@gaby
Copy link

gaby commented Feb 18, 2026

@cccat6
Copy link
Author

cccat6 commented Feb 18, 2026

@cccat6 Can you also test transformers 5.2

https://github.com/huggingface/transformers/releases/tag/v5.2.0

Will do. Thanks.

@cccat6
Copy link
Author

cccat6 commented Feb 18, 2026

Follow-up on compatibility testing request:

I created a fresh local venv (/workspace/.venv-tf53) and re-ran targeted regression in that isolated environment.

Validated with transformers==5.2.0:

  • pytest -q -rs tests/model_executor/test_weight_utils.py -> 2 passed
  • pytest -q -rs tests/models/multimodal/processing/test_common.py::test_processing_correctness -k 'AIDC-AI/Ovis2.5-2B or allenai/Molmo2-8B or allenai/Molmo2-O-7B or zai-org/GLM-ASR-Nano-2512' -> 12 passed

No additional code changes were required from this round.

@cccat6
Copy link
Author

cccat6 commented Feb 18, 2026

Update: addressed the version-cap request and validated on Transformers 5.2.0.

  • Changed requirements/common.txt to transformers >= 4.56.0, <= 5.2.0.
  • Re-tested in a fresh GPU-enabled env with transformers==5.2.0:
    • pytest -q -rs tests/model_executor/test_weight_utils.py -> 2 passed
    • pytest -q -rs tests/models/multimodal/processing/test_common.py::test_processing_correctness -k 'AIDC-AI/Ovis2.5-2B or allenai/Molmo2-8B or allenai/Molmo2-O-7B or zai-org/GLM-ASR-Nano-2512' -> 12 passed, 351 deselected

This keeps v4/v5 transition compatibility while explicitly covering 5.2.0 as requested.

@mergify mergify bot added the ci/build label Feb 18, 2026
Signed-off-by: Codex Bot <codex-bot@users.noreply.github.com>
@cccat6 cccat6 force-pushed the pr/transformers-v4-v5-runtime branch from 06ba906 to 320ac1a Compare February 18, 2026 09:23
@hmellor
Copy link
Member

hmellor commented Feb 19, 2026

Thank you for this PR @cccat6, I'm going to ask @zucchini-nlp to help review this so that the processor changes are compatible with the vision for processors in Transformers. Then, when these models are upstreamed into Transformers, we can seamlessly transition to requiring no custom code on the HF Hub or in vLLM

Comment on lines +47 to +64
missing = [
token_name
for token_name in REQUIRED_SPECIAL_TOKENS.values()
if _get_token_id(tokenizer, token_name) is None
]
if not missing:
return set()

add_special_tokens = getattr(tokenizer, "add_special_tokens", None)
if callable(add_special_tokens):
# Keep order stable so token IDs remain deterministic across runs.
add_special_tokens({"additional_special_tokens": missing})
missing = [
token_name
for token_name in missing
if _get_token_id(tokenizer, token_name) is None
]

Copy link
Contributor

Choose a reason for hiding this comment

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

this is quite a long of manipulations to get a token-id. TBH I'd expect that if the token id exists, then convert_tokens_to_ids will return an int otherwise we can stop searching

Better to ask for a confirmation from @itazap

Comment on lines +36 to +42
def _ensure_processing_utils_compat() -> None:
# Some remote processors still import this alias, which is missing in
# newer Transformers releases.
if not hasattr(hf_processing_utils, "ChatTemplateLoadKwargs"):
fallback = getattr(hf_processing_utils, "ProcessorChatTemplateKwargs", None)
if fallback is not None:
hf_processing_utils.ChatTemplateLoadKwargs = fallback
Copy link
Contributor

Choose a reason for hiding this comment

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

indeed, I think it was only MiniCPM. Would be great to nudge authors to update their remote code if possible

Copy link
Member

@hmellor hmellor left a comment

Choose a reason for hiding this comment

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

Blocking for now as this PR does a few too many things

blake3
py-cpuinfo
transformers >= 4.56.0, < 5
transformers >= 4.56.0, <= 5.2.0
Copy link
Member

Choose a reason for hiding this comment

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

We're not ready to do this yet

Suggested change
transformers >= 4.56.0, <= 5.2.0
transformers >= 4.56.0, < 5

Copy link
Member

Choose a reason for hiding this comment

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

Not related to compatibility, but is a QoL feature. I've extracted this to #35098

Copy link
Member

Choose a reason for hiding this comment

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

Surely turning all the missing methods into noops will break whatever model had them?

@hmellor
Copy link
Member

hmellor commented Feb 23, 2026

Closing as the fixes that are usable have been superseded by other PRs and the ones that remain are not correct

@hmellor hmellor closed this Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci/build multi-modality Related to multi-modality (#4194)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants