BonanDing commited on
Commit
ea701fb
·
1 Parent(s): ead4a8b

Refactor DeMemWM inference memory streams by key

Browse files
Files changed (1) hide show
  1. algorithms/dememwm/df_video.py +34 -16
algorithms/dememwm/df_video.py CHANGED
@@ -1523,20 +1523,20 @@ class DeMemWMMinecraft(DiffusionForcingBase):
1523
  target_length_step = local_slice.stop - local_slice.start
1524
  query_offset = curr_frame - start_frame
1525
 
1526
- stream_indices = {
1527
  key: torch.full((batch_size, int(stream_lengths[key])), -1, device=xs.device, dtype=torch.long)
1528
  for key in _DEMEMWM_STREAM_KEYS
1529
  }
1530
- stream_masks = {key: torch.zeros_like(indices, dtype=torch.bool) for key, indices in stream_indices.items()}
1531
  target_positions = np.arange(target_slice.start, target_slice.stop, dtype=np.int64)
1532
  source_latents = xs_pred[:curr_frame]
1533
  source_actions = target_actions[:curr_frame]
1534
  source_poses = target_poses[:curr_frame]
1535
  source_frame_indices = target_frame_indices[:curr_frame]
1536
  for batch_i in range(batch_size):
1537
- anchor_count = int(stream_indices["anchor"].shape[1])
1538
- dynamic_count = int(stream_indices["dynamic"].shape[1])
1539
- revisit_count = int(stream_indices["revisit"].shape[1])
1540
  selected = {"anchor": online_anchor_indices[batch_i]}
1541
  if dynamic_policy == "recent":
1542
  selected["dynamic"] = _select_online_dynamic_indices(
@@ -1583,35 +1583,53 @@ class DeMemWMMinecraft(DiffusionForcingBase):
1583
  }
1584
  )
1585
  selected_masks = {key: np.ones(len(value), dtype=bool) for key, value in selected.items()}
1586
- for key, indices in stream_indices.items():
1587
  count = indices.shape[1]
1588
  selected_key = np.asarray(selected.get(key, []), dtype=np.int64)[:count]
1589
  mask_key = np.asarray(selected_masks.get(key, []), dtype=bool)[:count]
1590
  indices[batch_i, : len(selected_key)] = torch.as_tensor(selected_key, device=xs.device, dtype=torch.long)
1591
- stream_masks[key][batch_i, : len(mask_key)] = torch.as_tensor(mask_key, device=xs.device, dtype=torch.bool)
1592
 
1593
  frame_memory_masks = {
1594
  "target": torch.ones((batch_size, target_length_step), device=xs.device, dtype=torch.bool)
1595
  if target_mask is None
1596
  else target_mask[:, local_slice],
1597
- **stream_masks,
 
 
 
 
 
 
 
 
 
 
 
 
1598
  }
