Spaces:
Sleeping
Sleeping
File size: 6,511 Bytes
6e6f7d1 b4bbb92 6e6f7d1 b4bbb92 6e6f7d1 b4bbb92 6e6f7d1 b4bbb92 6e6f7d1 b4bbb92 6e6f7d1 | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | import random
from typing import Literal, Sequence
import torch
import torch.nn as nn
import torch.nn.functional as F
from accel_hydra.utils.torch import create_mask_from_length
from x_transformers.x_transformers import Encoder
from ..mmdit.modules import ChannelLastConv1d, ConvMLP, MLP
class CLIPEncoder(nn.Module):
def __init__(
self,
video_feat_dim: int,
embed_dim: int,
):
super().__init__()
self.mlp = nn.Linear(video_feat_dim, embed_dim)
self.init_weights()
def init_weights(self):
def _init_weights(module):
if isinstance(module, nn.Linear):
nn.init.xavier_uniform_(module.weight)
if module.bias is not None:
nn.init.constant_(module.bias, 0.)
self.apply(_init_weights)
def forward(
self, clip: torch.Tensor, clip_lengths: Sequence[int], **kwargs
):
device = clip.device
x = F.normalize(clip, p=2, dim=-1)
x = self.mlp(x)
mask = create_mask_from_length(clip_lengths).to(device)
return {
"ta_output": x,
"ta_mask": mask,
"nta_output": None,
"nta_mask": None
}
class CLIPInterpolateVideoEncoder(nn.Module):
def __init__(
self,
video_feat_dim: int,
embed_dim: int,
latent_token_rate: int,
transformer_cfg: dict | None = None,
):
super().__init__()
self.mlp = nn.Linear(video_feat_dim, embed_dim)
self.latent_token_rate = latent_token_rate
self.transformer_enhancer = None
if transformer_cfg is not None:
self.transformer_enhancer = Encoder(
dim=embed_dim,
**transformer_cfg,
)
self.init_weights()
def init_weights(self):
def _init_weights(module):
if isinstance(module, nn.Linear):
nn.init.xavier_uniform_(module.weight)
if module.bias is not None:
nn.init.constant_(module.bias, 0.)
self.apply(_init_weights)
def forward(self, clip: torch.Tensor, duration: Sequence[float], **kwargs):
device = clip.device
x = F.normalize(clip, p=2, dim=-1)
x = self.mlp(x)
target_len = int(max(duration) * self.latent_token_rate)
x = x.transpose(1, 2)
x = F.interpolate(x, target_len, mode='nearest-exact')
x = x.transpose(1, 2)
latent_length = torch.as_tensor(duration) * self.latent_token_rate
mask = create_mask_from_length(latent_length).to(device)
if self.transformer_enhancer is not None:
x = self.transformer_enhancer(x, mask=mask)
return {
"ta_output": x,
"ta_mask": mask,
"nta_output": None,
"nta_mask": None
}
class CLIPSyncEncoder(nn.Module):
def __init__(
self,
clip_dim: int,
sync_dim: int,
embed_dim: int,
latent_token_rate: int,
fusion: Literal["add", "concat"] = "add",
sync_drop_ratio: float = 0.0,
) -> None:
super().__init__()
self.fusion = fusion
self.sync_drop_ratio = sync_drop_ratio
self.clip_proj = nn.Linear(clip_dim, embed_dim)
self.sync_proj = nn.Linear(sync_dim, embed_dim)
self.sync_pos_emb = nn.Parameter(torch.zeros((1, 1, 8, sync_dim)))
if fusion == "concat":
self.fusion_proj = nn.Linear(embed_dim * 2, embed_dim)
self.sync_frame_rate = 25
self.sync_num_frames_per_segment = 16
self.sync_step_size = 8
self.sync_downsample_rate = 2
self.latent_token_rate = latent_token_rate
self.initialize_weights()
def initialize_weights(self):
def _basic_init(module):
if isinstance(module, nn.Linear):
torch.nn.init.xavier_uniform_(module.weight)
if module.bias is not None:
nn.init.constant_(module.bias, 0)
self.apply(_basic_init)
def get_sync_seq_len(self, duration):
num_frames = duration * self.sync_frame_rate
num_segments = (
num_frames - self.sync_num_frames_per_segment
) // self.sync_step_size + 1
sync_seq_len = int(
num_segments * self.sync_num_frames_per_segment /
self.sync_downsample_rate
)
return sync_seq_len
def forward(
self, clip: torch.Tensor, sync: torch.Tensor,
duration: Sequence[float], clip_lengths: Sequence[int],
sync_lengths: Sequence[int], **kwargs
):
clip = F.normalize(clip, p=2, dim=-1)
sync = F.normalize(sync, p=2, dim=-1)
latent_length = (torch.as_tensor(duration) *
self.latent_token_rate).int()
ta_mask = create_mask_from_length(latent_length).to(sync.device)
bs = clip.shape[0]
# target_len = int(max(duration) * self.latent_token_rate)
target_len = latent_length.max()
sync_seq_len = self.get_sync_seq_len(max(duration))
# B * num_segments (24) * 8 * 768
num_sync_segments = sync_seq_len // 8
sync = sync.view(bs, num_sync_segments, 8, -1) + self.sync_pos_emb
sync = sync.flatten(1, 2) # (B, VN, D)
# extend vf to match x
clip = self.clip_proj(clip)
sync = self.sync_proj(sync) # (B, D, VN)
# upsample the sync features to match the audio
sync = sync.transpose(1, 2) # (B, D, VN)
sync = F.interpolate(sync, size=target_len, mode='nearest-exact')
sync = sync.transpose(1, 2) # (B, N, D)
if self.training:
if self.sync_drop_ratio > 0:
if random.random() < self.sync_drop_ratio:
sync = torch.zeros_like(sync)
# upsample the clip features to match the audio
clip = clip.transpose(1, 2) # (B, D, VN)
clip = F.interpolate(clip, size=target_len, mode='nearest-exact')
clip = clip.transpose(1, 2) # (B, N, D)
if self.fusion == "add":
ta_output = clip + sync
elif self.fusion == "concat":
ta_output = self.fusion_proj(torch.cat([clip, sync], dim=-1))
else:
raise ValueError(f"Invalid fusion method: {self.fusion}")
return {
"ta_output": ta_output,
"ta_mask": ta_mask,
"nta_output": None,
"nta_mask": None
}
|