Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files- app_quant_latent.py +35 -26
app_quant_latent.py
CHANGED
|
@@ -691,57 +691,66 @@ def generate_image_all_latents(prompt, height, width, steps, seed, guidance_scal
|
|
| 691 |
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
| 692 |
LOGS = []
|
| 693 |
device = "cuda"
|
|
|
|
| 694 |
generator = torch.Generator(device).manual_seed(int(seed))
|
| 695 |
|
| 696 |
placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
|
| 697 |
latent_gallery = []
|
| 698 |
final_gallery = []
|
| 699 |
|
| 700 |
-
last_latents = []
|
| 701 |
|
| 702 |
try:
|
| 703 |
-
# ---
|
| 704 |
latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
|
| 705 |
-
latents = latents.float().to(
|
| 706 |
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
prompt=prompt,
|
| 710 |
-
num_inference_steps=min(3, steps), # 1-3 steps to inject image info
|
| 711 |
-
guidance_scale=guidance_scale,
|
| 712 |
-
generator=generator,
|
| 713 |
-
output_type="latent"
|
| 714 |
-
)
|
| 715 |
|
| 716 |
-
|
| 717 |
-
num_previews = 5
|
| 718 |
-
for i, alpha in enumerate(np.linspace(0.2, 1.0, num_previews)):
|
| 719 |
try:
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
|
| 731 |
except Exception as e:
|
| 732 |
LOGS.append(f"⚠️ Latent preview decode failed: {e}")
|
| 733 |
latent_img = placeholder
|
| 734 |
|
| 735 |
latent_gallery.append(latent_img)
|
| 736 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
yield None, latent_gallery[-5:], LOGS
|
| 738 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
except Exception as e:
|
| 740 |
LOGS.append(f"⚠️ Latent generation failed: {e}")
|
| 741 |
latent_gallery.append(placeholder)
|
| 742 |
yield None, latent_gallery[-5:], LOGS
|
| 743 |
|
| 744 |
-
# ---
|
| 745 |
try:
|
| 746 |
output = pipe(
|
| 747 |
prompt=prompt,
|
|
@@ -755,7 +764,7 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
|
| 755 |
final_gallery.append(final_img)
|
| 756 |
latent_gallery.append(final_img)
|
| 757 |
LOGS.append("✅ Standard pipeline succeeded.")
|
| 758 |
-
yield final_img, latent_gallery[-5:] + [final_img], LOGS
|
| 759 |
|
| 760 |
except Exception as e2:
|
| 761 |
LOGS.append(f"❌ Standard pipeline failed: {e2}")
|
|
|
|
| 691 |
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
| 692 |
LOGS = []
|
| 693 |
device = "cuda"
|
| 694 |
+
cpu_device = "cpu"
|
| 695 |
generator = torch.Generator(device).manual_seed(int(seed))
|
| 696 |
|
| 697 |
placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
|
| 698 |
latent_gallery = []
|
| 699 |
final_gallery = []
|
| 700 |
|
| 701 |
+
last_latents = [] # store last 5 preview latents on CPU
|
| 702 |
|
| 703 |
try:
|
| 704 |
+
# --- Initial latents ---
|
| 705 |
latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
|
| 706 |
+
latents = latents.float().to(cpu_device) # move to CPU
|
| 707 |
|
| 708 |
+
num_previews = min(10, steps)
|
| 709 |
+
preview_indices = torch.linspace(0, steps - 1, num_previews).long()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 710 |
|
| 711 |
+
for i, step_idx in enumerate(preview_indices):
|
|
|
|
|
|
|
| 712 |
try:
|
| 713 |
+
with torch.no_grad():
|
| 714 |
+
# --- Z-Image Turbo-style denoise simulation ---
|
| 715 |
+
t = 1.0 - (i / num_previews) # linear decay [1.0 -> 0.0]
|
| 716 |
+
noise_scale = t ** 0.5 # reduce noise over steps (sqrt for smoother)
|
| 717 |
+
denoise_latent = latents * t + torch.randn_like(latents) * noise_scale
|
| 718 |
+
|
| 719 |
+
# Move to VAE device & dtype
|
| 720 |
+
denoise_latent = denoise_latent.to(pipe.vae.device).to(pipe.vae.dtype)
|
| 721 |
|
| 722 |
+
# Decode latent to image
|
| 723 |
+
decoded = pipe.vae.decode(denoise_latent, return_dict=False)[0]
|
| 724 |
+
decoded = (decoded / 2 + 0.5).clamp(0, 1)
|
| 725 |
+
decoded = decoded.cpu().permute(0, 2, 3, 1).float().numpy()
|
| 726 |
+
decoded = (decoded * 255).round().astype("uint8")
|
| 727 |
+
latent_img = Image.fromarray(decoded[0])
|
| 728 |
|
| 729 |
except Exception as e:
|
| 730 |
LOGS.append(f"⚠️ Latent preview decode failed: {e}")
|
| 731 |
latent_img = placeholder
|
| 732 |
|
| 733 |
latent_gallery.append(latent_img)
|
| 734 |
+
|
| 735 |
+
# Keep last 5 latents only
|
| 736 |
+
last_latents.append(denoise_latent.cpu().clone())
|
| 737 |
+
if len(last_latents) > 5:
|
| 738 |
+
last_latents.pop(0)
|
| 739 |
+
|
| 740 |
+
# Show only last 5 previews in UI
|
| 741 |
yield None, latent_gallery[-5:], LOGS
|
| 742 |
|
| 743 |
+
# Optionally: upload last 5 latents
|
| 744 |
+
# latent_dict = {"latents": last_latents, "prompt": prompt, "seed": seed}
|
| 745 |
+
# hf_url = upload_latents_to_hf(latent_dict, filename=f"latents_last5_{seed}.pt")
|
| 746 |
+
# LOGS.append(f"🔹 Last 5 latents uploaded: {hf_url}")
|
| 747 |
+
|
| 748 |
except Exception as e:
|
| 749 |
LOGS.append(f"⚠️ Latent generation failed: {e}")
|
| 750 |
latent_gallery.append(placeholder)
|
| 751 |
yield None, latent_gallery[-5:], LOGS
|
| 752 |
|
| 753 |
+
# --- Final image on GPU ---
|
| 754 |
try:
|
| 755 |
output = pipe(
|
| 756 |
prompt=prompt,
|
|
|
|
| 764 |
final_gallery.append(final_img)
|
| 765 |
latent_gallery.append(final_img)
|
| 766 |
LOGS.append("✅ Standard pipeline succeeded.")
|
| 767 |
+
yield final_img, latent_gallery[-5:] + [final_img], LOGS # last 5 previews + final
|
| 768 |
|
| 769 |
except Exception as e2:
|
| 770 |
LOGS.append(f"❌ Standard pipeline failed: {e2}")
|