Fix DeMemWM validation rollout packing
Browse files- algorithms/dememwm/df_video.py +58 -13
algorithms/dememwm/df_video.py
CHANGED
|
@@ -212,6 +212,33 @@ def _gather_online_memory_tensor(source, indices, masks):
|
|
| 212 |
return gathered * mask.to(dtype=gathered.dtype)
|
| 213 |
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
def _shift_memory_noise_levels(levels, shift: float, diffusion_model, bounds):
|
| 216 |
if levels.numel() == 0 or float(shift) == 0.0 or not hasattr(diffusion_model, "snr"):
|
| 217 |
return levels
|
|
@@ -1057,6 +1084,10 @@ class DeMemWMMinecraft(DiffusionForcingBase):
|
|
| 1057 |
horizon = min(target_length - curr_frame, self.chunk_size) if self.chunk_size > 0 else target_length - curr_frame
|
| 1058 |
target_slice = slice(curr_frame, curr_frame + horizon)
|
| 1059 |
xs_pred[target_slice] = torch.randn_like(xs_pred[target_slice]).clamp(-self.clip_noise, self.clip_noise)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1060 |
|
| 1061 |
stream_indices = {
|
| 1062 |
key: torch.full((batch_size, int(stream_lengths[key])), -1, device=xs.device, dtype=torch.long)
|
|
@@ -1071,6 +1102,17 @@ class DeMemWMMinecraft(DiffusionForcingBase):
|
|
| 1071 |
memory_selection_cfg,
|
| 1072 |
split=namespace,
|
| 1073 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1074 |
for key, indices in stream_indices.items():
|
| 1075 |
count = indices.shape[1]
|
| 1076 |
selected_key = np.asarray(selected.get(key, []), dtype=np.int64)[:count]
|
|
@@ -1082,44 +1124,47 @@ class DeMemWMMinecraft(DiffusionForcingBase):
|
|
| 1082 |
source_poses = target_poses[:curr_frame]
|
| 1083 |
source_frame_indices = target_frame_indices[:curr_frame]
|
| 1084 |
frame_memory_masks = {
|
| 1085 |
-
"target": torch.ones((batch_size,
|
| 1086 |
if target_mask is None
|
| 1087 |
-
else target_mask[:,
|
| 1088 |
**stream_masks,
|
| 1089 |
}
|
| 1090 |
stream_latents = [_gather_online_memory_tensor(source_latents, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
|
| 1091 |
stream_poses = [_gather_online_memory_tensor(source_poses, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
|
| 1092 |
stream_frame_indices = [_gather_online_memory_tensor(source_frame_indices, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
|
| 1093 |
-
target_conditions = target_tensors["action_conditions"][
|
| 1094 |
memory_length = sum(mask.shape[1] for mask in stream_masks.values())
|
| 1095 |
|
| 1096 |
-
packed_latents = torch.cat([xs_pred[
|
| 1097 |
packed_conditions = torch.cat(
|
| 1098 |
[target_conditions, target_conditions.new_zeros((memory_length, batch_size, target_conditions.shape[-1]))],
|
| 1099 |
dim=0,
|
| 1100 |
)
|
| 1101 |
-
frame_memory_pose = torch.cat([target_poses[
|
| 1102 |
-
frame_indices = torch.cat([target_frame_indices[
|
| 1103 |
frame_memory_segments = {
|
| 1104 |
-
"target": int(
|
| 1105 |
**{key: int(stream_masks[key].shape[1]) for key in _DEMEMWM_STREAM_KEYS},
|
| 1106 |
}
|
| 1107 |
scheduling_matrix = self._generate_scheduling_matrix(horizon)
|
| 1108 |
|
| 1109 |
for m in range(scheduling_matrix.shape[0] - 1):
|
| 1110 |
-
|
| 1111 |
-
|
|
|
|
|
|
|
|
|
|
| 1112 |
memory_noise_levels = _memory_noise_levels_for_streams(
|
| 1113 |
getattr(self, "cfg", None),
|
| 1114 |
self.diffusion_model,
|
| 1115 |
-
|
| 1116 |
frame_memory_segments,
|
| 1117 |
mode=namespace,
|
| 1118 |
)
|
| 1119 |
memory_noise_levels = torch.cat([memory_noise_levels[key] for key in _DEMEMWM_STREAM_KEYS], dim=0)
|
| 1120 |
routed_frame_memory_masks = _apply_memory_route_masks(
|
| 1121 |
frame_memory_masks,
|
| 1122 |
-
|
| 1123 |
getattr(self, "cfg", None),
|
| 1124 |
self.diffusion_model,
|
| 1125 |
mode=namespace,
|
|
@@ -1139,9 +1184,9 @@ class DeMemWMMinecraft(DiffusionForcingBase):
|
|
| 1139 |
frame_memory_pose=frame_memory_pose,
|
| 1140 |
image_hw=image_hw,
|
| 1141 |
)
|
| 1142 |
-
packed_latents[:
|
| 1143 |
|
| 1144 |
-
xs_pred[
|
| 1145 |
curr_frame += horizon
|
| 1146 |
|
| 1147 |
eval_start = n_context_frames
|
|
|
|
| 212 |
return gathered * mask.to(dtype=gathered.dtype)
|
| 213 |
|
| 214 |
|
| 215 |
+
def _select_online_anchor_indices(curr_frame: int, n_context_frames: int, count: int, cfg) -> np.ndarray:
|
| 216 |
+
if count <= 0:
|
| 217 |
+
return np.empty((0,), dtype=np.int64)
|
| 218 |
+
candidates = np.arange(0, min(int(curr_frame), int(n_context_frames)), dtype=np.int64)
|
| 219 |
+
if len(candidates) == 0:
|
| 220 |
+
return candidates
|
| 221 |
+
if not bool(_cfg_get(cfg, "anchor_diverse_selection", True)) or len(candidates) <= count:
|
| 222 |
+
return candidates[:count].astype(np.int64)
|
| 223 |
+
|
| 224 |
+
positions = np.linspace(0, len(candidates) - 1, num=count, dtype=np.int64)
|
| 225 |
+
selected = list(dict.fromkeys(candidates[positions].tolist()))
|
| 226 |
+
if len(selected) < count:
|
| 227 |
+
for idx in candidates:
|
| 228 |
+
if int(idx) not in selected:
|
| 229 |
+
selected.append(int(idx))
|
| 230 |
+
if len(selected) == count:
|
| 231 |
+
break
|
| 232 |
+
return np.asarray(selected[:count], dtype=np.int64)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def _select_online_dynamic_indices(start_frame: int, count: int) -> np.ndarray:
|
| 236 |
+
if count <= 0 or start_frame <= 0:
|
| 237 |
+
return np.empty((0,), dtype=np.int64)
|
| 238 |
+
start = max(0, int(start_frame) - int(count))
|
| 239 |
+
return np.arange(start, int(start_frame), dtype=np.int64)[-count:]
|
| 240 |
+
|
| 241 |
+
|
| 242 |
def _shift_memory_noise_levels(levels, shift: float, diffusion_model, bounds):
|
| 243 |
if levels.numel() == 0 or float(shift) == 0.0 or not hasattr(diffusion_model, "snr"):
|
| 244 |
return levels
|
|
|
|
| 1084 |
horizon = min(target_length - curr_frame, self.chunk_size) if self.chunk_size > 0 else target_length - curr_frame
|
| 1085 |
target_slice = slice(curr_frame, curr_frame + horizon)
|
| 1086 |
xs_pred[target_slice] = torch.randn_like(xs_pred[target_slice]).clamp(-self.clip_noise, self.clip_noise)
|
| 1087 |
+
start_frame = max(0, curr_frame + horizon - self.n_tokens)
|
| 1088 |
+
local_slice = slice(start_frame, curr_frame + horizon)
|
| 1089 |
+
target_length_step = local_slice.stop - local_slice.start
|
| 1090 |
+
query_offset = curr_frame - start_frame
|
| 1091 |
|
| 1092 |
stream_indices = {
|
| 1093 |
key: torch.full((batch_size, int(stream_lengths[key])), -1, device=xs.device, dtype=torch.long)
|
|
|
|
| 1102 |
memory_selection_cfg,
|
| 1103 |
split=namespace,
|
| 1104 |
)
|
| 1105 |
+
anchor_count = int(stream_indices["anchor"].shape[1])
|
| 1106 |
+
dynamic_count = int(stream_indices["dynamic"].shape[1])
|
| 1107 |
+
selected["anchor"] = _select_online_anchor_indices(
|
| 1108 |
+
curr_frame,
|
| 1109 |
+
n_context_frames,
|
| 1110 |
+
anchor_count,
|
| 1111 |
+
memory_selection_cfg,
|
| 1112 |
+
)
|
| 1113 |
+
selected_masks["anchor"] = np.ones(len(selected["anchor"]), dtype=bool)
|
| 1114 |
+
selected["dynamic"] = _select_online_dynamic_indices(start_frame, dynamic_count)
|
| 1115 |
+
selected_masks["dynamic"] = np.ones(len(selected["dynamic"]), dtype=bool)
|
| 1116 |
for key, indices in stream_indices.items():
|
| 1117 |
count = indices.shape[1]
|
| 1118 |
selected_key = np.asarray(selected.get(key, []), dtype=np.int64)[:count]
|
|
|
|
| 1124 |
source_poses = target_poses[:curr_frame]
|
| 1125 |
source_frame_indices = target_frame_indices[:curr_frame]
|
| 1126 |
frame_memory_masks = {
|
| 1127 |
+
"target": torch.ones((batch_size, target_length_step), device=xs.device, dtype=torch.bool)
|
| 1128 |
if target_mask is None
|
| 1129 |
+
else target_mask[:, local_slice],
|
| 1130 |
**stream_masks,
|
| 1131 |
}
|
| 1132 |
stream_latents = [_gather_online_memory_tensor(source_latents, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
|
| 1133 |
stream_poses = [_gather_online_memory_tensor(source_poses, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
|
| 1134 |
stream_frame_indices = [_gather_online_memory_tensor(source_frame_indices, stream_indices[key], stream_masks[key]) for key in _DEMEMWM_STREAM_KEYS]
|
| 1135 |
+
target_conditions = target_tensors["action_conditions"][local_slice].to(device=xs.device)
|
| 1136 |
memory_length = sum(mask.shape[1] for mask in stream_masks.values())
|
| 1137 |
|
| 1138 |
+
packed_latents = torch.cat([xs_pred[local_slice], *stream_latents], dim=0)
|
| 1139 |
packed_conditions = torch.cat(
|
| 1140 |
[target_conditions, target_conditions.new_zeros((memory_length, batch_size, target_conditions.shape[-1]))],
|
| 1141 |
dim=0,
|
| 1142 |
)
|
| 1143 |
+
frame_memory_pose = torch.cat([target_poses[local_slice], *stream_poses], dim=0)
|
| 1144 |
+
frame_indices = torch.cat([target_frame_indices[local_slice], *stream_frame_indices], dim=0)
|
| 1145 |
frame_memory_segments = {
|
| 1146 |
+
"target": int(target_length_step),
|
| 1147 |
**{key: int(stream_masks[key].shape[1]) for key in _DEMEMWM_STREAM_KEYS},
|
| 1148 |
}
|
| 1149 |
scheduling_matrix = self._generate_scheduling_matrix(horizon)
|
| 1150 |
|
| 1151 |
for m in range(scheduling_matrix.shape[0] - 1):
|
| 1152 |
+
from_query = torch.as_tensor(scheduling_matrix[m], device=xs.device, dtype=torch.long)[:, None].repeat(1, batch_size)
|
| 1153 |
+
to_query = torch.as_tensor(scheduling_matrix[m + 1], device=xs.device, dtype=torch.long)[:, None].repeat(1, batch_size)
|
| 1154 |
+
context_levels = from_query.new_zeros((query_offset, batch_size))
|
| 1155 |
+
from_target = torch.cat([context_levels, from_query], dim=0)
|
| 1156 |
+
to_target = torch.cat([context_levels, to_query], dim=0)
|
| 1157 |
memory_noise_levels = _memory_noise_levels_for_streams(
|
| 1158 |
getattr(self, "cfg", None),
|
| 1159 |
self.diffusion_model,
|
| 1160 |
+
from_query,
|
| 1161 |
frame_memory_segments,
|
| 1162 |
mode=namespace,
|
| 1163 |
)
|
| 1164 |
memory_noise_levels = torch.cat([memory_noise_levels[key] for key in _DEMEMWM_STREAM_KEYS], dim=0)
|
| 1165 |
routed_frame_memory_masks = _apply_memory_route_masks(
|
| 1166 |
frame_memory_masks,
|
| 1167 |
+
from_query,
|
| 1168 |
getattr(self, "cfg", None),
|
| 1169 |
self.diffusion_model,
|
| 1170 |
mode=namespace,
|
|
|
|
| 1184 |
frame_memory_pose=frame_memory_pose,
|
| 1185 |
image_hw=image_hw,
|
| 1186 |
)
|
| 1187 |
+
packed_latents[:target_length_step] = sampled_target[:target_length_step]
|
| 1188 |
|
| 1189 |
+
xs_pred[local_slice] = packed_latents[:target_length_step]
|
| 1190 |
curr_frame += horizon
|
| 1191 |
|
| 1192 |
eval_start = n_context_frames
|