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
feat(qwen-image-2-1): add edits from reference images
Alexander Eichhorn3250c61adb6d
diff --git a/docs/src/content/docs/Users Guide/03.Models/02.model-families.mdx b/docs/src/content/docs/Users Guide/03.Models/02.model-families.mdxindex 757cd7a354..aad924586b 100644--- a/docs/src/content/docs/Users Guide/03.Models/02.model-families.mdx+++ b/docs/src/content/docs/Users Guide/03.Models/02.model-families.mdx@@ -123,7 +123,7 @@ faster generation.| **Z-Image** | Images | 6B model in a fast, few-step Turbo variant and a Base variant. Understands English and Chinese prompts, with ControlNets. | Apache 2.0 | [Z-Image](/users-guide/models/local-models/z-image/) || **ERNIE-Image** | Images | Baidu's 8B text-to-image model, in standard and distilled Turbo variants. | Apache 2.0 | [ERNIE-Image](/users-guide/models/local-models/ernie-image/) || **Qwen Image** | Images | Text-to-image (2512) and instruction-based image editing (Edit 2511), with fast Lightning variants. | Apache 2.0 | [Qwen Image](/users-guide/models/local-models/qwen-image/) |-| **Qwen-Image-2.1** | Images | 7B text-to-image model with strong text rendering and transparent (RGBA) output, in a base and an 8-step Turbo variant. | Non-commercial | [Qwen-Image-2.1](/users-guide/models/local-models/qwen-image-2-1/) |+| **Qwen-Image-2.1** | Images | 7B text-to-image model with strong text rendering and transparent (RGBA) output, in a base and an 8-step Turbo variant. Edits from reference images. | Non-commercial | [Qwen-Image-2.1](/users-guide/models/local-models/qwen-image-2-1/) || **Krea-2** | Images | 12B model in fast Turbo and undistilled Raw variants, with training-free style reference. | Community, free under $1M revenue | [Krea-2](/users-guide/models/local-models/krea-2/) || **Ideogram 4** | Images | Structured prompts that place described elements in regions of the image. | Non-commercial | [Ideogram 4](/users-guide/models/local-models/ideogram4/) || **Anima** | Images | Compact 2B anime-focused model, with ControlNet-LLLite adapters for inpainting and sketch guidance. | Non-commercial | [Anima](/users-guide/models/local-models/anima/) |diff --git a/docs/src/content/docs/Users Guide/03.Models/Local Models/qwen-image-2-1.mdx b/docs/src/content/docs/Users Guide/03.Models/Local Models/qwen-image-2-1.mdxindex f7d9298c74..ecbb119018 100644--- a/docs/src/content/docs/Users Guide/03.Models/Local Models/qwen-image-2-1.mdx+++ b/docs/src/content/docs/Users Guide/03.Models/Local Models/qwen-image-2-1.mdx@@ -14,7 +14,7 @@ Two variants are available, and the variant is detected on install:- **Qwen-Image-2.1** — the base model. 40 steps by default.- **Qwen-Image-2.1 Turbo** — distilled for 8 steps on a fixed schedule of its own.-Text-to-image, image-to-image, inpainting and outpainting on the Canvas are supported. Reference images (image editing), LoRAs and regional guidance are not supported yet.+Text-to-image, image-to-image, inpainting and outpainting on the Canvas are supported, and with the Diffusers models also edits from [reference images](#reference-images). LoRAs and regional guidance are not supported yet.### License@@ -57,6 +57,15 @@ The text encoder runs first and is unloaded from the GPU when the transformer neThe VAE needs about 8 GB of working memory to decode a 1024×1024 image, and is priced the same to encode one for image-to-image, inpainting or outpainting (twice that with `precision: float32`). When an image is too large for the card to handle in one piece, Invoke decodes and encodes it in tiles; see [Large decodes](/configuration/optimization/low-vram-mode/). Invoke picks 1024-pixel tiles, and 768 or 512 only on cards with less memory: measured at 2048×2048, 1024-pixel tiles came close to an untiled decode (47.7 dB PSNR), while the 256-pixel tiles other VAEs default to left visible seams (35.6 dB).+### Reference images++With a Qwen-Image-2.1 **Diffusers** model, add up to four images with **Add reference image** in the Guidance section (see [Adding reference images](/users-guide/image-generation/generate-panel/#adding-reference-images)) and say in the prompt what to do with them, for example *put the jacket from the first image on the person in the second* or *the same cat, as a watercolor*. The model reads each reference twice: its Qwen3-VL encoder looks at it alongside the prompt, and the transformer sees it as clean latents next to the image it generates.++- **Diffusers models only.** Only the encoder that comes with the Diffusers model keeps the Qwen3-VL vision tower, which reads the images. A single-file or GGUF transformer with a standalone encoder does not offer reference images, and on a Diffusers model the references refuse to run while a standalone **Qwen3-VL Encoder** is selected under **Components**; clear it to edit.+- **Size.** Each reference is read at about one megapixel at its own aspect ratio, whatever its size. The generated image takes the size set in the panel; **Use size** on a reference's thumbnail matches it.+- **Transparency.** The encoder sees a transparent reference over white, as the model was trained; its latents keep the transparency.+- **Memory.** The vision tower adds about 1.1 GB to the Diffusers model's encoder, on every generation with it. Each reference adds about 4,100 tokens to the start of the sequence, which the model keeps for the whole generation: about 2 GB per reference, twice that with **CFG** above 1. Where that would take more than half of the GPU, Invoke reprocesses the references at every step instead of keeping them, which uses less memory but takes noticeably longer. Reading the references also needs encoder memory that grows quickly with their number: about 7 GB beyond its weights for four, so on a 24 GB card part of the encoder then runs from system memory.+### Transparent imagesThe Qwen-Image-2.1 VAE decodes an alpha channel along with the colors, so a prompt such as *a sticker of a red fox on a transparent background* produces a cut-out image. An image is saved with transparency only when it has some: when more than 0.1% of its pixels are noticeably transparent. Opaque images are saved as ordinary RGB, so the faint edges tiled decoding leaves in the alpha channel never turn an opaque image transparent.diff --git a/docs/src/content/docs/Users Guide/04.Image Generation/generate-panel.mdx b/docs/src/content/docs/Users Guide/04.Image Generation/generate-panel.mdxindex 1ec9cbe574..b5f3de5a4c 100644--- a/docs/src/content/docs/Users Guide/04.Image Generation/generate-panel.mdx+++ b/docs/src/content/docs/Users Guide/04.Image Generation/generate-panel.mdx@@ -120,7 +120,7 @@ What a reference does, and what options it has, depends on the model family:| **SD 1.5, SDXL** | An IP-Adapter model carries the reference's look into the image. | **Mode**: **Style**, **Composition** or **Both**. **Weight**. Under **Advanced**: the IP-Adapter model, a **Style variant** and the **Active steps** range. || **FLUX.1** | An IP-Adapter, or a **FLUX Redux** model, which transfers the reference's content. | IP-Adapter options as above, or **Image influence** (Lowest to Highest) for Redux. || **FLUX.1 Kontext** | Kontext edits or recombines the reference images according to your prompt. | None beyond the model. |-| **FLUX.2, Qwen Image Edit** | The model reads the references directly and uses them as your prompt instructs, for example *put the jacket from the first image on the person in the second*. | None. |+| **FLUX.2, Qwen Image Edit, Qwen-Image-2.1** | The model reads the references directly and uses them as your prompt instructs, for example *put the jacket from the first image on the person in the second*. | None. || **Krea-2** | One reference's look — palette, texture, rendering — is transferred into the image while the prompt keeps driving the content; see [Krea-2](/users-guide/models/local-models/krea-2/#style-reference). | **Style strength** (0–2; 0 turns the reference off). |Reference images require an extra model for SD 1.5, SDXL and FLUX.1 (an IP-Adapter or FLUX Redux model). These are available as starter models in the Model Manager, and an installed IP-Adapter is selected automatically.diff --git a/docs/src/content/docs/contributing/new-model-integration.mdx b/docs/src/content/docs/contributing/new-model-integration.mdxindex 04e4530b2d..aea4011261 100644--- a/docs/src/content/docs/contributing/new-model-integration.mdx+++ b/docs/src/content/docs/contributing/new-model-integration.mdx@@ -884,8 +884,8 @@ Paths below are relative to `invokeai/frontend/webv2/src/features/generation/cor### Reference images-- Backend: set `FeaturesFacet.max_reference_images` (and `reference_images_require_variant` if only one variant supports them). Wire the images into the text encoder and/or the denoise node, as the reference pipeline does.-- webv2: extend the reference-image config union (`types.ts`), `getDefaultReferenceImageConfig` and `getReferenceImageConfigSupported` (`baseGenerationPolicies.ts`), and wire the images in the builder.+- Backend: set `FeaturesFacet.max_reference_images`, plus `reference_images_require_variant` if only one variant supports them or `reference_images_require_format` if only one format does. Wire the images into the text encoder and/or the denoise node, as the reference pipeline does.+- webv2: extend the reference-image config union (`types.ts`); the config-type set and the normalize, clone and sync switches in `settings.ts`; the persisted union in `src/workbench/canvasMigration.ts`; and `getDefaultReferenceImageConfig` and `getReferenceImageConfigSupported` (`baseGenerationPolicies.ts`). Wire the images in the builder, and report any setting that keeps them from running in `getReferenceImageValidationReasons`, not only from the builder.### Control and regional guidancediff --git a/docs/src/content/docs/development/Guides/recall-api.mdx b/docs/src/content/docs/development/Guides/recall-api.mdxindex 8478a3fe4a..771f1d917d 100644--- a/docs/src/content/docs/development/Guides/recall-api.mdx+++ b/docs/src/content/docs/development/Guides/recall-api.mdx@@ -250,12 +250,13 @@ separate adapter model:- **FLUX.2 Klein** — built-in reference image support.- **FLUX Kontext** — reference image associated with the main model.- **Qwen Image Edit** — reference image associated with the main model.+- **Qwen-Image-2.1** — reference images read by its own encoder, on the Diffusers model only.Because there is no adapter model to resolve, these entries carry only`image_name`. When the frontend receives them, it picks the appropriateconfig flavor (`flux2_reference_image`, `flux_kontext_reference_image`,-or `qwen_image_reference_image`) based on the currently-selected main-model, matching the behavior of a manual drag-and-drop.+`qwen_image_reference_image` or `qwen_image_2_1_reference_image`) based on+the currently-selected main model, matching the behavior of a manual drag-and-drop.## Usage Examplesdiff --git a/invokeai/app/invocations/qwen_image_2_1/qwen_image_2_1_denoise.py b/invokeai/app/invocations/qwen_image_2_1/qwen_image_2_1_denoise.pyindex 9c428c8a72..7f69766c47 100644--- a/invokeai/app/invocations/qwen_image_2_1/qwen_image_2_1_denoise.py+++ b/invokeai/app/invocations/qwen_image_2_1/qwen_image_2_1_denoise.py@@ -1,3 +1,4 @@+import itertoolsimport mathfrom typing import Optional@@ -63,6 +64,61 @@ def clip_sigmas(return list(sigmas[first_step : last_step + 1]), first_step+def slot_runs(image_pad_mask: torch.Tensor | None) -> list[int]:+ """The lengths of the runs of image slots in a prompt's mask: one run per reference image, in order."""+ if image_pad_mask is None:+ return []+ return [len(list(run)) for is_slot, run in itertools.groupby(image_pad_mask[0].tolist()) if is_slot]+++def _describe_grids(grids: list[tuple[int, int]]) -> str:+ # A slot is 32x32 pixels of the reference as the encoder read it.+ return ", ".join(f"{columns * 32}x{rows * 32}" for rows, columns in grids) or "none"+++def check_reference_slots(+ info: QwenImage21ConditioningInfo, reference_shapes: list[tuple[int, int, int]], prompt: str+) -> None:+ """Refuse references whose latents do not match the prompt's image slots, reference by reference.++ Each reference's latents fill one run of the prompt's slots, 2x2 latents per slot, at the slot grid the+ encoder read it at. A mismatch means the prompt was encoded with other images, in another order or at another+ size than the latents: the transformer would place one reference's latents in another's slots.+ """+ expected = [(h // 2, w // 2) for _, h, w in reference_shapes]+ found = list(info.reference_grids)+ if found != expected or slot_runs(info.image_pad_mask) != [rows * columns for rows, columns in found]:+ raise ValueError(+ f"The {prompt} prompt was encoded with {len(found)} reference image(s) ({_describe_grids(found)}), but "+ f"the denoise node received {len(expected)} reference latent(s) ({_describe_grids(expected)}). Encode "+ "each reference with Image to Latents in reference mode, from the same images and in the same order "+ "as the prompts."+ )+++def prefix_length(info: QwenImage21ConditioningInfo) -> int:+ """Tokens before the target in the joint sequence: the prompt, each reference slot widened to 2x2 latents."""+ slots = 0 if info.image_pad_mask is None else int(info.image_pad_mask.sum())+ return info.prompt_embeds.shape[1] + (TOKENS_PER_SLOT - 1) * slots+++# K and V of one prefix token in all 32 blocks: 2 x 4096 x 32 x 2 bytes in bf16.+KV_BYTES_PER_PREFIX_TOKEN = 2 * 4096 * 32 * 2+++def prefix_cache_fits(cache_bytes: int, device: torch.device) -> bool:+ """Whether the prefix caches can stay on the GPU for the whole run.++ They cannot be offloaded the way the transformer's weights can, so caches larger than half the card would+ leave too little for the model however it streams: four references at CFG above 1 hold ~16 GiB. Past that the+ prefix is recomputed every step instead -- slower, but within the memory of an ordinary run. Memory that is+ not a dedicated card's (CPU, MPS) is not limited here.+ """+ if device.type != "cuda":+ return True+ return cache_bytes <= torch.cuda.get_device_properties(device).total_memory // 2++def pack_latents(latents: torch.Tensor) -> torch.Tensor:"""(B, C, h, w) -> (B, h*w, C): one token per latent pixel, in raster order (patch size 1)."""batch, channels, height, width = latents.shape@@ -80,7 +136,7 @@ def unpack_latents(latents: torch.Tensor, height: int, width: int) -> torch.Tenstitle="Denoise - Qwen-Image-2.1",tags=["image", "qwen_image_2_1", "qwen-image-2.1"],category="image",- version="1.0.1",+ version="1.1.0",classification=Classification.Prototype,)class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):@@ -92,6 +148,13 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):denoise_mask: Optional[DenoiseMaskField] = InputField(default=None, description=FieldDescriptions.denoise_mask, input=Input.Connection)+ reference_latents: LatentsField | list[LatentsField] | None = InputField(+ default=None,+ description="Latents of the reference images an edit reads, from Image to Latents in reference mode, in the "+ "order the prompts were encoded with them.",+ input=Input.Connection,+ title="Reference Latents",+ )denoising_start: float = InputField(default=0.0, ge=0, le=1, description=FieldDescriptions.denoising_start)denoising_end: float = InputField(default=1.0, ge=0, le=1, description=FieldDescriptions.denoising_end)transformer: TransformerField = InputField(@@ -135,11 +198,30 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):def _load_conditioning(self, context: InvocationContext, field: QwenImage21ConditioningField, dtype: torch.dtype, device: torch.device- ) -> torch.Tensor:+ ) -> QwenImage21ConditioningInfo:data = context.conditioning.load(field.conditioning_name)if len(data.conditionings) != 1 or not isinstance(data.conditionings[0], QwenImage21ConditioningInfo):raise ValueError("Expected exactly one Qwen-Image-2.1 conditioning.")- return data.conditionings[0].to(dtype=dtype, device=device).prompt_embeds+ return data.conditionings[0].to(dtype=dtype, device=device)++ def _load_references(self, context: InvocationContext, device: torch.device) -> list[torch.Tensor]:+ """The reference latents, `(1, C, h, w)` each, in the order the prompts were encoded with them."""+ if self.reference_latents is None:+ return []+ fields = self.reference_latents if isinstance(self.reference_latents, list) else [self.reference_latents]+ references = []+ for field in fields:+ reference = context.tensors.load(field.latents_name).to(device=device, dtype=torch.float32)+ if reference.dim() == 5:+ reference = reference.squeeze(2)+ if reference.shape[1] != LATENT_CHANNELS or reference.shape[-2] % 2 or reference.shape[-1] % 2:+ raise ValueError(+ f"Reference latents are {tuple(reference.shape[1:])}; Qwen-Image-2.1 reads {LATENT_CHANNELS} "+ "channels with even sides. Encode the reference with Image to Latents - Qwen-Image-2.1 in "+ "reference mode."+ )+ references.append(reference)+ return referencesdef _prepare_cfg_scale(self, num_steps: int) -> list[float]:if isinstance(self.cfg_scale, list):@@ -174,7 +256,6 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):device = TorchDevice.choose_torch_device()dtype = TorchDevice.choose_bfloat16_safe_dtype(device)transformer_config = context.models.get_config(self.transformer.transformer)- transformer_info = context.models.load(self.transformer.transformer)latent_height, latent_width = self.height // VAE_SCALE_FACTOR, self.width // VAE_SCALE_FACTORimage_seq_len = latent_height * latent_width@@ -190,13 +271,26 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):sigmas = torch.tensor(sigmas, dtype=torch.float32, device=device)cfg_scale = self._prepare_cfg_scale(len(full_sigmas) - 1)[first_step : first_step + len(sigmas) - 1]- pos_embeds = self._load_conditioning(context, self.positive_conditioning, dtype, device)+ pos = self._load_conditioning(context, self.positive_conditioning, dtype, device)do_cfg = self.negative_conditioning is not None and any(value > 1.0 for value in cfg_scale)- neg_embeds = (+ neg = (self._load_conditioning(context, self.negative_conditioning, dtype, device)if do_cfg and self.negative_conditioning is not Noneelse None)+ pos_embeds = pos.prompt_embeds+ neg_embeds = neg.prompt_embeds if neg is not None else None++ references = self._load_references(context, device)+ reference_shapes = [(1, r.shape[-2], r.shape[-1]) for r in references]+ check_reference_slots(pos, reference_shapes, "positive")+ if neg is not None:+ check_reference_slots(neg, reference_shapes, "negative")+ # Clean latents, condition images first: the transformer drops them into the prompt's image slots.+ reference_tokens = torch.cat([pack_latents(r) for r in references], dim=1).to(dtype) if references else None++ # Only now, with every input checked: loading reads the whole transformer into RAM.+ transformer_info = context.models.load(self.transformer.transformer)# Drawn as the pipeline draws it, (B, frame, C, h, w), on the CPU so a seed means the same on any device.noise = torch.randn(@@ -234,29 +328,41 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):# upcast); matching that is what makes this node reproduce its outputs.latents = pack_latents(latents).to(dtype)- def image_slot_mask(text_len: int) -> torch.Tensor:- # The joint sequence's slots: the text, then one slot per 2x2 group of target latents. Condition- # images would sit between the two.- text = torch.zeros(1, text_len, dtype=torch.bool, device=device)+ def image_slot_mask(info: QwenImage21ConditioningInfo) -> torch.Tensor:+ # The joint sequence's slots: the prompt with its reference slots, then one slot per 2x2 group of+ # target latents.+ text_len = info.prompt_embeds.shape[1]+ prompt = (+ info.image_pad_mask.bool()+ if info.image_pad_mask is not None+ else torch.zeros(1, text_len, dtype=torch.bool, device=device)+ )target = torch.ones(1, image_seq_len // TOKENS_PER_SLOT, dtype=torch.bool, device=device)- return torch.cat([text, target], dim=1)-- img_shapes = [[(1, latent_height, latent_width)]]- pos_img_mask = image_slot_mask(pos_embeds.shape[1])- neg_img_mask = image_slot_mask(neg_embeds.shape[1]) if neg_embeds is not None else None-+ return torch.cat([prompt, target], dim=1)++ img_shapes = [[*reference_shapes, (1, latent_height, latent_width)]]+ pos_img_mask = image_slot_mask(pos)+ neg_img_mask = image_slot_mask(neg) if neg is not None else None++ # Each reference slot widens to 2x2 latent tokens in the joint sequence; with the text they are the+ # prefix the cache holds.+ pos_prefix = prefix_length(pos)+ neg_prefix = prefix_length(neg) if neg is not None else None+ # Text and reference keys and values do not depend on the step under `causal_condition`: the first step+ # prefills them, the rest compute the image tokens only -- when the caches fit.+ cache_bytes = KV_BYTES_PER_PREFIX_TOKEN * (pos_prefix + (neg_prefix or 0))+ use_cache = bool(getattr(transformer_info.model.config, "causal_condition", False))+ if use_cache and not prefix_cache_fits(cache_bytes, device):+ use_cache = False+ logger.info(+ f"Qwen-Image-2.1: the prefix caches would hold {cache_bytes / 1024**3:.1f} GiB, more than half of "+ "this GPU; recomputing the prefix every step instead, which is slower."+ )working_memory = self._estimate_working_memory(- image_seq_len,- pos_embeds.shape[1],- None if neg_embeds is None else neg_embeds.shape[1],- do_cfg,- dtype.itemsize,+ image_seq_len, pos_prefix, neg_prefix, do_cfg, use_cache, dtype.itemsize)working_memory += self._attention_score_bytes(- transformer_info.model,- image_seq_len + max(pos_embeds.shape[1], 0 if neg_embeds is None else neg_embeds.shape[1]),- device,- dtype,+ transformer_info.model, image_seq_len, max(pos_prefix, neg_prefix or 0), device, dtype)working_memory += peak_dequant_transient_bytes(transformer_info.model, dtype)log_attention_backends(logger, device)@@ -274,16 +380,15 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):)with transformer_info.model_on_device(working_mem_bytes=working_memory) as (_, transformer):- # Text keys and values do not depend on the step under `causal_condition`: the first step prefills- # them, the rest compute the image tokens only. One cache per guidance branch, for this run only.+ # One cache per guidance branch, for this run only.num_blocks = len(transformer.transformer_blocks)- use_cache = bool(transformer.config.causal_condition)pos_cache = QwenImage21KVCache(num_blocks) if use_cache else Noneneg_cache = QwenImage21KVCache(num_blocks) if use_cache and neg_embeds is not None else Nonedef predict(embeds: torch.Tensor, img_mask: torch.Tensor, cache, mode, timestep) -> torch.Tensor:+ hidden_states = latents if reference_tokens is None else torch.cat([reference_tokens, latents], dim=1)out = transformer(- hidden_states=latents,+ hidden_states=hidden_states,encoder_hidden_states=embeds,encoder_hidden_states_mask=None,timestep=timestep,@@ -335,26 +440,36 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):@staticmethoddef _estimate_working_memory(- image_seq_len: int, pos_text_len: int, neg_text_len: int | None, do_cfg: bool, element_size: int+ image_seq_len: int,+ pos_prefix: int,+ neg_prefix: int | None,+ do_cfg: bool,+ use_cache: bool,+ element_size: int,) -> int:"""Peak transformer activations, in bytes, for the model cache to keep free.Measured on an RTX 4090 in bf16 with the transformer fully resident: 0.55 GiB of activations at1024x1024 (4096 image tokens) and 2.2 GiB at 2048x2048 (16384), about 0.14 MiB per token. 0.25 MiB pertoken plus a 1 GiB base leaves room for the allocator and the prefix cache, which holds K and V of every- text token in all 32 blocks (~0.5 MiB per text token, per guidance branch). Both per-token terms are bf16- figures and scale with `element_size`, the compute dtype's width: a float32 fallback needs twice as much.+ prefix token -- text and reference latents -- in all 32 blocks (~0.5 MiB per token, per guidance branch).+ Both per-token terms are bf16 figures and scale with `element_size`, the compute dtype's width: a+ float32 fallback needs twice as much."""mib, gib = 1024**2, 1024**3width = element_size / 2- text_len = max(pos_text_len, neg_text_len or 0)- estimated = int((image_seq_len + text_len) * 0.25 * mib * width) + gib- branches = 2 if do_cfg else 1- estimated += int(branches * (pos_text_len if neg_text_len is None else text_len) * 0.5 * mib * width)+ prefix = max(pos_prefix, neg_prefix or 0)+ estimated = int((image_seq_len + prefix) * 0.25 * mib * width) + gib+ if use_cache:+ branches = 2 if do_cfg else 1+ prefix_tokens = branches * (pos_prefix if neg_prefix is None else prefix)+ estimated += int(prefix_tokens * KV_BYTES_PER_PREFIX_TOKEN * width)return estimated@staticmethod- def _attention_score_bytes(transformer: object, seq_len: int, device: torch.device, dtype: torch.dtype) -> int:+ def _attention_score_bytes(+ transformer: object, image_seq_len: int, prefix: int, device: torch.device, dtype: torch.dtype+ ) -> int:"""The score matrix attention materializes on a build without a fused kernel for it (0 where it has one)."""config = getattr(transformer, "config", None)num_heads = getattr(config, "num_attention_heads", None)@@ -363,13 +478,14 @@ class QwenImage21DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):return 0# The largest call, the image queries over every key, goes unmasked both in the prefill and on decode steps:# only the text segments carry a mask, and they are as short as the prompt. All of them go through diffusers'- # attention dispatch.+ # attention dispatch. Its score matrix is image x (prefix + image), which the square helper prices at the+ # side with the same area.return sdpa_score_matrix_bytes(device=device,dtype=dtype,num_heads=num_heads,head_dim=head_dim,- seq_len=seq_len,+ seq_len=math.isqrt(image_seq_len * (image_seq_len + prefix)),via_diffusers_dispatch=True,)diff --git a/invokeai/app/invocations/text_encoder/qwen_image_2_1_text_encoder.py b/invokeai/app/invocations/text_encoder/qwen_image_2_1_text_encoder.pyindex 0bea270aa4..7b59a30b17 100644--- a/invokeai/app/invocations/text_encoder/qwen_image_2_1_text_encoder.py+++ b/invokeai/app/invocations/text_encoder/qwen_image_2_1_text_encoder.py@@ -1,56 +1,115 @@+from contextlib import nullcontext+import torchfrom invokeai.app.invocations.baseinvocation import BaseInvocation, Classification, invocation-from invokeai.app.invocations.fields import FieldDescriptions, Input, InputField, UIComponent-from invokeai.app.invocations.model import Qwen3VLEncoderField+from invokeai.app.invocations.fields import FieldDescriptions, ImageField, Input, InputField, UIComponent+from invokeai.app.invocations.model import ModelIdentifierField, Qwen3VLEncoderFieldfrom invokeai.app.invocations.primitives import QwenImage21ConditioningOutputfrom invokeai.app.services.shared.invocation_context import InvocationContextfrom invokeai.backend.model_manager.load.model_cache.utils import get_effective_device+from invokeai.backend.model_manager.taxonomy import BaseModelType, ModelFormat, ModelType, SubModelTypefrom invokeai.backend.quantization.dequantizing_linear import peak_dequant_transient_bytes-from invokeai.backend.qwen_image_2_1.text_encoding import encode_prompt+from invokeai.backend.qwen_image_2_1.text_encoding import (+ encode_prompt,+ has_vision_tower,+ sequence_length,+ working_memory_bytes,+)+from invokeai.backend.qwen_image_2_1.vae import to_referencefrom invokeai.backend.stable_diffusion.diffusion.conditioning_data import (ConditioningFieldData,QwenImage21ConditioningInfo,)from invokeai.backend.util.devices import TorchDevice+_NO_VISION_TOWER = (+ "Reference images are read by the Qwen3-VL vision tower, which only the Qwen-Image-2.1 Diffusers model's own "+ "encoder brings. Clear the standalone encoder, or use a Qwen-Image-2.1 Diffusers model."+)+@invocation("qwen_image_2_1_text_encoder",title="Prompt - Qwen-Image-2.1",tags=["prompt", "conditioning", "qwen_image_2_1", "qwen-image-2.1"],category="conditioning",- version="1.0.0",+ version="1.1.0",classification=Classification.Prototype,idle_gpu_offloadable=True,)class QwenImage21TextEncoderInvocation(BaseInvocation):- """Encodes a text prompt for Qwen-Image-2.1 with its Qwen3-VL-8B encoder."""+ """Encodes a prompt for Qwen-Image-2.1 with its Qwen3-VL-8B encoder, with the reference images an edit reads.++ Each reference also goes to the denoise node, encoded by Image to Latents in its reference mode, in the same+ order.+ """prompt: str = InputField(description="Text prompt describing the desired image.", ui_component=UIComponent.Textarea)qwen3_vl_encoder: Qwen3VLEncoderField = InputField(title="Qwen3-VL Encoder", description=FieldDescriptions.qwen3_vl_encoder, input=Input.Connection)+ reference_images: list[ImageField] = InputField(+ default=[],+ description="Reference images to edit or draw from, in the order the denoise node receives their latents. "+ "Needs a Qwen-Image-2.1 Diffusers model's own encoder.",+ )@torch.no_grad()def invoke(self, context: InvocationContext) -> QwenImage21ConditioningOutput:- tokenizer_info = context.models.load(self.qwen3_vl_encoder.tokenizer)+ images = [to_reference(context.images.get_pil(field.image_name)) for field in self.reference_images]+ processor_id = self._processor_identifier(context) if images else None++ # The encoder first: loading it evicts unlocked small entries from the RAM cache, which would otherwise drop+ # the tokenizer and processor loaded before it.text_encoder_info = context.models.load(self.qwen3_vl_encoder.text_encoder)+ tokenizer_info = context.models.load(self.qwen3_vl_encoder.tokenizer)+ processor_info = context.models.load(processor_id) if processor_id is not None else None# A GGUF or int8 encoder dequantizes one layer at a time; that transient is the working memory.dequant_bytes = peak_dequant_transient_bytes(text_encoder_info.model, TorchDevice.choose_bfloat16_safe_dtype(text_encoder_info.compute_device))+ # Before the encoder moves to the GPU: a graph can pair this pipeline's tokenizer with another encoder.+ if images and not has_vision_tower(text_encoder_info.model):+ raise ValueError(_NO_VISION_TOWER)context.util.signal_progress("Running Qwen3-VL text encoder")- with (- tokenizer_info as tokenizer,- text_encoder_info.model_on_device(working_mem_bytes=dequant_bytes) as (- _,- text_encoder,- ),- ):- embeds = encode_prompt(text_encoder, tokenizer, self.prompt, get_effective_device(text_encoder))+ with tokenizer_info as tokenizer:+ # Reference images make the sequence long, and its attention grows with the square of it: room the+ # model cache has to make, or the driver spills the encode to system memory and it crawls.+ working_memory = dequant_bytes + working_memory_bytes(sequence_length(tokenizer, self.prompt, images))+ with (+ processor_info.model_on_device() if processor_info is not None else nullcontext((None, None)) as (+ _,+ processor,+ ),+ text_encoder_info.model_on_device(working_mem_bytes=working_memory) as (_, text_encoder),+ ):+ encoding = encode_prompt(+ text_encoder,+ tokenizer,+ self.prompt,+ get_effective_device(text_encoder),+ processor=processor,+ images=images,+ )- conditioning = QwenImage21ConditioningInfo(prompt_embeds=embeds.detach().cpu())+ conditioning = QwenImage21ConditioningInfo(+ prompt_embeds=encoding.embeds.detach().cpu(),+ image_pad_mask=None if encoding.image_pad_mask is None else encoding.image_pad_mask.cpu(),+ reference_grids=encoding.reference_grids,+ )name = context.conditioning.save(ConditioningFieldData(conditionings=[conditioning]))return QwenImage21ConditioningOutput.build(name)++ def _processor_identifier(self, context: InvocationContext) -> ModelIdentifierField:+ """The image processor of the model the tokenizer came from, which has to be a Qwen-Image-2.1 pipeline."""+ tokenizer = self.qwen3_vl_encoder.tokenizer+ config = context.models.get_config(tokenizer)+ if (+ config.base is not BaseModelType.QwenImage21+ or config.type is not ModelType.Main+ or config.format is not ModelFormat.Diffusers+ ):+ raise ValueError(_NO_VISION_TOWER)+ return tokenizer.model_copy(update={"submodel_type": SubModelType.Processor})diff --git a/invokeai/app/invocations/vae/qwen_image_2_1_image_to_latents.py b/invokeai/app/invocations/vae/qwen_image_2_1_image_to_latents.pyindex 991e744184..e8aa7d57e9 100644--- a/invokeai/app/invocations/vae/qwen_image_2_1_image_to_latents.py+++ b/invokeai/app/invocations/vae/qwen_image_2_1_image_to_latents.py@@ -14,7 +14,13 @@ from invokeai.app.invocations.fields import (from invokeai.app.invocations.model import VAEFieldfrom invokeai.app.invocations.primitives import LatentsOutputfrom invokeai.app.services.shared.invocation_context import InvocationContext-from invokeai.backend.qwen_image_2_1.vae import SPATIAL_SCALE, choose_tile_size, to_vae_input, working_memory_bytes+from invokeai.backend.qwen_image_2_1.vae import (+ SPATIAL_SCALE,+ choose_tile_size,+ to_reference,+ to_vae_input,+ working_memory_bytes,+)from invokeai.backend.util.devices import TorchDevicefrom invokeai.backend.util.qwen_image_vae import patch_qwen_image_vae_tiling@@ -27,26 +33,41 @@ _GRID = 32title="Image to Latents - Qwen-Image-2.1",tags=["image", "latents", "vae", "i2l", "qwen_image_2_1"],category="image",- version="1.0.0",+ version="1.1.0",classification=Classification.Prototype,)class QwenImage21ImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard):- """Encodes an image into Qwen-Image-2.1 latents. The image is encoded opaque; its alpha is not read."""+ """Encodes an image into Qwen-Image-2.1 latents.++ For image-to-image and the canvas, the image is encoded opaque at its own size; its alpha is not read. As a+ reference for an edit, it is resized to the area the text encoder reads it at and keeps its alpha.+ """image: ImageField = InputField(description="The image to encode.")vae: VAEField = InputField(description=FieldDescriptions.vae, input=Input.Connection)tiled: bool = InputField(default=False, description=FieldDescriptions.tiled)tile_size: int = InputField(default=0, ge=0, multiple_of=16, description=FieldDescriptions.vae_tile_size)+ reference: bool = InputField(+ default=False,+ description="Encode as a reference image for the denoise node's reference latents: resized to ~1 megapixel, "+ "as the text encoder reads it, with its alpha kept.",+ )@torch.no_grad()def invoke(self, context: InvocationContext) -> LatentsOutput:image = context.images.get_pil(self.image.image_name)- # Crop-free resize onto the 32px grid the transformer needs, as the canvas sizes its own images.- width, height = image.width // _GRID * _GRID, image.height // _GRID * _GRID- if width == 0 or height == 0:- raise ValueError(f"Qwen-Image-2.1 encodes images of at least {_GRID}x{_GRID}; this one is {image.size}.")- if (width, height) != image.size:- image = image.resize((width, height), resample=PILImage.LANCZOS)+ if self.reference:+ image = to_reference(image)+ width, height = image.size+ else:+ # Crop-free resize onto the 32px grid the transformer needs, as the canvas sizes its own images.+ width, height = image.width // _GRID * _GRID, image.height // _GRID * _GRID+ if width == 0 or height == 0:+ raise ValueError(+ f"Qwen-Image-2.1 encodes images of at least {_GRID}x{_GRID}; this one is {image.size}."+ )+ if (width, height) != image.size:+ image = image.resize((width, height), resample=PILImage.LANCZOS)vae_info = context.models.load(self.vae.vae)device = vae_info.compute_device@@ -66,12 +87,16 @@ class QwenImage21ImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoarworking_memory = working_memory_bytes(height, width, tile_size, element_size)with vae_info.model_on_device(working_mem_bytes=working_memory) as (_, vae):- pixels = to_vae_input(image).to(device=device, dtype=vae.dtype).unsqueeze(2)+ pixels = to_vae_input(image, keep_alpha=self.reference).to(device=device, dtype=vae.dtype).unsqueeze(2)with torch.inference_mode(), patch_qwen_image_vae_tiling(vae, tile_size, SPATIAL_SCALE):- z = vae.encode(pixels).latent_dist.mode().float()[:, :, 0]- mean = torch.tensor(vae.config.latents_mean, device=z.device).view(1, -1, 1, 1)- std = torch.tensor(vae.config.latents_std, device=z.device).view(1, -1, 1, 1)- latents = ((z - mean) / std).cpu()+ z = vae.encode(pixels).latent_dist.mode()[:, :, 0]+ # A reference is normalized in the VAE's dtype, as the pipeline normalizes it, so the transformer reads+ # the same values; the canvas's latents only seed a sampler, and keep fp32.+ z = z if self.reference else z.float()+ # float32 first, then the latents' dtype: straight from Python floats to bf16 can round differently.+ mean = torch.tensor(vae.config.latents_mean).to(z.device, z.dtype).view(1, -1, 1, 1)+ std = torch.tensor(vae.config.latents_std).to(z.device, z.dtype).view(1, -1, 1, 1)+ latents = ((z - mean) / std).float().cpu()TorchDevice.empty_cache()name = context.tensors.save(tensor=latents)diff --git a/invokeai/backend/architectures/capabilities.py b/invokeai/backend/architectures/capabilities.pyindex cbfccd5619..e72665da0d 100644--- a/invokeai/backend/architectures/capabilities.py+++ b/invokeai/backend/architectures/capabilities.py@@ -72,6 +72,10 @@ class ArchitectureFeatures(BaseModel):default=None,description="If set, reference images are only accepted for models of this variant.",)+ reference_images_require_format: str | None = Field(+ default=None,+ description="If set, reference images are only accepted for models of this format.",+ )supports_regional_guidance: bool = Falseregional_negative: bool = Field(default=False, description="Whether a region's negative prompt is masked, rather than applied globally."@@ -137,6 +141,7 @@ def _features_of(facet: FeaturesFacet, variant: AnyVariant | None = None) -> Arccontrol_kinds=sorted(facet.control_kinds),max_reference_images=facet.max_reference_images,reference_images_require_variant=facet.reference_images_require_variant,+ reference_images_require_format=facet.reference_images_require_format,supports_regional_guidance=facet.supports_regional_guidance,regional_negative=facet.regional_negative,clip_skip_max=facet.clip_skip_max,@@ -171,7 +176,8 @@ def architecture_capabilities() -> list[ArchitectureCapabilities]:is rendered in full, so a client never has to know which fields a variant row is allowed to omit.Differences too small to have earned a mapping are expressed on the base row instead, by- `features.reference_images_require_variant`; Qwen-Image is the only one.+ `features.reference_images_require_variant` (Qwen-Image) and+ `features.reference_images_require_format` (Qwen-Image-2.1).Sorted by base value, then variant, so the response is stable and diffable."""diff --git a/invokeai/backend/architectures/defs/qwen_image_2_1.py b/invokeai/backend/architectures/defs/qwen_image_2_1.pyindex 0a52383509..8dea696ac0 100644--- a/invokeai/backend/architectures/defs/qwen_image_2_1.py+++ b/invokeai/backend/architectures/defs/qwen_image_2_1.py@@ -34,6 +34,9 @@ register(dimension_grid=32,guidance_label="CFG",scheduler_set="flow",+ # Each reference holds ~1 megapixel of latents in the prefix the cache keeps: ~2 GiB per guidance branch.+ max_reference_images=4,+ reference_images_require_format="diffusers",),# Values are qwen_image_21_* because variant strings resolve without the base and must be unique.VariantFacet({ModelType.Main: QwenImage21VariantType}),diff --git a/invokeai/backend/architectures/facets/features.py b/invokeai/backend/architectures/facets/features.pyindex 0cae3669ef..95887564fa 100644--- a/invokeai/backend/architectures/facets/features.py+++ b/invokeai/backend/architectures/facets/features.py@@ -111,8 +111,11 @@ class FeaturesFacet(Facet):control_kinds: frozenset[ControlKind] = frozenset()max_reference_images: int = 0reference_images_require_variant: str | None = None- """Qwen-Image accepts reference images only as the `edit` variant — the one feature in this+ """Qwen-Image accepts reference images only as the `edit` variant — one of the two features in thistable that a base alone cannot answer."""+ reference_images_require_format: str | None = None+ """The other: Qwen-Image-2.1 reads reference images only as a Diffusers pipeline, whose encoder is+ the only one that keeps the vision tower."""supports_regional_guidance: bool = Falseregional_negative: bool = Falsediff --git a/invokeai/backend/model_manager/load/model_loaders/qwen_image_2_1.py b/invokeai/backend/model_manager/load/model_loaders/qwen_image_2_1.pyindex f94637da23..08ff7d2999 100644--- a/invokeai/backend/model_manager/load/model_loaders/qwen_image_2_1.py+++ b/invokeai/backend/model_manager/load/model_loaders/qwen_image_2_1.py@@ -18,7 +18,6 @@ from invokeai.backend.model_manager.load.load_default import ModelLoader, _modelfrom invokeai.backend.model_manager.load.model_loader_registry import ModelLoaderRegistryfrom invokeai.backend.model_manager.load.model_loaders.generic_diffusers import GenericDiffusersLoaderfrom invokeai.backend.model_manager.taxonomy import AnyModel, BaseModelType, ModelFormat, ModelType, SubModelType-from invokeai.backend.model_manager.util.qwen3_vl import drop_qwen3vl_visual_towerfrom invokeai.backend.quantization.fp8_scaled import (attach_fp8_scales,cast_state_dict,@@ -62,10 +61,13 @@ def _build_transformer(sd: dict[str, Any]) -> torch.nn.Module:class QwenImage21DiffusersModel(GenericDiffusersLoader):"""Loads the submodels of a Qwen-Image-2.1 diffusers pipeline.- The tokenizer lives in `processor/` (the pipeline declares a `Qwen3VLProcessor`, of which text prompts- need only the tokenizer). The text encoder loads as `Qwen3VLModel` rather than the declared- `Qwen3VLForConditionalGeneration`: the LM head is ~1.2 GiB the encoder never runs, and `Qwen3VLModel` is- what the standalone Qwen3-VL loaders return, so the text encoder node sees one class.+ The tokenizer lives in `processor/` (the pipeline declares a `Qwen3VLProcessor`); the processor itself, which+ also cuts reference images into patches, loads only for an edit. The text encoder loads as `Qwen3VLModel`+ rather than the declared `Qwen3VLForConditionalGeneration`: the LM head is ~1.2 GiB the encoder never runs,+ and `Qwen3VLModel` is what the standalone Qwen3-VL loaders return, so the text encoder node sees one class.++ Unlike those loaders, this one keeps the vision tower (~1.1 GiB): reference images reach the encoder only+ through it, and only this pipeline's encoder carries one into Qwen-Image-2.1."""def _load_model(self, config: AnyModelConfig, submodel_type: Optional[SubModelType] = None) -> AnyModel:@@ -79,6 +81,10 @@ class QwenImage21DiffusersModel(GenericDiffusersLoader):model_path = Path(config.path)if submodel_type is SubModelType.Tokenizer:return AutoTokenizer.from_pretrained(model_path / "processor", local_files_only=True)+ if submodel_type is SubModelType.Processor:+ from transformers import AutoProcessor++ return AutoProcessor.from_pretrained(model_path / "processor", local_files_only=True)if submodel_type is SubModelType.VAE:# The configured precision, except float16, which is unstable on this autoencoder: bfloat16 where the@@ -108,10 +114,6 @@ class QwenImage21DiffusersModel(GenericDiffusersLoader):else:raise- if submodel_type is SubModelType.TextEncoder:- # Text prompts never reach the vision tower; reference images (which would) are not wired up.- drop_qwen3vl_visual_tower(result)-return self._apply_fp8_layerwise_casting(result, config, submodel_type)diff --git a/invokeai/backend/qwen_image_2_1/text_encoding.py b/invokeai/backend/qwen_image_2_1/text_encoding.pyindex 45f9508427..a9f145ef78 100644--- a/invokeai/backend/qwen_image_2_1/text_encoding.py+++ b/invokeai/backend/qwen_image_2_1/text_encoding.py@@ -1,17 +1,48 @@-"""Qwen-Image-2.1's prompt encoding, as `QwenImage21Pipeline._get_qwen_prompt_embeds` does it for text prompts."""+"""Qwen-Image-2.1's prompt encoding, as `QwenImage21Pipeline._get_qwen_prompt_embeds` does it."""++from collections.abc import Sequence+from typing import NamedTupleimport torch+from PIL import ImageSYSTEM_PROMPT = "Comprehend and analyze the provided prompt."SYSTEM_BLOCK = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"# A raw string handed to the tokenizer, not `apply_chat_template`: the two tokenize differently, and the# checkpoint was trained on this one.T2I_TEMPLATE = SYSTEM_BLOCK + "<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n"+IMAGE_PAD = "<|image_pad|>"+# Pixels per side of one vision token: 16px patches, merged 2x2.+_PIXELS_PER_TOKEN = 32++# What one encode needs above the weights for a sequence of T tokens, measured on an RTX 4090 (torch 2.13,+# transformers 5.10): ~286 B * T^2 + 224 KiB * T at peak. The quadratic term is the language model's attention,+# which SDPA runs on its math kernel for grouped-query heads; four 1-megapixel references (~4.1k tokens) need+# ~5.4 GiB. The allocator reserves about a fifth more than it hands out.+_BYTES_PER_TOKEN_PAIR = 286+_BYTES_PER_TOKEN = 224 * 1024+_RESERVED_OVER_ALLOCATED = 1.25-def format_prompt(prompt: str) -> str:+def format_prompt(prompt: str, num_images: int = 0) -> str:+ """The prompt as the checkpoint was trained on it, with one vision placeholder per reference image first."""+ placeholders = " ".join(f"<image{i}><|vision_start|>{IMAGE_PAD}<|vision_end|>" for i in range(1, num_images + 1))# Qwen has no BOS token, so an empty prompt would leave the encoder nothing to read.- return T2I_TEMPLATE.format(prompt or " ")+ return T2I_TEMPLATE.format(placeholders + (prompt or " "))+++def sequence_length(tokenizer, prompt: str, images: Sequence[Image.Image] = ()) -> int:+ """Tokens the encoder reads: the formatted prompt, each reference's placeholder one token per 32x32 pixels.++ `images` are the references at `vae.reference_size`, which the image processor leaves at their size.+ """+ text = len(tokenizer(format_prompt(prompt, len(images))).input_ids) - len(images)+ return text + sum((w // _PIXELS_PER_TOKEN) * (h // _PIXELS_PER_TOKEN) for w, h in (i.size for i in images))+++def working_memory_bytes(num_tokens: int) -> int:+ """Device memory an encode of `num_tokens` tokens needs above the encoder's weights."""+ return int((_BYTES_PER_TOKEN_PAIR * num_tokens**2 + _BYTES_PER_TOKEN * num_tokens) * _RESERVED_OVER_ALLOCATED)def system_prefix_length(tokenizer) -> int:@@ -29,20 +60,83 @@ def _base_model(text_encoder: torch.nn.Module) -> torch.nn.Module:return text_encoder if hasattr(text_encoder, "language_model") else text_encoder.model+def has_vision_tower(text_encoder: torch.nn.Module) -> bool:+ """Whether this encoder can read reference images: only the Qwen-Image-2.1 pipeline's keeps its vision tower.++ The standalone Qwen3-VL loaders replace the tower with an `Identity` rather than deleting it.+ """+ visual = getattr(_base_model(text_encoder), "visual", None)+ return isinstance(visual, torch.nn.Module) and not isinstance(visual, torch.nn.Identity)+++def on_white(image: Image.Image) -> Image.Image:+ """A reference as the vision encoder reads it: alpha composited over white, as the checkpoint was trained."""+ if image.mode != "RGBA":+ return image.convert("RGB")+ white = Image.new("RGB", image.size, (255, 255, 255))+ white.paste(image, mask=image.getchannel("A"))+ return white+++class PromptEncoding(NamedTuple):+ embeds: torch.Tensor+ """`(1, L, hidden)`, the system turn dropped."""+ image_pad_mask: torch.Tensor | None+ """`(1, L)`, True at each `<|image_pad|>` slot, where the transformer places 2x2 of a reference's latents."""+ reference_grids: tuple[tuple[int, int], ...]+ """Per reference, in order: its slots as (rows, columns), one slot per 32x32 pixels."""++@torch.no_grad()-def encode_prompt(text_encoder: torch.nn.Module, tokenizer, prompt: str, device: torch.device) -> torch.Tensor:- """Encode a prompt into `(1, L, hidden)` embeddings, as the pipeline encodes a single prompt.+def encode_prompt(+ text_encoder: torch.nn.Module,+ tokenizer,+ prompt: str,+ device: torch.device,+ *,+ processor=None,+ images: Sequence[Image.Image] = (),+) -> PromptEncoding:+ """Encode a prompt, and the reference images it edits.The transformer reads the last decoder layer's output BEFORE the final RMSNorm. transformers 5 tiesthe last hidden state to the normalized output, so a forward hook returning the norm's inputneutralizes the norm for this call -- the pipeline's approach. The system turn every prompt shares isdropped from the result.++ `images` are the references, already at `vae.reference_size`; reading them needs the vision tower and the+ `processor` that cuts them into patches."""- inputs = tokenizer([format_prompt(prompt)], return_tensors="pt").to(device)base = _base_model(text_encoder)+ if images:+ if processor is None or not has_vision_tower(text_encoder):+ raise ValueError("Reading reference images needs the Qwen3-VL vision tower and its image processor.")+ inputs = processor(+ text=[format_prompt(prompt, len(images))], images=[on_white(i) for i in images], return_tensors="pt"+ ).to(device)+ forward_kwargs = {+ "input_ids": inputs.input_ids,+ "attention_mask": inputs.attention_mask,+ "pixel_values": inputs.pixel_values,+ "image_grid_thw": inputs.image_grid_thw,+ }+ if "mm_token_type_ids" in inputs:+ forward_kwargs["mm_token_type_ids"] = inputs.mm_token_type_ids+ else:+ inputs = tokenizer([format_prompt(prompt)], return_tensors="pt").to(device)+ forward_kwargs = {"input_ids": inputs.input_ids, "attention_mask": inputs.attention_mask}+handle = base.language_model.norm.register_forward_hook(lambda module, args, output: args[0])try:- hidden = base(input_ids=inputs.input_ids, attention_mask=inputs.attention_mask).last_hidden_state+ # One forward, so the KV cache the language model would build over the whole sequence is waste.+ hidden = base(**forward_kwargs, use_cache=False).last_hidden_statefinally:handle.remove()- return hidden[:, system_prefix_length(tokenizer) :]++ drop = system_prefix_length(tokenizer)+ if not images:+ return PromptEncoding(hidden[:, drop:], None, ())+ image_pad_id = tokenizer.convert_tokens_to_ids(IMAGE_PAD)+ merge = processor.image_processor.merge_size+ grids = tuple((int(h) // merge, int(w) // merge) for _, h, w in inputs.image_grid_thw.tolist())+ return PromptEncoding(hidden[:, drop:], (inputs.input_ids == image_pad_id)[:, drop:], grids)diff --git a/invokeai/backend/qwen_image_2_1/vae.py b/invokeai/backend/qwen_image_2_1/vae.pyindex f787f2bb44..4fe48a69c9 100644--- a/invokeai/backend/qwen_image_2_1/vae.py+++ b/invokeai/backend/qwen_image_2_1/vae.py@@ -4,6 +4,8 @@ The tile geometry itself is applied by `patch_qwen_image_vae_tiling`, which thisand Wan VAEs (same tiling attributes, 16x instead of 8x)."""+import math+import numpy as npimport torchfrom PIL import Image@@ -96,11 +98,38 @@ def to_image(decoded: torch.Tensor) -> Image.Image:return Image.fromarray(pixels, mode="RGBA")-def to_vae_input(image: Image.Image) -> torch.Tensor:- """An image as the VAE's `(1, 4, H, W)` input in [-1, 1], encoded opaque.+def to_vae_input(image: Image.Image, keep_alpha: bool = False) -> torch.Tensor:+ """An image as the VAE's `(1, 4, H, W)` input in [-1, 1].- Alpha is set to 1 rather than read from the image: the canvas hands its masks over in alpha, and a- transparent region there means "repaint", not "transparent pixels".+ For image-to-image and the canvas, alpha is set to 1 rather than read from the image: the canvas hands its masks+ over in alpha, and a transparent region there means "repaint", not "transparent pixels". A reference image keeps+ its alpha (`keep_alpha`), as the pipeline encodes it."""- rgb = torch.from_numpy(np.asarray(image.convert("RGB"), dtype=np.float32)).permute(2, 0, 1) / 127.5 - 1- return torch.cat([rgb, torch.ones_like(rgb[:1])], dim=0)[None]+ rgba = image.convert("RGBA")+ # In the pipeline's `VaeImageProcessor` order, so the result matches it to the bit.+ pixels = torch.from_numpy(np.asarray(rgba).astype(np.float32) / 255.0).permute(2, 0, 1) * 2.0 - 1.0+ if not keep_alpha:+ pixels[3] = 1.0+ return pixels[None]+++# The area every reference image is resized to, as the pipeline's default `output_resolution` sets it.+REFERENCE_AREA = 1024 * 1024+++def reference_size(width: int, height: int) -> tuple[int, int]:+ """The size a `width` x `height` reference is read at: `REFERENCE_AREA` at its aspect ratio, on the 32px grid.++ The text encoder and the VAE must both read it at this size: each `<|image_pad|>` slot the encoder emits+ stands for 2x2 of the reference's latents, and the transformer drops the latents into those slots.+ """+ ratio = width / height+ scaled_width = math.sqrt(REFERENCE_AREA * ratio)+ # Python's round (half to even) is the pipeline's `calculate_dimensions`.+ return round(scaled_width / 32) * 32, round(scaled_width / ratio / 32) * 32+++def to_reference(image: Image.Image) -> Image.Image:+ """A reference image as both the text encoder and the VAE read it: RGBA, resized to `reference_size`."""+ rgba = image.convert("RGBA")+ return rgba.resize(reference_size(*rgba.size), resample=Image.LANCZOS)diff --git a/invokeai/backend/stable_diffusion/diffusion/conditioning_data.py b/invokeai/backend/stable_diffusion/diffusion/conditioning_data.pyindex 04591b0f23..43c24a6d11 100644--- a/invokeai/backend/stable_diffusion/diffusion/conditioning_data.py+++ b/invokeai/backend/stable_diffusion/diffusion/conditioning_data.py@@ -166,8 +166,18 @@ class QwenImage21ConditioningInfo:prompt_embeds: torch.Tensor"""Shape: (1, seq_len, hidden=4096). One prompt, so no padding and no mask."""+ image_pad_mask: torch.Tensor | None = None+ """Shape: (1, seq_len), True at the reference images' `<|image_pad|>` slots; None without references. Each+ slot stands for 2x2 latent tokens of a reference, in the order the references were encoded."""++ reference_grids: tuple[tuple[int, int], ...] = ()+ """Per reference, in order: its slots as (rows, columns), so the denoise node can match each reference's+ latents by shape, not only by count."""+def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)+ if self.image_pad_mask is not None:+ self.image_pad_mask = self.image_pad_mask.to(device=device)return selfdiff --git a/invokeai/frontend/api/openapi.json b/invokeai/frontend/api/openapi.jsonindex 537d0836d5..3bd8fae510 100644--- a/invokeai/frontend/api/openapi.json+++ b/invokeai/frontend/api/openapi.json@@ -20698,6 +20698,18 @@"title": "Reference Images Require Variant","description": "If set, reference images are only accepted for models of this variant."},+ "reference_images_require_format": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "title": "Reference Images Require Format",+ "description": "If set, reference images are only accepted for models of this format."+ },"supports_regional_guidance": {"type": "boolean","title": "Supports Regional Guidance",@@ -95409,6 +95421,29 @@"orig_default": null,"orig_required": false},+ "reference_latents": {+ "anyOf": [+ {+ "$ref": "#/components/schemas/LatentsField"+ },+ {+ "items": {+ "$ref": "#/components/schemas/LatentsField"+ },+ "type": "array"+ },+ {+ "type": "null"+ }+ ],+ "default": null,+ "description": "Latents of the reference images an edit reads, from Image to Latents in reference mode, in the order the prompts were encoded with them.",+ "field_kind": "input",+ "input": "connection",+ "orig_default": null,+ "orig_required": false,+ "title": "Reference Latents"+ },"denoising_start": {"default": 0.0,"description": "When to start denoising, expressed a percentage of total steps",@@ -95557,7 +95592,7 @@"tags": ["image", "qwen_image_2_1", "qwen-image-2.1"],"title": "Denoise - Qwen-Image-2.1","type": "object",- "version": "1.0.1",+ "version": "1.1.0","output": {"$ref": "#/components/schemas/LatentsOutput"}@@ -95566,7 +95601,7 @@"category": "image","class": "invocation","classification": "prototype",- "description": "Encodes an image into Qwen-Image-2.1 latents. The image is encoded opaque; its alpha is not read.",+ "description": "Encodes an image into Qwen-Image-2.1 latents.\n\nFor image-to-image and the canvas, the image is encoded opaque at its own size; its alpha is not read. As a\nreference for an edit, it is resized to the area the text encoder reads it at and keeps its alpha.","node_pack": "invokeai","properties": {"board": {@@ -95677,6 +95712,16 @@"title": "Tile Size","type": "integer"},+ "reference": {+ "default": false,+ "description": "Encode as a reference image for the denoise node's reference latents: resized to ~1 megapixel, as the text encoder reads it, with its alpha kept.",+ "field_kind": "input",+ "input": "any",+ "orig_default": false,+ "orig_required": false,+ "title": "Reference",+ "type": "boolean"+ },"type": {"const": "qwen_image_2_1_i2l","default": "qwen_image_2_1_i2l",@@ -95689,7 +95734,7 @@"tags": ["image", "latents", "vae", "i2l", "qwen_image_2_1"],"title": "Image to Latents - Qwen-Image-2.1","type": "object",- "version": "1.0.0",+ "version": "1.1.0","output": {"$ref": "#/components/schemas/LatentsOutput"}@@ -95963,7 +96008,7 @@"category": "conditioning","class": "invocation","classification": "prototype",- "description": "Encodes a text prompt for Qwen-Image-2.1 with its Qwen3-VL-8B encoder.",+ "description": "Encodes a prompt for Qwen-Image-2.1 with its Qwen3-VL-8B encoder, with the reference images an edit reads.\n\nEach reference also goes to the denoise node, encoded by Image to Latents in its reference mode, in the same\norder.","node_pack": "invokeai","properties": {"id": {@@ -96023,6 +96068,19 @@"orig_required": true,"title": "Qwen3-VL Encoder"},+ "reference_images": {+ "default": [],+ "description": "Reference images to edit or draw from, in the order the denoise node receives their latents. Needs a Qwen-Image-2.1 Diffusers model's own encoder.",+ "field_kind": "input",+ "input": "any",+ "items": {+ "$ref": "#/components/schemas/ImageField"+ },+ "orig_default": [],+ "orig_required": false,+ "title": "Reference Images",+ "type": "array"+ },"type": {"const": "qwen_image_2_1_text_encoder","default": "qwen_image_2_1_text_encoder",@@ -96035,7 +96093,7 @@"tags": ["prompt", "conditioning", "qwen_image_2_1", "qwen-image-2.1"],"title": "Prompt - Qwen-Image-2.1","type": "object",- "version": "1.0.0",+ "version": "1.1.0","output": {"$ref": "#/components/schemas/QwenImage21ConditioningOutput"}diff --git a/invokeai/frontend/api/schema.ts b/invokeai/frontend/api/schema.tsindex a845bdf0a0..76c5e0e9de 100644--- a/invokeai/frontend/api/schema.ts+++ b/invokeai/frontend/api/schema.ts@@ -5755,6 +5755,11 @@ export type components = {* @description If set, reference images are only accepted for models of this variant.*/reference_images_require_variant?: string | null;+ /**+ * Reference Images Require Format+ * @description If set, reference images are only accepted for models of this format.+ */+ reference_images_require_format?: string | null;/*** Supports Regional Guidance* @default false@@ -41186,6 +41191,12 @@ export type components = {* @default null*/denoise_mask?: components["schemas"]["DenoiseMaskField"] | null;+ /**+ * Reference Latents+ * @description Latents of the reference images an edit reads, from Image to Latents in reference mode, in the order the prompts were encoded with them.+ * @default null+ */+ reference_latents?: components["schemas"]["LatentsField"] | components["schemas"]["LatentsField"][] | null;/*** Denoising Start* @description When to start denoising, expressed a percentage of total steps@@ -41253,7 +41264,10 @@ export type components = {};/*** Image to Latents - Qwen-Image-2.1- * @description Encodes an image into Qwen-Image-2.1 latents. The image is encoded opaque; its alpha is not read.+ * @description Encodes an image into Qwen-Image-2.1 latents.+ *+ * For image-to-image and the canvas, the image is encoded opaque at its own size; its alpha is not read. As a+ * reference for an edit, it is resized to the area the text encoder reads it at and keeps its alpha.*/QwenImage21ImageToLatentsInvocation: {/**@@ -41305,6 +41319,12 @@ export type components = {* @default 0*/tile_size?: number;+ /**+ * Reference+ * @description Encode as a reference image for the denoise node's reference latents: resized to ~1 megapixel, as the text encoder reads it, with its alpha kept.+ * @default false+ */+ reference?: boolean;/*** type* @default qwen_image_2_1_i2l@@ -41451,7 +41471,10 @@ export type components = {};/*** Prompt - Qwen-Image-2.1- * @description Encodes a text prompt for Qwen-Image-2.1 with its Qwen3-VL-8B encoder.+ * @description Encodes a prompt for Qwen-Image-2.1 with its Qwen3-VL-8B encoder, with the reference images an edit reads.+ *+ * Each reference also goes to the denoise node, encoded by Image to Latents in its reference mode, in the same+ * order.*/QwenImage21TextEncoderInvocation: {/**@@ -41483,6 +41506,12 @@ export type components = {* @default null*/qwen3_vl_encoder?: components["schemas"]["Qwen3VLEncoderField"] | null;+ /**+ * Reference Images+ * @description Reference images to edit or draw from, in the order the denoise node receives their latents. Needs a Qwen-Image-2.1 Diffusers model's own encoder.+ * @default []+ */+ reference_images?: components["schemas"]["ImageField"][];/*** type* @default qwen_image_2_1_text_encoderdiff --git a/invokeai/frontend/webv2/src/features/generation/core/__fixtures__/architectureCapabilities.json b/invokeai/frontend/webv2/src/features/generation/core/__fixtures__/architectureCapabilities.jsonindex 005e93e496..7be2f86da6 100644--- a/invokeai/frontend/webv2/src/features/generation/core/__fixtures__/architectureCapabilities.json+++ b/invokeai/frontend/webv2/src/features/generation/core/__fixtures__/architectureCapabilities.json@@ -18,6 +18,7 @@],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -76,6 +77,7 @@],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -132,6 +134,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -173,6 +176,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -224,6 +228,7 @@],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -268,6 +273,7 @@],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -312,6 +318,7 @@],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -353,6 +360,7 @@"control_kinds": [],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -394,6 +402,7 @@"control_kinds": [],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -435,6 +444,7 @@"control_kinds": [],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -476,6 +486,7 @@"control_kinds": [],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -517,6 +528,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -565,6 +577,7 @@"control_kinds": [],"max_reference_images": 1,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -617,6 +630,7 @@"control_kinds": [],"max_reference_images": 1,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -669,6 +683,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -710,6 +725,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -751,6 +767,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -792,6 +809,7 @@"control_kinds": [],"max_reference_images": 5,"reference_images_require_variant": "edit",+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -842,8 +860,9 @@"scheduler_set": "flow","scheduler_applies_to_graph": false,"control_kinds": [],- "max_reference_images": 0,+ "max_reference_images": 4,"reference_images_require_variant": null,+ "reference_images_require_format": "diffusers","supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -883,8 +902,9 @@"scheduler_set": "flow","scheduler_applies_to_graph": false,"control_kinds": [],- "max_reference_images": 0,+ "max_reference_images": 4,"reference_images_require_variant": null,+ "reference_images_require_format": "diffusers","supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -929,6 +949,7 @@],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": true,"clip_skip_max": 12,@@ -970,6 +991,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": true,"clip_skip_max": 24,@@ -1011,6 +1033,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -1055,6 +1078,7 @@],"max_reference_images": 5,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": true,"clip_skip_max": null,@@ -1096,6 +1120,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -1137,6 +1162,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -1185,6 +1211,7 @@"control_kinds": [],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": false,"regional_negative": false,"clip_skip_max": null,@@ -1235,6 +1262,7 @@],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,@@ -1285,6 +1313,7 @@],"max_reference_images": 0,"reference_images_require_variant": null,+ "reference_images_require_format": null,"supports_regional_guidance": true,"regional_negative": false,"clip_skip_max": null,diff --git a/invokeai/frontend/webv2/src/features/generation/core/architectureCapabilities.ts b/invokeai/frontend/webv2/src/features/generation/core/architectureCapabilities.tsindex fdae256b51..794f176700 100644--- a/invokeai/frontend/webv2/src/features/generation/core/architectureCapabilities.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/architectureCapabilities.ts@@ -26,6 +26,7 @@ export interface ArchitectureCapabilitiesRow {control_kinds: string[];max_reference_images: number;reference_images_require_variant: string | null;+ reference_images_require_format: string | null;supports_regional_guidance: boolean;regional_negative: boolean;clip_skip_max: number | null;diff --git a/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.test.ts b/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.test.tsindex d4b75ad47a..ff4436edbf 100644--- a/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.test.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.test.ts@@ -35,6 +35,7 @@ import {isReferenceImageSupported,isGenerateModelSelectable,isSupportedGenerateModel,+ QWEN_IMAGE_21_STANDALONE_ENCODER_REASON,} from './baseGenerationPolicies';import { SUPPORTED_GENERATE_BASES } from './supportedBases';@@ -1106,6 +1107,15 @@ describe('component policies', () => {expect(isReferenceImageSupported(undefined)).toBe(false);});+ it('offers Qwen-Image-2.1 reference images only on its Diffusers pipeline, whose encoder reads images', () => {+ const diffusers = createModel('qwen-image-2-1', { format: 'diffusers' });++ expect(isReferenceImageSupported(diffusers)).toBe(true);+ expect(getMaxReferenceImages(diffusers)).toBe(4);+ expect(isReferenceImageSupported(createModel('qwen-image-2-1', { format: 'gguf_quantized' }))).toBe(false);+ expect(isReferenceImageSupported(createModel('qwen-image-2-1', { format: 'checkpoint' }))).toBe(false);+ });+it('derives the reference image limit from external provider capabilities', () => {expect(getMaxReferenceImages(createModel('sdxl'))).toBe(5);expect(getMaxReferenceImages(createModel('cogview4'))).toBe(0);@@ -1171,6 +1181,31 @@ describe('component policies', () => {).toContain('Reference Image #1 is not supported by sd-3 model.');});+ it('blocks Qwen-Image-2.1 references while a standalone encoder replaces the one that reads them', () => {+ const model = createModel('qwen-image-2-1', { format: 'diffusers' });+ const encoder = {+ base: 'any',+ key: 'qwen3-vl-8b',+ name: 'Qwen3-VL 8B',+ type: 'qwen3_vl_encoder',+ variant: 'qwen3_vl_8b',+ } as const;+ const referenceImages = [+ {+ config: { image: referenceImage, type: 'qwen_image_2_1_reference_image' as const },+ id: 'ref-1',+ isEnabled: true,+ },+ ];++ expect(+ getGenerationValidationReasons(model, createSettings(model, { qwen3VLEncoderModel: encoder, referenceImages }))+ ).toContain(QWEN_IMAGE_21_STANDALONE_ENCODER_REASON);+ expect(getGenerationValidationReasons(model, createSettings(model, { referenceImages }))).not.toContain(+ QWEN_IMAGE_21_STANDALONE_ENCODER_REASON+ );+ });+it('reports missing reference image model dependencies', () => {const model = createModel('sd-1');diff --git a/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.ts b/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.tsindex c70f7ca260..b3aa665136 100644--- a/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/baseGenerationPolicies.ts@@ -1048,7 +1048,8 @@ const getBaseComponentSectionPolicy = ({...qwen3VlEncoderSlot('Qwen-Image-2.1 conditions on the Qwen3-VL 8B encoder, not the 4B one Krea-2 uses. Required for ' +- 'non-Diffusers Qwen-Image-2.1 models.',+ "non-Diffusers Qwen-Image-2.1 models. On a Diffusers model it replaces the model's own encoder, the " ++ 'only one that reads reference images.',isQwen3Vl8bEncoder),required: (ctx) => ctx.model.format !== 'diffusers',@@ -1308,9 +1309,11 @@ export const isReferenceImageSupported = (model: GenerateModelConfig | undefinedreturn false;}- // The backend declares which reference-image features require a specific variant.+ // The backend declares which reference-image features require a specific variant or format.return (- features.reference_images_require_variant === null || model.variant === features.reference_images_require_variant+ (features.reference_images_require_variant === null ||+ model.variant === features.reference_images_require_variant) &&+ (features.reference_images_require_format === null || model.format === features.reference_images_require_format));};@@ -1352,6 +1355,10 @@ export const getDefaultReferenceImageConfig = (return { image, styleStrength: DEFAULT_KREA2_STYLE_STRENGTH, type: 'krea2_reference_image' };}+ if (modelBase === 'qwen-image-2-1') {+ return { image, type: 'qwen_image_2_1_reference_image' };+ }+if (isFluxKontextModel(model)) {return { image, model, type: 'flux_kontext_reference_image' };}@@ -1383,6 +1390,10 @@ const getReferenceImageConfigSupported = (return model.type !== 'external_image_generator' && model.base === 'qwen-image' && model.variant === 'edit';case 'krea2_reference_image':return model.type !== 'external_image_generator' && model.base === 'krea-2';+ case 'qwen_image_2_1_reference_image':+ return (+ model.type !== 'external_image_generator' && model.base === 'qwen-image-2-1' && isReferenceImageSupported(model)+ );case 'flux_kontext_reference_image':return isFluxKontextModel(model);case 'flux_redux':@@ -1677,6 +1688,11 @@ const getPidValidationReasons = (model: GenerateModelConfig, settings: GenerateSreturn reasons;};+/** Only the encoder of a Qwen-Image-2.1 Diffusers model keeps the vision tower that reads references. */+export const QWEN_IMAGE_21_STANDALONE_ENCODER_REASON =+ "Reference images need the Qwen-Image-2.1 model's own encoder, the only one with a vision tower. " ++ 'Clear the Qwen3-VL Encoder component.';+const getReferenceImageValidationReasons = (model: GenerateModelConfig, settings: GenerateSettings): string[] => {const reasons: string[] = [];const enabled = settings.referenceImages.filter((referenceImage) => referenceImage.isEnabled);@@ -1721,6 +1737,17 @@ const getReferenceImageValidationReasons = (model: GenerateModelConfig, settings}});+ // The encoder slot stays filled across models (Ideogram 4 and the GGUF builds share it), so a Diffusers model can+ // inherit one that cannot read the references.+ if (+ model.base === 'qwen-image-2-1' &&+ settings.qwen3VLEncoderModel &&+ isQwen3Vl8bEncoder(settings.qwen3VLEncoderModel) &&+ enabled.some((referenceImage) => referenceImage.config.type === 'qwen_image_2_1_reference_image')+ ) {+ reasons.push(QWEN_IMAGE_21_STANDALONE_ENCODER_REASON);+ }+return reasons;};diff --git a/invokeai/frontend/webv2/src/features/generation/core/graph.test.ts b/invokeai/frontend/webv2/src/features/generation/core/graph.test.tsindex a70cebf808..5a9454a595 100644--- a/invokeai/frontend/webv2/src/features/generation/core/graph.test.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/graph.test.ts@@ -4,6 +4,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';import type {ComponentModelConfig,GenerateModelConfig,+ GenerateReferenceImage,GenerateSettings,LoraModelConfig,MainModelConfig,@@ -744,6 +745,65 @@ describe('compileGenerateGraph', () => {expect(getEdge(graph, 'pos_cond', 'reference_images')).toBeDefined();expect(getEdge(graph, 'denoise_latents', 'reference_latents')).toBeDefined();});++ describe('Qwen-Image-2.1 reference images', () => {+ const qwenImage21Model: MainModelConfig = {+ base: 'qwen-image-2-1',+ format: 'diffusers',+ key: 'qwen21',+ name: 'Qwen-Image-2.1',+ type: 'main',+ };+ const references = (...names: string[]): GenerateReferenceImage[] =>+ names.map((name) => ({+ config: {+ image: { original: { image: { height: 768, image_name: name, width: 512 } } },+ type: 'qwen_image_2_1_reference_image',+ },+ id: name,+ isEnabled: true,+ }));++ it('reads each reference in both prompts and, in the same order, as latents in denoise', () => {+ const graph = compile(qwenImage21Model, { cfgScale: 4, referenceImages: references('a.png', 'b.png') });++ const nameOf = (imageNode: string) =>+ (graph.nodes[imageNode]?.image as { image_name: string } | undefined)?.image_name;+ // Each chain is followed back from its end: the last collect holds b, the one before it a.+ const chain = (lastCollect: string) => [+ getEdge(graph, lastCollect, 'collection')?.source.node_id ?? '',+ lastCollect,+ ];++ const imagesInto = getEdge(graph, 'pos_cond', 'reference_images')?.source.node_id ?? '';+ expect(getEdge(graph, 'neg_cond', 'reference_images')?.source.node_id).toBe(imagesInto);+ expect(chain(imagesInto).map((collect) => nameOf(getEdge(graph, collect, 'item')?.source.node_id ?? ''))).toEqual(+ ['a.png', 'b.png']+ );++ const latentsInto = getEdge(graph, 'denoise_latents', 'reference_latents')?.source.node_id ?? '';+ const latentImages = chain(latentsInto).map((collect) => {+ const i2l = getEdge(graph, collect, 'item')?.source.node_id ?? '';+ expect(graph.nodes[i2l]).toMatchObject({ reference: true, type: 'qwen_image_2_1_i2l' });+ return nameOf(getEdge(graph, i2l, 'image')?.source.node_id ?? '');+ });+ expect(latentImages).toEqual(['a.png', 'b.png']);+ });++ it('refuses references with a standalone encoder, which has no vision tower', () => {+ const encoder: ComponentModelConfig = {+ base: 'any',+ key: 'qwen3-vl-8b',+ name: 'Qwen3-VL 8B',+ type: 'qwen3_vl_encoder',+ variant: 'qwen3_vl_8b',+ };++ expect(() =>+ compile(qwenImage21Model, { qwen3VLEncoderModel: encoder, referenceImages: references('a.png') })+ ).toThrow(/vision tower/);+ });+ });});describe('generate seeds', () => {diff --git a/invokeai/frontend/webv2/src/features/generation/core/graph.ts b/invokeai/frontend/webv2/src/features/generation/core/graph.tsindex 598c1dbbf0..55a600b3a9 100644--- a/invokeai/frontend/webv2/src/features/generation/core/graph.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/graph.ts@@ -25,6 +25,7 @@ import {getFlux2DiffusersComponentSource,getGenerationDimensions,getGenerationValidationReasons,+ QWEN_IMAGE_21_STANDALONE_ENCODER_REASON,type SupportedGenerateBase,} from './baseGenerationPolicies';import {@@ -1438,14 +1439,86 @@ const buildQwenImage21Graph = (}addEdge(graph, seed, 'value', denoise, 'seed');addEdge(graph, denoise, 'latents', output, 'latents');+ addQwenImage21ReferenceImages(graph, settings, {+ conditionings: negCond ? [posCond, negCond] : [posCond],+ denoise,+ hasStandaloneEncoder: Boolean(qwen3VlEncoderModel),+ modelLoader,+ });addMetadata(graph, output, settings, model, 'qwen_image_2_1_txt2img', projectSettings, {qwen3_vl_encoder: qwen3VlEncoderModel ?? undefined,vae: vaeModel ?? undefined,});+ addReferenceImageMetadata(graph, output, settings);return graph;};+/**+ * Qwen-Image-2.1 reads each reference twice, at one size: the prompt encoder sees it through the vision tower, and+ * the denoise node places its clean latents in the prompt's image slots. Both prompts are encoded with the+ * references, and the latents follow in the same order.+ */+const addQwenImage21ReferenceImages = (+ graph: BackendGraphContract,+ settings: GenerateSettings,+ nodes: {+ conditionings: BackendInvocationContract[];+ denoise: BackendInvocationContract;+ hasStandaloneEncoder: boolean;+ modelLoader: BackendInvocationContract;+ }+): void => {+ const referenceImages = getEnabledReferenceImages(settings, 'qwen_image_2_1_reference_image');+ if (referenceImages.length === 0) {+ return;+ }+ // A backstop: validation reports this before Invoke.+ if (nodes.hasStandaloneEncoder) {+ throw new Error(QWEN_IMAGE_21_STANDALONE_ENCODER_REASON);+ }++ let imageCollect: BackendInvocationContract | null = null;+ let latentsCollect: BackendInvocationContract | null = null;++ for (const { config } of referenceImages) {+ if (!config.image) {+ continue;+ }++ const imageNode = addNode(graph, {+ id: createId('qwen21_ref_img'),+ image: toImageField(config.image),+ type: 'image',+ });+ const i2l = addNode(graph, { id: createId('qwen21_ref_i2l'), reference: true, type: 'qwen_image_2_1_i2l' });+ addEdge(graph, imageNode, 'image', i2l, 'image');+ addEdge(graph, nodes.modelLoader, 'vae', i2l, 'vae');++ const nextImageCollect = addNode(graph, { id: createId('qwen21_ref_img_collect'), type: 'collect' });+ addEdge(graph, imageNode, 'image', nextImageCollect, 'item');+ if (imageCollect) {+ addEdge(graph, imageCollect, 'collection', nextImageCollect, 'collection');+ }+ imageCollect = nextImageCollect;++ const nextLatentsCollect = addNode(graph, { id: createId('qwen21_ref_latents_collect'), type: 'collect' });+ addEdge(graph, i2l, 'latents', nextLatentsCollect, 'item');+ if (latentsCollect) {+ addEdge(graph, latentsCollect, 'collection', nextLatentsCollect, 'collection');+ }+ latentsCollect = nextLatentsCollect;+ }++ if (!imageCollect || !latentsCollect) {+ return;+ }+ for (const conditioning of nodes.conditionings) {+ addEdge(graph, imageCollect, 'collection', conditioning, 'reference_images');+ }+ addEdge(graph, latentsCollect, 'collection', nodes.denoise, 'reference_latents');+};+const buildIdeogram4Graph = (settings: GenerateSettings,model: MainModelConfig,diff --git a/invokeai/frontend/webv2/src/features/generation/core/settings.ts b/invokeai/frontend/webv2/src/features/generation/core/settings.tsindex dea964ea14..178d589f33 100644--- a/invokeai/frontend/webv2/src/features/generation/core/settings.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/settings.ts@@ -312,6 +312,7 @@ const REFERENCE_IMAGE_CONFIG_TYPES = new Set(['ip_adapter','krea2_reference_image','qwen_image_reference_image',+ 'qwen_image_2_1_reference_image',]);const isClipVisionModel = (value: unknown): value is 'ViT-H' | 'ViT-G' | 'ViT-L' =>@@ -347,6 +348,7 @@ const normalizeReferenceImageConfig = (value: unknown): GenerateReferenceImageCocase 'external_reference_image':case 'flux2_reference_image':case 'qwen_image_reference_image':+ case 'qwen_image_2_1_reference_image':return { image, type: value.type };case 'flux_kontext_reference_image':return { image, model: getMainModelOrNull(value.model), type: value.type };@@ -419,6 +421,7 @@ const cloneReferenceImageConfig = (config: GenerateReferenceImageConfig): Generacase 'flux2_reference_image':case 'krea2_reference_image':case 'qwen_image_reference_image':+ case 'qwen_image_2_1_reference_image':return { ...config, image: config.image ? cloneCroppableImage(config.image) : null };case 'flux_kontext_reference_image':return {@@ -589,6 +592,7 @@ const syncReferenceImageConfigWithModels = (case 'flux2_reference_image':case 'krea2_reference_image':case 'qwen_image_reference_image':+ case 'qwen_image_2_1_reference_image':return config;}};diff --git a/invokeai/frontend/webv2/src/features/generation/core/types.ts b/invokeai/frontend/webv2/src/features/generation/core/types.tsindex a2ba70b472..ca7597e15f 100644--- a/invokeai/frontend/webv2/src/features/generation/core/types.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/types.ts@@ -142,6 +142,7 @@ export type GenerateReferenceImageConfig =| { type: 'qwen_image_reference_image'; image: GenerateReferenceImageAsset | null }/** Krea-2 training-free style transfer: no adapter model, a single reference, `styleStrength` 0–2 (0 bypasses). */| { type: 'krea2_reference_image'; image: GenerateReferenceImageAsset | null; styleStrength: number }+ | { type: 'qwen_image_2_1_reference_image'; image: GenerateReferenceImageAsset | null }| { type: 'external_reference_image'; image: GenerateReferenceImageAsset | null };export interface GenerateReferenceImage {diff --git a/invokeai/frontend/webv2/src/workbench/canvasMigration.ts b/invokeai/frontend/webv2/src/workbench/canvasMigration.tsindex 9d91441f0c..710dd33fa8 100644--- a/invokeai/frontend/webv2/src/workbench/canvasMigration.ts+++ b/invokeai/frontend/webv2/src/workbench/canvasMigration.ts@@ -183,6 +183,7 @@ const zReferenceImage = z.object({}),z.object({ image: zGeneratedImage.nullable(), type: z.literal('flux2_reference_image') }),z.object({ image: zGeneratedImage.nullable(), type: z.literal('qwen_image_reference_image') }),+ z.object({ image: zGeneratedImage.nullable(), type: z.literal('qwen_image_2_1_reference_image') }),z.object({ image: zGeneratedImage.nullable(), type: z.literal('external_reference_image') }),]),id: z.string(),diff --git a/tests/app/invocations/test_qwen_image_2_1_nodes.py b/tests/app/invocations/test_qwen_image_2_1_nodes.pyindex 20e7fa0ad0..cb6e29a747 100644--- a/tests/app/invocations/test_qwen_image_2_1_nodes.py+++ b/tests/app/invocations/test_qwen_image_2_1_nodes.py@@ -4,29 +4,36 @@ What the GPU parity run cannot see: the order the prefix cache is filled in whenan image-to-image run starts on each variant's schedule, and which components the loader refuses."""-from contextlib import contextmanager+from contextlib import contextmanager, nullcontextfrom types import SimpleNamespaceimport numpy as npimport pytestimport torch+from PIL import Image-from invokeai.app.invocations.fields import LatentsField, QwenImage21ConditioningField-from invokeai.app.invocations.model import ModelIdentifierField, TransformerField+from invokeai.app.invocations.fields import ImageField, LatentsField, QwenImage21ConditioningField+from invokeai.app.invocations.model import ModelIdentifierField, Qwen3VLEncoderField, TransformerField, VAEFieldfrom invokeai.app.invocations.qwen_image_2_1.qwen_image_2_1_denoise import (+ KV_BYTES_PER_PREFIX_TOKEN,LATENT_CHANNELS,QwenImage21DenoiseInvocation,clip_sigmas,pack_latents,+ prefix_cache_fits,+ prefix_length,unpack_latents,)from invokeai.app.invocations.qwen_image_2_1.qwen_image_2_1_model_loader import QwenImage21ModelLoaderInvocation+from invokeai.app.invocations.text_encoder.qwen_image_2_1_text_encoder import QwenImage21TextEncoderInvocation+from invokeai.app.invocations.vae.qwen_image_2_1_image_to_latents import QwenImage21ImageToLatentsInvocationfrom invokeai.backend.model_manager.taxonomy import (BaseModelType,ModelFormat,ModelType,Qwen3VLVariantType,QwenImage21VariantType,+ SubModelType,)from invokeai.backend.stable_diffusion.diffusion.conditioning_data import (ConditioningFieldData,@@ -52,13 +59,28 @@ class _Transformer:def __init__(self) -> None:self.calls: list[tuple[str, int, str | None, float]] = []+ self.inputs: list[SimpleNamespace] = []def modules(self):return iter(())- def __call__(self, *, hidden_states, encoder_hidden_states, timestep, kv_cache, kv_cache_mode, **_kwargs):+ def __call__(+ self,+ *,+ hidden_states,+ encoder_hidden_states,+ timestep,+ kv_cache,+ kv_cache_mode,+ img_shapes,+ img_mask,+ **_kwargs,+ ):prompt = "pos" if float(encoder_hidden_states.mean()) > 0 else "neg"self.calls.append((prompt, id(kv_cache), kv_cache_mode, round(float(timestep[0]), 3)))+ self.inputs.append(+ SimpleNamespace(hidden_states=hidden_states.clone(), img_shapes=img_shapes, img_mask=img_mask)+ )# The transformer returns the joint sequence; the node keeps the image tokens at its end.return (torch.zeros(1, encoder_hidden_states.shape[1] + hidden_states.shape[1], hidden_states.shape[2]),)@@ -72,18 +94,27 @@ class _TransformerInfo:yield ({}, self.model)-def _context(transformer: _Transformer, variant: QwenImage21VariantType, init_latents: torch.Tensor | None = None):- conditionings = {- "pos": ConditioningFieldData(conditionings=[QwenImage21ConditioningInfo(prompt_embeds=torch.ones(1, 3, 8))]),- "neg": ConditioningFieldData(conditionings=[QwenImage21ConditioningInfo(prompt_embeds=-torch.ones(1, 2, 8))]),+def _context(+ transformer: _Transformer,+ variant: QwenImage21VariantType,+ init_latents: torch.Tensor | None = None,+ *,+ infos: dict[str, QwenImage21ConditioningInfo] | None = None,+ tensors: dict[str, torch.Tensor] | None = None,+):+ infos = infos or {+ "pos": QwenImage21ConditioningInfo(prompt_embeds=torch.ones(1, 3, 8)),+ "neg": QwenImage21ConditioningInfo(prompt_embeds=-torch.ones(1, 2, 8)),}+ conditionings = {name: ConditioningFieldData(conditionings=[info]) for name, info in infos.items()}+ tensors = tensors or {}return SimpleNamespace(models=SimpleNamespace(load=lambda _identifier: _TransformerInfo(transformer),get_config=lambda _identifier: SimpleNamespace(variant=variant),),conditioning=SimpleNamespace(load=lambda name: conditionings[name]),- tensors=SimpleNamespace(load=lambda _name: init_latents),+ tensors=SimpleNamespace(load=lambda name: tensors.get(name, init_latents)),util=SimpleNamespace(sd_step_callback=lambda *_args: None),)@@ -96,6 +127,7 @@ def _denoise(**fields) -> QwenImage21DenoiseInvocation:"negative_conditioning": QwenImage21ConditioningField(conditioning_name="neg"),"latents": None,"denoise_mask": None,+ "reference_latents": None,"denoising_start": 0.0,"denoising_end": 1.0,"cfg_scale": 1.0,@@ -201,8 +233,8 @@ def test_a_strength_on_base_names_whole_steps_and_always_runs_one(def test_a_float32_fallback_reserves_twice_the_bfloat16_activations() -> None:estimate = QwenImage21DenoiseInvocation._estimate_working_memorybase = 1024**3- bf16 = estimate(4096, 300, 300, True, 2) - base- assert estimate(4096, 300, 300, True, 4) - base == 2 * bf16+ bf16 = estimate(4096, 300, 300, True, True, 2) - base+ assert estimate(4096, 300, 300, True, True, 4) - base == 2 * bf16@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])@@ -349,3 +381,263 @@ def test_the_4b_encoder_is_refused() -> None:loader = _loader(qwen3_vl_encoder_model=("encoder", BaseModelType.Any, ModelType.Qwen3VLEncoder))with pytest.raises(ValueError, match="8B"):loader.invoke(_loader_context(ModelFormat.Diffusers, encoder_variant=Qwen3VLVariantType.Qwen3VL_4B))+++def _edit_prompt(sign: float, grids: list[tuple[int, int]]) -> QwenImage21ConditioningInfo:+ """A prompt encoded with references: a token, then one run of image slots per reference, then a token."""+ mask = [False]+ for rows, columns in grids:+ mask += [True] * (rows * columns) + [False]+ return QwenImage21ConditioningInfo(+ prompt_embeds=sign * torch.ones(1, len(mask), 8),+ image_pad_mask=torch.tensor([mask]),+ reference_grids=tuple(grids),+ )+++def _edit_context(transformer, prompt_grids, references: dict[str, torch.Tensor], negative=None):+ infos = {"pos": _edit_prompt(1.0, prompt_grids), "neg": negative or _edit_prompt(-1.0, prompt_grids)}+ return _context(transformer, QwenImage21VariantType.Base, infos=infos, tensors=references)+++def test_references_lead_the_image_tokens_and_fill_the_prompts_slots() -> None:+ transformer = _Transformer()+ # A 4x8 reference is 32 latent tokens in 2x4 slots; the 64x64 target is 4x4.+ reference = torch.randn(1, LATENT_CHANNELS, 4, 8)+ _denoise(reference_latents=[LatentsField(latents_name="ref")], steps=1)._run_diffusion(+ _edit_context(transformer, [(2, 4)], {"ref": reference})+ )++ seen = transformer.inputs[0]+ assert seen.img_shapes == [[(1, 4, 8), (1, 4, 4)]]+ assert seen.hidden_states.shape[1] == 32 + 16+ assert torch.equal(seen.hidden_states[:, :32], pack_latents(reference))+ # The prompt's own mask, then one slot per 2x2 group of the target's 4x4 latents.+ assert seen.img_mask.tolist() == [[False] + [True] * 8 + [False] + [True] * 4]+++def test_references_that_do_not_fill_the_slots_are_refused() -> None:+ context = _edit_context(_Transformer(), [(2, 2)], {"ref": torch.zeros(1, LATENT_CHANNELS, 4, 8)})+ with pytest.raises(ValueError, match=r"positive prompt was encoded with 1 reference image\(s\) \(64x64\)"):+ _denoise(reference_latents=LatentsField(latents_name="ref"))._run_diffusion(context)+++def test_two_references_of_one_size_in_swapped_order_are_refused() -> None:+ # Landscape then portrait in the prompt; portrait then landscape as latents. The slot counts agree.+ context = _edit_context(+ _Transformer(),+ [(2, 4), (4, 2)],+ {"portrait": torch.zeros(1, LATENT_CHANNELS, 8, 4), "landscape": torch.zeros(1, LATENT_CHANNELS, 4, 8)},+ )+ latents = [LatentsField(latents_name="portrait"), LatentsField(latents_name="landscape")]+ with pytest.raises(ValueError, match="in reference mode, from the same images and in the same order"):+ _denoise(reference_latents=latents)._run_diffusion(context)+++def test_latents_from_another_vae_are_refused() -> None:+ context = _edit_context(_Transformer(), [(2, 4)], {"ref": torch.zeros(1, 16, 4, 8)})+ with pytest.raises(ValueError, match="64 channels"):+ _denoise(reference_latents=LatentsField(latents_name="ref"))._run_diffusion(context)+++def test_a_negative_prompt_encoded_without_the_references_is_refused() -> None:+ context = _edit_context(+ _Transformer(),+ [(2, 4)],+ {"ref": torch.zeros(1, LATENT_CHANNELS, 4, 8)},+ negative=QwenImage21ConditioningInfo(prompt_embeds=-torch.ones(1, 2, 8)),+ )+ with pytest.raises(ValueError, match="negative prompt was encoded with 0"):+ _denoise(reference_latents=LatentsField(latents_name="ref"), cfg_scale=4.0)._run_diffusion(context)+++def test_the_prefix_counts_each_reference_slot_as_four_latent_tokens() -> None:+ # A 1 MP reference is 32x32 slots: 1024 slots in the prompt, 4096 tokens in the joint sequence.+ info = _edit_prompt(1.0, [(32, 32)])+ assert prefix_length(info) == info.prompt_embeds.shape[1] + 3 * 1024+++def test_the_prefix_caches_are_reserved_only_when_they_are_kept() -> None:+ estimate = QwenImage21DenoiseInvocation._estimate_working_memory+ prefix = 4200+ cached = estimate(4096, prefix, prefix, True, True, 2)+ recomputed = estimate(4096, prefix, prefix, True, False, 2)+ # K and V of every prefix token in all 32 blocks, per guidance branch: ~2 GiB per 1 MP reference.+ assert cached - recomputed == 2 * prefix * KV_BYTES_PER_PREFIX_TOKEN+++def test_caches_larger_than_half_the_card_are_not_kept(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(torch.cuda, "get_device_properties", lambda _device: SimpleNamespace(total_memory=24 * 2**30))+ cuda = torch.device("cuda")+ # Two references at CFG fit a 24 GB card; four do not.+ assert prefix_cache_fits(2 * 2 * 4200 * KV_BYTES_PER_PREFIX_TOKEN, cuda)+ assert not prefix_cache_fits(2 * 4 * 4200 * KV_BYTES_PER_PREFIX_TOKEN, cuda)+++def test_without_room_for_the_caches_every_step_runs_the_whole_prefix(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(f"{DENOISE}.prefix_cache_fits", lambda _bytes, _device: False)+ transformer = _Transformer()+ _denoise(reference_latents=LatentsField(latents_name="ref"), cfg_scale=4.0)._run_diffusion(+ _edit_context(transformer, [(2, 4)], {"ref": torch.zeros(1, LATENT_CHANNELS, 4, 8)})+ )+ assert {(cache, mode) for _prompt, cache, mode, _t in transformer.calls} == {(id(None), None)}+++def test_a_standalone_encoder_cannot_read_references() -> None:+ encoder = ModelIdentifierField(+ key="enc", hash="h", name="qwen3vl_8b", base=BaseModelType.Any, type=ModelType.Qwen3VLEncoder+ )+ node = QwenImage21TextEncoderInvocation.model_construct(+ prompt="swap them",+ qwen3_vl_encoder=Qwen3VLEncoderField(tokenizer=encoder, text_encoder=encoder, loras=[]),+ reference_images=[ImageField(image_name="ref.png")],+ )+ context = SimpleNamespace(+ images=SimpleNamespace(get_pil=lambda _name: Image.new("RGB", (64, 64))),+ models=SimpleNamespace(+ get_config=lambda _identifier: SimpleNamespace(+ base=BaseModelType.Any, type=ModelType.Qwen3VLEncoder, format=ModelFormat.Checkpoint+ )+ ),+ )+ with pytest.raises(ValueError, match="vision tower"):+ node.invoke(context)+++def test_the_processor_comes_from_the_qwen_image_2_1_pipeline_the_tokenizer_is_from() -> None:+ main = ModelIdentifierField(+ key="qwen21", hash="h", name="Qwen-Image-2.1", base=BaseModelType.QwenImage21, type=ModelType.Main+ )+ tokenizer = main.model_copy(update={"submodel_type": SubModelType.Tokenizer})+ node = QwenImage21TextEncoderInvocation.model_construct(+ prompt="swap them",+ qwen3_vl_encoder=Qwen3VLEncoderField(tokenizer=tokenizer, text_encoder=tokenizer, loras=[]),+ reference_images=[ImageField(image_name="ref.png")],+ )+ context = SimpleNamespace(+ models=SimpleNamespace(+ get_config=lambda _identifier: SimpleNamespace(+ base=BaseModelType.QwenImage21, type=ModelType.Main, format=ModelFormat.Diffusers+ )+ )+ )+ processor = node._processor_identifier(context)+ assert (processor.key, processor.submodel_type) == ("qwen21", SubModelType.Processor)+++class _ReservationRecorded(Exception):+ pass++[email protected]("num_references", [0, 4])+def test_the_encoder_reserves_room_for_the_attention_its_references_need(num_references: int) -> None:+ from invokeai.backend.qwen3_vl.qwen3_vl_assets import load_bundled_qwen3_vl_tokenizer++ main = ModelIdentifierField(+ key="qwen21", hash="h", name="Qwen-Image-2.1", base=BaseModelType.QwenImage21, type=ModelType.Main+ )+ tokenizer = main.model_copy(update={"submodel_type": SubModelType.Tokenizer})+ encoder = main.model_copy(update={"submodel_type": SubModelType.TextEncoder})+ reserved: list[int] = []++ @contextmanager+ def on_device(*, working_mem_bytes: int):+ reserved.append(working_mem_bytes)+ raise _ReservationRecorded+ yield # pragma: no cover++ text_encoder = torch.nn.Module()+ text_encoder.language_model = torch.nn.Linear(2, 2)+ text_encoder.visual = torch.nn.Linear(2, 2)+ infos = {+ SubModelType.TextEncoder: SimpleNamespace(+ model=text_encoder, compute_device=torch.device("cpu"), model_on_device=on_device+ ),+ SubModelType.Tokenizer: nullcontext(load_bundled_qwen3_vl_tokenizer()),+ SubModelType.Processor: SimpleNamespace(model_on_device=lambda: nullcontext((None, None))),+ }+ node = QwenImage21TextEncoderInvocation.model_construct(+ prompt="Combine the subjects of all reference images.",+ qwen3_vl_encoder=Qwen3VLEncoderField(tokenizer=tokenizer, text_encoder=encoder, loras=[]),+ reference_images=[ImageField(image_name=f"ref{i}.png") for i in range(num_references)],+ )+ context = SimpleNamespace(+ images=SimpleNamespace(get_pil=lambda _name: Image.new("RGB", (1024, 1024))),+ models=SimpleNamespace(+ get_config=lambda _identifier: SimpleNamespace(+ base=BaseModelType.QwenImage21, type=ModelType.Main, format=ModelFormat.Diffusers+ ),+ load=lambda identifier: infos[identifier.submodel_type],+ ),+ util=SimpleNamespace(signal_progress=lambda _message: None),+ )++ with pytest.raises(_ReservationRecorded):+ node.invoke(context)++ if num_references:+ # Measured on an RTX 4090: four 1-megapixel references peak 5.44 GiB above the weights, and the allocator+ # reserves about a fifth more. Less than that and the driver spills the encode to system memory.+ assert reserved[0] >= 1.2 * 5.44 * 2**30+ else:+ assert reserved[0] < 2**28+++class _VAE:+ """Records what it encodes and answers with bf16 latents, as the real VAE runs in bf16."""++ dtype = torch.bfloat16+ config = SimpleNamespace(latents_mean=[0.3] * LATENT_CHANNELS, latents_std=[1.7] * LATENT_CHANNELS)++ def __init__(self) -> None:+ self.pixels: torch.Tensor | None = None+ self.z: torch.Tensor | None = None++ def parameters(self):+ return iter([torch.zeros(1, dtype=torch.bfloat16)])++ def encode(self, pixels: torch.Tensor):+ self.pixels = pixels+ _, _, frames, height, width = pixels.shape+ generator = torch.Generator().manual_seed(0)+ self.z = torch.randn(1, LATENT_CHANNELS, frames, height // 16, width // 16, generator=generator).bfloat16()+ return SimpleNamespace(latent_dist=SimpleNamespace(mode=lambda: self.z))+++def test_a_reference_encodes_at_its_reading_size_with_its_alpha_normalized_in_bf16(monkeypatch) -> None:+ from invokeai.app.invocations.vae import qwen_image_2_1_image_to_latents as i2l_module++ monkeypatch.setattr(i2l_module, "patch_qwen_image_vae_tiling", lambda *_args: nullcontext())+ vae = _VAE()+ saved: dict[str, torch.Tensor] = {}++ @contextmanager+ def on_device(**_kwargs):+ yield ({}, vae)++ context = SimpleNamespace(+ images=SimpleNamespace(get_pil=lambda _name: Image.new("RGBA", (500, 300), (255, 0, 0, 64))),+ models=SimpleNamespace(+ load=lambda _identifier: SimpleNamespace(+ compute_device=torch.device("cpu"), model=vae, model_on_device=on_device+ )+ ),+ config=SimpleNamespace(get=lambda: SimpleNamespace(force_tiled_decode=False, auto_tiled_decode=False)),+ tensors=SimpleNamespace(save=lambda tensor: saved.update(latents=tensor) or "latents"),+ )+ vae_field = VAEField(+ vae=ModelIdentifierField(key="v", hash="h", name="v", base=BaseModelType.QwenImage21, type=ModelType.VAE)+ )+ output = QwenImage21ImageToLatentsInvocation.model_construct(+ image=ImageField(image_name="ref.png"), vae=vae_field, tiled=False, tile_size=0, reference=True+ ).invoke(context)++ # 500x300 reads at 1312x800; the alpha is the image's own (64/255), not the canvas encode's 1.+ assert (output.width, output.height) == (1312, 800)+ assert vae.pixels is not None and vae.pixels.shape == (1, 4, 1, 800, 1312)+ assert vae.pixels[0, 3].unique().tolist() == [torch.tensor(64 / 255 * 2 - 1).bfloat16().item()]+ # Normalized in bf16, as the pipeline normalizes it -- which differs from normalizing in fp32.+ mean, std = torch.tensor(0.3).bfloat16(), torch.tensor(1.7).bfloat16()+ expected = ((vae.z - mean) / std).float()[:, :, 0]+ assert torch.equal(saved["latents"], expected)+ assert not torch.equal(expected, ((vae.z.float() - 0.3) / 1.7)[:, :, 0])diff --git a/tests/backend/architectures/test_features.py b/tests/backend/architectures/test_features.pyindex 5b8aa98956..65fae7c66d 100644--- a/tests/backend/architectures/test_features.py+++ b/tests/backend/architectures/test_features.py@@ -80,13 +80,20 @@ def test_control_kinds_match_the_frontend_policy(kind: ControlKind, expected: seassert declared == expected-def test_reference_images_and_the_one_variant_condition() -> None:- """Qwen-Image is the only base whose answer depends on the variant, so it is the only one with- `reference_images_require_variant` set."""+def test_reference_images_and_the_conditions_on_them() -> None:+ """Qwen-Image is the only base whose answer depends on the variant, and Qwen-Image-2.1 the only one whose+ answer depends on the format."""supported = {b.value for b in generative_bases() if (f := get(b, FeaturesFacet)) is not None and f.supports_reference_images}- assert supported == {"flux", "flux2", "sd-1", "sdxl", "qwen-image", "krea-2"}+ assert supported == {"flux", "flux2", "sd-1", "sdxl", "qwen-image", "krea-2", "qwen-image-2-1"}++ by_format = {+ b.value: f.reference_images_require_format+ for b in generative_bases()+ if (f := get(b, FeaturesFacet)) is not None and f.reference_images_require_format is not None+ }+ assert by_format == {"qwen-image-2-1": "diffusers"}conditional = {b.valuediff --git a/tests/backend/model_manager/load/test_qwen3_vl_visual_tower.py b/tests/backend/model_manager/load/test_qwen3_vl_visual_tower.pyindex 55c6d116c7..c807f5c73c 100644--- a/tests/backend/model_manager/load/test_qwen3_vl_visual_tower.py+++ b/tests/backend/model_manager/load/test_qwen3_vl_visual_tower.py@@ -300,3 +300,24 @@ def test_the_folder_loader_drops_the_tower(tmp_path: Path) -> None:model = loader._load_model(config, SubModelType.TextEncoder)_assert_tower_gone(model)+++def test_the_qwen_image_2_1_pipeline_encoder_keeps_the_tower(monkeypatch, tmp_path: Path) -> None:+ """The one encoder that keeps it: Qwen-Image-2.1 reads reference images through it, and the node refuses an+ encoder without it."""+ from transformers import Qwen3VLModel++ from invokeai.backend.model_manager.configs.main import Main_Diffusers_QwenImage21_Config+ from invokeai.backend.model_manager.load.model_loaders.qwen_image_2_1 import QwenImage21DiffusersModel+ from invokeai.backend.qwen_image_2_1.text_encoding import has_vision_tower++ encoder = _tiny_encoder()+ config = Main_Diffusers_QwenImage21_Config.model_construct(path=str(tmp_path), repo_variant=None)+ loader = object.__new__(QwenImage21DiffusersModel)+ loader._apply_fp8_layerwise_casting = lambda model, _config, _submodel: model+ monkeypatch.setattr(Qwen3VLModel, "from_pretrained", classmethod(lambda _cls, *_a, **_k: encoder))++ model = loader._load_model(config, SubModelType.TextEncoder)++ assert has_vision_tower(model)+ assert list(model.visual.parameters())diff --git a/tests/backend/qwen_image_2_1/test_text_encoding.py b/tests/backend/qwen_image_2_1/test_text_encoding.pyindex 5797589414..349af91694 100644--- a/tests/backend/qwen_image_2_1/test_text_encoding.py+++ b/tests/backend/qwen_image_2_1/test_text_encoding.py@@ -1,13 +1,20 @@-"""Qwen-Image-2.1's prompt encoding: the template the checkpoint was trained on, read before the final norm."""+"""Qwen-Image-2.1's prompt encoding: the template the checkpoint was trained on, read before the final norm, and+the reference images an edit reads through the vision tower."""++import itertoolsimport pytestimport torch+from PIL import Imagefrom invokeai.backend.qwen3_vl.qwen3_vl_assets import load_bundled_qwen3_vl_tokenizerfrom invokeai.backend.qwen_image_2_1.text_encoding import (SYSTEM_BLOCK,encode_prompt,format_prompt,+ has_vision_tower,+ on_white,+ sequence_length,system_prefix_length,)from tests.backend.model_manager.load.qwen3vl_gguf_fixture import tiny_qwen3vl_config@@ -40,7 +47,7 @@ def test_the_embeddings_are_the_last_layer_before_the_final_norm(text_encoder, tlanguage_model = text_encoder.language_modelhandle = language_model.norm.register_forward_hook(lambda m, args, out: captured.update(pre=args[0], post=out))try:- embeds = encode_prompt(text_encoder, tokenizer, "a fox", torch.device("cpu"))+ embeds, image_slots, grids = encode_prompt(text_encoder, tokenizer, "a fox", torch.device("cpu"))ids = tokenizer([format_prompt("a fox")], return_tensors="pt").input_idstext_encoder(input_ids=ids)finally:@@ -49,3 +56,89 @@ def test_the_embeddings_are_the_last_layer_before_the_final_norm(text_encoder, tdrop = system_prefix_length(tokenizer)torch.testing.assert_close(embeds[0], captured["pre"][0, drop:])assert not torch.allclose(embeds[0], captured["post"][0, drop:])+ assert image_slots is None and grids == ()+++def test_reference_placeholders_lead_the_prompt_as_the_pipeline_writes_them() -> None:+ assert format_prompt("swap them", 2) == (+ SYSTEM_BLOCK + "<|im_start|>user\n<image1><|vision_start|><|image_pad|><|vision_end|> "+ "<image2><|vision_start|><|image_pad|><|vision_end|>swap them<|im_end|>\n<|im_start|>assistant\n"+ )++[email protected](scope="module")+def processor(tokenizer):+ from transformers import Qwen2VLImageProcessor, Qwen3VLProcessor+ from transformers.models.qwen3_vl.video_processing_qwen3_vl import Qwen3VLVideoProcessor++ # The released processor's geometry: 16px patches merged 2x2, so one slot per 32x32 pixels.+ image_processor = Qwen2VLImageProcessor(patch_size=16, merge_size=2, temporal_patch_size=2)+ return Qwen3VLProcessor(+ image_processor=image_processor, tokenizer=tokenizer, video_processor=Qwen3VLVideoProcessor()+ )+++def _runs(mask: torch.Tensor) -> list[int]:+ return [len(list(run)) for is_slot, run in itertools.groupby(mask[0].tolist()) if is_slot]+++def _encode(text_encoder, tokenizer, processor, references):+ return encode_prompt(+ text_encoder, tokenizer, "swap them", torch.device("cpu"), processor=processor, images=references+ )+++def test_each_reference_fills_one_run_of_slots_per_32px_block(text_encoder, tokenizer, processor) -> None:+ references = [Image.new("RGBA", (64, 96), (255, 0, 0, 128)), Image.new("RGB", (128, 64), (0, 255, 0))]+ embeds, image_slots, grids = _encode(text_encoder, tokenizer, processor, references)+ assert image_slots is not None and image_slots.shape == embeds.shape[:2]+ # 64x96 is 3 rows of 2 blocks, 128x64 is 2 rows of 4, in the order given; the system turn is gone from both.+ assert grids == ((3, 2), (2, 4))+ assert _runs(image_slots) == [6, 8]+++def test_the_sequence_length_is_counted_before_encoding(text_encoder, tokenizer, processor) -> None:+ # The working memory is reserved before the encoder runs, from this count; it has to be the length it reads.+ references = [Image.new("RGB", (64, 96)), Image.new("RGB", (128, 64))]+ embeds, _, _ = _encode(text_encoder, tokenizer, processor, references)+ assert sequence_length(tokenizer, "swap them", references) == embeds.shape[1] + system_prefix_length(tokenizer)+++def test_the_vision_tower_reads_the_pixels(text_encoder, tokenizer, processor) -> None:+ red, green = Image.new("RGB", (64, 64), (255, 0, 0)), Image.new("RGB", (64, 64), (0, 255, 0))+ red_embeds, red_slots, _ = _encode(text_encoder, tokenizer, processor, [red])+ green_embeds, _, _ = _encode(text_encoder, tokenizer, processor, [green])+ # Same tokens either way; only the vision tower can tell the two apart.+ assert not torch.allclose(red_embeds[red_slots], green_embeds[red_slots])+++def test_a_transparent_reference_reads_as_its_colors_over_white(text_encoder, tokenizer, processor) -> None:+ # Fully transparent black must read as plain white, not as the black its RGB channels hold.+ transparent = Image.new("RGBA", (64, 64), (0, 0, 0, 0))+ white = Image.new("RGB", (64, 64), (255, 255, 255))+ transparent_embeds, _, _ = _encode(text_encoder, tokenizer, processor, [transparent])+ white_embeds, _, _ = _encode(text_encoder, tokenizer, processor, [white])+ torch.testing.assert_close(transparent_embeds, white_embeds)+++def test_references_need_the_vision_tower(tokenizer, processor) -> None:+ from transformers import Qwen3VLModel++ from invokeai.backend.model_manager.util.qwen3_vl import drop_qwen3vl_visual_tower++ config = tiny_qwen3vl_config()+ config.text_config.vocab_size = len(tokenizer)+ encoder = Qwen3VLModel(config).float().eval()+ # As the standalone loaders leave it: an Identity where the tower was.+ drop_qwen3vl_visual_tower(encoder)+ assert not has_vision_tower(encoder)+ with pytest.raises(ValueError, match="vision tower"):+ encode_prompt(+ encoder, tokenizer, "a", torch.device("cpu"), processor=processor, images=[Image.new("RGB", (32, 32))]+ )+++def test_transparency_is_flattened_over_white_for_the_vision_encoder() -> None:+ half_red = Image.new("RGBA", (1, 1), (255, 0, 0, 128))+ assert on_white(half_red).getpixel((0, 0)) == (255, 127, 127)+ assert on_white(Image.new("RGBA", (1, 1), (0, 0, 0, 0))).getpixel((0, 0)) == (255, 255, 255)diff --git a/tests/backend/qwen_image_2_1/test_vae.py b/tests/backend/qwen_image_2_1/test_vae.pyindex 6927099581..987523b359 100644--- a/tests/backend/qwen_image_2_1/test_vae.py+++ b/tests/backend/qwen_image_2_1/test_vae.py@@ -1,4 +1,5 @@-"""Qwen-Image-2.1's RGBA VAE: how a decode becomes an image, what an encode reads, and the tile geometry it runs in."""+"""Qwen-Image-2.1's RGBA VAE: how a decode becomes an image, what an encode reads -- a canvas image or an edit's+reference -- and the tile geometry it runs in."""import numpy as npimport pytest@@ -11,7 +12,9 @@ from invokeai.backend.qwen_image_2_1.vae import (MIN_TILE_SIZE,SPATIAL_SCALE,choose_tile_size,+ reference_size,to_image,+ to_reference,to_vae_input,working_memory_bytes,)@@ -142,3 +145,34 @@ def test_the_pipelines_vae_loads_at_the_configured_precision_but_never_float16(loader._load_model(SimpleNamespace(path=str(tmp_path)), SubModelType.VAE)assert loaded["dtype"] is expected+++ ("size", "expected"),+ [+ # sqrt(1024^2 * 5/3) = 1322.0 -> 41.3 x 32 -> 1312; 1322.0 / (5/3) = 793.2 -> 24.8 x 32 -> 800.+ ((500, 300), (1312, 800)),+ ((1024, 1024), (1024, 1024)),+ # sqrt(1024^2 * 3) = 1773.6 -> 1760; 591.2 -> 576.+ ((3000, 1000), (1760, 576)),+ ],+)+def test_a_reference_is_read_at_one_megapixel_on_the_32px_grid(+ size: tuple[int, int], expected: tuple[int, int]+) -> None:+ assert reference_size(*size) == expected+++def test_a_reference_encodes_exactly_as_the_pipeline_preprocesses_it() -> None:+ from diffusers.image_processor import VaeImageProcessor++ generator = torch.Generator().manual_seed(0)+ pixels = torch.randint(0, 256, (300, 500, 4), generator=generator, dtype=torch.uint8).numpy()+ image = Image.fromarray(pixels, mode="RGBA")+ width, height = reference_size(*image.size)+ expected = VaeImageProcessor(vae_scale_factor=16, vae_latent_channels=64).preprocess(+ image, width=width, height=height+ )++ # The alpha is the reference's own, unlike a canvas encode.+ assert torch.equal(to_vae_input(to_reference(image), keep_alpha=True), expected.float())