Prune inactive DeMemWM inference memory streams
Browse files- algorithms/dememwm/df_video.py +102 -32
algorithms/dememwm/df_video.py
CHANGED
|
@@ -577,6 +577,72 @@ def _apply_memory_route_masks(frame_memory_masks, query_noise_levels, cfg, diffu
|
|
| 577 |
return routed
|
| 578 |
|
| 579 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
def random_transform(tensor):
|
| 581 |
"""
|
| 582 |
Apply the same random translation, rotation, and scaling to all frames in the batch.
|
|
@@ -1609,25 +1675,8 @@ class DeMemWMMinecraft(DiffusionForcingBase):
|
|
| 1609 |
for key in _DEMEMWM_STREAM_KEYS
|
| 1610 |
}
|
| 1611 |
target_conditions = target_tensors["action_conditions"][local_slice].to(device=xs.device)
|
| 1612 |
-
|
| 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 |
}
|
|
@@ -1639,14 +1688,13 @@ class DeMemWMMinecraft(DiffusionForcingBase):
|
|
| 1639 |
context_levels = from_query.new_zeros((query_offset, batch_size))
|
| 1640 |
from_target = torch.cat([context_levels, from_query], dim=0)
|
| 1641 |
to_target = torch.cat([context_levels, to_query], dim=0)
|
| 1642 |
-
|
| 1643 |
getattr(self, "cfg", None),
|
| 1644 |
self.diffusion_model,
|
| 1645 |
from_query,
|
| 1646 |
-
|
| 1647 |
mode=namespace,
|
| 1648 |
)
|
| 1649 |
-
memory_noise_levels = torch.cat([memory_noise_levels[key] for key in _DEMEMWM_STREAM_KEYS], dim=0)
|
| 1650 |
routed_frame_memory_masks = _apply_memory_route_masks(
|
| 1651 |
frame_memory_masks,
|
| 1652 |
from_query,
|
|
@@ -1654,24 +1702,46 @@ class DeMemWMMinecraft(DiffusionForcingBase):
|
|
| 1654 |
self.diffusion_model,
|
| 1655 |
mode=namespace,
|
| 1656 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1657 |
sampled_target = self.diffusion_model.sample_step(
|
| 1658 |
-
|
| 1659 |
-
|
| 1660 |
None,
|
| 1661 |
-
torch.cat([from_target,
|
| 1662 |
-
torch.cat([to_target,
|
| 1663 |
current_frame=curr_frame,
|
| 1664 |
mode=namespace,
|
| 1665 |
reference_length=0,
|
| 1666 |
-
frame_idx=
|
| 1667 |
-
frame_memory_segments=
|
| 1668 |
-
frame_memory_masks=
|
| 1669 |
-
frame_memory_pose=
|
| 1670 |
image_hw=image_hw,
|
| 1671 |
)
|
| 1672 |
-
|
| 1673 |
|
| 1674 |
-
xs_pred[local_slice] =
|
| 1675 |
chunk_seconds = max(time.perf_counter() - chunk_start_time, 1e-9)
|
| 1676 |
end_frame = curr_frame + horizon
|
| 1677 |
curr_frame = end_frame
|
|
|
|
| 577 |
return routed
|
| 578 |
|
| 579 |
|
| 580 |
+
def _pack_active_inference_memory_streams(
|
| 581 |
+
target_latents,
|
| 582 |
+
target_conditions,
|
| 583 |
+
target_poses,
|
| 584 |
+
target_frame_indices,
|
| 585 |
+
target_mask,
|
| 586 |
+
stream_latents_by_key,
|
| 587 |
+
stream_poses_by_key,
|
| 588 |
+
stream_frame_indices_by_key,
|
| 589 |
+
routed_stream_masks,
|
| 590 |
+
memory_noise_levels_by_key,
|
| 591 |
+
):
|
| 592 |
+
active_streams = []
|
| 593 |
+
pruned_streams = []
|
| 594 |
+
active_latents = [target_latents]
|
| 595 |
+
active_poses = [target_poses]
|
| 596 |
+
active_frame_indices = [target_frame_indices]
|
| 597 |
+
active_frame_memory_segments = {"target": int(target_latents.shape[0])}
|
| 598 |
+
active_frame_memory_masks = {"target": target_mask}
|
| 599 |
+
active_memory_noise_levels = []
|
| 600 |
+
active_memory_length = 0
|
| 601 |
+
|
| 602 |
+
for key in _DEMEMWM_STREAM_KEYS:
|
| 603 |
+
routed_mask = routed_stream_masks[key]
|
| 604 |
+
if bool(routed_mask.any().item()):
|
| 605 |
+
active_streams.append(key)
|
| 606 |
+
active_latents.append(stream_latents_by_key[key])
|
| 607 |
+
active_poses.append(stream_poses_by_key[key])
|
| 608 |
+
active_frame_indices.append(stream_frame_indices_by_key[key])
|
| 609 |
+
active_frame_memory_segments[key] = int(stream_latents_by_key[key].shape[0])
|
| 610 |
+
active_frame_memory_masks[key] = routed_mask
|
| 611 |
+
active_memory_noise_levels.append(memory_noise_levels_by_key[key])
|
| 612 |
+
active_memory_length += int(stream_latents_by_key[key].shape[0])
|
| 613 |
+
else:
|
| 614 |
+
pruned_streams.append(key)
|
| 615 |
+
active_frame_memory_segments[key] = 0
|
| 616 |
+
active_frame_memory_masks[key] = routed_mask[:, :0]
|
| 617 |
+
|
| 618 |
+
active_packed_latents = torch.cat(active_latents, dim=0)
|
| 619 |
+
active_packed_conditions = torch.cat(
|
| 620 |
+
[
|
| 621 |
+
target_conditions,
|
| 622 |
+
target_conditions.new_zeros((active_memory_length, target_conditions.shape[1], target_conditions.shape[-1])),
|
| 623 |
+
],
|
| 624 |
+
dim=0,
|
| 625 |
+
)
|
| 626 |
+
active_frame_memory_pose = torch.cat(active_poses, dim=0)
|
| 627 |
+
active_frame_indices = torch.cat(active_frame_indices, dim=0)
|
| 628 |
+
if active_memory_noise_levels:
|
| 629 |
+
active_memory_noise_levels = torch.cat(active_memory_noise_levels, dim=0)
|
| 630 |
+
else:
|
| 631 |
+
active_memory_noise_levels = target_frame_indices.new_zeros((0, target_frame_indices.shape[1]))
|
| 632 |
+
|
| 633 |
+
return (
|
| 634 |
+
active_packed_latents,
|
| 635 |
+
active_packed_conditions,
|
| 636 |
+
active_frame_memory_pose,
|
| 637 |
+
active_frame_indices,
|
| 638 |
+
active_frame_memory_segments,
|
| 639 |
+
active_frame_memory_masks,
|
| 640 |
+
active_memory_noise_levels,
|
| 641 |
+
active_streams,
|
| 642 |
+
pruned_streams,
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
|
| 646 |
def random_transform(tensor):
|
| 647 |
"""
|
| 648 |
Apply the same random translation, rotation, and scaling to all frames in the batch.
|
|
|
|
| 1675 |
for key in _DEMEMWM_STREAM_KEYS
|
| 1676 |
}
|
| 1677 |
target_conditions = target_tensors["action_conditions"][local_slice].to(device=xs.device)
|
| 1678 |
+
target_latents_step = xs_pred[local_slice]
|
| 1679 |
+
full_frame_memory_segments = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1680 |
"target": int(target_length_step),
|
| 1681 |
**{key: int(stream_masks_by_key[key].shape[1]) for key in _DEMEMWM_STREAM_KEYS},
|
| 1682 |
}
|
|
|
|
| 1688 |
context_levels = from_query.new_zeros((query_offset, batch_size))
|
| 1689 |
from_target = torch.cat([context_levels, from_query], dim=0)
|
| 1690 |
to_target = torch.cat([context_levels, to_query], dim=0)
|
| 1691 |
+
memory_noise_levels_by_key = _memory_noise_levels_for_streams(
|
| 1692 |
getattr(self, "cfg", None),
|
| 1693 |
self.diffusion_model,
|
| 1694 |
from_query,
|
| 1695 |
+
full_frame_memory_segments,
|
| 1696 |
mode=namespace,
|
| 1697 |
)
|
|
|
|
| 1698 |
routed_frame_memory_masks = _apply_memory_route_masks(
|
| 1699 |
frame_memory_masks,
|
| 1700 |
from_query,
|
|
|
|
| 1702 |
self.diffusion_model,
|
| 1703 |
mode=namespace,
|
| 1704 |
)
|
| 1705 |
+
(
|
| 1706 |
+
active_packed_latents,
|
| 1707 |
+
active_packed_conditions,
|
| 1708 |
+
active_frame_memory_pose,
|
| 1709 |
+
active_frame_indices,
|
| 1710 |
+
active_frame_memory_segments,
|
| 1711 |
+
active_frame_memory_masks,
|
| 1712 |
+
active_memory_noise_levels,
|
| 1713 |
+
_active_streams,
|
| 1714 |
+
_pruned_streams,
|
| 1715 |
+
) = _pack_active_inference_memory_streams(
|
| 1716 |
+
target_latents_step,
|
| 1717 |
+
target_conditions,
|
| 1718 |
+
target_poses[local_slice],
|
| 1719 |
+
target_frame_indices[local_slice],
|
| 1720 |
+
frame_memory_masks["target"],
|
| 1721 |
+
stream_latents_by_key,
|
| 1722 |
+
stream_poses_by_key,
|
| 1723 |
+
stream_frame_indices_by_key,
|
| 1724 |
+
routed_frame_memory_masks,
|
| 1725 |
+
memory_noise_levels_by_key,
|
| 1726 |
+
)
|
| 1727 |
sampled_target = self.diffusion_model.sample_step(
|
| 1728 |
+
active_packed_latents,
|
| 1729 |
+
active_packed_conditions,
|
| 1730 |
None,
|
| 1731 |
+
torch.cat([from_target, active_memory_noise_levels], dim=0),
|
| 1732 |
+
torch.cat([to_target, active_memory_noise_levels], dim=0),
|
| 1733 |
current_frame=curr_frame,
|
| 1734 |
mode=namespace,
|
| 1735 |
reference_length=0,
|
| 1736 |
+
frame_idx=active_frame_indices,
|
| 1737 |
+
frame_memory_segments=active_frame_memory_segments,
|
| 1738 |
+
frame_memory_masks=active_frame_memory_masks,
|
| 1739 |
+
frame_memory_pose=active_frame_memory_pose,
|
| 1740 |
image_hw=image_hw,
|
| 1741 |
)
|
| 1742 |
+
target_latents_step = sampled_target[:target_length_step]
|
| 1743 |
|
| 1744 |
+
xs_pred[local_slice] = target_latents_step
|
| 1745 |
chunk_seconds = max(time.perf_counter() - chunk_start_time, 1e-9)
|
| 1746 |
end_frame = curr_frame + horizon
|
| 1747 |
curr_frame = end_frame
|