Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(qwen-image): forward multimodal token types (#10050)
Guishi Wang02578c93d8db
diff --git a/invokeai/app/invocations/text_encoder/qwen_image_text_encoder.py b/invokeai/app/invocations/text_encoder/qwen_image_text_encoder.pyindex 93b7340055..4136c600b4 100644--- a/invokeai/app/invocations/text_encoder/qwen_image_text_encoder.py+++ b/invokeai/app/invocations/text_encoder/qwen_image_text_encoder.py@@ -330,6 +330,8 @@ class QwenImageTextEncoderInvocation(BaseInvocation):attention_mask=model_inputs.attention_mask,pixel_values=getattr(model_inputs, "pixel_values", None),image_grid_thw=getattr(model_inputs, "image_grid_thw", None),+ # Transformers 5 needs the processor's token types to build 3-D positions for image tokens.+ mm_token_type_ids=getattr(model_inputs, "mm_token_type_ids", None),output_hidden_states=True,)diff --git a/tests/app/invocations/test_qwen_image_text_encoder.py b/tests/app/invocations/test_qwen_image_text_encoder.pyindex e9914eea79..78586f8186 100644--- a/tests/app/invocations/test_qwen_image_text_encoder.py+++ b/tests/app/invocations/test_qwen_image_text_encoder.py@@ -6,6 +6,7 @@ import threadingimport tracebackimport weakreffrom pathlib import Path+from types import SimpleNamespacefrom unittest.mock import MagicMock, patchimport pytest@@ -16,6 +17,7 @@ from transformers import Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGenerationfrom invokeai.app.invocations.model import ModelIdentifierField, QwenVLEncoderFieldfrom invokeai.app.invocations.text_encoder.qwen_image_text_encoder import (+ _EDIT_DROP_IDX,_GENERATE_DROP_IDX,QwenImageTextEncoderInvocation,_build_prompt,@@ -79,6 +81,60 @@ class TestBuildPrompt:assert "add {curly} braces & <angle> brackets" in prompt+def test_run_encoder_uses_multimodal_positions_for_image_tokens() -> None:+ """The processor's token types must reach transformers so image tokens get 3-D M-RoPE positions."""+ with torch.random.fork_rng():+ torch.manual_seed(0)+ text_encoder = Qwen2_5_VLForConditionalGeneration(_tiny_config()).eval()++ image_slice = slice(_EDIT_DROP_IDX, _EDIT_DROP_IDX + 4)+ seq_len = _EDIT_DROP_IDX + 6+ input_ids = torch.ones(1, seq_len, dtype=torch.long)+ input_ids[:, image_slice] = text_encoder.config.image_token_id+ attention_mask = torch.ones_like(input_ids)+ mm_token_type_ids = torch.zeros_like(input_ids)+ mm_token_type_ids[:, image_slice] = 1+ image_grid_thw = torch.tensor([[1, 4, 4]], dtype=torch.long)+ patch_width = (+ text_encoder.config.vision_config.in_channels+ * text_encoder.config.vision_config.temporal_patch_size+ * text_encoder.config.vision_config.patch_size**2+ )+ pixel_values = torch.zeros(16, patch_width)++ shared_inputs = {+ "input_ids": input_ids,+ "attention_mask": attention_mask,+ "pixel_values": pixel_values,+ "image_grid_thw": image_grid_thw,+ }+ positions: list[torch.Tensor] = []+ handle = text_encoder.model.language_model.rotary_emb.register_forward_pre_hook(+ lambda _module, args: positions.append(args[1].detach().clone())+ )+ try:+ QwenImageTextEncoderInvocation._run_encoder(+ text_encoder,+ SimpleNamespace(**shared_inputs, mm_token_type_ids=mm_token_type_ids),+ edit_mode=True,+ )+ QwenImageTextEncoderInvocation._run_encoder(text_encoder, SimpleNamespace(**shared_inputs), edit_mode=True)+ finally:+ handle.remove()++ start = _EDIT_DROP_IDX+ assert torch.equal(+ positions[0][:, 0, image_slice],+ torch.tensor(+ [[start, start, start, start], [start, start, start + 1, start + 1], [start, start + 1, start, start + 1]]+ ),+ )+ assert torch.equal(+ positions[1][:, 0, image_slice],+ torch.arange(start, start + 4).repeat(3, 1),+ )++class TestResizeForVLEncoder:"""Test the image resizing logic for the VL encoder."""@@ -162,6 +218,9 @@ def _tiny_config() -> Qwen2_5_VLConfig:"vocab_size": 512,"max_position_embeddings": 256,"tie_word_embeddings": False,+ "bos_token_id": 1,+ "eos_token_id": 2,+ "pad_token_id": 0,"rope_scaling": {"type": "mrope", "mrope_section": [2, 3, 3]},},vision_config={@@ -177,6 +236,10 @@ def _tiny_config() -> Qwen2_5_VLConfig:"window_size": 112,"fullatt_block_indexes": [1],},+ image_token_id=5,+ video_token_id=6,+ vision_start_token_id=7,+ vision_end_token_id=8,)