Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files- app_quant_latent.py +22 -22
app_quant_latent.py
CHANGED
|
@@ -691,37 +691,36 @@ 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 = [] # store last 5 preview latents
|
| 701 |
|
| 702 |
-
# --- Generate latent previews ---
|
| 703 |
try:
|
|
|
|
| 704 |
latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
|
| 705 |
-
latents = latents.float()
|
| 706 |
|
| 707 |
num_previews = min(10, steps)
|
| 708 |
preview_indices = torch.linspace(0, steps - 1, num_previews).long()
|
| 709 |
|
| 710 |
-
# clone latents for preview
|
| 711 |
-
preview_latents = latents.clone()
|
| 712 |
-
|
| 713 |
for i, step_idx in enumerate(preview_indices):
|
| 714 |
try:
|
| 715 |
with torch.no_grad():
|
| 716 |
-
# ---
|
| 717 |
-
|
| 718 |
-
|
|
|
|
| 719 |
|
| 720 |
-
#
|
| 721 |
-
|
| 722 |
|
| 723 |
-
#
|
| 724 |
-
decoded = pipe.vae.decode(
|
| 725 |
decoded = (decoded / 2 + 0.5).clamp(0, 1)
|
| 726 |
decoded = decoded.cpu().permute(0, 2, 3, 1).float().numpy()
|
| 727 |
decoded = (decoded * 255).round().astype("uint8")
|
|
@@ -733,14 +732,15 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
|
| 733 |
|
| 734 |
latent_gallery.append(latent_img)
|
| 735 |
|
| 736 |
-
# Keep last 5 latents
|
| 737 |
-
last_latents.append(
|
| 738 |
if len(last_latents) > 5:
|
| 739 |
last_latents.pop(0)
|
| 740 |
|
| 741 |
-
|
|
|
|
| 742 |
|
| 743 |
-
# Optionally:
|
| 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}")
|
|
@@ -748,9 +748,9 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
|
| 748 |
except Exception as e:
|
| 749 |
LOGS.append(f"⚠️ Latent generation failed: {e}")
|
| 750 |
latent_gallery.append(placeholder)
|
| 751 |
-
yield None, latent_gallery, LOGS
|
| 752 |
|
| 753 |
-
# --- Final image
|
| 754 |
try:
|
| 755 |
output = pipe(
|
| 756 |
prompt=prompt,
|
|
@@ -762,15 +762,15 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
|
| 762 |
)
|
| 763 |
final_img = output.images[0]
|
| 764 |
final_gallery.append(final_img)
|
| 765 |
-
latent_gallery.append(final_img)
|
| 766 |
LOGS.append("✅ Standard pipeline succeeded.")
|
| 767 |
-
yield final_img, latent_gallery, LOGS
|
| 768 |
|
| 769 |
except Exception as e2:
|
| 770 |
LOGS.append(f"❌ Standard pipeline failed: {e2}")
|
| 771 |
final_gallery.append(placeholder)
|
| 772 |
latent_gallery.append(placeholder)
|
| 773 |
-
yield placeholder, latent_gallery, LOGS
|
| 774 |
|
| 775 |
|
| 776 |
# this is astable vesopn tha can gen final and a noise to latent
|
|
|
|
| 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")
|
|
|
|
| 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}")
|
|
|
|
| 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,
|
|
|
|
| 762 |
)
|
| 763 |
final_img = output.images[0]
|
| 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}")
|
| 771 |
final_gallery.append(placeholder)
|
| 772 |
latent_gallery.append(placeholder)
|
| 773 |
+
yield placeholder, latent_gallery[-5:] + [placeholder], LOGS
|
| 774 |
|
| 775 |
|
| 776 |
# this is astable vesopn tha can gen final and a noise to latent
|