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.

23.7 KB
Raw Blame
from __future__ import annotations

import math
from dataclasses import dataclass, field
from enum import Enum
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import torch

from invokeai.backend.stable_diffusion.diffusion.regional_prompt_data import RegionalPromptData

if TYPE_CHECKING:
    from invokeai.backend.ip_adapter.ip_adapter import IPAdapter
    from invokeai.backend.stable_diffusion.denoise_context import UNetKwargs


@dataclass
class BasicConditioningInfo:
    """SD 1/2 text conditioning information produced by Compel."""

    embeds: torch.Tensor

    def to(self, device, dtype=None):
        self.embeds = self.embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class SDXLConditioningInfo(BasicConditioningInfo):
    """SDXL text conditioning information produced by Compel."""

    pooled_embeds: torch.Tensor
    add_time_ids: torch.Tensor

    def to(self, device, dtype=None):
        self.pooled_embeds = self.pooled_embeds.to(device=device, dtype=dtype)
        self.add_time_ids = self.add_time_ids.to(device=device, dtype=dtype)
        return super().to(device=device, dtype=dtype)


@dataclass
class FLUXConditioningInfo:
    clip_embeds: torch.Tensor
    t5_embeds: torch.Tensor

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.clip_embeds = self.clip_embeds.to(device=device, dtype=dtype)
        self.t5_embeds = self.t5_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class SD3ConditioningInfo:
    clip_l_pooled_embeds: torch.Tensor
    clip_l_embeds: torch.Tensor
    clip_g_pooled_embeds: torch.Tensor
    clip_g_embeds: torch.Tensor
    t5_embeds: torch.Tensor | None

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.clip_l_pooled_embeds = self.clip_l_pooled_embeds.to(device=device, dtype=dtype)
        self.clip_l_embeds = self.clip_l_embeds.to(device=device, dtype=dtype)
        self.clip_g_pooled_embeds = self.clip_g_pooled_embeds.to(device=device, dtype=dtype)
        self.clip_g_embeds = self.clip_g_embeds.to(device=device, dtype=dtype)
        if self.t5_embeds is not None:
            self.t5_embeds = self.t5_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class CogView4ConditioningInfo:
    glm_embeds: torch.Tensor

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.glm_embeds = self.glm_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class ZImageConditioningInfo:
    """Z-Image text conditioning information from Qwen3 text encoder."""

    prompt_embeds: torch.Tensor
    """Text embeddings from Qwen3 encoder. Shape: (batch_size, seq_len, hidden_size)."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class ErnieImageConditioningInfo:
    """ERNIE-Image text conditioning. Holds the unbatched, unpadded second-to-last
    hidden states of the Mistral3 encoder for a single prompt.
    """

    prompt_embeds: torch.Tensor
    """Encoder hidden states for a single prompt. Shape: (seq_len, hidden_size)."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class Ideogram4ConditioningInfo:
    """Ideogram 4 text conditioning from the Qwen3-VL encoder.

    prompt_embeds is the concatenation of hidden states from 13 Qwen3-VL layers.
    Shape: (seq_len, 53248) where 53248 = 4096 * 13.
    """

    prompt_embeds: torch.Tensor

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class QwenImageConditioningInfo:
    """Qwen Image Edit conditioning information from Qwen2.5-VL encoder."""

    prompt_embeds: torch.Tensor
    """Text/image embeddings from Qwen2.5-VL encoder. Shape: (batch_size, seq_len, hidden_size)."""

    prompt_embeds_mask: torch.Tensor | None = None
    """Attention mask for prompt_embeds. Shape: (batch_size, seq_len). 1 for valid, 0 for padding."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        if self.prompt_embeds_mask is not None:
            self.prompt_embeds_mask = self.prompt_embeds_mask.to(device=device)
        return self


@dataclass
class Krea2ConditioningInfo:
    """Krea-2 text conditioning from the Qwen3-VL encoder.

    Krea-2 taps 12 decoder hidden-state layers and stacks them per token, so prompt_embeds keeps a
    layer axis (the transformer's text-fusion stage consumes it). This is unique vs Z-Image (2D) and
    Qwen-Image (3D).
    """

    prompt_embeds: torch.Tensor
    """Stacked Qwen3-VL hidden states. Shape: (batch_size, seq_len, num_text_layers=12, hidden=2560)."""

    prompt_embeds_mask: torch.Tensor | None = None
    """Attention mask for prompt_embeds. Shape: (batch_size, seq_len). 1/True for valid, 0/False for padding."""

    num_reference_images: int = 0
    """How many reference images the prompt was encoded with (edit mode), as ``Picture 1..N`` in that order.

    0 for a text-only prompt. A denoise that receives reference latents needs as many, in the same order: the
    encoder and the transformer must see the same references, or the LoRA reads one image and edits another.
    """

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        if self.prompt_embeds_mask is not None:
            self.prompt_embeds_mask = self.prompt_embeds_mask.to(device=device)
        return self


@dataclass
class QwenImage21ConditioningInfo:
    """Qwen-Image-2.1 text conditioning from the Qwen3-VL-8B encoder.

    The last decoder layer's output before the final norm, with the system-prompt prefix removed.
    """

    prompt_embeds: torch.Tensor
    """Shape: (1, seq_len, hidden=4096). One prompt, so no padding and no mask."""

    image_pad_mask: torch.Tensor | None = None
    """Shape: (1, seq_len), True at the reference images' `<|image_pad|>` slots; None without references. Each
    slot stands for 2x2 latent tokens of a reference, in the order the references were encoded."""

    reference_grids: tuple[tuple[int, int], ...] = ()
    """Per reference, in order: its slots as (rows, columns), so the denoise node can match each reference's
    latents by shape, not only by count."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        if self.image_pad_mask is not None:
            self.image_pad_mask = self.image_pad_mask.to(device=device)
        return self


@dataclass
class Flux3ConditioningInfo:
    """FLUX.3 text conditioning from the Qwen3-VL 4B encoder.

    Eight decoder hidden-state layers are stacked per token along the channel axis. There is no mask: the
    prompt is right-padded to a multiple of 80 tokens and the FLUX.3 transformer attends to the padding,
    so the padded window is part of the conditioning.
    """

    prompt_embeds: torch.Tensor
    """Stacked Qwen3-VL hidden states. Shape: (batch_size, seq_len, 8 * hidden=20480)."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class AnimaConditioningInfo:
    """Anima text conditioning information from Qwen3 0.6B encoder + T5-XXL tokenizer.

    Anima uses a dual-conditioning scheme where Qwen3 hidden states are combined
    with T5-XXL token IDs inside the LLM Adapter (part of the transformer).
    """

    qwen3_embeds: torch.Tensor
    """Qwen3 0.6B hidden states. Shape: (seq_len, hidden_size) where hidden_size=1024."""

    t5xxl_ids: torch.Tensor
    """T5-XXL token IDs. Shape: (seq_len,)."""

    t5xxl_weights: Optional[torch.Tensor] = None
    """Per-token weights for prompt weighting. Shape: (seq_len,). None means uniform weight."""

    qwen35_states: Optional[torch.Tensor] = None
    """Qwen3.5 4B hidden states for Anima-3.8B's semantic connector, one row per tapped layer (7, 15,
    23, 31). Shape: (num_layers, seq_len, 2560). None when the prompt was encoded without Qwen3.5."""

    qwen35_mask: Optional[torch.Tensor] = None
    """True for valid Qwen3.5 tokens. Shape: (seq_len,). All False for an empty prompt."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.qwen3_embeds = self.qwen3_embeds.to(device=device, dtype=dtype)
        self.t5xxl_ids = self.t5xxl_ids.to(device=device)
        if self.t5xxl_weights is not None:
            self.t5xxl_weights = self.t5xxl_weights.to(device=device, dtype=dtype)
        if self.qwen35_states is not None:
            self.qwen35_states = self.qwen35_states.to(device=device, dtype=dtype)
        if self.qwen35_mask is not None:
            self.qwen35_mask = self.qwen35_mask.to(device=device)
        return self


