Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(anima): refuse a foreign VAE instead of decoding it into noise
Alexander Eichhorn156d9c4159f0
diff --git a/invokeai/app/invocations/vae/anima_image_to_latents.py b/invokeai/app/invocations/vae/anima_image_to_latents.pyindex 95afea885c..949514ca64 100644--- a/invokeai/app/invocations/vae/anima_image_to_latents.py+++ b/invokeai/app/invocations/vae/anima_image_to_latents.py@@ -11,8 +11,6 @@ For FLUX VAE (AutoEncoder):- Encoding is handled internally by the FLUX VAE"""-from typing import Union-import einopsimport torchfrom diffusers.models.autoencoders import AutoencoderKLWan@@ -29,17 +27,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.flux.modules.autoencoder import AutoEncoder as FluxAutoEncoderfrom invokeai.backend.model_manager.load.load_base import LoadedModelfrom invokeai.backend.stable_diffusion.diffusers_pipeline import image_resized_to_grid_as_tensorfrom invokeai.backend.util.devices import TorchDevicefrom invokeai.backend.util.vae_working_memory import (estimate_vae_working_memory_anima,- estimate_vae_working_memory_flux,)-AnimaVAE = Union[AutoencoderKLWan, FluxAutoEncoder]-@invocation("anima_i2l",@@ -57,57 +51,49 @@ class AnimaImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard):@staticmethoddef vae_encode(vae_info: LoadedModel, image_tensor: torch.Tensor) -> torch.Tensor:- if not isinstance(vae_info.model, (AutoencoderKLWan, FluxAutoEncoder)):+ if not isinstance(vae_info.model, AutoencoderKLWan):+ # The encode side of the same mismatch the decode refuses: a FLUX VAE has Anima's+ # channel count and compression but a different basis, so it would hand the denoiser+ # a latent that means something else. See `anima_latents_to_image` for the measurement.raise TypeError(- f"Expected AutoencoderKLWan or FluxAutoEncoder for Anima VAE, got {type(vae_info.model).__name__}."- )-- if isinstance(vae_info.model, AutoencoderKLWan):- estimated_working_memory = estimate_vae_working_memory_anima(- operation="encode",- image_tensor=image_tensor,- vae=vae_info.model,- tile_size=None,- )- else:- estimated_working_memory = estimate_vae_working_memory_flux(- operation="encode",- image_tensor=image_tensor,- vae=vae_info.model,+ "Anima encodes into the 16-channel Wan 2.1 latent space, and "+ f"{type(vae_info.model).__name__} is not that encoder. Choose a VAE registered "+ "under 'anima', 'qwen-image', or a 16-channel 'wan' VAE.")+ estimated_working_memory = estimate_vae_working_memory_anima(+ operation="encode",+ image_tensor=image_tensor,+ vae=vae_info.model,+ tile_size=None,+ )with vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):- if not isinstance(vae, (AutoencoderKLWan, FluxAutoEncoder)):- raise TypeError(f"Expected AutoencoderKLWan or FluxAutoEncoder, got {type(vae).__name__}.")+ if not isinstance(vae, AutoencoderKLWan):+ raise TypeError(f"Expected AutoencoderKLWan, got {type(vae).__name__}.")vae_dtype = next(iter(vae.parameters())).dtypeimage_tensor = image_tensor.to(device=TorchDevice.choose_torch_device(), dtype=vae_dtype)with torch.inference_mode():- if isinstance(vae, FluxAutoEncoder):- # FLUX VAE handles scaling internally- generator = torch.Generator(device=TorchDevice.choose_torch_device()).manual_seed(0)- latents = vae.encode(image_tensor, sample=True, generator=generator)- else:- # The cached VAE instance is shared with the decode invocation, which- # may have enabled tiling — encode untiled for exactness.- vae.disable_tiling()- # AutoencoderKLWan expects 5D input [B, C, T, H, W]- if image_tensor.ndim == 4:- image_tensor = image_tensor.unsqueeze(2) # [B, C, H, W] -> [B, C, 1, H, W]-- encoded = vae.encode(image_tensor, return_dict=False)[0]- latents = encoded.sample().to(dtype=vae_dtype)-- # Normalize to denoiser space: (latents - mean) / std- # This is the inverse of the denormalization in anima_latents_to_image.py- latents_mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1).to(latents)- latents_std = torch.tensor(vae.config.latents_std).view(1, -1, 1, 1, 1).to(latents)- latents = (latents - latents_mean) / latents_std-- # Remove temporal dimension: [B, C, 1, H, W] -> [B, C, H, W]- if latents.ndim == 5:- latents = latents.squeeze(2)+ # The cached VAE instance is shared with the decode invocation, which+ # may have enabled tiling — encode untiled for exactness.+ vae.disable_tiling()+ # AutoencoderKLWan expects 5D input [B, C, T, H, W]+ if image_tensor.ndim == 4:+ image_tensor = image_tensor.unsqueeze(2) # [B, C, H, W] -> [B, C, 1, H, W]++ encoded = vae.encode(image_tensor, return_dict=False)[0]+ latents = encoded.sample().to(dtype=vae_dtype)++ # Normalize to denoiser space: (latents - mean) / std+ # This is the inverse of the denormalization in anima_latents_to_image.py+ latents_mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1).to(latents)+ latents_std = torch.tensor(vae.config.latents_std).view(1, -1, 1, 1, 1).to(latents)+ latents = (latents - latents_mean) / latents_std++ # Remove temporal dimension: [B, C, 1, H, W] -> [B, C, H, W]+ if latents.ndim == 5:+ latents = latents.squeeze(2)return latents@@ -120,10 +106,8 @@ class AnimaImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard):image_tensor = einops.rearrange(image_tensor, "c h w -> 1 c h w")vae_info = context.models.load(self.vae.vae)- if not isinstance(vae_info.model, (AutoencoderKLWan, FluxAutoEncoder)):- raise TypeError(- f"Expected AutoencoderKLWan or FluxAutoEncoder for Anima VAE, got {type(vae_info.model).__name__}."- )+ if not isinstance(vae_info.model, AutoencoderKLWan):+ raise TypeError(f"Expected AutoencoderKLWan for Anima VAE, got {type(vae_info.model).__name__}.")context.util.signal_progress("Running Anima VAE encode")latents = self.vae_encode(vae_info=vae_info, image_tensor=image_tensor)diff --git a/invokeai/app/invocations/vae/anima_latents_to_image.py b/invokeai/app/invocations/vae/anima_latents_to_image.pyindex 7ef766f048..90c19bff32 100644--- a/invokeai/app/invocations/vae/anima_latents_to_image.py+++ b/invokeai/app/invocations/vae/anima_latents_to_image.py@@ -27,7 +27,6 @@ from invokeai.app.invocations.fields import (from invokeai.app.invocations.model import VAEFieldfrom invokeai.app.invocations.primitives import ImageOutputfrom invokeai.app.services.shared.invocation_context import InvocationContext-from invokeai.backend.flux.modules.autoencoder import AutoEncoder as FluxAutoEncoderfrom invokeai.backend.krea2.vae_compat import patch_qwen_image_vae_tilingfrom invokeai.backend.util.devices import TorchDevicefrom invokeai.backend.util.oom import is_oom_error@@ -39,7 +38,6 @@ from invokeai.backend.util.vae_decode_diagnostics import ()from invokeai.backend.util.vae_working_memory import (estimate_vae_working_memory_anima,- estimate_vae_working_memory_flux,)# Tile geometry for tiled Wan VAE decode. 512px tiles with a 384px stride (128px blended@@ -93,37 +91,39 @@ class AnimaLatentsToImageInvocation(BaseInvocation, WithMetadata, WithBoard):latents = context.tensors.load(self.latents.latents_name)vae_info = context.models.load(self.vae.vae)- if not isinstance(vae_info.model, (AutoencoderKLWan, FluxAutoEncoder)):+ if not isinstance(vae_info.model, AutoencoderKLWan):+ # A FLUX VAE reaches here with the right shape -- 16 channels at 8x, same as Anima --+ # and decodes without raising, which is why it was offered for a while. It is a+ # different basis: measured against the correct decode of the same latent, 8.67 dB+ # PSNR, a magenta moire in place of the subject, and the run still reports success.+ # Refuse until someone implements the change of basis; a wrong image is worse than+ # none, because nothing downstream can tell it apart from an intended one.raise TypeError(- f"Expected AutoencoderKLWan or FluxAutoEncoder for Anima VAE, got {type(vae_info.model).__name__}."+ "Anima decodes in the 16-channel Wan 2.1 latent space, and "+ f"{type(vae_info.model).__name__} is not that decoder. Choose a VAE registered under "+ "'anima', 'qwen-image', or a 16-channel 'wan' VAE -- all three are the same "+ "194-tensor checkpoint. A FLUX VAE has the same channel count but a different "+ "basis, and converting between them is not implemented.")- use_tiling = False- if isinstance(vae_info.model, AutoencoderKLWan):- full_decode_working_memory = estimate_vae_working_memory_anima(- operation="decode",- image_tensor=latents,- vae=vae_info.model,- tile_size=None,- )- use_tiling = self._use_tiled_decode(TorchDevice.choose_torch_device(), full_decode_working_memory)- estimated_working_memory = estimate_vae_working_memory_anima(- operation="decode",- image_tensor=latents,- vae=vae_info.model,- tile_size=ANIMA_VAE_TILE_SIZE if use_tiling else None,- )- else:- estimated_working_memory = estimate_vae_working_memory_flux(- operation="decode",- image_tensor=latents,- vae=vae_info.model,- )+ full_decode_working_memory = estimate_vae_working_memory_anima(+ operation="decode",+ image_tensor=latents,+ vae=vae_info.model,+ tile_size=None,+ )+ use_tiling = self._use_tiled_decode(TorchDevice.choose_torch_device(), full_decode_working_memory)+ estimated_working_memory = estimate_vae_working_memory_anima(+ operation="decode",+ image_tensor=latents,+ vae=vae_info.model,+ tile_size=ANIMA_VAE_TILE_SIZE if use_tiling else None,+ )with vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):context.util.signal_progress("Running Anima VAE decode")- if not isinstance(vae, (AutoencoderKLWan, FluxAutoEncoder)):- raise TypeError(f"Expected AutoencoderKLWan or FluxAutoEncoder, got {type(vae).__name__}.")+ if not isinstance(vae, AutoencoderKLWan):+ raise TypeError(f"Expected AutoencoderKLWan, got {type(vae).__name__}.")vae_dtype = next(iter(vae.parameters())).dtype# Use the VAE's intended compute device (CUDA/MPS, or CPU if configured cpu_only). Do NOT infer it from@@ -134,64 +134,60 @@ class AnimaLatentsToImageInvocation(BaseInvocation, WithMetadata, WithBoard):TorchDevice.empty_cache()with torch.inference_mode():- if isinstance(vae, FluxAutoEncoder):- # FLUX VAE handles scaling internally, expects 4D [B, C, H, W]- img = vae.decode(latents)- else:- # Expects 5D latents [B, C, T, H, W]- if latents.ndim == 4:- latents = latents.unsqueeze(2) # [B, C, H, W] -> [B, C, 1, H, W]-- # Denormalize from denoiser space to raw VAE space- # (same as diffusers WanPipeline and ComfyUI Wan21.process_out)- latents_mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1).to(latents)- latents_std = torch.tensor(vae.config.latents_std).view(1, -1, 1, 1, 1).to(latents)- latents = latents * latents_std + latents_mean- # Cheap (latents are small); the black-image triage below needs it to tell- # upstream corruption from decode-side corruption.- latents_finite = bool(torch.isfinite(latents).all())-- def decode_once() -> torch.Tensor:- out = vae.decode(latents, return_dict=False)[0]- if not bool(torch.isfinite(out).all()):- # NaN survives clamp(-1, 1) and quantizes to 0: without this, the- # failure renders as a silent black image. Diagnose and try to recover.- out = self._recover_nonfinite_decode(context, vae, latents, out, latents_finite)- return out-- # The cached VAE instance is shared across invocations and with the Qwen-Image- # nodes, so the tile geometry is scoped: `enable_tiling` writes it onto the- # module and `disable_tiling` restores only the flag, never the sizes.- try:- with patch_qwen_image_vae_tiling(vae, ANIMA_VAE_TILE_SIZE if use_tiling else None):- decoded = decode_once()- except RuntimeError as e:- if use_tiling or not is_oom_error(e):- raise- # The working-memory estimate was insufficient on this system;- # retry once with tiling, which caps the peak allocation.- context.util.signal_progress("VAE decode ran out of memory, retrying tiled")- context.logger.warning(- "VAE decode ran out of memory; retrying with tiling. The tiled result is not identical to an "- "untiled decode -- the decoder's normalisation and attention are global, so the difference is "- "spread over the image rather than confined to the seams."- )- # Drop the failed attempt's traceback before retrying. It pins that- # decode's frames, and their locals hold the full-resolution- # activations -- exception/traceback/frame is a reference cycle rooted- # on the stack, so `empty_cache()` frees nothing while `e` is bound and- # the retry has to fit on top of it. Measured at 1536px: 1.4 GiB held,- # and a retry that fails with it and succeeds without. Same reasoning as- # `ModelConfigFactory._detach_traceback`.- e.__traceback__ = None- TorchDevice.empty_cache()- with patch_qwen_image_vae_tiling(vae, ANIMA_VAE_TILE_SIZE):- decoded = decode_once()-- # Output is 5D [B, C, T, H, W] — squeeze temporal dim- if decoded.ndim == 5:- decoded = decoded.squeeze(2)- img = decoded+ # Expects 5D latents [B, C, T, H, W]+ if latents.ndim == 4:+ latents = latents.unsqueeze(2) # [B, C, H, W] -> [B, C, 1, H, W]++ # Denormalize from denoiser space to raw VAE space+ # (same as diffusers WanPipeline and ComfyUI Wan21.process_out)+ latents_mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1).to(latents)+ latents_std = torch.tensor(vae.config.latents_std).view(1, -1, 1, 1, 1).to(latents)+ latents = latents * latents_std + latents_mean+ # Cheap (latents are small); the black-image triage below needs it to tell+ # upstream corruption from decode-side corruption.+ latents_finite = bool(torch.isfinite(latents).all())++ def decode_once() -> torch.Tensor:+ out = vae.decode(latents, return_dict=False)[0]+ if not bool(torch.isfinite(out).all()):+ # NaN survives clamp(-1, 1) and quantizes to 0: without this, the+ # failure renders as a silent black image. Diagnose and try to recover.+ out = self._recover_nonfinite_decode(context, vae, latents, out, latents_finite)+ return out++ # The cached VAE instance is shared across invocations and with the Qwen-Image+ # nodes, so the tile geometry is scoped: `enable_tiling` writes it onto the+ # module and `disable_tiling` restores only the flag, never the sizes.+ try:+ with patch_qwen_image_vae_tiling(vae, ANIMA_VAE_TILE_SIZE if use_tiling else None):+ decoded = decode_once()+ except RuntimeError as e:+ if use_tiling or not is_oom_error(e):+ raise+ # The working-memory estimate was insufficient on this system;+ # retry once with tiling, which caps the peak allocation.+ context.util.signal_progress("VAE decode ran out of memory, retrying tiled")+ context.logger.warning(+ "VAE decode ran out of memory; retrying with tiling. The tiled result is not identical to an "+ "untiled decode -- the decoder's normalisation and attention are global, so the difference is "+ "spread over the image rather than confined to the seams."+ )+ # Drop the failed attempt's traceback before retrying. It pins that+ # decode's frames, and their locals hold the full-resolution+ # activations -- exception/traceback/frame is a reference cycle rooted+ # on the stack, so `empty_cache()` frees nothing while `e` is bound and+ # the retry has to fit on top of it. Measured at 1536px: 1.4 GiB held,+ # and a retry that fails with it and succeeds without. Same reasoning as+ # `ModelConfigFactory._detach_traceback`.+ e.__traceback__ = None+ TorchDevice.empty_cache()+ with patch_qwen_image_vae_tiling(vae, ANIMA_VAE_TILE_SIZE):+ decoded = decode_once()++ # Output is 5D [B, C, T, H, W] — squeeze temporal dim+ if decoded.ndim == 5:+ decoded = decoded.squeeze(2)+ img = decodedimg = img.clamp(-1, 1)img = rearrange(img[0], "c h w -> h w c")diff --git a/tests/app/invocations/test_anima_vae.py b/tests/app/invocations/test_anima_vae.pyindex e08b7b7da0..59234e57d1 100644--- a/tests/app/invocations/test_anima_vae.py+++ b/tests/app/invocations/test_anima_vae.py@@ -122,6 +122,48 @@ def _build_l2i_invocation() -> AnimaLatentsToImageInvocation:)+class TestAnimaRefusesAForeignDecoder:+ """A FLUX VAE has Anima's channel count and compression but a different basis.++ It used to be accepted -- the node had a `FluxAutoEncoder` branch, the picker offered it, and+ the decode returned a magenta moire with the subject barely visible while the run reported+ success. Measured against the correct decode of the same latent: 8.67 dB PSNR, mean absolute+ error 84 of 255. Nothing downstream can tell such an image from an intended one, so the node+ refuses instead of guessing."""++ def test_a_flux_vae_is_refused_before_any_decode(self):+ from invokeai.backend.flux.modules.autoencoder import AutoEncoder as FluxAutoEncoder++ vae_info = MagicMock()+ vae_info.model = MagicMock(spec=FluxAutoEncoder)+ context = MagicMock()+ context.models.load.return_value = vae_info+ context.tensors.load.return_value = torch.zeros(1, 16, 64, 64)++ with pytest.raises(TypeError, match="16-channel Wan 2.1 latent space"):+ _build_l2i_invocation().invoke(context)++ # Refused before the model is placed on a device: a wrong decode costs VRAM and time+ # before it produces the wrong image.+ vae_info.model_on_device.assert_not_called()++ def test_the_message_names_what_to_choose_instead(self):+ from invokeai.backend.flux.modules.autoencoder import AutoEncoder as FluxAutoEncoder++ vae_info = MagicMock()+ vae_info.model = MagicMock(spec=FluxAutoEncoder)+ context = MagicMock()+ context.models.load.return_value = vae_info+ context.tensors.load.return_value = torch.zeros(1, 16, 64, 64)++ with pytest.raises(TypeError) as excinfo:+ _build_l2i_invocation().invoke(context)++ message = str(excinfo.value)+ for base in ("'anima'", "'qwen-image'", "'wan'"):+ assert base in message, message++class TestAnimaLatentsToImageOomFallback:@pytest.mark.parametrize("oom_error",