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.

fix(flux2): apply BN normalization to latents for inpainting (#8868)

Alexander Eichhorn467ae66a8774
diff --git a/invokeai/app/invocations/flux2_denoise.py b/invokeai/app/invocations/flux2_denoise.py
index c181063603..645f376eed 100644
--- a/invokeai/app/invocations/flux2_denoise.py
+++ b/invokeai/app/invocations/flux2_denoise.py
@@ -51,7 +51,7 @@ from invokeai.backend.util.devices import TorchDevice
title="FLUX2 Denoise",
tags=["image", "flux", "flux2", "klein", "denoise"],
category="image",
- version="1.3.0",
+ version="1.4.0",
classification=Classification.Prototype,
)
class Flux2DenoiseInvocation(BaseInvocation):
@@ -329,13 +329,29 @@ class Flux2DenoiseInvocation(BaseInvocation):
noise_packed = pack_flux2(noise)
x = pack_flux2(x)
- # BN normalization for txt2img:
- # - DO NOT normalize random noise (it's already N(0,1) distributed)
- # - Diffusers only normalizes image latents from VAE (for img2img/kontext)
+ # BN normalization for img2img/inpainting:
+ # - The init_latents from VAE encode are NOT BN-normalized
+ # - The transformer operates in BN-normalized space
+ # - We must normalize x, init_latents, AND noise for InpaintExtension
# - Output MUST be denormalized after denoising before VAE decode
#
- # For img2img with init_latents, we should normalize init_latents on unpacked
- # shape (B, 128, H/16, W/16) - this is handled by _bn_normalize_unpacked below
+ # This ensures that:
+ # 1. x starts in the correct normalized space for the transformer
+ # 2. When InpaintExtension merges intermediate_latents with noised_init_latents,
+ # both are in the same scale/space (noise and init_latents must be in same space
+ # for the linear interpolation: noised = noise * t + init * (1-t))
+ if bn_mean is not None and bn_std is not None:
+ if init_latents_packed is not None:
+ init_latents_packed = self._bn_normalize(init_latents_packed, bn_mean, bn_std)
+ # Also normalize noise for InpaintExtension - it's used to compute
+ # noised_init_latents = noise * t + init_latents * (1-t)
+ # Both operands must be in the same normalized space
+ noise_packed = self._bn_normalize(noise_packed, bn_mean, bn_std)
+ # For img2img/inpainting, x is computed from init_latents and must also be normalized
+ # For txt2img, x is pure noise (already N(0,1)) - normalizing it would be incorrect
+ # We detect img2img by checking if init_latents was provided
+ if init_latents is not None:
+ x = self._bn_normalize(x, bn_mean, bn_std)
# Verify packed dimensions
assert packed_h * packed_w == x.shape[1]