BonanDing commited on
Commit
199b3f1
·
1 Parent(s): 8904839

Warn on missing DeMemWM Plucker geometry

Browse files
Files changed (1) hide show
  1. algorithms/dememwm/models/dit.py +54 -3
algorithms/dememwm/models/dit.py CHANGED
@@ -6,6 +6,7 @@ References:
6
  """
7
 
8
  from typing import Optional, Literal
 
9
  import torch
10
  from torch import nn
11
  from torch.nn import functional as F
@@ -19,6 +20,16 @@ from collections import namedtuple
19
  from typing import Optional, Callable
20
  from .cameractrl_module import SimpleCameraPoseEncoder
21
 
 
 
 
 
 
 
 
 
 
 
22
  def modulate(x, shift, scale):
23
  fixed_dims = [1] * len(shift.shape[1:])
24
  shift = shift.repeat(x.shape[0] // shift.shape[0], *fixed_dims)
@@ -173,10 +184,18 @@ class FrameMemoryReferenceAttention(nn.Module):
173
  query_rays = geometry_cache.get("query_rays")
174
  relative_rays = geometry_cache.get("relative_rays")
175
  if not torch.is_tensor(query_rays) or not torch.is_tensor(relative_rays):
 
 
 
 
176
  return None, None
177
- if tuple(query_rays.shape) != (B, T_target, H, W, 6):
178
- return None, None
179
- if tuple(relative_rays.shape) != (B, T_target, T_memory, H, W, 6):
 
 
 
 
180
  return None, None
181
  return (
182
  query_rays.to(device=device, dtype=dtype),
@@ -590,17 +609,49 @@ class DiT(nn.Module):
590
 
591
  def _build_frame_memory_geometry(self, frame_memory_segments, frame_memory_masks, frame_memory_pose, image_hw, grid_h, grid_w, device, dtype):
592
  if frame_memory_segments is None or frame_memory_pose is None or image_hw is None:
 
 
 
 
593
  return None
594
 
595
  pose = frame_memory_pose.to(device=device, dtype=dtype)
 
 
 
 
 
 
596
  B = pose.shape[0]
597
  target_frames = int(frame_memory_segments.get("target", 0))
 
 
 
 
 
 
 
 
 
 
598
  image_hw = image_hw if torch.is_tensor(image_hw) else torch.as_tensor(image_hw)
599
  image_hw = image_hw.to(device=device, dtype=dtype)
600
  if image_hw.ndim == 1:
601
  image_hw = image_hw.unsqueeze(0)
 
 
 
 
 
 
602
  if image_hw.shape[0] == 1 and B != 1:
603
  image_hw = image_hw.expand(B, -1)
 
 
 
 
 
 
604
  image_hw = image_hw.contiguous()
605
  image_h, image_w = image_hw[:, 0].view(B, 1, 1), image_hw[:, 1].view(B, 1, 1)
606
 
 
6
  """
7
 
8
  from typing import Optional, Literal
9
+ import logging
10
  import torch
11
  from torch import nn
12
  from torch.nn import functional as F
 
20
  from typing import Optional, Callable
21
  from .cameractrl_module import SimpleCameraPoseEncoder
22
 
23
+ logger = logging.getLogger(__name__)
24
+ _FRAME_MEMORY_GEOMETRY_WARNINGS = set()
25
+
26
+
27
+ def _warn_frame_memory_geometry_once(key, message):
28
+ if key not in _FRAME_MEMORY_GEOMETRY_WARNINGS:
29
+ _FRAME_MEMORY_GEOMETRY_WARNINGS.add(key)
30
+ logger.warning(message)
31
+
32
+
33
  def modulate(x, shift, scale):
34
  fixed_dims = [1] * len(shift.shape[1:])
35
  shift = shift.repeat(x.shape[0] // shift.shape[0], *fixed_dims)
 
184
  query_rays = geometry_cache.get("query_rays")
185
  relative_rays = geometry_cache.get("relative_rays")
186
  if not torch.is_tensor(query_rays) or not torch.is_tensor(relative_rays):
187
+ _warn_frame_memory_geometry_once(
188
+ "missing_ray_tensors",
189
+ "DeMemWM Plucker memory geometry is missing query/relative ray tensors; using content-only memory attention.",
190
+ )
191
  return None, None
192
+ expected_query_shape = (B, T_target, H, W, 6)
193
+ expected_relative_shape = (B, T_target, T_memory, H, W, 6)
194
+ if tuple(query_rays.shape) != expected_query_shape or tuple(relative_rays.shape) != expected_relative_shape:
195
+ _warn_frame_memory_geometry_once(
196
+ "malformed_ray_tensors",
197
+ "DeMemWM Plucker memory geometry has unexpected ray tensor shapes; using content-only memory attention.",
198
+ )
199
  return None, None
200
  return (
201
  query_rays.to(device=device, dtype=dtype),
 
609
 
610
  def _build_frame_memory_geometry(self, frame_memory_segments, frame_memory_masks, frame_memory_pose, image_hw, grid_h, grid_w, device, dtype):
611
  if frame_memory_segments is None or frame_memory_pose is None or image_hw is None:
612
+ _warn_frame_memory_geometry_once(
613
+ "missing_geometry_metadata",
614
+ "DeMemWM Plucker memory geometry metadata is missing; using content-only memory attention.",
615
+ )
616
  return None
617
 
618
  pose = frame_memory_pose.to(device=device, dtype=dtype)
619
+ if pose.ndim != 3 or pose.shape[-1] != 5:
620
+ _warn_frame_memory_geometry_once(
621
+ "malformed_pose_metadata",
622
+ "DeMemWM Plucker memory geometry pose metadata has an unexpected shape; using content-only memory attention.",
623
+ )
624
+ return None
625
  B = pose.shape[0]
626
  target_frames = int(frame_memory_segments.get("target", 0))
627
+ expected_pose_frames = target_frames + sum(
628
+ int(frame_memory_segments.get(stream_name, 0))
629
+ for stream_name in ("anchor", "dynamic", "revisit")
630
+ )
631
+ if pose.shape[1] < expected_pose_frames:
632
+ _warn_frame_memory_geometry_once(
633
+ "short_pose_metadata",
634
+ "DeMemWM Plucker memory geometry pose metadata does not cover the packed memory segments; using content-only memory attention.",
635
+ )
636
+ return None
637
  image_hw = image_hw if torch.is_tensor(image_hw) else torch.as_tensor(image_hw)
638
  image_hw = image_hw.to(device=device, dtype=dtype)
639
  if image_hw.ndim == 1:
640
  image_hw = image_hw.unsqueeze(0)
641
+ if image_hw.ndim != 2 or image_hw.shape[-1] < 2:
642
+ _warn_frame_memory_geometry_once(
643
+ "malformed_image_hw_metadata",
644
+ "DeMemWM Plucker memory geometry image size metadata has an unexpected shape; using content-only memory attention.",
645
+ )
646
+ return None
647
  if image_hw.shape[0] == 1 and B != 1:
648
  image_hw = image_hw.expand(B, -1)
649
+ if image_hw.shape[0] != B:
650
+ _warn_frame_memory_geometry_once(
651
+ "mismatched_image_hw_metadata",
652
+ "DeMemWM Plucker memory geometry image size metadata does not match the batch; using content-only memory attention.",
653
+ )
654
+ return None
655
  image_hw = image_hw.contiguous()
656
  image_h, image_w = image_hw[:, 0].view(B, 1, 1), image_hw[:, 1].view(B, 1, 1)
657