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
"""Loader for Tiny AutoEncoder preview decoders (madebyollin's TAESD and TAEHV families)."""
from pathlib import Path
from typing import Any, Optional
import torch
from safetensors.torch import load_file as safetensors_load_file
from invokeai.backend.model_manager.configs.factory import AnyModelConfig
from invokeai.backend.model_manager.configs.tiny_autoencoder import TinyAutoencoder_Checkpoint_Config_Base
from invokeai.backend.model_manager.load.load_default import ModelLoader
from invokeai.backend.model_manager.load.model_loader_registry import ModelLoaderRegistry
from invokeai.backend.model_manager.model_on_disk import unwrap_state_dict
from invokeai.backend.model_manager.taxonomy import AnyModel, BaseModelType, ModelFormat, ModelType, SubModelType
from invokeai.backend.tiny_autoencoder.checkpoint import (
PICKLE_SUFFIXES,
SAFETENSORS_SUFFIX,
build_tiny_autoencoder_decoder,
detect_tiny_autoencoder_layout,
tiny_autoencoder_state_dict,
)
def _read_checkpoint(path: Path) -> dict[str, Any]:
if path.suffix.lower() == SAFETENSORS_SUFFIX:
return safetensors_load_file(path)
if path.suffix.lower() in PICKLE_SUFFIXES:
return unwrap_state_dict(torch.load(path, map_location="cpu", weights_only=True))
raise ValueError(f"Unrecognised Tiny AutoEncoder checkpoint extension: {path.suffix!r}")
@ModelLoaderRegistry.register(
base=BaseModelType.StableDiffusion1, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint
)
@ModelLoaderRegistry.register(
base=BaseModelType.StableDiffusionXL, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint
)
@ModelLoaderRegistry.register(
base=BaseModelType.StableDiffusion3, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint
)
@ModelLoaderRegistry.register(base=BaseModelType.Flux, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint)
@ModelLoaderRegistry.register(base=BaseModelType.Flux2, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint)
@ModelLoaderRegistry.register(
base=BaseModelType.QwenImage21, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint
)
@ModelLoaderRegistry.register(base=BaseModelType.Wan, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint)
@ModelLoaderRegistry.register(base=BaseModelType.LTX2, type=ModelType.TinyAutoencoder, format=ModelFormat.Checkpoint)
class TinyAutoencoderLoader(ModelLoader):
"""Builds the TAE decoder a checkpoint holds and loads it strictly, dropping any encoder."""
def _load_model(self, config: AnyModelConfig, submodel_type: Optional[SubModelType] = None) -> AnyModel:
if submodel_type is not None:
raise ValueError("Unexpected submodel requested for a Tiny AutoEncoder.")
if not isinstance(config, TinyAutoencoder_Checkpoint_Config_Base):
raise ValueError(f"Unexpected model config type: {type(config)}.")
checkpoint = _read_checkpoint(Path(config.path))
layout = detect_tiny_autoencoder_layout(checkpoint)
if layout is None:
raise ValueError(f"'{config.name}' is not a supported Tiny AutoEncoder checkpoint.")
decoder = build_tiny_autoencoder_decoder(layout)
decoder.load_state_dict(tiny_autoencoder_state_dict(checkpoint, layout), strict=True)
# float32 for CPU-only decoders, the policy of CPU-only text LLMs (`text_llm.py`): CPU float16 is emulated
# and slow, CPU bfloat16 is fast only with native support (about 4.7x slower than float32 without it), and
# torch's CPU bfloat16 kernels fault with an illegal instruction on some CPUs.
dtype = torch.float32 if config.cpu_only is True else self._torch_dtype
return decoder.to(dtype).eval().requires_grad_(False)