@dataclass
class MiniMaxH3ConditioningInfo:
    """MiniMax H3 text conditioning from the Qwen3-VL-32B conditioner.

    ``prompt_embeds`` is the *unnormalized* hidden state after the conditioner's 50th decoder
    layer. ``text_token_tags`` is the per-row modality tag of every embedding row (text rows
    tagged 1, keyframe vision-block rows tagged 0 = video); the denoise node builds the packed
    sequence layout from it, so it must travel with the embeddings.
    """

    prompt_embeds: torch.Tensor
    """Qwen3-VL layer-50 hidden states. Shape: (1, num_text_tokens, 5120)."""

    text_token_tags: torch.Tensor
    """Per-row modality tags. Shape: (num_text_tokens,), dtype long."""

    keyframe_anchors: tuple[str, ...] = ()
    """Which keyframes ("first"/"last", packed order) were part of the vision context. The
    denoise node cross-checks this against its frame-conditioning input: keyframes must reach
    the text conditioning and the VAE condition rows together, or not at all."""

    width: int | None = None
    """Canvas width the keyframes were prepared at (None when no keyframes)."""

    height: int | None = None
    """Canvas height the keyframes were prepared at (None when no keyframes)."""

    reference_signature: tuple[str, ...] = ()
    """Per-reference fingerprints (packed order) of a Ref2VA request's vision context. The
    denoise node cross-checks this against its reference-conditioning input: the references
    must reach the text conditioning and the VAE condition rows together, in the same order,
    with the same options."""

    reference_num_frames: int | None = None
    """The generated frame count the references were truncated for (None without references)."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        # Tags are structural (long); only the device moves.
        self.text_token_tags = self.text_token_tags.to(device=device)
        return self


@dataclass
class LTX2ConditioningInfo:
    """LTX-2 text conditioning, already passed through the LTX-2 text connectors.

    The Gemma-4 hidden states are stacked (all layers), normalised and projected per modality by the
    connectors inside the text-encoder invocation, so the denoise node receives the two streams the
    transformer's prompt cross-attention consumes and never touches the connectors itself.
    """

    video_embeds: torch.Tensor
    """Video-branch prompt embeddings. Shape: (1, num_text_tokens, 4096)."""

    audio_embeds: torch.Tensor
    """Audio-branch prompt embeddings. Shape: (1, num_text_tokens, 2048)."""

    attention_mask: torch.Tensor
    """Binary token mask over the connector output (1 = attend). Shape: (1, num_text_tokens)."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.video_embeds = self.video_embeds.to(device=device, dtype=dtype)
        self.audio_embeds = self.audio_embeds.to(device=device, dtype=dtype)
        # The mask is structural; only the device moves.
        self.attention_mask = self.attention_mask.to(device=device)
        return self


