rahul7star commited on
Commit
1eead2e
·
verified ·
1 Parent(s): d6d03b3

Update app_quant_latent.py

Browse files
Files changed (1) hide show
  1. app_quant_latent.py +27 -37
app_quant_latent.py CHANGED
@@ -698,59 +698,51 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
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,15 +756,13 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.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
777
  @spaces.GPU
778
  def generate_image_verygood_realnoise(prompt, height, width, steps, seed, guidance_scale=0.0):
 
698
  latent_gallery = []
699
  final_gallery = []
700
 
701
+ last_latents = []
702
 
703
  try:
704
+ # --- Initial latents (noise) ---
705
  latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
706
+ latents = latents.float().to(device)
707
+
708
+ # --- Run 1-2 diffusion steps to inject prompt info ---
709
+ with torch.no_grad():
710
+ partial_output = pipe(
711
+ prompt=prompt,
712
+ num_inference_steps=min(2, steps),
713
+ guidance_scale=guidance_scale,
714
+ generator=generator,
715
+ output_type="latent"
716
+ )
717
+ latents = partial_output # latents now contain partial image info
718
 
719
+ # --- Last 5 previews: interpolate from partial to final latent ---
720
+ num_previews = 5
721
+ for i, alpha in enumerate(np.linspace(0.2, 1.0, num_previews)):
722
  try:
723
+ preview_latent = latents * alpha
724
+ preview_latent = preview_latent.to(pipe.vae.device).to(pipe.vae.dtype)
 
 
 
725
 
726
+ decoded = pipe.vae.decode(preview_latent, return_dict=False)[0]
727
+ decoded = (decoded / 2 + 0.5).clamp(0, 1)
728
+ decoded = decoded.cpu().permute(0, 2, 3, 1).float().numpy()
729
+ decoded = (decoded * 255).round().astype("uint8")
730
+ latent_img = Image.fromarray(decoded[0])
 
 
 
 
731
 
732
  except Exception as e:
733
  LOGS.append(f"⚠️ Latent preview decode failed: {e}")
734
  latent_img = placeholder
735
 
736
  latent_gallery.append(latent_img)
737
+ last_latents.append(preview_latent.cpu())
 
 
 
 
 
 
738
  yield None, latent_gallery[-5:], LOGS
739
 
 
 
 
 
 
740
  except Exception as e:
741
  LOGS.append(f"⚠️ Latent generation failed: {e}")
742
  latent_gallery.append(placeholder)
743
  yield None, latent_gallery[-5:], LOGS
744
 
745
+ # --- Final image: full pipeline ---
746
  try:
747
  output = pipe(
748
  prompt=prompt,
 
756
  final_gallery.append(final_img)
757
  latent_gallery.append(final_img)
758
  LOGS.append("✅ Standard pipeline succeeded.")
759
+ yield final_img, latent_gallery[-5:] + [final_img], LOGS
760
 
761
  except Exception as e2:
762
  LOGS.append(f"❌ Standard pipeline failed: {e2}")
763
  final_gallery.append(placeholder)
764
  latent_gallery.append(placeholder)
765
  yield placeholder, latent_gallery[-5:] + [placeholder], LOGS
 
 
766
  # this is astable vesopn tha can gen final and a noise to latent
767
  @spaces.GPU
768
  def generate_image_verygood_realnoise(prompt, height, width, steps, seed, guidance_scale=0.0):