Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix(imagegen): unpack ERNIE patched latents before stepwise preview decode

Ilya Eivyd26cca2b8d6c
diff --git a/scripts/_runner_common.py b/scripts/_runner_common.py
index 3c794a6c6d..d0a9b5b27e 100644
--- a/scripts/_runner_common.py
+++ b/scripts/_runner_common.py
@@ -116,6 +116,7 @@ def make_stepwise_callback(
width: int,
*,
unpack_latents=None,
+ preview_decoder=None,
):
"""Return a `callback_on_step_end` that decodes the running latent into
a small preview PNG. `local.js#processLatestFrame` watches this dir and
@@ -130,6 +131,11 @@ def make_stepwise_callback(
- flux2: transformer-packed latents come back as
`(B, num_patches, C*p*p)`. Pass a callable that takes
`(latents, height, width)` and returns the unpacked tensor.
+ - ernie: latents stay 4-D but are 2x2 patch-packed (e.g. `(B, 128, H/2, W/2)`
+ for a 32-ch VAE) AND need pipeline-specific BN-stats unnormalization
+ before `vae.decode`, not the standard `latents/scaling + shift`.
+ Pass `preview_decoder=fn(pipe, latents) -> image_tensor` to fully
+ override the per-step decode path.
"""
if not stepwise_dir:
return None
@@ -173,10 +179,16 @@ def make_stepwise_callback(
latents = unpack_latents(latents, height, width)
elif latents.dim() != 4:
return callback_kwargs
- scaled = latents / scaling + shift
- if vae_dtype is not None and (scaled.dtype != vae_dtype or scaled.device != vae_device):
- scaled = scaled.to(device=vae_device, dtype=vae_dtype)
- decoded = vae.decode(scaled, return_dict=False)[0]
+ if preview_decoder is not None:
+ aligned = latents
+ if vae_dtype is not None and (aligned.dtype != vae_dtype or aligned.device != vae_device):
+ aligned = aligned.to(device=vae_device, dtype=vae_dtype)
+ decoded = preview_decoder(pipe, aligned)
+ else:
+ scaled = latents / scaling + shift
+ if vae_dtype is not None and (scaled.dtype != vae_dtype or scaled.device != vae_device):
+ scaled = scaled.to(device=vae_device, dtype=vae_dtype)
+ decoded = vae.decode(scaled, return_dict=False)[0]
decoded = (decoded.clamp(-1, 1) + 1) / 2
arr = (decoded[0].float().cpu().permute(1, 2, 0).numpy() * 255).astype("uint8")
img = Image.fromarray(arr)
diff --git a/scripts/z_image_turbo.py b/scripts/z_image_turbo.py
index 51cfb3891d..d62f12e62d 100644
--- a/scripts/z_image_turbo.py
+++ b/scripts/z_image_turbo.py
@@ -131,7 +131,28 @@ def main() -> None:
seed = args.seed if args.seed is not None else int(torch.randint(0, 2**31 - 1, (1,)).item())
generator = make_generator(device, seed)
- callback = make_stepwise_callback(args.stepwise_image_output_dir, pipe, int(args.height), int(args.width))
+ # ERNIE keeps latents 2x2 patch-packed during the loop (e.g. 32→128 ch at
+ # half spatial) and decodes via vae.bn unnormalization + _unpatchify_latents
+ # before vae.decode — the generic scaling/shift path fails with a channel
+ # mismatch ([32,32,1,1] weight vs 128-ch input). When the loaded pipeline
+ # exposes both hooks, pass an ERNIE-aware preview decoder.
+ preview_decoder = None
+ pipe_vae = getattr(pipe, "vae", None)
+ if hasattr(pipe, "_unpatchify_latents") and pipe_vae is not None and hasattr(pipe_vae, "bn"):
+ def preview_decoder(p, latents): # noqa: E306 — local closure on purpose
+ vae = p.vae
+ mean = vae.bn.running_mean.view(1, -1, 1, 1).to(device=latents.device, dtype=latents.dtype)
+ std = torch.sqrt(vae.bn.running_var.view(1, -1, 1, 1) + 1e-5).to(device=latents.device, dtype=latents.dtype)
+ unpacked = p._unpatchify_latents(latents * std + mean)
+ return vae.decode(unpacked, return_dict=False)[0]
+
+ callback = make_stepwise_callback(
+ args.stepwise_image_output_dir,
+ pipe,
+ int(args.height),
+ int(args.width),
+ preview_decoder=preview_decoder,
+ )
accepted = set(inspect.signature(pipe.__call__).parameters.keys())
pipe_kwargs = dict(