Repository navigation
Add _get_num_multimodal_tokens to more image/video processors - #50671
Conversation
vLLM's Transformers modeling backend needs `_get_num_multimodal_tokens` to size placeholders without running the processor. Add it for image, video and audio processors whose models scatter features into `inputs_embeds`. The common image test now skips processors without an image processor, and the audio test's two-audios-per-text half can be disabled for processors that pair one audio with each text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
|
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. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
zucchini-nlp
left a comment
There was a problem hiding this comment.
very nice if that helps vllm to support more models with the backend! I did a quick pass but lmk if you need a proper review of code inside each get_xx_length
I have one big q: are we still relying on mm_token_type_ids and this helper on vllm side since the last time iirc we decided to put it as legacy path. I was actually hoping we wont need it anymore and just use whatever processor.call reutrns 😅
| height, width = image.shape[-2:] | ||
| new_height, new_width = get_rescaled_size( | ||
| height, width, rescale_image_frac, rescale_image_max_upscaled_long_edge | ||
| ) | ||
| if (new_height, new_width) != (height, width): | ||
| image = self.resize(image, SizeDict(height=new_height, width=new_width), resample=resample) | ||
| image_patches = divide_to_patches(image, size.height) |
There was a problem hiding this comment.
i see the point and imo would be good to sync with @molbap when he is back, he is adding a proper helper to get image's fina resized size in here
https://github.com/huggingface/transformers/pull/46771/changes#r4310085639
There was a problem hiding this comment.
Agreed, worth syncing. Inkling's rescale (fraction of the long edge with an upscale cap) isn't one of the standard size modes, so it wouldn't go through _resize_output_size and I'd keep get_rescaled_size local. Where the helper would really help is the counting methods: Idefics3, SmolVLM and UltravBERT re-derive the standard resize in get_number_of_image_patches. Those only have (height, width), so it'd be great if the helper took sizes rather than an image tensor. Happy to switch them over once #46771 lands.
(Reply drafted with AI assistance.)
| # `<image>` is expanded into `<IMG_ATOM>` tokens, which are the ones holding the image features | ||
| self.image_token_ids = [tokenizer.convert_tokens_to_ids("<IMG_ATOM>")] |
vLLM's Transformers backend only calls it with image and video sizes, so revert the audio-only processors and the audio branches of Inkling and Nemotron-H Omni. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
_get_num_multimodal_tokens to more processors_get_num_multimodal_tokens to more image/video processors
… overrides vLLM reads placeholders from `text_replacement_offsets`, not `mm_token_type_ids`, so revert the overrides and test the counts against the replacements instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
…cation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
`mm_token_type_ids` marks start/end/separator tokens for some models and not others, so compare `_get_num_multimodal_tokens` against the most repeated token in each item's `text_replacement_offsets` replacement instead, which is what vLLM counts. Align the helpers that also counted those extra tokens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
…ests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
zucchini-nlp
left a comment
There was a problem hiding this comment.
Oke, only one comment and i didnt go further. After your reply that audio uses a different way than get_num_multimodal_tokens, I went to check how vllm uses it and imo the usage is limted to obtaining num_patches for flattened pixels
If i got it right, I'd say to drop the helper and move to proper public helper on image processors. Butlmk if i misread vllm code since i only tried search by keyword
zucchini-nlp
left a comment
There was a problem hiding this comment.
so for the helper's num_image_tokens output - i am seeing that we try to sync with the actual length of image features, i.e. BOI/EOI/row/col tokens do not count anymore. Just want to make sure this is exactly what vllm wants and happy to "break" if so, i don't think anyone uses these helpers anyway
Also, if so, theer are still some old models which add row/col tokens into final num_image_tokens e.g. Ideifcs3. Might be worth revisitng them to sync
| if "mm_token_type_ids" not in inputs: | ||
| self.skipTest("Processor doesn't support `mm_token_type_ids`") | ||
|
|
||
| num_image_tokens_from_call = inputs.mm_token_type_ids.sum(-1).tolist() | ||
| num_image_tokens_from_helper = processor._get_num_multimodal_tokens(image_sizes=image_sizes) | ||
| num_image_tokens_from_call = self._count_embed_tokens(processor, inputs, "image") | ||
| num_image_tokens_from_helper = processor._get_num_multimodal_tokens( | ||
| image_sizes=image_sizes, **self.num_image_tokens_images_kwargs | ||
| ) |
There was a problem hiding this comment.
why did we have to chnage the check? i know vllm dones't rely on mm_token_type_ids anymore but still these three values are all supposed to be same at the end, no?
There was a problem hiding this comment.
They're only the same for models without BOI/EOI/separator tokens. mm_token_type_ids marks those for some models (Aya Vision, InternVL, Pixtral) but not others (Qwen, GLM), which is why some models needed overrides to pass the old check. vLLM doesn't read mm_token_type_ids anymore, it counts the embedding tokens in each text_replacement_offsets replacement, so the test now compares the helper with exactly that.
(Reply drafted with AI assistance.)
There was a problem hiding this comment.
m_token_type_ids marks those for some models (Aya Vision, InternVL, Pixtral) but not others (Qwen, GLM),
nooo 😭 can you open an issue an assign me so we can make it uniform at some point, even if breaking?
There was a problem hiding this comment.
Opened #51234 and assigned you.
(Reply drafted with AI assistance.)
- LFM2-VL: drop `get_image_layout` and `get_number_of_image_patches` from the image processor; the processor resolves the kwargs and calls `_get_split_layout` itself. - Ovis2: inline `_get_tile_grid` back into its callers in both backends. - UnlimitedOCR: move the local patch grid computation from the image processor into the processor. - Molmo2: compute the `<im_patch>` count from the grid instead of building the replacement string. - DeepSeek-OCR-2: clarify why one patch is subtracted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
[For maintainers] Suggested jobs to run (before merge) run-slow: aya_vision, blip_2, chameleon, deepseek_ocr2, deepseek_vl, deepseek_vl_hybrid, got_ocr2, idefics2, inkling, instructblip, internvl, lfm2_vl, llama4, molmo2, nemotron_h_omni, ovis2 |
|
Re the BOI/EOI question: yes, excluding them is what vLLM wants.
So the helper should return the number of embedding tokens. Overcounting (like Idefics3) only reserves a bit more encoder cache than needed, while undercounting could leave the cache too small for the largest image, which is what the new equality test guards against. My earlier comment on Aya Vision saying the count should include BOI/EOI and tile tokens was wrong, sorry for the confusion. For Idefics3 and the other models that still count separators in their own test overrides, I'd align them in a follow-up rather than grow this PR further. (Reply drafted with AI assistance.) |
zucchini-nlp
left a comment
There was a problem hiding this comment.
For Idefics3 and the other models that still count separators in their own test overrides, I'd align them in a follow-up rather than grow this PR further.
oke, sounds good to me! Does that mean idefics3 doesn't work properly in vllm atm, or iiuc it affects only the encoder budget during profiling so not too bad?
Also can you open gh issue for mm-token-type-ids for me?
Mostly the budget. vLLM only uses There is one real failure, though. With
Done: #51234 (assigned to you). (Reply drafted with AI assistance.) |
|
The Idefics3/Fuyu alignment follow-up is up as a draft: #51236 (Comment drafted with AI assistance.) |
What does this PR do?
Adds
_get_num_multimodal_tokensto 18 processors whose models scatter image or video features intoinputs_embeds, so that vLLM's Transformers modeling backend can size their placeholders without running the processor (e.g. PaddleOCR-VL currently fails with'PaddleOCRVLProcessor' object has no attribute '_get_num_multimodal_tokens').blip_2, deepseek_ocr2, deepseek_vl, deepseek_vl_hybrid, got_ocr2, idefics2, inkling, instructblip, lfm2_vl, llama4, nemotron_h_omni, ovis2, paddleocr_vl, phi4_multimodal, smolvlm, step3p7, unlimited_ocr, video_llava
__call__and the new method rather than duplicated.DeepseekOcr2ImageProcessorPilinherited GOT-OCR2'sget_number_of_image_patches, which disagreed with its own cropping; it now matches the torchvision backend.Tests
test_get_num_multimodal_tokens_matches_processor_call*tests compare the helper with the embedding tokens in each item'stext_replacement_offsetsreplacement, per item, instead of withmm_token_type_ids(which marks start/end/separator tokens for some models and not others).num_image_tokens_images_kwargs, which Llama4 and Ovis2 use to enable tiling.max_image_tokens). ShieldGemma2 skips it because it creates one sample per image and policy.Not covered:
pixel_valuesrow), Kosmos-2/2.5 (no single placeholder id) and InstructBLIP-Video (token count is fixed at 4 frames, independent of the video).Testing
pytest tests/models/*/test_processing_*.py -k get_num_multimodal_tokens: 82 passed (83 withRUN_SLOW=1).pytest tests/models/<model>/test_processing_*.py tests/models/<model>/test_image_processing_*.pyfor every changed model: all pass.This PR was written with AI assistance (Claude Code).
🤖 Generated with Claude Code