Repository navigation
Fix LLaVA image token count when processor_config lacks num_additional_image_tokens - #13091
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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.
Fixes #2225
Also fixes #3783 (same root cause on llava-1.5).
Problem
unsloth/llava-v1.6-mistral-7b-hfandunsloth/llava-1.5-7b-hfship aprocessor_config.jsonwithoutnum_additional_image_tokens(the-bnb-4bitcopies and the officialllava-hfrepos have1). The processor then defaults to0, while the CLIP vision tower emits a CLS token on top of the patches, so every image gets one placeholder token too few and generation or training fails:Any user checkpoint saved from these processors carries the same gap.
Fix
_apply_post_load_tokenizer_fixes(run byFastVisionModel/FastModelafter loading) setsnum_additional_image_tokens = 1when the processor has it at0and the model'svision_config.model_typeisclip_vision_model. A CLIP tower with0mismatches for every image under bothdefaultandfullfeature strategies, so only setups that currently crash change. Processors already at1, SigLIP towers (0is correct there), Florence2, text tokenizers and calls without a config are untouched. The value is saved with the processor, so new fine-tunes ship a correctprocessor_config.json.Verification
Real checkpoints through
FastVisionModel.from_pretrained(..., load_in_4bit = False)+generateon a 640x480 image (B200, transformers 5.17.0):ValueError ... tokens: 2339, features: 2340ValueError ... tokens: 575, features: 23592963-step LoRA SFT on
unsloth/llava-1.5-7b-hf(16-bit,UnslothVisionDataCollator, batch 2): main fails at step 1 withValueError: Image features and image tokens do not match, tokens: 1150, features: 4718592; this PR trains, losses[6.102, 6.117, 5.996].Tiny LLaVA / LLaVA-Next with the key forced to
0, forward pass, then save and reload:tests/test_llava_num_additional_image_tokens.py: 8 passed; with the call to the fix removed, both tiny-model repair tests fail.