Thanks to visit codestin.com
Credit goes to github.com

Skip to content

🚨 Modularize ProcessorMixin into smaller components#45493

Merged
zucchini-nlp merged 46 commits into
huggingface:mainfrom
zucchini-nlp:replace-image-tokens
May 20, 2026
Merged

🚨 Modularize ProcessorMixin into smaller components#45493
zucchini-nlp merged 46 commits into
huggingface:mainfrom
zucchini-nlp:replace-image-tokens

Conversation

@zucchini-nlp

@zucchini-nlp zucchini-nlp commented Apr 17, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Modularizes ProcessorMixin to make it easier for new processors to override smaller fn rather than the whole __call__. Splits __call__ into smaller functions such as validation, input preparation, replacing multimodal placeholders, and a few properties for common special tokens

In simple cases like llava or qwen2-vl, the processor only has to override one method -> replace_image_tokens. It takes a single image input and returns the corresponding placeholder text. More complicated models can override and add their own validation and input preparation, e.g. gemma3 requires nested images and has lots of sanity checks

Converted a bunch of processors with different modalities to check that it works. I think for the rest we can either ask community to contrib or do it in a separate PR. This PR is already bloating up

Best way to review: non-model files -> llava -> gemma3 -> audioflamingo -> idefics3 -> gemma4 -> test files (already includes variety of processor types)

The "breaking" part is due to moving self.image_ids/video_ids/audio_ids from instance attr to properties. Thus they will not be saved on config which is the correct way we want!

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

@zucchini-nlp zucchini-nlp changed the title [WIP] Major processing refactor Major processing refactor Apr 23, 2026
@zucchini-nlp zucchini-nlp requested a review from yonigozlan April 23, 2026 13:39
@zucchini-nlp zucchini-nlp changed the title Modularize ProcessorMixin into smaller components :rotating-light: Modularize ProcessorMixin into smaller components May 19, 2026
@zucchini-nlp zucchini-nlp changed the title :rotating-light: Modularize ProcessorMixin into smaller components 🚨 Modularize ProcessorMixin into smaller components May 19, 2026
@zucchini-nlp

Copy link
Copy Markdown
Member Author

Btw, re attr, they are still present under the same name and with the same content. The only diff is that it was an instance attr and not is a property, just in case it was not clear :)

There is a tiiny chance that it got saved in processor_config.json, and if someone tries to manually change the config it won't take effect. Though I hope, no one would go changing it manually in config 🙈 (added the rotating light for this and gemma3 breaking changes)

@zucchini-nlp zucchini-nlp enabled auto-merge May 20, 2026 01:29
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: aria, audioflamingo3, aya_vision, blip, chameleon, cohere2_vision, cohere_asr, colmodernvbert, exaone4_5, florence2, fuyu, gemma3, gemma4, glm46v, glm4v, glmasr

@zucchini-nlp zucchini-nlp added this pull request to the merge queue May 20, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 20, 2026
@zucchini-nlp zucchini-nlp added this pull request to the merge queue May 20, 2026
Merged via the queue into huggingface:main with commit ba06e3f May 20, 2026
92 of 94 checks passed
@zucchini-nlp zucchini-nlp deleted the replace-image-tokens branch May 20, 2026 02:45
yuchenxie4645 pushed a commit to yuchenxie4645/transformers that referenced this pull request May 28, 2026
)

* tmp

* more

* .

* .

* qwen

* apply to video with timestamps processing

* mllama with no image tokens

* delete dups

* split modality fn

* stricter check and consistent naming

* fix videos and audio

* bc for non-MLLM processors

* some renaming and reordering

* two more models

* check wih audio processor

* oops

* delete more similar code

* two more models

* a bit more

* more models

* fix tests

* fix idefics

* fix repo

* now it should pass CI

* omg, a typo

* fix tests

* comments from eustlb

* oops, typo

* delete url from images utils

* fix

* some comments

* docs

* what, gh ui rebased weirdly

* !

* fix repo

* Apply suggestions from code review

Co-authored-by: Steven Liu <[email protected]>

* Update src/transformers/models/aya_vision/processing_aya_vision.py

Co-authored-by: Anton Vlasjuk <[email protected]>

* revert gemma3

---------

Co-authored-by: Steven Liu <[email protected]>
Co-authored-by: Anton Vlasjuk <[email protected]>
lucianommartins added a commit to lucianommartins/my-transformers that referenced this pull request May 29, 2026
…ultimodal tokens

- Handle multimodal tokens in text that have no corresponding
  replacement data (e.g. <|video|> with videos=None)
- Use next(iter, None) sentinel instead of bare next() to avoid
  StopIteration when the replacement iterator is exhausted
- Preserve the original token text in the output when no replacement
  is available, matching pre-huggingface#45493 behavior
- Regression introduced in ba06e3f (huggingface#45493) which refactored
  ProcessorMixin.__call__ into get_text_with_replacements with
  strict iterator consumption over all regex-matched tokens
- Reproduces when downstream frameworks (e.g. vLLM) send profiling
  prompts containing multimodal placeholder tokens without actual
  multimodal data attached
)

Signed-off-by: Luciano Martins <[email protected]>
kashif pushed a commit to kashif/transformers that referenced this pull request Jun 1, 2026
)

