Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions invokeai/app/invocations/denoise_latents.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from invokeai.app.invocations.t2i_adapter import T2IAdapterField
from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.app.util.controlnet_utils import prepare_control_image
from invokeai.backend.architectures import get_max_unet_downscale
from invokeai.backend.ip_adapter.ip_adapter import IPAdapter
from invokeai.backend.model_manager.configs.factory import AnyModelConfig
from invokeai.backend.model_manager.taxonomy import BaseModelType, ModelVariantType
Expand Down Expand Up @@ -676,17 +677,13 @@ def run_t2i_adapters(
t2i_adapter_model_config = context.models.get_config(t2i_adapter_field.t2i_adapter_model.key)
image = context.images.get_pil(t2i_adapter_field.image.image_name, mode="RGB")

# The max_unet_downscale is the maximum amount that the UNet model downscales the latent image internally.
if t2i_adapter_model_config.base == BaseModelType.StableDiffusion1:
max_unet_downscale = 8
elif t2i_adapter_model_config.base == BaseModelType.StableDiffusionXL:
max_unet_downscale = 4
# Raises for a base without a UNet, before the BGR swap below -- same order as before.
max_unet_downscale = get_max_unet_downscale(t2i_adapter_model_config.base)

if t2i_adapter_model_config.base == BaseModelType.StableDiffusionXL:
# SDXL adapters are trained on cv2's BGR outputs
r, g, b = image.split()
image = Image.merge("RGB", (b, g, r))
else:
raise ValueError(f"Unexpected T2I-Adapter base model type: '{t2i_adapter_model_config.base}'.")

t2i_adapter_model: T2IAdapter
with context.models.load(t2i_adapter_field.t2i_adapter_model) as t2i_adapter_model:
Expand Down Expand Up @@ -985,6 +982,11 @@ def step_callback(state: PipelineIntermediateState) -> None:
# ext = extension_field.to_extension(exit_stack, context, ext_manager)
# ext_manager.add_extension(ext)
self.parse_controlnet_field(exit_stack, context, self.control, ext_manager)
# NOTE: this decides the BGR swap from the *UNet's* base, while run_t2i_adapters above
# decides it from each *adapter's* base. The two disagree for an SD1 adapter on an SDXL
# UNet. Left as-is deliberately: fixing it changes behaviour, and this refactor does
# not. The natural fix is to fold `bgr_input` into UNetDownscaleFacet so both paths read
# one declaration.
bgr_mode = self.unet.unet.base == BaseModelType.StableDiffusionXL
self.parse_t2i_adapter_field(exit_stack, context, self.t2i_adapter, ext_manager, bgr_mode)

Expand Down
29 changes: 13 additions & 16 deletions invokeai/app/invocations/ideogram4_denoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
from invokeai.app.invocations.model import TransformerField
from invokeai.app.invocations.primitives import LatentsOutput
from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.app.util.step_callback import (
FLUX2_LATENT_RGB_BIAS,
FLUX2_LATENT_RGB_FACTORS,
sample_to_lowres_estimated_image,
)
from invokeai.backend.architectures import get_latent_space
from invokeai.backend.ideogram4 import run_ideogram4_denoise
from invokeai.backend.ideogram4.latent_norm import get_latent_norm
from invokeai.backend.ideogram4.sampler_configs import PRESETS
from invokeai.backend.ideogram4.sampling_utils import unpatchify_and_denormalize
from invokeai.backend.ideogram4.transformer_pair import Ideogram4TransformerPair
from invokeai.backend.model_manager.taxonomy import BaseModelType
from invokeai.backend.stable_diffusion.diffusion.conditioning_data import Ideogram4ConditioningInfo
from invokeai.backend.util.devices import TorchDevice

Expand Down Expand Up @@ -122,12 +119,13 @@ def invoke(self, context: InvocationContext) -> LatentsOutput:
assert isinstance(info, Ideogram4ConditioningInfo)
llm_features = info.prompt_embeds.to(device=device, dtype=torch.float32)

# Progress-preview setup: Ideogram uses a FLUX.2-style 32-channel VAE, so the FLUX.2
# latent->RGB factors give a usable (approximate) low-res preview of the forming image at each
# step, without a full VAE decode. Denormalization params come from get_latent_norm (no VAE).
# Progress-preview setup: Ideogram's latent space gives a usable (approximate) low-res
# preview of the forming image at each step, without a full VAE decode. Denormalization
# params come from get_latent_norm (no VAE). This does not go through
# diffusion_step_callback: the callback signature here is (step, total, packed_latents) and
# the latents must be unpatchified and denormalized first.
latent_shift, latent_scale = get_latent_norm()
rgb_factors = torch.tensor(FLUX2_LATENT_RGB_FACTORS, dtype=torch.float32)
rgb_bias = torch.tensor(FLUX2_LATENT_RGB_BIAS, dtype=torch.float32)
latent_space = get_latent_space(BaseModelType.Ideogram4)

def step_callback(step: int, total: int, packed_latents: torch.Tensor) -> None:
preview = None
Expand All @@ -138,11 +136,7 @@ def step_callback(step: int, total: int, packed_latents: torch.Tensor) -> None:
latent_shift.to(packed_latents.device),
latent_scale.to(packed_latents.device),
)
preview = sample_to_lowres_estimated_image(
samples=vae_latent,
latent_rgb_factors=rgb_factors.to(vae_latent.device),
latent_rgb_bias=rgb_bias.to(vae_latent.device),
)
preview = latent_space.preview(vae_latent)
except Exception:
# A preview must never break generation — fall back to a plain progress signal.
preview = None
Expand All @@ -151,7 +145,10 @@ def step_callback(step: int, total: int, packed_latents: torch.Tensor) -> None:
"Running Ideogram 4 denoising",
step / total,
preview,
(preview.width * 8, preview.height * 8),
(
preview.width * latent_space.spatial_compression,
preview.height * latent_space.spatial_compression,
),
)
else:
context.util.signal_progress("Running Ideogram 4 denoising", step / total)
Expand Down
Loading
Loading