Download flow4rt/model/motion_blocks.py from Shenhan/Flow4RT: direct link, hf CLI and curl.
- Browser
- Download file 3.36 kB
-
https://huggingface.co/spaces/Shenhan/Flow4RT/resolve/main/flow4rt/model/motion_blocks.py
- Command line
-
hf download hf://spaces/Shenhan/Flow4RT/flow4rt/model/motion_blocks.py
-
curl -L -o motion_blocks.py https://huggingface.co/spaces/Shenhan/Flow4RT/resolve/main/flow4rt/model/motion_blocks.py
3.36 kB
| # 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() | |