* tmp

* more

* .

* .

* qwen

* apply to video with timestamps processing

* mllama with no image tokens

* delete dups

* split modality fn

* stricter check and consistent naming

* fix videos and audio

* bc for non-MLLM processors

* some renaming and reordering

* two more models

* check wih audio processor

* oops

* delete more similar code

* two more models

* a bit more

* more models

* fix tests

* fix idefics

* fix repo

* now it should pass CI

* omg, a typo

* fix tests

* comments from eustlb

* oops, typo

* delete url from images utils

* fix

* some comments

* docs

* what, gh ui rebased weirdly

* !

* fix repo

* Apply suggestions from code review

Co-authored-by: Steven Liu <[email protected]>

* Update src/transformers/models/aya_vision/processing_aya_vision.py

Co-authored-by: Anton Vlasjuk <[email protected]>

* revert gemma3

---------

Co-authored-by: Steven Liu <[email protected]>
Co-authored-by: Anton Vlasjuk <[email protected]>
ArthurZucker added a commit that referenced this pull request Jun 4, 2026
Migrate the image/video/audio token expansion of these processors to the
shared ProcessorMixin helper introduced in #45493 instead of bespoke logic:
each processor now defines `replace_image_token`/`replace_video_token`/
`replace_audio_token` and routes expansion through `get_text_with_replacements`.

Models: deepseek_ocr2, colqwen2, ernie4_5_vl_moe (image+video), glm_image,
llava_next (+ granite4_vision via modular), llava_onevision, granite_speech.

Note: `get_text_with_replacements` mutates the text list in place, so callers
that must not edit the caller's input pass a copy.
ArthurZucker added a commit that referenced this pull request Jun 4, 2026
Replace the earlier text-mutation test with one that spies on
`get_text_with_replacements` and asserts the processor routes multimodal token
expansion through it (the #45493 API) rather than bespoke logic. Skip it for
processors that legitimately don't expand tokens: ColQwen2/ColModernVBert
(text XOR images) and Mllama (one image token per image + cross-attention).
ArthurZucker added a commit that referenced this pull request Jun 4, 2026
Route image/video/audio placeholder-token expansion through the shared
ProcessorMixin.get_text_with_replacements helper (#45493) for the processors
that still used bespoke `.replace`/`re.sub`/sentinel logic, each now defining
replace_image_token/replace_video_token/replace_audio_token (or building the
replacement list inline where the expansion is context-dependent):

pixtral, internvl, qianfan_ocr, lighton_ocr, qwen2_audio, csm, video_llava,
smolvlm, higgs_audio_v2, emu3, cohere2_vision, kosmos2, florence2, deepseek_vl,
deepseek_vl_hybrid, gemma3, minicpmv4_6, paddleocr_vl, phi4_multimodal,
qwen2_5_omni, qwen3_omni_moe.

Output is byte-for-byte identical (validated against each model's existing
token-count tests, or byte-equivalence harnesses where tests are gated by
torchaudio/unreleased checkpoints). Mllama is intentionally left as-is (one
image token per image + cross-attention, nothing to expand). gemma3n is
deferred: its Hub chat template emits soft tokens, so it needs a soft-token
anchored migration rather than a boi/eoi one.
lucianommartins added a commit to lucianommartins/my-transformers that referenced this pull request Jun 4, 2026
…ultimodal tokens

- Handle multimodal tokens in text that have no corresponding
  replacement data (e.g. <|video|> with videos=None)
- Use next(iter, None) sentinel instead of bare next() to avoid
  StopIteration when the replacement iterator is exhausted
- Preserve the original token text in the output when no replacement
  is available, matching pre-huggingface#45493 behavior
- Regression introduced in ba06e3f (huggingface#45493) which refactored
  ProcessorMixin.__call__ into get_text_with_replacements with
  strict iterator consumption over all regex-matched tokens
- Reproduces when downstream frameworks (e.g. vLLM) send profiling
  prompts containing multimodal placeholder tokens without actual
  multimodal data attached
)

Signed-off-by: Luciano Martins <[email protected]>
khushali9 pushed a commit to khushali9/transformers that referenced this pull request Jun 8, 2026
)

* tmp

* more

* .

* .

* qwen

* apply to video with timestamps processing

* mllama with no image tokens

* delete dups

* split modality fn

* stricter check and consistent naming

* fix videos and audio

* bc for non-MLLM processors

* some renaming and reordering

* two more models

* check wih audio processor

* oops

* delete more similar code

* two more models

* a bit more

* more models

* fix tests

* fix idefics

* fix repo

* now it should pass CI

* omg, a typo

* fix tests

* comments from eustlb

* oops, typo

* delete url from images utils

* fix

* some comments

* docs

* what, gh ui rebased weirdly

* !

* fix repo

* Apply suggestions from code review

Co-authored-by: Steven Liu <[email protected]>

* Update src/transformers/models/aya_vision/processing_aya_vision.py

Co-authored-by: Anton Vlasjuk <[email protected]>

* revert gemma3

---------

Co-authored-by: Steven Liu <[email protected]>
Co-authored-by: Anton Vlasjuk <[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.

7 participants