1599
- stream_latents = [_gather_online_memory_tensor(source_latents, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
1600
- stream_poses = [_gather_online_memory_tensor(source_poses, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
1601
- stream_frame_indices = [_gather_online_memory_tensor(source_frame_indices, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
1602
  target_conditions = target_tensors["action_conditions"][local_slice].to(device=xs.device)
1603
- memory_length = sum(mask.shape[1] for mask in stream_masks.values())
1604
 
1605
- packed_latents = torch.cat([xs_pred[local_slice], *stream_latents], dim=0)
 
 
 
1606
  packed_conditions = torch.cat(
1607
  [target_conditions, target_conditions.new_zeros((memory_length, batch_size, target_conditions.shape[-1]))],
1608
  dim=0,
1609
  )
1610
- frame_memory_pose = torch.cat([target_poses[local_slice], *stream_poses], dim=0)
1611
- frame_indices = torch.cat([target_frame_indices[local_slice], *stream_frame_indices], dim=0)
 
 
 
 
 
 
1612
  frame_memory_segments = {
1613
  "target": int(target_length_step),
1614
- **{key: int(stream_masks[key].shape[1]) for key in _DEMEMWM_STREAM_KEYS},
1615
  }
1616
  scheduling_matrix = self._generate_scheduling_matrix(horizon)
1617
 
 
1523
  target_length_step = local_slice.stop - local_slice.start
1524
  query_offset = curr_frame - start_frame
1525
 
1526
+ stream_indices_by_key = {
1527
  key: torch.full((batch_size, int(stream_lengths[key])), -1, device=xs.device, dtype=torch.long)
1528
  for key in _DEMEMWM_STREAM_KEYS
1529
  }
1530
+ stream_masks_by_key = {key: torch.zeros_like(indices, dtype=torch.bool) for key, indices in stream_indices_by_key.items()}
1531
  target_positions = np.arange(target_slice.start, target_slice.stop, dtype=np.int64)
1532
  source_latents = xs_pred[:curr_frame]
1533
  source_actions = target_actions[:curr_frame]
1534
  source_poses = target_poses[:curr_frame]
1535
  source_frame_indices = target_frame_indices[:curr_frame]
1536
  for batch_i in range(batch_size):
1537
+ anchor_count = int(stream_indices_by_key["anchor"].shape[1])
1538
+ dynamic_count = int(stream_indices_by_key["dynamic"].shape[1])
1539
+ revisit_count = int(stream_indices_by_key["revisit"].shape[1])
1540
  selected = {"anchor": online_anchor_indices[batch_i]}
1541
  if dynamic_policy == "recent":
1542
  selected["dynamic"] = _select_online_dynamic_indices(
 
1583
  }
1584
  )
1585
  selected_masks = {key: np.ones(len(value), dtype=bool) for key, value in selected.items()}
1586
+ for key, indices in stream_indices_by_key.items():
1587
  count = indices.shape[1]
1588
  selected_key = np.asarray(selected.get(key, []), dtype=np.int64)[:count]
1589
  mask_key = np.asarray(selected_masks.get(key, []), dtype=bool)[:count]
1590
  indices[batch_i, : len(selected_key)] = torch.as_tensor(selected_key, device=xs.device, dtype=torch.long)
1591
+ stream_masks_by_key[key][batch_i, : len(mask_key)] = torch.as_tensor(mask_key, device=xs.device, dtype=torch.bool)
1592
 
1593
  frame_memory_masks = {
1594
  "target": torch.ones((batch_size, target_length_step), device=xs.device, dtype=torch.bool)
1595
  if target_mask is None
1596
  else target_mask[:, local_slice],
1597
+ **stream_masks_by_key,
1598
+ }
1599
+ stream_latents_by_key = {
1600
+ key: _gather_online_memory_tensor(source_latents, stream_indices_by_key[key], stream_masks_by_key[key])
1601
+ for key in _DEMEMWM_STREAM_KEYS
1602
+ }
1603
+ stream_poses_by_key = {
1604
+ key: _gather_online_memory_tensor(source_poses, stream_indices_by_key[key], stream_masks_by_key[key])
1605
+ for key in _DEMEMWM_STREAM_KEYS
1606
+ }
1607
+ stream_frame_indices_by_key = {
1608
+ key: _gather_online_memory_tensor(source_frame_indices, stream_indices_by_key[key], stream_masks_by_key[key])
1609
+ for key in _DEMEMWM_STREAM_KEYS
1610
  }
 
 
 
1611
  target_conditions = target_tensors["action_conditions"][local_slice].to(device=xs.device)
1612
+ memory_length = sum(stream_masks_by_key[key].shape[1] for key in _DEMEMWM_STREAM_KEYS)
1613
 
1614
+ packed_latents = torch.cat(
1615
+ [xs_pred[local_slice], *[stream_latents_by_key[key] for key in _DEMEMWM_STREAM_KEYS]],
1616
+ dim=0,
1617
+ )
1618
  packed_conditions = torch.cat(
1619
  [target_conditions, target_conditions.new_zeros((memory_length, batch_size, target_conditions.shape[-1]))],
1620
  dim=0,
1621
  )
1622
+ frame_memory_pose = torch.cat(
1623
+ [target_poses[local_slice], *[stream_poses_by_key[key] for key in _DEMEMWM_STREAM_KEYS]],
1624
+ dim=0,
1625
+ )
1626
+ frame_indices = torch.cat(
1627
+ [target_frame_indices[local_slice], *[stream_frame_indices_by_key[key] for key in _DEMEMWM_STREAM_KEYS]],
1628
+ dim=0,
1629
+ )
1630
  frame_memory_segments = {
1631
  "target": int(target_length_step),
1632
+ **{key: int(stream_masks_by_key[key].shape[1]) for key in _DEMEMWM_STREAM_KEYS},
1633
  }
1634
  scheduling_matrix = self._generate_scheduling_matrix(horizon)
1635