BonanDing commited on
Commit
cb4bbd0
·
1 Parent(s): b143792

Add DeMemWM rollout progress metrics

Browse files
Files changed (1) hide show
  1. algorithms/dememwm/df_video.py +53 -1
algorithms/dememwm/df_video.py CHANGED
@@ -1,6 +1,7 @@
1
  from collections.abc import Mapping
2
  import os
3
  import random
 
4
  import numpy as np
5
  import torch
6
  import torch.distributed as dist
@@ -39,6 +40,25 @@ def _cfg_get(cfg, key: str, default=None):
39
  return getattr(cfg, key, default)
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def _trainability_cfg(cfg):
43
  return _cfg_get(cfg, "trainability", {})
44
 
@@ -714,8 +734,20 @@ class DeMemWMMinecraft(DiffusionForcingBase):
714
  xs_pred = xs.clone()
715
  n_context_frames = min(self.context_frames // self.frame_stack, target_length)
716
  curr_frame = n_context_frames
 
 
 
 
 
 
 
 
 
 
 
717
 
718
  while curr_frame < target_length:
 
719
  horizon = min(target_length - curr_frame, self.chunk_size) if self.chunk_size > 0 else target_length - curr_frame
720
  target_slice = slice(curr_frame, curr_frame + horizon)
721
  xs_pred[target_slice] = torch.randn_like(xs_pred[target_slice]).clamp(-self.clip_noise, self.clip_noise)
@@ -822,7 +854,27 @@ class DeMemWMMinecraft(DiffusionForcingBase):
822
  packed_latents[:target_length_step] = sampled_target[:target_length_step]
823
 
824
  xs_pred[local_slice] = packed_latents[:target_length_step]
825
- curr_frame += horizon
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
826
 
827
  eval_start = n_context_frames
828
  latent_loss = F.mse_loss(xs_pred[eval_start:target_length], xs[eval_start:target_length], reduction="none")
 
1
  from collections.abc import Mapping
2
  import os
3
  import random
4
+ import time
5
  import numpy as np
6
  import torch
7
  import torch.distributed as dist
 
40
  return getattr(cfg, key, default)
41
 
42
 
43
+ def _cuda_device_index(device):
44
+ device = torch.device(device)
45
+ if device.type != "cuda" or not torch.cuda.is_available():
46
+ return None
47
+ return torch.cuda.current_device() if device.index is None else device.index
48
+
49
+
50
+ def _cuda_vram_postfix(device):
51
+ device_index = _cuda_device_index(device)
52
+ if device_index is None:
53
+ return None
54
+
55
+ gib = 1024**3
56
+ allocated = torch.cuda.memory_allocated(device_index) / gib
57
+ reserved = torch.cuda.memory_reserved(device_index) / gib
58
+ peak = torch.cuda.max_memory_allocated(device_index) / gib
59
+ return f"{allocated:.1f}/{reserved:.1f}G peak {peak:.1f}G"
60
+
61
+
62
  def _trainability_cfg(cfg):
63
  return _cfg_get(cfg, "trainability", {})
64
 
 
734
  xs_pred = xs.clone()
735
  n_context_frames = min(self.context_frames // self.frame_stack, target_length)
736
  curr_frame = n_context_frames
737
+ generated_frames = 0
738
+ rollout_start_time = time.perf_counter()
739
+ pbar = None
740
+ if curr_frame < target_length:
741
+ pbar = tqdm(
742
+ total=target_length,
743
+ initial=curr_frame,
744
+ desc=f"{namespace} sampling[{batch_idx}]",
745
+ unit="frame",
746
+ dynamic_ncols=True,
747
+ )
748
 
749
  while curr_frame < target_length:
750
+ chunk_start_time = time.perf_counter()
751
  horizon = min(target_length - curr_frame, self.chunk_size) if self.chunk_size > 0 else target_length - curr_frame
752
  target_slice = slice(curr_frame, curr_frame + horizon)
753
  xs_pred[target_slice] = torch.randn_like(xs_pred[target_slice]).clamp(-self.clip_noise, self.clip_noise)
 
854
  packed_latents[:target_length_step] = sampled_target[:target_length_step]
855
 
856
  xs_pred[local_slice] = packed_latents[:target_length_step]
857
+ chunk_seconds = max(time.perf_counter() - chunk_start_time, 1e-9)
858
+ end_frame = curr_frame + horizon
859
+ curr_frame = end_frame
860
+ generated_frames += horizon
861
+
862
+ if pbar is not None:
863
+ rollout_seconds = max(time.perf_counter() - rollout_start_time, 1e-9)
864
+ postfix = {
865
+ "range": f"{start_frame}:{end_frame}",
866
+ "sec/frame": f"{chunk_seconds / horizon:.3f}",
867
+ "frames/s": f"{horizon / chunk_seconds:.2f}",
868
+ "avg_frames/s": f"{generated_frames / rollout_seconds:.2f}",
869
+ }
870
+ vram = _cuda_vram_postfix(xs.device)
871
+ if vram is not None:
872
+ postfix["vram"] = vram
873
+ pbar.update(horizon)
874
+ pbar.set_postfix(postfix)
875
+
876
+ if pbar is not None:
877
+ pbar.close()
878
 
879
  eval_start = n_context_frames
880
  latent_loss = F.mse_loss(xs_pred[eval_start:target_length], xs[eval_start:target_length], reduction="none")