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.

2.8 KB
Raw Blame
from typing import Literal

import torch

from invokeai.app.invocations.constants import LATENT_SCALE_FACTOR
from invokeai.backend.util.devices import TorchDevice

LatentNoiseType = Literal["SD", "FLUX", "FLUX.2", "SD3", "CogView4", "Z-Image", "Anima"]


def validate_noise_dimensions(noise_type: LatentNoiseType, width: int, height: int) -> None:
    multiple_of = 8
    if noise_type in ("FLUX", "FLUX.2", "SD3", "Z-Image"):
        multiple_of = 16
    elif noise_type == "CogView4":
        multiple_of = 32

    if width % multiple_of != 0 or height % multiple_of != 0:
        raise ValueError(f"{noise_type} noise width and height must be a multiple of {multiple_of}")


def get_expected_noise_shape(
    noise_type: LatentNoiseType,
    width: int,
    height: int,
) -> tuple[int, ...]:
    validate_noise_dimensions(noise_type, width, height)

    if noise_type == "SD":
        return (1, 4, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "FLUX":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "FLUX.2":
        return (1, 32, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "SD3":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "CogView4":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "Z-Image":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "Anima":
        return (1, 16, 1, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    raise ValueError(f"Unsupported noise type: {noise_type}")


def validate_noise_tensor_shape(noise: torch.Tensor, noise_type: LatentNoiseType, width: int, height: int) -> None:
    expected_shape = get_expected_noise_shape(noise_type, width, height)
    if tuple(noise.shape) != expected_shape:
        raise ValueError(f"Expected noise with shape {expected_shape}, got {tuple(noise.shape)}")


def generate_noise_tensor(
    noise_type: LatentNoiseType,
    width: int,
    height: int,
    seed: int,
    device: torch.device,
    dtype: torch.dtype,
    use_cpu: bool = True,
) -> torch.Tensor:
    shape = get_expected_noise_shape(noise_type, width, height)
    rand_device = "cpu" if use_cpu else device.type
    if noise_type in ("Z-Image", "Anima"):
        output_dtype = rand_dtype = torch.float32
    else:
        # The tensor keeps the device precision it always had; only the dtype it is drawn in follows `noise_dtype`.
        output_dtype = dtype
        rand_dtype = TorchDevice.choose_noise_dtype(dtype)
    return torch.randn(
        *shape,
        device=rand_device,
        dtype=rand_dtype,
        generator=torch.Generator(device=rand_device).manual_seed(seed),
    ).to(device="cpu", dtype=output_dtype)