Skip to content

Fix LLaVA image token count when processor_config lacks num_additional_image_tokens - #13091

Merged
danielhanchen merged 1 commit into
mainfrom
fix-llava-num-additional-image-tokens
Oct 9, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix-llava-num-additional-image-tokens

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes #2225
Also fixes #3783 (same root cause on llava-1.5).

Problem

unsloth/llava-v1.6-mistral-7b-hf and unsloth/llava-1.5-7b-hf ship a processor_config.json without num_additional_image_tokens (the -bnb-4bit copies and the official llava-hf repos have 1). The processor then defaults to 0, 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:

ValueError: Image features and image tokens do not match, tokens: 2339, features: 2340   # llava-v1.6-mistral-7b-hf
ValueError: Image features and image tokens do not match, tokens: 575, features: 2359296  # llava-1.5-7b-hf

Any user checkpoint saved from these processors carries the same gap.

Fix

_apply_post_load_tokenizer_fixes (run by FastVisionModel / FastModel after loading) sets num_additional_image_tokens = 1 when the processor has it at 0 and the model's vision_config.model_type is clip_vision_model. A CLIP tower with 0 mismatches for every image under both default and full feature strategies, so only setups that currently crash change. Processors already at 1, SigLIP towers (0 is 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 correct processor_config.json.

Verification

Real checkpoints through FastVisionModel.from_pretrained(..., load_in_4bit = False) + generate on a 640x480 image (B200, transformers 5.17.0):

Checkpoint main this PR
unsloth/llava-v1.6-mistral-7b-hf ValueError ... tokens: 2339, features: 2340 2340 tokens, generates
unsloth/llava-1.5-7b-hf ValueError ... tokens: 575, features: 2359296 576 tokens, generates
unsloth/llava-v1.6-mistral-7b-hf-bnb-4bit (control, already has the key) generates generates, identical text

3-step LoRA SFT on unsloth/llava-1.5-7b-hf (16-bit, UnslothVisionDataCollator, batch 2): main fails at step 1 with ValueError: 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:

transformers main this PR
4.52.4 (floor) ValueError ok, reload keeps 1
4.57.6 ValueError ok, reload keeps 1
5.17.0 (pin ceiling) ValueError ok
5.19.0 (latest) ValueError ok, reload keeps 1

tests/test_llava_num_additional_image_tokens.py: 8 passed; with the call to the fix removed, both tiny-model repair tests fail.

@danielhanchen danielhanchen self-assigned this Oct 8, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T17:20:49.896234Z bbc61a0 Manual request
🔒 Security Review ⚠️ Failed 2026-10-08T17:17:47.195703Z bbc61a0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: bbc61a0a58

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant