Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix LLaVA image token count when processor_config lacks num_additional_image_tokens (#13091)
Daniel Han60d2f0c97513
diff --git a/tests/test_llava_num_additional_image_tokens.py b/tests/test_llava_num_additional_image_tokens.pynew file mode 100644index 00000000000..3bb72f9cb34--- /dev/null+++ b/tests/test_llava_num_additional_image_tokens.py@@ -0,0 +1,91 @@+"""LLaVA processors saved without num_additional_image_tokens (#2225, #3783)."""++from __future__ import annotations++import types++import pytest+import torch++transformers = pytest.importorskip("transformers")++from unsloth.tokenizer_utils import (+ _apply_post_load_tokenizer_fixes,+ _fix_llava_num_additional_image_tokens,+)++TINY = {+ "llava": "peft-internal-testing/tiny-LlavaForConditionalGeneration",+ "llava_next": "trl-internal-testing/tiny-LlavaNextForConditionalGeneration",+}+++def _config(vision_model_type):+ return types.SimpleNamespace(vision_config = types.SimpleNamespace(model_type = vision_model_type))+++def _load(repo):+ try:+ model = transformers.AutoModelForImageTextToText.from_pretrained(repo)+ processor = transformers.AutoProcessor.from_pretrained(repo)+ except OSError as e:+ pytest.skip(f"cannot fetch {repo}: {e}")+ return model.eval(), processor+++def _forward(model, processor):+ from PIL import Image++ image = Image.new("RGB", (640, 480), (120, 30, 200))+ inputs = processor(images = image, text = "<image>\nDescribe", return_tensors = "pt")+ with torch.no_grad():+ model(**inputs)++[email protected]("arch", sorted(TINY))+def test_processor_missing_key_is_repaired(arch):+ model, processor = _load(TINY[arch])+ # The unsloth/llava-* 16-bit repos ship processor_config.json without the key.+ processor.num_additional_image_tokens = 0+ with pytest.raises(ValueError, match = "Image features and image tokens do not match"):+ _forward(model, processor)++ fixed = _apply_post_load_tokenizer_fixes(processor, fix_tokenizer = False, config = model.config)+ assert fixed is processor and processor.num_additional_image_tokens == 1+ _forward(model, processor)+++def test_saved_processor_keeps_repair(tmp_path):+ model, processor = _load(TINY["llava"])+ processor.num_additional_image_tokens = 0+ _fix_llava_num_additional_image_tokens(processor, config = model.config)+ processor.save_pretrained(tmp_path)+ reloaded = transformers.AutoProcessor.from_pretrained(tmp_path)+ assert reloaded.num_additional_image_tokens == 1+++ "value, vision_model_type, config_style",+ [+ (1, "clip_vision_model", "object"), # already right+ (0, "siglip_vision_model", "object"), # no CLS token: 0 is right+ (0, None, "object"),+ (0, "clip_vision_model", "none"),+ ],+)+def test_other_processors_untouched(value, vision_model_type, config_style):+ processor = types.SimpleNamespace(num_additional_image_tokens = value)+ config = None if config_style == "none" else _config(vision_model_type)+ _fix_llava_num_additional_image_tokens(processor, config = config)+ assert processor.num_additional_image_tokens == value+++def test_dict_vision_config_and_text_tokenizer():+ processor = types.SimpleNamespace(num_additional_image_tokens = 0)+ config = types.SimpleNamespace(vision_config = {"model_type": "clip_vision_model"})+ _fix_llava_num_additional_image_tokens(processor, config = config)+ assert processor.num_additional_image_tokens == 1++ tokenizer = types.SimpleNamespace()+ _fix_llava_num_additional_image_tokens(tokenizer, config = _config("clip_vision_model"))+ assert not hasattr(tokenizer, "num_additional_image_tokens")diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.pyindex 0d592ab73d5..9b0cb93f85d 100644--- a/unsloth/tokenizer_utils.py+++ b/unsloth/tokenizer_utils.py@@ -214,6 +214,23 @@ def _fix_gemma4_base_bos_token(tokenizer, config = None):return tokenizer+def _fix_llava_num_additional_image_tokens(tokenizer, config = None):+ # A CLIP tower emits a CLS token on top of the patches, so a LLaVA processor saved without+ # num_additional_image_tokens (default 0) is one image token short and generate raises+ # "Image features and image tokens do not match" (#2225, #3783; llava-hf ships 1).+ if getattr(tokenizer, "num_additional_image_tokens", None) != 0:+ return tokenizer+ vision_config = getattr(config, "vision_config", None)+ if isinstance(vision_config, dict):+ model_type = vision_config.get("model_type")+ else:+ model_type = getattr(vision_config, "model_type", None)+ if model_type != "clip_vision_model":+ return tokenizer+ tokenizer.num_additional_image_tokens = 1+ return tokenizer++# v5 loads byte-level BPE repos declaring LlamaTokenizerFast with Metaspace, dropping spaces (transformers#45488, #48206)._BACKEND_ROUNDTRIP_PROBE = "Hello world, this is a test."_BACKEND_IDS_PROBE = (@@ -340,6 +357,7 @@ def _apply_post_load_tokenizer_fixes(tokenizer = _repair_tokenizer_backend_from_json(tokenizer, cache_dir = cache_dir, revision = revision)+ tokenizer = _fix_llava_num_additional_image_tokens(tokenizer, config = config)if not fix_tokenizer:return tokenizertokenizer = _fix_gemma4_base_bos_token(tokenizer, config = config)