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.

7.5 KB
Raw Blame
from typing import Optional

from invokeai.app.invocations.baseinvocation import (
    BaseInvocation,
    BaseInvocationOutput,
    Classification,
    invocation,
    invocation_output,
)
from invokeai.app.invocations.fields import FieldDescriptions, Input, InputField, OutputField
from invokeai.app.invocations.model import (
    ModelIdentifierField,
    Qwen3VLEncoderField,
    TransformerField,
    VAEField,
)
from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.backend.architectures import accepted_vae_bases, accepts_vae
from invokeai.backend.model_manager.qwen3_vl_labels import (
    raise_for_unfit_vision_tower,
    raise_for_wrong_qwen3_vl_variant,
)
from invokeai.backend.model_manager.taxonomy import (
    BaseModelType,
    ModelFormat,
    ModelType,
    Qwen3VLVariantType,
    SubModelType,
)


@invocation_output("krea2_model_loader_output")
class Krea2ModelLoaderOutput(BaseInvocationOutput):
    """Krea-2 base model loader output."""

    transformer: TransformerField = OutputField(description=FieldDescriptions.transformer, title="Transformer")
    qwen3_vl_encoder: Qwen3VLEncoderField = OutputField(
        description=FieldDescriptions.qwen3_vl_encoder, title="Qwen3-VL Encoder"
    )
    vae: VAEField = OutputField(description=FieldDescriptions.vae, title="VAE")


@invocation(
    "krea2_model_loader",
    title="Main Model - Krea-2",
    tags=["model", "krea2", "krea-2"],
    category="model",
    version="1.1.0",
    classification=Classification.Prototype,
)
class Krea2ModelLoaderInvocation(BaseInvocation):
    """Loads a Krea-2 model, outputting its submodels.

    By default the VAE (Qwen-Image VAE) and Qwen3-VL text encoder are extracted from the Krea-2
    diffusers pipeline. Standalone overrides may be supplied (e.g. when the transformer is a
    single-file checkpoint that has no bundled VAE / encoder).
    """

    model: ModelIdentifierField = InputField(
        description=FieldDescriptions.krea2_model,
        input=Input.Direct,
        ui_model_base=BaseModelType.Krea2,
        ui_model_type=ModelType.Main,
        title="Transformer",
    )

    vae_model: Optional[ModelIdentifierField] = InputField(
        default=None,
        description="Standalone VAE model. Krea-2 uses the Qwen-Image VAE (16-channel). "
        "If not provided, the VAE is loaded from the Krea-2 (diffusers) model.",
        input=Input.Direct,
        ui_model_base=accepted_vae_bases(BaseModelType.Krea2),
        ui_model_type=ModelType.VAE,
        title="VAE",
    )

    qwen3_vl_encoder_model: Optional[ModelIdentifierField] = InputField(
        default=None,
        description="Standalone Qwen3-VL Encoder model. "
        "If not provided, the encoder is loaded from the Krea-2 (diffusers) model.",
        input=Input.Direct,
        ui_model_type=ModelType.Qwen3VLEncoder,
        title="Qwen3-VL Encoder",
    )

    qwen3_vl_vision_tower_model: Optional[ModelIdentifierField] = InputField(
        default=None,
        description="Qwen3-VL 4B vision tower (the llama.cpp mmproj file). "
        "Lets a GGUF encoder read edit reference images.",
        input=Input.Direct,
        ui_model_base=BaseModelType.Any,
        ui_model_type=ModelType.Qwen3VLVisionTower,
        title="Vision Tower",
    )

    def invoke(self, context: InvocationContext) -> Krea2ModelLoaderOutput:
        main_config = context.models.get_config(self.model)
        if main_config.base is not BaseModelType.Krea2 or main_config.type is not ModelType.Main:
            raise ValueError(
                f"Model '{main_config.name}' is not a Krea-2 main model. Select a Krea-2 transformer model."
            )

        # Transformer always comes from the main model.
        transformer = self.model.model_copy(update={"submodel_type": SubModelType.Transformer})

        # Determine VAE source.
        if self.vae_model is not None:
            vae_config = context.models.get_config(self.vae_model)
            if vae_config.type is not ModelType.VAE or not accepts_vae(
                BaseModelType.Krea2, vae_config.base, getattr(vae_config, "latent_channels", None)
            ):
                accepted = " or ".join(base.value for base in accepted_vae_bases(BaseModelType.Krea2))
                raise ValueError(
                    f"VAE '{vae_config.name}' is not compatible with Krea-2. Select a VAE of base {accepted}."
                )
            vae = self.vae_model.model_copy(update={"submodel_type": SubModelType.VAE})
        else:
            self._validate_diffusers_format(context, self.model, "Krea-2")
            vae = self.model.model_copy(update={"submodel_type": SubModelType.VAE})

        # Determine Qwen3-VL Encoder source.
        if self.qwen3_vl_encoder_model is not None:
            encoder_config = context.models.get_config(self.qwen3_vl_encoder_model)
            if encoder_config.type is not ModelType.Qwen3VLEncoder:
                raise ValueError(f"Encoder '{encoder_config.name}' is not a Qwen3-VL encoder compatible with Krea-2.")
            # Two Qwen3-VL encoders install under this type and they are not interchangeable: Krea-2
            # conditions on the 4B, Ideogram 4 on the 8B.
            raise_for_wrong_qwen3_vl_variant(
                encoder_config.name, getattr(encoder_config, "variant", None), Qwen3VLVariantType.Qwen3VL_4B, "Krea-2"
            )
            tokenizer = self.qwen3_vl_encoder_model.model_copy(update={"submodel_type": SubModelType.Tokenizer})
            text_encoder = self.qwen3_vl_encoder_model.model_copy(update={"submodel_type": SubModelType.TextEncoder})
        else:
            self._validate_diffusers_format(context, self.model, "Krea-2")
            tokenizer = self.model.model_copy(update={"submodel_type": SubModelType.Tokenizer})
            text_encoder = self.model.model_copy(update={"submodel_type": SubModelType.TextEncoder})

        vision_tower = self._vision_tower(context)

        return Krea2ModelLoaderOutput(
            transformer=TransformerField(transformer=transformer, loras=[]),
            qwen3_vl_encoder=Qwen3VLEncoderField(
                tokenizer=tokenizer, text_encoder=text_encoder, vision_tower=vision_tower, loras=[]
            ),
            vae=VAEField(vae=vae),
        )

    def _vision_tower(self, context: InvocationContext) -> Optional[ModelIdentifierField]:
        """The mmproj tower for a GGUF encoder, checked against it; None when none was selected."""
        if self.qwen3_vl_vision_tower_model is None:
            return None
        encoder_config = (
            context.models.get_config(self.qwen3_vl_encoder_model) if self.qwen3_vl_encoder_model is not None else None
        )
        raise_for_unfit_vision_tower(
            context.models.get_config(self.qwen3_vl_vision_tower_model),
            encoder_config,
            Qwen3VLVariantType.Qwen3VL_4B,
            "Krea-2",
        )
        return self.qwen3_vl_vision_tower_model

    def _validate_diffusers_format(
        self, context: InvocationContext, model: ModelIdentifierField, model_name: str
    ) -> None:
        """Validate that a model is in Diffusers format (required to extract VAE / encoder submodels)."""
        config = context.models.get_config(model)
        if config.format != ModelFormat.Diffusers:
            raise ValueError(
                f"To extract the VAE and Qwen3-VL encoder, the {model_name} model must be in Diffusers format. "
                f"The selected model '{config.name}' is in {config.format.value} format — provide a standalone "
                "VAE and Qwen3-VL Encoder instead."
            )