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): add a tiling option to the image-to-latents node
Alexander Eichhornb2a31dff335d
diff --git a/invokeai/app/invocations/qwen_image_image_to_latents.py b/invokeai/app/invocations/qwen_image_image_to_latents.pyindex f56b0695dc..785fb8e73c 100644--- a/invokeai/app/invocations/qwen_image_image_to_latents.py+++ b/invokeai/app/invocations/qwen_image_image_to_latents.py@@ -26,7 +26,7 @@ from invokeai.backend.util.vae_working_memory import estimate_vae_working_memorytitle="Image to Latents - Qwen Image",tags=["image", "latents", "vae", "i2l", "qwen_image"],category="image",- version="1.0.0",+ version="1.1.0",classification=Classification.Prototype,)class QwenImageImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard):@@ -34,6 +34,10 @@ class QwenImageImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard)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)+ # NOTE: tile_size = 0 is a special value meaning "use the model's default", matching the+ # SD/SDXL i2l node. `int | None` is avoided because the workflow UI does not handle it well.+ tile_size: int = InputField(default=0, multiple_of=8, description=FieldDescriptions.vae_tile_size)width: int | None = InputField(default=None,description="Resize the image to this width before encoding. If not set, encodes at the image's original size.",@@ -44,21 +48,39 @@ class QwenImageImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard))@staticmethod- def vae_encode(vae_info: LoadedModel, image_tensor: torch.Tensor) -> torch.Tensor:+ def vae_encode(+ vae_info: LoadedModel, image_tensor: torch.Tensor, tiled: bool = False, tile_size: int = 0+ ) -> torch.Tensor:# NOTE: vae_info.model may be an AutoencoderKLWan (a native-layout qwen_image_vae single file is# classified with the Anima base); it is reinterpreted as AutoencoderKLQwenImage inside the# model_on_device context below. The working-memory estimate only reads tensor shape + element# size, so it is safe to run on either class here.+ # Resolve tile_size=0 ("model default") before estimating, so the reserved working memory+ # matches the tiles the VAE will actually use.+ effective_tile_size = None+ if tiled:+ effective_tile_size = tile_size if tile_size > 0 else getattr(vae_info.model, "tile_sample_min_height", 256)+estimated_working_memory = estimate_vae_working_memory_qwen_image(operation="encode",image_tensor=image_tensor,vae=vae_info.model,+ tile_size=effective_tile_size,)with vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):# Reinterpret an Anima-classified Wan VAE as AutoencoderKLQwenImage (identical weights).vae = as_qwen_image_vae(vae)- vae.disable_tiling()+ # Tiling bounds the encode's peak memory to a single tile, which is what makes large+ # inputs (e.g. a 2560x1440 upscale round-trip) encodable while a multi-GB transformer+ # is still resident. Off by default: full-frame is faster and avoids tile blending.+ if tiled:+ if tile_size > 0:+ vae.enable_tiling(tile_sample_min_height=tile_size, tile_sample_min_width=tile_size)+ else:+ vae.enable_tiling()+ else:+ vae.disable_tiling()image_tensor = image_tensor.to(device=TorchDevice.choose_torch_device(), dtype=vae.dtype)with torch.inference_mode():@@ -102,7 +124,12 @@ class QwenImageImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard)vae_info = context.models.load(self.vae.vae)- latents = self.vae_encode(vae_info=vae_info, image_tensor=image_tensor)+ latents = self.vae_encode(+ vae_info=vae_info,+ image_tensor=image_tensor,+ tiled=self.tiled or context.config.get().force_tiled_decode,+ tile_size=self.tile_size,+ )latents = latents.to("cpu")name = context.tensors.save(tensor=latents)diff --git a/invokeai/backend/util/vae_working_memory.py b/invokeai/backend/util/vae_working_memory.pyindex fbd1c0e128..c256d3e3cd 100644--- a/invokeai/backend/util/vae_working_memory.py+++ b/invokeai/backend/util/vae_working_memory.py@@ -165,14 +165,22 @@ def estimate_vae_working_memory_wan(def estimate_vae_working_memory_qwen_image(- operation: Literal["encode", "decode"], image_tensor: torch.Tensor, vae: AutoencoderKLQwenImage+ operation: Literal["encode", "decode"],+ image_tensor: torch.Tensor,+ vae: AutoencoderKLQwenImage,+ tile_size: int | None = None,) -> int:"""Estimate the working memory required by the invocation in bytes.The Qwen Image VAE is a video-style autoencoder that operates on 5D tensors of shape- (B, C, num_frames, H, W). Tiling is not used, so peak working memory scales with the full- spatial output. The two trailing dimensions are the spatial H/W in latent space (decode) or- pixel space (encode), matching the convention used by the other estimators here.+ (B, C, num_frames, H, W). The two trailing dimensions are the spatial H/W in latent space+ (decode) or pixel space (encode), matching the convention used by the other estimators here.++ Without tiling, peak working memory scales with the full spatial extent. With tiling it is+ bounded by a single tile instead, so the estimate must follow suit — otherwise the cache keeps+ reserving the full-frame figure (~11.8 GB for a 2560x1440 encode on CUDA) and tiling buys+ nothing. Mirrors ``estimate_vae_working_memory_wan``: one tile plus 25% for the tile overlap,+ plus the full RGB image, which stays resident on the execution device either way."""latent_scale_factor_for_operation = LATENT_SCALE_FACTOR if operation == "decode" else 1@@ -210,7 +218,13 @@ def estimate_vae_working_memory_qwen_image(else: # encodescaling_constant = 6300 if is_rocm else 1600- working_memory = h * w * element_size * scaling_constant+ if tile_size is not None and tile_size > 0:+ # Bounded by one tile (plus overlap) rather than the full frame.+ working_memory = tile_size * tile_size * element_size * scaling_constant * 1.25+ # The full RGB image is the encode input / decode output and stays resident regardless.+ working_memory += 3 * h * w * element_size+ else:+ working_memory = h * w * element_size * scaling_constantreturn int(working_memory)diff --git a/invokeai/frontend/web/openapi.json b/invokeai/frontend/web/openapi.jsonindex add72e7ac5..3443471415 100644--- a/invokeai/frontend/web/openapi.json+++ b/invokeai/frontend/web/openapi.json@@ -71710,6 +71710,27 @@"input": "connection","orig_required": true},+ "tiled": {+ "default": false,+ "description": "Processing using overlapping tiles (reduce memory consumption)",+ "field_kind": "input",+ "input": "any",+ "orig_default": false,+ "orig_required": false,+ "title": "Tiled",+ "type": "boolean"+ },+ "tile_size": {+ "default": 0,+ "description": "The tile size for VAE tiling in pixels (image space). If set to 0, the default tile size for the model will be used. Larger tile sizes generally produce better results at the cost of higher memory usage.",+ "field_kind": "input",+ "input": "any",+ "multipleOf": 8,+ "orig_default": 0,+ "orig_required": false,+ "title": "Tile Size",+ "type": "integer"+ },"width": {"anyOf": [{@@ -71756,7 +71777,7 @@"tags": ["image", "latents", "vae", "i2l", "qwen_image"],"title": "Image to Latents - Qwen Image","type": "object",- "version": "1.0.0",+ "version": "1.1.0","output": {"$ref": "#/components/schemas/LatentsOutput"}diff --git a/invokeai/frontend/web/src/services/api/schema.ts b/invokeai/frontend/web/src/services/api/schema.tsindex 001aa2233c..a51b010759 100644--- a/invokeai/frontend/web/src/services/api/schema.ts+++ b/invokeai/frontend/web/src/services/api/schema.ts@@ -30307,6 +30307,18 @@ export type components = {* @default null*/vae?: components["schemas"]["VAEField"] | null;+ /**+ * Tiled+ * @description Processing using overlapping tiles (reduce memory consumption)+ * @default false+ */+ tiled?: boolean;+ /**+ * Tile Size+ * @description The tile size for VAE tiling in pixels (image space). If set to 0, the default tile size for the model will be used. Larger tile sizes generally produce better results at the cost of higher memory usage.+ * @default 0+ */+ tile_size?: number;/*** Width* @description Resize the image to this width before encoding. If not set, encodes at the image's original size.