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.

19.5 KB
Raw Blame
"""Class for LoRA model loading in InvokeAI."""

from logging import Logger
from pathlib import Path
from typing import Optional

import torch
from safetensors.torch import load_file

from invokeai.app.services.config import InvokeAIAppConfig
from invokeai.backend.krea2.control_lora import krea2_control_lora_from_state_dict
from invokeai.backend.model_manager.configs.factory import AnyModelConfig
from invokeai.backend.model_manager.load.load_default import ModelLoader
from invokeai.backend.model_manager.load.model_cache.model_cache import ModelCache
from invokeai.backend.model_manager.load.model_loader_registry import ModelLoaderRegistry
from invokeai.backend.model_manager.model_on_disk import ModelOnDisk
from invokeai.backend.model_manager.omi.omi import convert_from_omi
from invokeai.backend.model_manager.taxonomy import (
    AnyModel,
    BaseModelType,
    ModelFormat,
    ModelType,
    SubModelType,
)
from invokeai.backend.patches.lora_conversions.anima_lora_conversion_utils import lora_model_from_anima_state_dict
from invokeai.backend.patches.lora_conversions.dora_magnitude_keys import DORA_MAGNITUDE_SUFFIX_TO_VALUE_KEY
from invokeai.backend.patches.lora_conversions.ernie_image_lora_conversion_utils import (
    lora_model_from_ernie_image_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux3_lora_constants import has_flux3_lora_keys
from invokeai.backend.patches.lora_conversions.flux3_lora_conversion_utils import lora_model_from_flux3_state_dict
from invokeai.backend.patches.lora_conversions.flux_aitoolkit_lora_conversion_utils import (
    is_state_dict_likely_in_flux_aitoolkit_format,
    lora_model_from_flux_aitoolkit_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux_bfl_peft_lora_conversion_utils import (
    is_state_dict_likely_in_flux_bfl_peft_format,
    lora_model_from_flux2_bfl_peft_state_dict,
    lora_model_from_flux_bfl_peft_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux_control_lora_utils import (
    is_state_dict_likely_flux_control,
    lora_model_from_flux_control_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux_diffusers_lora_conversion_utils import (
    is_state_dict_flux2_diffusers_format,
    is_state_dict_likely_in_flux_diffusers_format,
    lora_model_from_flux2_diffusers_state_dict,
    lora_model_from_flux_diffusers_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux_kohya_lora_conversion_utils import (
    is_state_dict_likely_in_flux_kohya_format,
    lora_model_from_flux_kohya_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux_onetrainer_bfl_lora_conversion_utils import (
    is_state_dict_likely_in_flux_onetrainer_bfl_format,
    lora_model_from_flux_onetrainer_bfl_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux_onetrainer_lora_conversion_utils import (
    is_state_dict_likely_in_flux_onetrainer_format,
    lora_model_from_flux_onetrainer_state_dict,
)
from invokeai.backend.patches.lora_conversions.flux_xlabs_lora_conversion_utils import (
    is_state_dict_likely_in_flux_xlabs_format,
    lora_model_from_flux_xlabs_state_dict,
)
from invokeai.backend.patches.lora_conversions.ideogram4_lora_conversion_utils import (
    lora_model_from_ideogram4_state_dict,
)
from invokeai.backend.patches.lora_conversions.krea2_lora_conversion_utils import (
    lora_model_from_krea2_state_dict,
)
from invokeai.backend.patches.lora_conversions.ltx2_lora_conversion_utils import lora_model_from_ltx2_state_dict
from invokeai.backend.patches.lora_conversions.lycoris_flat_lora_constants import lycoris_flat_family
from invokeai.backend.patches.lora_conversions.lycoris_flat_lora_conversion_utils import (
    lora_model_from_lycoris_flat_state_dict,
)
from invokeai.backend.patches.lora_conversions.minimax_h3_lora_conversion_utils import (
    lora_model_from_minimax_h3_state_dict,
)
from invokeai.backend.patches.lora_conversions.moss_image_lora_constants import has_moss_image_lora_keys
from invokeai.backend.patches.lora_conversions.moss_image_lora_conversion_utils import (
    lora_model_from_moss_image_state_dict,
)
from invokeai.backend.patches.lora_conversions.peft_adapter_utils import normalize_peft_adapter_names
from invokeai.backend.patches.lora_conversions.qwen_image_2_1_lora_conversion_utils import (
    lora_model_from_qwen_image_21_state_dict,
)
from invokeai.backend.patches.lora_conversions.qwen_image_lora_conversion_utils import (
    lora_model_from_qwen_image_state_dict,
)
from invokeai.backend.patches.lora_conversions.sd3_lora_conversion_utils import lora_model_from_sd3_state_dict
from invokeai.backend.patches.lora_conversions.sd_lora_conversion_utils import lora_model_from_sd_state_dict
from invokeai.backend.patches.lora_conversions.sdxl_lora_conversion_utils import convert_sdxl_keys_to_diffusers_format
from invokeai.backend.patches.lora_conversions.wan_lora_conversion_utils import lora_model_from_wan_state_dict
from invokeai.backend.patches.lora_conversions.z_image_lora_conversion_utils import lora_model_from_z_image_state_dict

_DORA_MAGNITUDE_SUFFIXES = (*DORA_MAGNITUDE_SUFFIX_TO_VALUE_KEY, ".dora_magnitude")
_FACTORIZED_LAYER_SUFFIXES = (".hada_w1_a", ".lokr_w1", ".lokr_w1_a")


def _layer_prefixes_with_suffixes(state_dict: dict[str, torch.Tensor], suffixes: tuple[str, ...]) -> set[str]:
    prefixes: set[str] = set()
    for key in state_dict:
        for suffix in suffixes:
            if key.endswith(suffix):
                prefixes.add(key[: -len(suffix)])
                break
    return prefixes


def _count_factorized_layers_with_dora_magnitude(state_dict: dict[str, torch.Tensor]) -> int:
    magnitude_layers = _layer_prefixes_with_suffixes(state_dict, _DORA_MAGNITUDE_SUFFIXES)
    factorized_layers = _layer_prefixes_with_suffixes(state_dict, _FACTORIZED_LAYER_SUFFIXES)
    return len(magnitude_layers & factorized_layers)


@ModelLoaderRegistry.register(base=BaseModelType.Flux, type=ModelType.LoRA, format=ModelFormat.OMI)
@ModelLoaderRegistry.register(base=BaseModelType.StableDiffusionXL, type=ModelType.LoRA, format=ModelFormat.OMI)
@ModelLoaderRegistry.register(base=BaseModelType.Any, type=ModelType.LoRA, format=ModelFormat.Diffusers)
@ModelLoaderRegistry.register(base=BaseModelType.Any, type=ModelType.LoRA, format=ModelFormat.LyCORIS)
@ModelLoaderRegistry.register(base=BaseModelType.Flux, type=ModelType.ControlLoRa, format=ModelFormat.LyCORIS)
@ModelLoaderRegistry.register(base=BaseModelType.Flux, type=ModelType.ControlLoRa, format=ModelFormat.Diffusers)
@ModelLoaderRegistry.register(base=BaseModelType.Krea2, type=ModelType.ControlLoRa, format=ModelFormat.LyCORIS)
class LoRALoader(ModelLoader):
    """Class to load LoRA models."""

    # We cheat a little bit to get access to the model base
    def __init__(
        self,
        app_config: InvokeAIAppConfig,
        logger: Logger,
        ram_cache: ModelCache,
    ):
        """Initialize the loader."""
        super().__init__(app_config, logger, ram_cache)
        self._model_base: Optional[BaseModelType] = None

    def _load_model(
        self,
        config: AnyModelConfig,
        submodel_type: Optional[SubModelType] = None,
    ) -> AnyModel:
        if submodel_type is not None:
            raise ValueError("There are no submodels in a LoRA model.")
        model_path = Path(config.path)
        assert self._model_base is not None

        # Load the state dict from the model file.
        if model_path.suffix == ".safetensors":
            state_dict = load_file(model_path.absolute().as_posix(), device="cpu")
        else:
            state_dict = torch.load(model_path, map_location="cpu")

        # Strip 'bundle_emb' keys - these are unused and currently cause downstream errors.
        # To revisit later to determine if they're needed/useful.
        state_dict = {k: v for k, v in state_dict.items() if not k.startswith("bundle_emb")}

        # Normalize PEFT named-adapter keys (e.g. `lora_A.default.weight` → `lora_A.weight`)
        # so the downstream format detectors and converters see canonical PEFT keys.
        state_dict = normalize_peft_adapter_names(state_dict)

        # At the time of writing, we support the OMI standard for base models Flux and SDXL
        if config.format == ModelFormat.OMI and self._model_base in [
            BaseModelType.StableDiffusionXL,
            BaseModelType.Flux,
        ]:
            state_dict = convert_from_omi(state_dict, config.base)  # type: ignore

        factorized_dora_layers = _count_factorized_layers_with_dora_magnitude(state_dict)

        # SimpleTuner's LyCORIS flattens the model's own module names behind `lycoris_` (#10174); map them back and
        # hand each family its own converter. State dicts this layout does not claim (SD 3's flat spelling, which
        # `lora_model_from_sd3_state_dict` resolves already, and every other format) fall through to the dispatch.
        if lycoris_flat_family(state_dict) is not None:
            model = lora_model_from_lycoris_flat_state_dict(state_dict, base=self._model_base)
        # Apply state_dict key conversions, if necessary.
        elif self._model_base == BaseModelType.StableDiffusionXL:
            state_dict = convert_sdxl_keys_to_diffusers_format(state_dict)
            model = lora_model_from_sd_state_dict(state_dict=state_dict)
        elif self._model_base in (BaseModelType.Flux, BaseModelType.Flux2):
            if has_flux3_lora_keys(state_dict):
                # Versions without FLUX.3 LoRA support registered these as FLUX.1 LoRAs, whose conversion would map
                # their layers onto nothing or onto the wrong modules.
                raise ValueError(
                    f"LoRA '{config.name}' is a FLUX.3 LoRA, registered as a {self._model_base.value} LoRA by an earlier "
                    "version of InvokeAI. Use Re-identify model in the Model Manager to register it as a FLUX.3 LoRA."
                )
            if config.format is ModelFormat.OMI:
                # HACK(ryand): We set alpha=None for diffusers PEFT format models. These models are typically
                # distributed as a single file without the associated metadata containing the alpha value. We chose
                # alpha=None, because this is treated as alpha=rank internally in `LoRALayerBase.scale()`. alpha=rank
                # is a popular choice. For example, in the diffusers training scripts:
                # https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/train_dreambooth_lora_flux.py#L1194
                #
                # We assume the same for LyCORIS models in diffusers key format.
                model = lora_model_from_flux_diffusers_state_dict(state_dict=state_dict, alpha=None)
            elif config.format is ModelFormat.LyCORIS:
                if is_state_dict_likely_in_flux_diffusers_format(state_dict=state_dict):
                    if is_state_dict_flux2_diffusers_format(state_dict=state_dict):
                        # Flux2 Klein native diffusers naming (to_qkv_mlp_proj, ff.linear_in, etc.)
                        model = lora_model_from_flux2_diffusers_state_dict(state_dict=state_dict, alpha=None)
                    else:
                        # Flux.1 diffusers naming (to_q/to_k/to_v, ff.net.0.proj, etc.)
                        model = lora_model_from_flux_diffusers_state_dict(state_dict=state_dict, alpha=None)
                elif is_state_dict_likely_in_flux_kohya_format(state_dict=state_dict):
                    model = lora_model_from_flux_kohya_state_dict(state_dict=state_dict)
                elif is_state_dict_likely_in_flux_onetrainer_bfl_format(state_dict=state_dict):
                    model = lora_model_from_flux_onetrainer_bfl_state_dict(state_dict=state_dict)
                elif is_state_dict_likely_in_flux_onetrainer_format(state_dict=state_dict):
                    model = lora_model_from_flux_onetrainer_state_dict(state_dict=state_dict)
                elif is_state_dict_likely_flux_control(state_dict=state_dict):
                    model = lora_model_from_flux_control_state_dict(state_dict=state_dict)
                elif is_state_dict_likely_in_flux_aitoolkit_format(state_dict=state_dict):
                    model = lora_model_from_flux_aitoolkit_state_dict(state_dict=state_dict)
                elif is_state_dict_likely_in_flux_xlabs_format(state_dict=state_dict):
                    model = lora_model_from_flux_xlabs_state_dict(state_dict=state_dict)
                elif is_state_dict_likely_in_flux_bfl_peft_format(state_dict=state_dict):
                    if self._model_base == BaseModelType.Flux2:
                        # FLUX.2 Klein uses Flux2Transformer2DModel (diffusers naming),
                        # so we need to convert BFL keys to diffusers naming.
                        model = lora_model_from_flux2_bfl_peft_state_dict(state_dict=state_dict, alpha=None)
                    else:
                        # FLUX.1 uses BFL Flux class, so BFL keys work directly.
                        model = lora_model_from_flux_bfl_peft_state_dict(state_dict=state_dict, alpha=None)
                else:
                    raise ValueError("LoRA model is in unsupported FLUX format")
            else:
                raise ValueError(f"LoRA model is in unsupported FLUX format: {config.format}")
        elif self._model_base == BaseModelType.Flux3:
            # diffusers/PEFT, BFL-native (ai-toolkit, ComfyUI) or raw PEFT keys, mapped onto the diffusers transformer;
            # the header holds a diffusers file's PEFT config, which sets its scale.
            model = lora_model_from_flux3_state_dict(
                state_dict=state_dict, name=config.name, metadata=ModelOnDisk(model_path).metadata()
            )
        elif self._model_base == BaseModelType.MossImage:
            # ai-toolkit, ComfyUI, kohya/LyCORIS, DiffSynth and diffusers/PEFT keys already name the transformer's
            # modules; a fused `proj.qkv` is cut into the `proj.q/k/v` it has. The header holds a diffusers file's
            # PEFT config, which sets its scale.
            model = lora_model_from_moss_image_state_dict(
                state_dict=state_dict, name=config.name, metadata=ModelOnDisk(model_path).metadata()
            )
        elif self._model_base in [BaseModelType.StableDiffusion1, BaseModelType.StableDiffusion2]:
            # Currently, we don't apply any conversions for SD1 and SD2 LoRA models.
            model = lora_model_from_sd_state_dict(state_dict=state_dict)
        elif self._model_base == BaseModelType.ZImage:
            if has_moss_image_lora_keys(state_dict):
                # Versions without MOSS-Image LoRA support registered these as Z-Image LoRAs, whose conversion would
                # map their layers onto nothing.
                raise ValueError(
                    f"LoRA '{config.name}' is a MOSS-Image LoRA, registered as a Z-Image LoRA by an earlier version of "
                    "InvokeAI. Use Re-identify model in the Model Manager to register it as a MOSS-Image LoRA."
                )
            # Z-Image LoRAs use diffusers PEFT format with transformer and/or Qwen3 encoder layers.
            # We set alpha=None to use rank as alpha (common default).
            model = lora_model_from_z_image_state_dict(state_dict=state_dict, alpha=None)
        elif self._model_base == BaseModelType.QwenImage:
            model = lora_model_from_qwen_image_state_dict(state_dict=state_dict, alpha=None)
        elif self._model_base == BaseModelType.QwenImage21:
            # ComfyUI, diffusers/PEFT or Kohya keys; ComfyUI's fused gate_up is split for diffusers.
            model = lora_model_from_qwen_image_21_state_dict(state_dict=state_dict)
        elif self._model_base == BaseModelType.Krea2 and config.type is ModelType.ControlLoRa:
            model = krea2_control_lora_from_state_dict(state_dict)
        elif self._model_base == BaseModelType.Krea2:
            # Krea-2 LoRAs use diffusers PEFT format targeting the Krea2 transformer (and optionally
            # the Qwen3-VL text encoder). alpha=None → alpha=rank (common diffusers default).
            model = lora_model_from_krea2_state_dict(state_dict=state_dict, alpha=None)
        elif self._model_base == BaseModelType.Anima:
            # Anima LoRAs use Kohya-style or diffusers PEFT format targeting Cosmos DiT blocks.
            model = lora_model_from_anima_state_dict(state_dict=state_dict, alpha=None)
        elif self._model_base == BaseModelType.Wan:
            # Wan LoRAs use Kohya / diffusers PEFT / native PEFT formats targeting
            # WanTransformer3DModel attention (attn1/attn2) and FFN blocks.
            model = lora_model_from_wan_state_dict(state_dict=state_dict, alpha=None)
        elif self._model_base == BaseModelType.MiniMaxH3:
            # MiniMax H3 LoRAs use PEFT lora_A/lora_B or kohya-flattened keys in the
            # checkpoint's native single-file layout (fused qkv_proj / SwiGLU fc1, adaln_proj).
            # alpha=None -> a file's own .alpha tensors, else alpha=rank, matching the
            # published Turbo LoRA's "no extra scaling".
            model = lora_model_from_minimax_h3_state_dict(state_dict=state_dict, alpha=None)
        elif self._model_base == BaseModelType.LTX2:
            # LTX-2 LoRAs use PEFT lora_A/lora_B keys in the official Lightricks single-file
            # layout; the conversion renames them onto the transformer's diffusers module paths
            # with the same map the base checkpoint goes through. alpha=None -> alpha=rank, which
            # is what the published distilled accelerator (no .alpha tensors) expects.
            model = lora_model_from_ltx2_state_dict(state_dict=state_dict, alpha=None)
        elif self._model_base == BaseModelType.ErnieImage:
            # ERNIE-Image LoRAs already name the transformer's own (diffusers) modules; only the container
            # prefix is stripped. Without .alpha tensors (ai-toolkit) alpha = rank, i.e. the patch applies unscaled.
            model = lora_model_from_ernie_image_state_dict(state_dict=state_dict)
        elif self._model_base == BaseModelType.Ideogram4:
            # Converted onto the vendored transformer every Ideogram 4 build loads into; diffusers' split q/k/v
            # are fused into its single `attention.qkv`. Without .alpha tensors alpha = rank (unscaled).
            model = lora_model_from_ideogram4_state_dict(state_dict=state_dict)
        elif self._model_base == BaseModelType.StableDiffusion3:
            # diffusers, PEFT, SimpleTuner LyCORIS and kohya MMDiT names, all onto diffusers' SD3Transformer2DModel
            # and the two CLIP encoders; T5 layers are dropped with a warning.
            model = lora_model_from_sd3_state_dict(state_dict=state_dict)
        else:
            raise ValueError(f"Unsupported LoRA base model: {self._model_base}")

        if factorized_dora_layers:
            self._logger.warning(
                f"LoRA '{config.name}' contains {factorized_dora_layers} LoKr/LoHa layers with unsupported DoRA "
                "magnitudes; applying those layers as plain LoKr/LoHa."
            )

        model.to(dtype=self._torch_dtype)
        return model

    def _get_model_path(self, config: AnyModelConfig) -> Path:
        # cheating a little - we remember this variable for using in the subsequent call to _load_model()
        self._model_base = config.base

        model_base_path = self._app_config.models_path
        model_path = model_base_path / config.path

        if config.format == ModelFormat.Diffusers:
            for ext in ["safetensors", "bin"]:  # return path to the safetensors file inside the folder
                path = model_base_path / config.path / f"pytorch_lora_weights.{ext}"
                if path.exists():
                    model_path = path
                    break

        return model_path.resolve()