@dataclass
class WanConditioningInfo:
    """Wan 2.2 text conditioning information from the UMT5-XXL encoder.

    The Wan transformer takes the encoder's last hidden state directly as
    cross-attention context (``encoder_hidden_states``).
    """

    prompt_embeds: torch.Tensor
    """UMT5-XXL hidden states. Shape: (seq_len, hidden_size) where hidden_size=4096."""

    prompt_attention_mask: torch.Tensor | None = None
    """Attention mask marking valid (non-padding) tokens. Shape: (seq_len,). 1 for valid, 0 for padding."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        if self.prompt_attention_mask is not None:
            self.prompt_attention_mask = self.prompt_attention_mask.to(device=device)
        return self


@dataclass
class MossImageConditioningInfo:
    """MOSS-Image text conditioning: Qwen3.5-9B's last hidden state, after its final norm, for one unpadded prompt."""

    prompt_embeds: torch.Tensor
    """Shape: (seq_len, 4096)."""

    def to(self, device: torch.device | None = None, dtype: torch.dtype | None = None):
        self.prompt_embeds = self.prompt_embeds.to(device=device, dtype=dtype)
        return self


@dataclass
class ConditioningFieldData:
    # If you change this class, adding more types, you _must_ update the instantiation of ObjectSerializerDisk in
    # invokeai/app/api/dependencies.py, adding the types to the list of safe globals. If you do not, torch will be
    # unable to deserialize the object and will raise an error.
    conditionings: (
        List[BasicConditioningInfo]
        | List[SDXLConditioningInfo]
        | List[FLUXConditioningInfo]
        | List[SD3ConditioningInfo]
        | List[CogView4ConditioningInfo]
        | List[ZImageConditioningInfo]
        | List[ErnieImageConditioningInfo]
        | List[Ideogram4ConditioningInfo]
        | List[QwenImageConditioningInfo]
        | List[Krea2ConditioningInfo]
        | List[QwenImage21ConditioningInfo]
        | List[Flux3ConditioningInfo]
        | List[AnimaConditioningInfo]
        | List[WanConditioningInfo]
        | List[MiniMaxH3ConditioningInfo]
        | List[LTX2ConditioningInfo]
        | List[MossImageConditioningInfo]
    )


@dataclass
class IPAdapterConditioningInfo:
    cond_image_prompt_embeds: torch.Tensor
    """IP-Adapter image encoder conditioning embeddings.
    Shape: (num_images, num_tokens, encoding_dim).
    """
    uncond_image_prompt_embeds: torch.Tensor
    """IP-Adapter image encoding embeddings to use for unconditional generation.
    Shape: (num_images, num_tokens, encoding_dim).
    """


@dataclass
class IPAdapterData:
    """Data class for IP-Adapter configuration.

    Attributes:
        ip_adapter_model: The IP-Adapter model to use.
        ip_adapter_conditioning: The IP-Adapter conditioning data.
        mask: The mask to apply to the IP-Adapter conditioning.
        target_blocks: List of target attention block names to apply IP-Adapter to.
        negative_blocks: List of target attention block names that should use negative attention.
        weight: The weight to apply to the IP-Adapter conditioning.
        begin_step_percent: The percentage of steps at which to start applying the IP-Adapter.
        end_step_percent: The percentage of steps at which to stop applying the IP-Adapter.
        method: The method to use for applying the IP-Adapter ('full', 'style', 'composition').
    """

    ip_adapter_model: IPAdapter
    ip_adapter_conditioning: IPAdapterConditioningInfo
    mask: torch.Tensor
    target_blocks: List[str]
    negative_blocks: List[str] = field(default_factory=list)
    weight: Union[float, List[float]] = 1.0
    begin_step_percent: float = 0.0
    end_step_percent: float = 1.0
    method: str = "full"

    def scale_for_step(self, step_index: int, total_steps: int) -> float:
        first_adapter_step = math.floor(self.begin_step_percent * total_steps)
        last_adapter_step = math.ceil(self.end_step_percent * total_steps)
        weight = self.weight[step_index] if isinstance(self.weight, List) else self.weight
        if step_index >= first_adapter_step and step_index <= last_adapter_step:
            # Only apply this IP-Adapter if the current step is within the IP-Adapter's begin/end step range.
            return weight
        # Otherwise, set the IP-Adapter's scale to 0, so it has no effect.
        return 0.0


@dataclass
class Range:
    start: int
    end: int


class TextConditioningRegions:
    def __init__(
        self,
        masks: torch.Tensor,
        ranges: list[Range],
    ):
        # A binary mask indicating the regions of the image that the prompt should be applied to.
        # Shape: (1, num_prompts, height, width)
        # Dtype: torch.bool
        self.masks = masks

        # A list of ranges indicating the start and end indices of the embeddings that corresponding mask applies to.
        # ranges[i] contains the embedding range for the i'th prompt / mask.
        self.ranges = ranges

        assert self.masks.shape[1] == len(self.ranges)


class ConditioningMode(Enum):
    Both = "both"
    Negative = "negative"
    Positive = "positive"


