Skip to content

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.

fix(model_manager): detect Flux1/2 VAE by latent space dimensions instead of filename (#8790)

Alexander Eichhorn0ae7392c810d
diff --git a/invokeai/backend/model_manager/configs/vae.py b/invokeai/backend/model_manager/configs/vae.py
index 00c5783284..cc079cb9aa 100644
--- a/invokeai/backend/model_manager/configs/vae.py
+++ b/invokeai/backend/model_manager/configs/vae.py
@@ -36,13 +36,20 @@ REGEX_TO_BASE: dict[str, BaseModelType] = {
def _is_flux2_vae(state_dict: dict[str | int, Any]) -> bool:
"""Check if state dict is a FLUX.2 VAE (AutoencoderKLFlux2).
- FLUX.2 VAE has a Batch Normalization layer (bn.running_mean, bn.running_var)
- which is unique to FLUX.2 and not present in FLUX.1 VAE.
+ FLUX.2 VAE can be identified by:
+ 1. Batch Normalization layers (bn.running_mean, bn.running_var) - unique to FLUX.2
+ 2. 32-dimensional latent space (decoder.conv_in has 32 input channels)
+
+ FLUX.1 VAE has 16-dimensional latent space and no BatchNorm layers.
"""
# Check for BN layer which is unique to FLUX.2 VAE
- # Note: We cannot use encoder.conv_out channel count because both FLUX.1 and FLUX.2
- # have 32 output channels (FLUX.1: 16 latent × 2, FLUX.2: 32 latent × 2 but patchified)
- return "bn.running_mean" in state_dict or "bn.running_var" in state_dict
+ has_bn = "bn.running_mean" in state_dict or "bn.running_var" in state_dict
+
+ # Check for 32-channel latent space (FLUX.2 has 32, FLUX.1 has 16)
+ decoder_conv_in_key = "decoder.conv_in.weight"
+ has_32_latent_channels = decoder_conv_in_key in state_dict and state_dict[decoder_conv_in_key].shape[1] == 32
+
+ return has_bn or has_32_latent_channels
class VAE_Checkpoint_Config_Base(Checkpoint_Config_Base):
@@ -89,7 +96,24 @@ class VAE_Checkpoint_Config_Base(Checkpoint_Config_Base):
@classmethod
def _get_base_or_raise(cls, mod: ModelOnDisk) -> BaseModelType:
- # Heuristic: VAEs of all architectures have a similar structure; the best we can do is guess based on name
+ # First, try to identify by latent space dimensions (most reliable)
+ state_dict = mod.load_state_dict()
+ decoder_conv_in_key = "decoder.conv_in.weight"
+ if decoder_conv_in_key in state_dict:
+ latent_channels = state_dict[decoder_conv_in_key].shape[1]
+ if latent_channels == 16:
+ # Flux1 VAE has 16-dimensional latent space
+ return BaseModelType.Flux
+ elif latent_channels == 4:
+ # SD/SDXL VAE has 4-dimensional latent space
+ # Try to distinguish SD1/SD2/SDXL by name, fallback to SD1
+ for regexp, base in REGEX_TO_BASE.items():
+ if re.search(regexp, mod.path.name, re.IGNORECASE):
+ return base
+ # Default to SD1 if we can't determine from name
+ return BaseModelType.StableDiffusion1
+
+ # Fallback: guess based on name
for regexp, base in REGEX_TO_BASE.items():
if re.search(regexp, mod.path.name, re.IGNORECASE):
return base