rahul7star commited on
Commit
2e8da0d
·
verified ·
1 Parent(s): 26bc6ef

Update app_quant_latent.py

Browse files
Files changed (1) hide show
  1. app_quant_latent.py +39 -43
app_quant_latent.py CHANGED
@@ -12,6 +12,7 @@ import time
12
  from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig
13
  from diffusers import ZImagePipeline, AutoModel
14
  from transformers import BitsAndBytesConfig as TransformersBitsAndBytesConfig
 
15
 
16
  # ============================================================
17
  # LOGGING BUFFER
@@ -248,58 +249,53 @@ log_system_stats("AFTER PIPELINE BUILD")
248
 
249
  @spaces.GPU
250
  def generate_image(prompt, height, width, steps, seed):
251
- try:
252
- generator = torch.Generator(device).manual_seed(seed)
253
- latent_history = []
254
-
255
- # Callback to save latents and GPU info
256
- def save_latents(step, timestep, latents):
257
- latent_history.append(latents.detach().clone())
258
- gpu_mem = torch.cuda.memory_allocated(0)/1e9
259
- log(f"Step {step} - GPU Memory Used: {gpu_mem:.2f} GB")
260
-
261
- # Step-wise loop just for latent capture
262
- for step, _ in pipe(
263
- prompt=prompt,
264
- height=height,
265
- width=width,
266
- num_inference_steps=steps,
267
- guidance_scale=0.0,
268
- generator=generator,
269
- callback=save_latents,
270
- callback_steps=1
271
- ).iter():
272
- pass # only capturing latents, ignoring intermediate images
273
-
274
- # Convert latents to PIL images for gallery
275
- latent_images = []
276
- for latent in latent_history:
277
- try:
278
- img_tensor = pipe.vae.decode(latent)
279
- img_tensor = (img_tensor / 2 + 0.5).clamp(0, 1)
280
- pil_img = T.ToPILImage()(img_tensor[0].cpu())
281
- latent_images.append(pil_img)
282
- except Exception as e:
283
- log(f"⚠️ Failed to convert latent to image: {e}")
284
-
285
- # Original final image generation
286
- output = pipe(
287
  prompt=prompt,
288
  height=height,
289
  width=width,
290
  num_inference_steps=steps,
291
- guidance_scale=0.0,
292
  generator=generator,
 
293
  )
294
 
295
- log("✅ Inference finished.")
296
- log_system_stats("AFTER INFERENCE")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
- return output.images[0], latent_images, LOGS
 
 
 
 
 
299
 
300
- except Exception as e:
301
- log(f"❌ Inference error: {e}")
302
- return None, None, LOGS
303
 
304
 
305
  # ============================================================
 
12
  from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig
13
  from diffusers import ZImagePipeline, AutoModel
14
  from transformers import BitsAndBytesConfig as TransformersBitsAndBytesConfig
15
+ latent_history = []
16
 
17
  # ============================================================
18
  # LOGGING BUFFER
 
249
 
250
  @spaces.GPU
251
  def generate_image(prompt, height, width, steps, seed):
252
+ global latent_history
253
+ latent_history = [] # reset every run
254
+
255
+ generator = torch.Generator("cuda").manual_seed(int(seed))
256
+
257
+ logs = []
258
+ def log(msg):
259
+ logs.append(msg)
260
+
261
+ # Run pipeline manually step by step
262
+ out = pipe(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  prompt=prompt,
264
  height=height,
265
  width=width,
266
  num_inference_steps=steps,
 
267
  generator=generator,
268
+ output_type="latent"
269
  )
270
 
271
+ latents = out.latents
272
+
273
+ # Denoising loop - MANUAL callback
274
+ for i, t in enumerate(pipe.scheduler.timesteps):
275
+ latents = pipe.unet(latents, t, encoder_hidden_states=out.prompt_embeds).sample
276
+
277
+ # Store cloned latent
278
+ latent_history.append(latents.detach().cpu().clone())
279
+
280
+ # Log GPU memory
281
+ gpu = torch.cuda.memory_allocated() / 1e9
282
+ log(f"Step {i+1}/{steps} — GPU: {gpu:.2f} GB")
283
+
284
+ # Step scheduler
285
+ latents = pipe.scheduler.step(latents, timestep=t).prev_sample
286
+
287
+ # Decode final image
288
+ final_image = pipe.vae.decode(latents / pipe.vae.config.scaling_factor).sample[0]
289
+ final_image = (final_image / 2 + 0.5).clamp(0,1).cpu().permute(1,2,0).numpy()
290
 
291
+ # Convert latents to preview images
292
+ latent_imgs = []
293
+ for l in latent_history:
294
+ img = pipe.vae.decode(l / pipe.vae.config.scaling_factor).sample[0]
295
+ img = (img / 2 + 0.5).clamp(0,1).cpu().permute(1,2,0).numpy()
296
+ latent_imgs.append(img)
297
 
298
+ return final_image, latent_imgs, "\n".join(logs)
 
 
299
 
300
 
301
  # ============================================================