class TextConditioningData:
    def __init__(
        self,
        uncond_text: Union[BasicConditioningInfo, SDXLConditioningInfo],
        cond_text: Union[BasicConditioningInfo, SDXLConditioningInfo],
        uncond_regions: Optional[TextConditioningRegions],
        cond_regions: Optional[TextConditioningRegions],
        guidance_scale: Union[float, List[float]],
        guidance_rescale_multiplier: float = 0,  # TODO: old backend, remove
    ):
        self.uncond_text = uncond_text
        self.cond_text = cond_text
        self.uncond_regions = uncond_regions
        self.cond_regions = cond_regions
        # Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
        # `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf).
        # Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate
        # images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
        self.guidance_scale = guidance_scale
        # TODO: old backend, remove
        # For models trained using zero-terminal SNR ("ztsnr"), it's suggested to use guidance_rescale_multiplier of 0.7.
        # See [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf).
        self.guidance_rescale_multiplier = guidance_rescale_multiplier

    def is_sdxl(self):
        assert isinstance(self.uncond_text, SDXLConditioningInfo) == isinstance(self.cond_text, SDXLConditioningInfo)
        return isinstance(self.cond_text, SDXLConditioningInfo)

    def to_unet_kwargs(self, unet_kwargs: UNetKwargs, conditioning_mode: ConditioningMode):
        """Fills unet arguments with data from provided conditionings.

        Args:
            unet_kwargs (UNetKwargs): Object which stores UNet model arguments.
            conditioning_mode (ConditioningMode): Describes which conditionings should be used.
        """
        _, _, h, w = unet_kwargs.sample.shape
        device = unet_kwargs.sample.device
        dtype = unet_kwargs.sample.dtype

        # TODO: combine regions with conditionings
        if conditioning_mode == ConditioningMode.Both:
            conditionings = [self.uncond_text, self.cond_text]
            c_regions = [self.uncond_regions, self.cond_regions]
        elif conditioning_mode == ConditioningMode.Positive:
            conditionings = [self.cond_text]
            c_regions = [self.cond_regions]
        elif conditioning_mode == ConditioningMode.Negative:
            conditionings = [self.uncond_text]
            c_regions = [self.uncond_regions]
        else:
            raise ValueError(f"Unexpected conditioning mode: {conditioning_mode}")

        encoder_hidden_states, encoder_attention_mask = self._concat_conditionings_for_batch(
            [c.embeds for c in conditionings]
        )

        unet_kwargs.encoder_hidden_states = encoder_hidden_states
        unet_kwargs.encoder_attention_mask = encoder_attention_mask

        if self.is_sdxl():
            added_cond_kwargs = dict(  # noqa: C408
                text_embeds=torch.cat([c.pooled_embeds for c in conditionings]),
                time_ids=torch.cat([c.add_time_ids for c in conditionings]),
            )

            unet_kwargs.added_cond_kwargs = added_cond_kwargs

        if any(r is not None for r in c_regions):
            tmp_regions = []
            for c, r in zip(conditionings, c_regions, strict=True):
                if r is None:
                    r = TextConditioningRegions(
                        masks=torch.ones((1, 1, h, w), dtype=dtype),
                        ranges=[Range(start=0, end=c.embeds.shape[1])],
                    )
                tmp_regions.append(r)

            if unet_kwargs.cross_attention_kwargs is None:
                unet_kwargs.cross_attention_kwargs = {}

            unet_kwargs.cross_attention_kwargs.update(
                regional_prompt_data=RegionalPromptData(regions=tmp_regions, device=device, dtype=dtype),
            )

    @staticmethod
    def _pad_zeros(t: torch.Tensor, pad_shape: tuple, dim: int) -> torch.Tensor:
        return torch.cat([t, torch.zeros(pad_shape, device=t.device, dtype=t.dtype)], dim=dim)

    @classmethod
    def _pad_conditioning(
        cls,
        cond: torch.Tensor,
        target_len: int,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """Pad provided conditioning tensor to target_len by zeros and returns mask of unpadded bytes.

        Args:
            cond (torch.Tensor): Conditioning tensor which to pads by zeros.
            target_len (int): To which length(tokens count) pad tensor.
        """
        conditioning_attention_mask = torch.ones((cond.shape[0], cond.shape[1]), device=cond.device, dtype=cond.dtype)

        if cond.shape[1] < target_len:
            conditioning_attention_mask = cls._pad_zeros(
                conditioning_attention_mask,
                pad_shape=(cond.shape[0], target_len - cond.shape[1]),
                dim=1,
            )

            cond = cls._pad_zeros(
                cond,
                pad_shape=(cond.shape[0], target_len - cond.shape[1], cond.shape[2]),
                dim=1,
            )

        return cond, conditioning_attention_mask

    @classmethod
    def _concat_conditionings_for_batch(
        cls,
        conditionings: List[torch.Tensor],
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
        """Concatenate provided conditioning tensors to one batched tensor.
        If tensors have different sizes then pad them by zeros and creates
        encoder_attention_mask to exclude padding from attention.

        Args:
            conditionings (List[torch.Tensor]): List of conditioning tensors to concatenate.
        """
        encoder_attention_mask = None
        max_len = max([c.shape[1] for c in conditionings])
        if any(c.shape[1] != max_len for c in conditionings):
            encoder_attention_masks = [None] * len(conditionings)
            for i in range(len(conditionings)):
                conditionings[i], encoder_attention_masks[i] = cls._pad_conditioning(conditionings[i], max_len)
            encoder_attention_mask = torch.cat(encoder_attention_masks)

        return torch.cat(conditionings), encoder_attention_mask