File size: 3,363 Bytes
a863b67 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | # Motion blocks: map the tokens of a source view toward a target view (its viewpoint and time).
#
# Adapted from the MotionDecoder of 4RC (https://github.com/Luo-Yihang/4RC, S-Lab License 1.0).
#
# Changes w.r.t. 4RC: the blocks operate on all geometry tokens (cls, time, patches) rather than
# on patches only, have no positional embedding, are conditioned (AdaLN) on the time token of the
# target view, and are split into groups, one per read-out layer of the geometry blocks. Both
# directions of every (source, target) pair are evaluated in one batch.
import torch
import torch.nn as nn
from torch.utils.checkpoint import checkpoint
from .layers import AdaLNBlock, CrossBlock
class MotionBlock(nn.Module):
"""Cross-attention from the source tokens to the target tokens, then self-attention with
AdaLN modulated by the target's time token."""
def __init__(self, dim: int, num_heads: int, mlp_ratio: float = 4.0, init_values: float = 0.01):
super().__init__()
self.cross_attn = CrossBlock(dim, num_heads, mlp_ratio=mlp_ratio, init_values=init_values, qk_norm=True)
self.self_attn = AdaLNBlock(dim, num_heads, mlp_ratio=mlp_ratio, qk_norm=True)
def forward(self, src: torch.Tensor, tgt: torch.Tensor, time_cond: torch.Tensor) -> torch.Tensor:
if self.training:
src = checkpoint(self.cross_attn, src, tgt, use_reentrant=False)
return checkpoint(self.self_attn, src, time_cond, use_reentrant=False)
return self.self_attn(self.cross_attn(src, tgt), time_cond)
class MotionBlocks(nn.ModuleList):
"""`depth` motion blocks, `depth // num_levels` per read-out layer.
Given the tokens of S views and the target of each view, returns tokens for 2S queries:
[ view i toward target_idx[i] ] for i in 0..S-1, then
[ view target_idx[i] toward i ] for i in 0..S-1.
"""
def __init__(self, dim: int = 1536, depth: int = 8, num_heads: int = 16, mlp_ratio: float = 4.0,
init_values: float = 0.01, num_levels: int = 4):
assert depth % num_levels == 0
super().__init__([MotionBlock(dim, num_heads, mlp_ratio, init_values) for _ in range(depth)])
self.blocks_per_level = depth // num_levels
def forward(self, tokens: torch.Tensor, target_idx: torch.Tensor, level: int,
patch_start_idx: int = 2) -> torch.Tensor:
"""
Args:
tokens: (B, S, N, C) geometry tokens [cls, time, patches...] of one read-out layer.
target_idx: (B, S) long, the target view of each view.
level: index of the read-out layer.
Returns:
(B*2S, P, C) motion tokens of the patches.
"""
B, S, N, C = tokens.shape
tokens_tgt = torch.gather(tokens, dim=1, index=target_idx[:, :, None, None].expand(-1, -1, N, C))
src = torch.cat([tokens, tokens_tgt], dim=1).flatten(0, 1) # (B*2S, N, C) the views to move
tgt = torch.cat([tokens_tgt, tokens], dim=1) # (B, 2S, N, C) the views to move toward
time_cond = tgt[:, :, patch_start_idx - 1].flatten(0, 1) # time token of the target, (B*2S, C)
tgt = tgt.flatten(0, 1)
for i in range(self.blocks_per_level * level, self.blocks_per_level * (level + 1)):
src = self[i](src, tgt, time_cond)
return src[:, patch_start_idx:].contiguous()
|