Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files512 unmodified lines513514515516517518519520521522523524525526527528529530531532533534512 unmodified lines# Helper: Safe latent extractor# --------------------------def safe_get_latents(pipe, height, width, generator, device, LOGS):try:latents = pipe.prepare_latents(batch_size=1,num_channels=getattr(pipe.unet, "in_channels", 4),height=height,width=width,dtype=torch.float32,device=device,generator=generator)LOGS.append(f"🔹 Latents shape: {latents.shape}, dtype: {latents.dtype}, device: {latents.device}")return latentsexcept Exception as e:LOGS.append(f"⚠️ Latent extraction failed: {e}")return torch.randn((1, 4, height // 8, width // 8), generator=generator, device=device)# --------------------------# Main generation function (kept exactly as your logic)512 unmodified lines513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551512 unmodified lines# Helper: Safe latent extractor# --------------------------def safe_get_latents(pipe, height, width, generator, device, LOGS):"""Safely prepare latents for any ZImagePipeline variant.Returns latents tensor, logs issues instead of failing."""try:# Determine number of channelsnum_channels = 4 # default fallbackif hasattr(pipe, "unet") and hasattr(pipe.unet, "in_channels"):num_channels = pipe.unet.in_channelselif hasattr(pipe, "vae") and hasattr(pipe.vae, "latent_channels"):num_channels = pipe.vae.latent_channels # some pipelines define thisLOGS.append(f"🔹 Using num_channels={num_channels} for latents")latents = pipe.prepare_latents(batch_size=1,num_channels_latents=num_channels,height=height,width=width,dtype=torch.float32,device=device,generator=generator,)LOGS.append(f"🔹 Latents shape: {latents.shape}, dtype: {latents.dtype}, device: {latents.device}")return latentsexcept Exception as e:LOGS.append(f"⚠️ Latent extraction failed: {e}")# fallback: guess a safe shapefallback_channels = 16 # try standard default for ZImage pipelineslatents = torch.randn((1, fallback_channels, height // 8, width // 8),generator=generator, device=device)LOGS.append(f"🔹 Using fallback random latents shape: {latents.shape}")return latents# --------------------------# Main generation function (kept exactly as your logic)
app_quant_latent.py+21-4