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): Fix FLUX.2 Klein image generation quality (#8838)

Alexander Eichhorna42fdb0f441a
diff --git a/invokeai/app/invocations/flux2_denoise.py b/invokeai/app/invocations/flux2_denoise.py
index f221c1a3c4..c181063603 100644
--- a/invokeai/app/invocations/flux2_denoise.py
+++ b/invokeai/app/invocations/flux2_denoise.py
@@ -329,15 +329,13 @@ class Flux2DenoiseInvocation(BaseInvocation):
noise_packed = pack_flux2(noise)
x = pack_flux2(x)
- # Apply BN normalization BEFORE denoising (as per diffusers Flux2KleinPipeline)
- # BN normalization: y = (x - mean) / std
- # This transforms latents to normalized space for the transformer
- # IMPORTANT: Also normalize init_latents and noise for inpainting to maintain consistency
- if bn_mean is not None and bn_std is not None:
- x = self._bn_normalize(x, bn_mean, bn_std)
- if init_latents_packed is not None:
- init_latents_packed = self._bn_normalize(init_latents_packed, bn_mean, bn_std)
- noise_packed = self._bn_normalize(noise_packed, bn_mean, bn_std)
+ # 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)
+ # - 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
# Verify packed dimensions
assert packed_h * packed_w == x.shape[1]
diff --git a/invokeai/app/invocations/flux2_vae_decode.py b/invokeai/app/invocations/flux2_vae_decode.py
index 8450d1ec85..ecbc7d9cb8 100644
--- a/invokeai/app/invocations/flux2_vae_decode.py
+++ b/invokeai/app/invocations/flux2_vae_decode.py
@@ -57,20 +57,6 @@ class Flux2VaeDecodeInvocation(BaseInvocation, WithMetadata, WithBoard):
# Decode using diffusers API
decoded = vae.decode(latents, return_dict=False)[0]
- # Debug: Log decoded output statistics
- print(
- f"[FLUX.2 VAE] Decoded output: shape={decoded.shape}, "
- f"min={decoded.min().item():.4f}, max={decoded.max().item():.4f}, "
- f"mean={decoded.mean().item():.4f}"
- )
- # Check per-channel statistics to diagnose color issues
- for c in range(min(3, decoded.shape[1])):
- ch = decoded[0, c]
- print(
- f"[FLUX.2 VAE] Channel {c}: min={ch.min().item():.4f}, "
- f"max={ch.max().item():.4f}, mean={ch.mean().item():.4f}"
- )
-
# Convert from [-1, 1] to [0, 1] then to [0, 255] PIL image
img = (decoded / 2 + 0.5).clamp(0, 1)
img = rearrange(img[0], "c h w -> h w c")
diff --git a/invokeai/backend/flux2/sampling_utils.py b/invokeai/backend/flux2/sampling_utils.py
index 6978d0f144..3981e91275 100644
--- a/invokeai/backend/flux2/sampling_utils.py
+++ b/invokeai/backend/flux2/sampling_utils.py
@@ -108,33 +108,27 @@ def unpack_flux2(x: torch.Tensor, height: int, width: int) -> torch.Tensor:
def compute_empirical_mu(image_seq_len: int, num_steps: int) -> float:
- """Compute empirical mu for FLUX.2 schedule shifting.
+ """Compute mu for FLUX.2 schedule shifting.
- This matches the diffusers Flux2Pipeline implementation.
- The mu value controls how much the schedule is shifted towards higher timesteps.
+ Uses a fixed mu value of 2.02, matching ComfyUI's proven FLUX.2 configuration.
+
+ The previous implementation (from diffusers' FLUX.1 pipeline) computed mu as a
+ linear function of image_seq_len, which produced excessively high values at
+ high resolutions (e.g., mu=3.23 at 2048x2048). This over-shifted the sigma
+ schedule, compressing almost all values above 0.9 and forcing the model to
+ denoise everything in the final 1-2 steps, causing severe grid/diamond artifacts.
+
+ ComfyUI uses a fixed shift=2.02 for FLUX.2 Klein at all resolutions and produces
+ artifact-free images even at 2048x2048.
Args:
- image_seq_len: Number of image tokens (packed_h * packed_w).
- num_steps: Number of denoising steps.
+ image_seq_len: Number of image tokens (packed_h * packed_w). Currently unused.
+ num_steps: Number of denoising steps. Currently unused.
Returns:
- The empirical mu value.
+ The mu value (fixed at 2.02).
"""
- a1, b1 = 8.73809524e-05, 1.89833333
- a2, b2 = 0.00016927, 0.45666666
-
- if image_seq_len > 4300:
- mu = a2 * image_seq_len + b2
- return float(mu)
-
- m_200 = a2 * image_seq_len + b2
- m_10 = a1 * image_seq_len + b1
-
- a = (m_200 - m_10) / 190.0
- b = m_200 - 200.0 * a
- mu = a * num_steps + b
-
- return float(mu)
+ return 2.02
def get_schedule_flux2(
@@ -169,11 +163,14 @@ def get_schedule_flux2(
def generate_img_ids_flux2(h: int, w: int, batch_size: int, device: torch.device) -> torch.Tensor:
- """Generate tensor of image position ids for FLUX.2.
+ """Generate tensor of image position ids for FLUX.2 with RoPE scaling.
FLUX.2 uses 4D position coordinates (T, H, W, L) for its rotary position embeddings.
This is different from FLUX.1 which uses 3D coordinates.
+ RoPE Scaling: For resolutions >1536x1536, position IDs are scaled down using
+ Position Interpolation to prevent RoPE degradation and diamond/grid artifacts.
+
IMPORTANT: Position IDs must use int64 (long) dtype like diffusers, not bfloat16.
Using floating point dtype for position IDs can cause NaN in rotary embeddings.