Repository navigation
feat(model): support SmolVLM and SmolVLM2, fix the idefics3 template - #10591
Open
Zephyr4772 wants to merge 1 commit into
Open
Zephyr4772 wants to merge 1 commit into
Zephyr4772 wants to merge 1 commit into
Conversation
- Expand each <image>/<video> with the HF processor, in order, using the processor's own image token instead of the hardcoded Idefics3-8B id. - Use the tokenizer's BOS and the chat template's `Role: text` / `Role:<image>` spacing, so encodings match the processor exactly. - Pad pixel_values/pixel_attention_mask across samples (batch size > 1). - Register the SmolVLM models under idefics3 and add smolvlm2 (SmolVLMForConditionalGeneration, image and video). - Remove align_image_inputs, which is no longer used. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR type
PR information
Fixes #10590. Refs #2989, #6419.
The
idefics3template only worked for Idefics3-8B, and even there it drifted from the model's chat template. This PR:<image>/<video>with the HF processor, in order of appearance, using the processor's own image token (Idefics3-8B: 128257, SmolVLM: 49190, SmolVLM-Instruct: 49153) instead of a hardcoded id. Video uses the processor's frame sampling (1 fps, up to 64 frames), as its chat template does.<|begin_of_text|>/<|im_start|>) and the chat template's spacing:Role: text, butRole:<image>when a turn starts with an image; the response gets the leading space because the generation prompt ends withAssistant:.pixel_values/pixel_attention_maskacross samples, so batch size > 1 works (Error finetuning SmolVLM (idefics3 template) with batch size > 1 #6419). The model drops all-zero patches; batched logits match one-by-one logits (max diff ~3e-4 in fp32).idefics3, and addssmolvlm2(SmolVLMForConditionalGeneration) for SmolVLM2-256M/500M-Video-Instruct and 2.2B-Instruct, tags vision + video. SmolVLM2 needsnum2words.align_image_inputs, which only this template used.vLLM/LMDeploy keep the plain placeholders (not tested here).
Experiment results
tests/general/test_idefics3_template.py(processor files only, no weights): input_ids, labels, pixel_values and pixel_attention_mask equal the HF processor + chat template for system + multi-turn, two images inside text, text-only and SmolVLM2 video; the collator pads patches. 9 passed with this PR, 9 failed on main.Same comparison on the processors of Idefics3-8B, SmolVLM-Instruct (2.2B) and SmolVLM2-2.2B-Instruct: all match. On main, Idefics3-8B differs on every text turn, e.g.
User:hello ... Assistant:worldvsUser: hello ... Assistant: world.Greedy inference (TransformersEngine) gives exactly the same answers as plain
transformersgenerate()with the processor's chat template, for image, text and video (SmolVLM-256M, SmolVLM2-256M). Batched inference with mixed image sizes and a video equals one-by-one inference.Training (RTX 4060, LoRA):
Checklist
This PR was prepared with AI assistance (Claude Code), which wrote the change and ran the tests and checks above. I am responsible for it and will answer review questions.