multimodalart HF Staff commited on
Commit
cc64ef2
·
verified ·
1 Parent(s): 1318b7d

Step-1 mode2 memory from environment preview clip (authors' training-aligned path); timing logs

Browse files
Files changed (1) hide show
  1. app.py +22 -3
app.py CHANGED
@@ -373,6 +373,22 @@ def make_preview_clip(image: Image.Image, path: str):
373
  imageio.mimwrite(path, [frame] * FPS, fps=FPS)
374
 
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  @torch.no_grad()
377
  def planner_generate(messages, suppress_initial_special_tokens=False) -> str:
378
  from qwen_vl_utils import process_vision_info
@@ -456,7 +472,7 @@ def render_log(state) -> str:
456
 
457
  def _duration(start_image=None, category=None, subject=None, state=None, override_action="",
458
  video_length=45, num_inference_steps=30, *args, **kwargs):
459
- return int(40 + int(num_inference_steps) * 3.2 * (int(video_length) / 45.0))
460
 
461
 
462
  @spaces.GPU(duration=_duration, size="xlarge")
@@ -522,7 +538,7 @@ def next_step(
522
  else:
523
  action, completed = plan_next(goal, step_number, visual, state["history"], is_preview)
524
  source = "planner"
525
- print(f"[step {step_number}] action={action!r} completed={completed}", flush=True)
526
  if not action:
527
  state["done"] = True
528
  gr.Info("The planner reports the task is complete.")
@@ -534,7 +550,9 @@ def next_step(
534
  step_prompt, step_text = normalize_step_prompt(action, step_number)
535
  latent_length = latent_length_from_video_length(video_length)
536
  memory = None
537
- if state["latents"]:
 
 
538
  memory = pad_or_trim_memory_latents(torch.cat(state["latents"], dim=2), max(latent_length - 1, 0))
539
  memory = memory.to("cuda") * MEMORY_BLEND
540
  viewmats = torch.eye(4).unsqueeze(0).repeat(latent_length, 1, 1).unsqueeze(0)
@@ -573,6 +591,7 @@ def next_step(
573
  memory_history_latents=memory,
574
  memory_reference_images=list(state["refs"]),
575
  )
 
576
  clip_path = os.path.join(out_dir, f"step_{step_number:02d}.mp4")
577
  save_video(out.videos, clip_path)
578
  state["latents"] = (state["latents"] + [out.latents.detach().to("cpu", copy=True)])[-MEMORY_STEPS:]
 
373
  imageio.mimwrite(path, [frame] * FPS, fps=FPS)
374
 
375
 
376
+ @torch.no_grad()
377
+ def encode_preview_memory(preview_path: str, latent_length: int):
378
+ """Authors' build_global_clip_memory_latents: VAE-encode the environment preview clip
379
+ as step-1 mode2 memory (the executor was trained with this memory present)."""
380
+ reader = imageio.get_reader(preview_path)
381
+ try:
382
+ frames = np.stack([f[:, :, :3] for f in reader])
383
+ finally:
384
+ reader.close()
385
+ video = torch.from_numpy(frames).permute(3, 0, 1, 2).float() / 127.5 - 1.0 # C,T,H,W
386
+ video = video.unsqueeze(0).to("cuda")
387
+ with torch.autocast(device_type="cuda", dtype=torch.float16):
388
+ mem = pipe.vae.encode(video).latent_dist.mode() * pipe.vae.config.scaling_factor
389
+ return pad_or_trim_memory_latents(mem, max(latent_length - 1, 0)) * MEMORY_BLEND
390
+
391
+
392
  @torch.no_grad()
393
  def planner_generate(messages, suppress_initial_special_tokens=False) -> str:
394
  from qwen_vl_utils import process_vision_info
 
472
 
473
  def _duration(start_image=None, category=None, subject=None, state=None, override_action="",
474
  video_length=45, num_inference_steps=30, *args, **kwargs):
475
+ return 300
476
 
477
 
478
  @spaces.GPU(duration=_duration, size="xlarge")
 
538
  else:
539
  action, completed = plan_next(goal, step_number, visual, state["history"], is_preview)
540
  source = "planner"
541
+ print(f"[step {step_number}] action={action!r} completed={completed} plan_time={time.time() - t_start:.1f}s", flush=True)
542
  if not action:
543
  state["done"] = True
544
  gr.Info("The planner reports the task is complete.")
 
550
  step_prompt, step_text = normalize_step_prompt(action, step_number)
551
  latent_length = latent_length_from_video_length(video_length)
552
  memory = None
553
+ if step_idx == 0:
554
+ memory = encode_preview_memory(os.path.join(out_dir, "preview.mp4"), latent_length)
555
+ elif state["latents"]:
556
  memory = pad_or_trim_memory_latents(torch.cat(state["latents"], dim=2), max(latent_length - 1, 0))
557
  memory = memory.to("cuda") * MEMORY_BLEND
558
  viewmats = torch.eye(4).unsqueeze(0).repeat(latent_length, 1, 1).unsqueeze(0)
 
591
  memory_history_latents=memory,
592
  memory_reference_images=list(state["refs"]),
593
  )
594
+ print(f"[step {step_number}] generation done at {time.time() - t_start:.1f}s", flush=True)
595
  clip_path = os.path.join(out_dir, f"step_{step_number:02d}.mp4")
596
  save_video(out.videos, clip_path)
597
  state["latents"] = (state["latents"] + [out.latents.detach().to("cpu", copy=True)])[-MEMORY_STEPS:]