Spaces:
Running on Zero
Running on Zero
| # Copyright 2024-2025 The Alibaba Wan Team Authors. All rights reserved. | |
| import math | |
| import torch | |
| import torch.cuda.amp as amp | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from diffusers.configuration_utils import ConfigMixin, register_to_config | |
| from diffusers.models.modeling_utils import ModelMixin | |
| from .attention import flash_attention | |
| __all__ = ['SCAILModel'] | |
| T5_CONTEXT_TOKEN_NUMBER = 512 | |
| FIRST_LAST_FRAME_CONTEXT_TOKEN_NUMBER = 257 * 2 | |
| def sinusoidal_embedding_1d(dim, position): | |
| # preprocess | |
| assert dim % 2 == 0 | |
| half = dim // 2 | |
| position = position.type(torch.float64) | |
| # calculation | |
| sinusoid = torch.outer( | |
| position, torch.pow(10000, -torch.arange(half).to(position).div(half))) | |
| x = torch.cat([torch.cos(sinusoid), torch.sin(sinusoid)], dim=1) | |
| return x | |
| def rope_params(max_seq_len, dim, theta=10000): | |
| assert dim % 2 == 0 | |
| freqs = torch.outer( | |
| torch.arange(max_seq_len), | |
| 1.0 / torch.pow(theta, | |
| torch.arange(0, dim, 2).to(torch.float64).div(dim))) | |
| freqs = torch.polar(torch.ones_like(freqs), freqs) | |
| return freqs | |
| def rope_apply_ref(x, freqs, **kwargs): | |
| f = 1 | |
| h = kwargs["rope_H"] | |
| w = kwargs["rope_W"] | |
| shift_f = 0 | |
| shift_h = kwargs["rope_H_shift"] | |
| shift_w = kwargs["rope_W_shift"] | |
| n, c = x.size(2), x.size(3) // 2 | |
| # split freqs | |
| freqs = freqs.split([c - 2 * (c // 3), c // 3, c // 3], dim=1) | |
| # loop over samples | |
| output = [] | |
| for i in range(x.size(0)): | |
| seq_len = f * h * w | |
| assert seq_len == x.size(1) | |
| # precompute multipliers | |
| x_i = torch.view_as_complex(x[i, :seq_len].to(torch.float64).reshape( | |
| seq_len, n, -1, 2)) | |
| freqs_i = torch.cat([ | |
| freqs[0][shift_f:shift_f+f].view(f, 1, 1, -1).expand(f, h, w, -1), | |
| freqs[1][shift_h:shift_h+h].view(1, h, 1, -1).expand(f, h, w, -1), | |
| freqs[2][shift_w:shift_w+w].view(1, 1, w, -1).expand(f, h, w, -1) | |
| ], | |
| dim=-1).reshape(seq_len, 1, -1) | |
| # apply rotary embedding | |
| x_i = torch.view_as_real(x_i * freqs_i).flatten(2) | |
| x_i = torch.cat([x_i, x[i, seq_len:]]) | |
| # append to collection | |
| output.append(x_i) | |
| return torch.stack(output).float() | |
| def rope_apply_video(x, freqs, **kwargs): | |
| f = kwargs["rope_T"] | |
| h = kwargs["rope_H"] | |
| w = kwargs["rope_W"] | |
| shift_f = 1 # reference frame | |
| shift_h = kwargs["rope_H_shift"] | |
| shift_w = kwargs["rope_W_shift"] | |
| n, c = x.size(2), x.size(3) // 2 | |
| # split freqs | |
| freqs = freqs.split([c - 2 * (c // 3), c // 3, c // 3], dim=1) | |
| # loop over samples | |
| output = [] | |
| for i in range(x.size(0)): | |
| seq_len = f * h * w | |
| assert seq_len == x.size(1) | |
| # precompute multipliers | |
| x_i = torch.view_as_complex(x[i, :seq_len].to(torch.float64).reshape( | |
| seq_len, n, -1, 2)) | |
| freqs_i = torch.cat([ | |
| freqs[0][shift_f:shift_f+f].view(f, 1, 1, -1).expand(f, h, w, -1), | |
| freqs[1][shift_h:shift_h+h].view(1, h, 1, -1).expand(f, h, w, -1), | |
| freqs[2][shift_w:shift_w+w].view(1, 1, w, -1).expand(f, h, w, -1) | |
| ], | |
| dim=-1).reshape(seq_len, 1, -1) | |
| # apply rotary embedding | |
| x_i = torch.view_as_real(x_i * freqs_i).flatten(2) | |
| x_i = torch.cat([x_i, x[i, seq_len:]]) | |
| # append to collection | |
| output.append(x_i) | |
| return torch.stack(output).float() | |
| def rope_apply_pose(x, freqs, **kwargs): | |
| f = kwargs["rope_T"] | |
| h = kwargs["rope_H"] | |
| w = kwargs["rope_W"] | |
| shift_f = 1 # reference frame | |
| shift_h = kwargs["rope_H_shift"] + kwargs["global_rope_H"] | |
| shift_w = kwargs["rope_W_shift"] + kwargs["global_rope_W"] | |
| n, c = x.size(2), x.size(3) // 2 | |
| # split freqs | |
| freqs = freqs.split([c - 2 * (c // 3), c // 3, c // 3], dim=1) | |
| # loop over samples | |
| output = [] | |
| for i in range(x.size(0)): | |
| seq_len = f * (h // 2) * (w // 2) # downsampled | |
| assert seq_len == x.size(1) | |
| # precompute multipliers | |
| x_i = torch.view_as_complex(x[i, :seq_len].to(torch.float64).reshape( | |
| seq_len, n, -1, 2)) | |
| freqs_i = torch.cat([ | |
| freqs[0][shift_f:shift_f+f].view(f, 1, 1, -1).expand(f, h, w, -1), | |
| freqs[1][shift_h:shift_h+h].view(1, h, 1, -1).expand(f, h, w, -1), | |
| freqs[2][shift_w:shift_w+w].view(1, 1, w, -1).expand(f, h, w, -1) | |
| ], | |
| dim=-1) # T H W D | |
| assert shift_w + w <= freqs[2].size(0), f"{shift_w + w} > {freqs[2].size(0)}" | |
| # downsample | |
| freqs_i_real = F.avg_pool2d( | |
| freqs_i.real.permute(0, 3, 1, 2), kernel_size=2, stride=2 | |
| ).permute( | |
| 0, 2, 3, 1 | |
| ) # T H W D -> T D H W -> T D H/2 W/2 -> T H/2 W/2 D | |
| freqs_i_imag = F.avg_pool2d( | |
| freqs_i.imag.permute(0, 3, 1, 2), kernel_size=2, stride=2 | |
| ).permute( | |
| 0, 2, 3, 1 | |
| ) # T H W D -> T D H W -> T D H/2 W/2 -> T H/2 W/2 D | |
| freqs_i = torch.complex(freqs_i_real, freqs_i_imag) | |
| freqs_i = freqs_i.reshape(seq_len, 1, -1) | |
| # apply rotary embedding | |
| x_i = torch.view_as_real(x_i * freqs_i).flatten(2) | |
| x_i = torch.cat([x_i, x[i, seq_len:]]) | |
| # append to collection | |
| output.append(x_i) | |
| return torch.stack(output).float() | |
| def rope_apply_scail(x, **kwargs): | |
| """ | |
| x: [b, s, n, d] | |
| """ | |
| ref_length = kwargs["ref_length"] | |
| video_length = kwargs["seq_length"] | |
| pose_length = kwargs["pose_length"] | |
| x_ref = x[:, :ref_length] | |
| x_video = x[:, ref_length:ref_length+video_length] | |
| x_pose = x[:, -pose_length:] | |
| return torch.cat([ | |
| rope_apply_ref(x_ref, **kwargs), | |
| rope_apply_video(x_video, **kwargs), | |
| rope_apply_pose(x_pose, **kwargs), | |
| ], dim=1) | |
| class WanRMSNorm(nn.Module): | |
| def __init__(self, dim, eps=1e-5): | |
| super().__init__() | |
| self.dim = dim | |
| self.eps = eps | |
| self.weight = nn.Parameter(torch.ones(dim)) | |
| def forward(self, x): | |
| r""" | |
| Args: | |
| x(Tensor): Shape [B, L, C] | |
| """ | |
| return self._norm(x.float()).type_as(x) * self.weight | |
| def _norm(self, x): | |
| return x * torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + self.eps) | |
| class WanLayerNorm(nn.LayerNorm): | |
| def __init__(self, dim, eps=1e-6, elementwise_affine=False): | |
| super().__init__(dim, elementwise_affine=elementwise_affine, eps=eps) | |
| def forward(self, x): | |
| r""" | |
| Args: | |
| x(Tensor): Shape [B, L, C] | |
| """ | |
| return super().forward(x.float()).type_as(x) | |
| class WanSelfAttention(nn.Module): | |
| def __init__(self, | |
| dim, | |
| num_heads, | |
| window_size=(-1, -1), | |
| qk_norm=True, | |
| eps=1e-6): | |
| assert dim % num_heads == 0 | |
| super().__init__() | |
| self.dim = dim | |
| self.num_heads = num_heads | |
| self.head_dim = dim // num_heads | |
| self.window_size = window_size | |
| self.qk_norm = qk_norm | |
| self.eps = eps | |
| # layers | |
| self.q = nn.Linear(dim, dim) | |
| self.k = nn.Linear(dim, dim) | |
| self.v = nn.Linear(dim, dim) | |
| self.o = nn.Linear(dim, dim) | |
| self.norm_q = WanRMSNorm(dim, eps=eps) if qk_norm else nn.Identity() | |
| self.norm_k = WanRMSNorm(dim, eps=eps) if qk_norm else nn.Identity() | |
| def forward(self, x, seq_lens, rope_apply_func, **kwargs): | |
| r""" | |
| Args: | |
| x(Tensor): Shape [B, L, num_heads, C / num_heads] | |
| seq_lens(Tensor): Shape [B] | |
| grid_sizes(Tensor): Shape [B, 3], the second dimension contains (F, H, W) | |
| freqs(Tensor): Rope freqs, shape [1024, C / num_heads / 2] | |
| """ | |
| b, s, n, d = *x.shape[:2], self.num_heads, self.head_dim | |
| # query, key, value function | |
| def qkv_fn(x): | |
| q = self.norm_q(self.q(x)).view(b, s, n, d) | |
| k = self.norm_k(self.k(x)).view(b, s, n, d) | |
| v = self.v(x).view(b, s, n, d) | |
| return q, k, v | |
| q, k, v = qkv_fn(x) | |
| x = flash_attention( | |
| q=rope_apply_func(q), | |
| k=rope_apply_func(k), | |
| v=v, | |
| k_lens=seq_lens, | |
| window_size=self.window_size) | |
| # output | |
| x = x.flatten(2) | |
| x = self.o(x) | |
| return x | |
| class WanT2VCrossAttention(WanSelfAttention): | |
| def forward(self, x, context, context_lens): | |
| r""" | |
| Args: | |
| x(Tensor): Shape [B, L1, C] | |
| context(Tensor): Shape [B, L2, C] | |
| context_lens(Tensor): Shape [B] | |
| """ | |
| b, n, d = x.size(0), self.num_heads, self.head_dim | |
| # compute query, key, value | |
| q = self.norm_q(self.q(x)).view(b, -1, n, d) | |
| k = self.norm_k(self.k(context)).view(b, -1, n, d) | |
| v = self.v(context).view(b, -1, n, d) | |
| # compute attention | |
| x = flash_attention(q, k, v, k_lens=context_lens) | |
| # output | |
| x = x.flatten(2) | |
| x = self.o(x) | |
| return x | |
| class WanI2VCrossAttention(WanSelfAttention): | |
| def __init__(self, | |
| dim, | |
| num_heads, | |
| window_size=(-1, -1), | |
| qk_norm=True, | |
| eps=1e-6): | |
| super().__init__(dim, num_heads, window_size, qk_norm, eps) | |
| self.k_img = nn.Linear(dim, dim) | |
| self.v_img = nn.Linear(dim, dim) | |
| # self.alpha = nn.Parameter(torch.zeros((1, ))) | |
| self.norm_k_img = WanRMSNorm(dim, eps=eps) if qk_norm else nn.Identity() | |
| def forward(self, x, context, context_lens): | |
| r""" | |
| Args: | |
| x(Tensor): Shape [B, L1, C] | |
| context(Tensor): Shape [B, L2, C] | |
| context_lens(Tensor): Shape [B] | |
| """ | |
| image_context_length = context.shape[1] - T5_CONTEXT_TOKEN_NUMBER | |
| context_img = context[:, :image_context_length] | |
| context = context[:, image_context_length:] | |
| b, n, d = x.size(0), self.num_heads, self.head_dim | |
| # compute query, key, value | |
| q = self.norm_q(self.q(x)).view(b, -1, n, d) | |
| k = self.norm_k(self.k(context)).view(b, -1, n, d) | |
| v = self.v(context).view(b, -1, n, d) | |
| k_img = self.norm_k_img(self.k_img(context_img)).view(b, -1, n, d) | |
| v_img = self.v_img(context_img).view(b, -1, n, d) | |
| img_x = flash_attention(q, k_img, v_img, k_lens=None) | |
| # compute attention | |
| x = flash_attention(q, k, v, k_lens=context_lens) | |
| # output | |
| x = x.flatten(2) | |
| img_x = img_x.flatten(2) | |
| x = x + img_x | |
| x = self.o(x) | |
| return x | |
| WAN_CROSSATTENTION_CLASSES = { | |
| 't2v_cross_attn': WanT2VCrossAttention, | |
| 'i2v_cross_attn': WanI2VCrossAttention, | |
| } | |
| class WanAttentionBlock(nn.Module): | |
| def __init__(self, | |
| cross_attn_type, | |
| dim, | |
| ffn_dim, | |
| num_heads, | |
| window_size=(-1, -1), | |
| qk_norm=True, | |
| cross_attn_norm=False, | |
| eps=1e-6): | |
| super().__init__() | |
| self.dim = dim | |
| self.ffn_dim = ffn_dim | |
| self.num_heads = num_heads | |
| self.window_size = window_size | |
| self.qk_norm = qk_norm | |
| self.cross_attn_norm = cross_attn_norm | |
| self.eps = eps | |
| # layers | |
| self.norm1 = WanLayerNorm(dim, eps) | |
| self.self_attn = WanSelfAttention(dim, num_heads, window_size, qk_norm, | |
| eps) | |
| self.norm3 = WanLayerNorm( | |
| dim, eps, | |
| elementwise_affine=True) if cross_attn_norm else nn.Identity() | |
| self.cross_attn = WAN_CROSSATTENTION_CLASSES[cross_attn_type](dim, | |
| num_heads, | |
| (-1, -1), | |
| qk_norm, | |
| eps) | |
| self.norm2 = WanLayerNorm(dim, eps) | |
| self.ffn = nn.Sequential( | |
| nn.Linear(dim, ffn_dim), nn.GELU(approximate='tanh'), | |
| nn.Linear(ffn_dim, dim)) | |
| # modulation | |
| self.modulation = nn.Parameter(torch.randn(1, 6, dim) / dim**0.5) | |
| def forward( | |
| self, | |
| x, | |
| e, | |
| seq_lens, | |
| context, | |
| context_lens, | |
| **kwargs, # contains rope_apply_func | |
| ): | |
| r""" | |
| Args: | |
| x(Tensor): Shape [B, L, C] | |
| e(Tensor): Shape [B, 6, C] | |
| seq_lens(Tensor): Shape [B], length of each sequence in batch | |
| grid_sizes(Tensor): Shape [B, 3], the second dimension contains (F, H, W) | |
| freqs(Tensor): Rope freqs, shape [1024, C / num_heads / 2] | |
| """ | |
| assert e.dtype == torch.float32 | |
| with amp.autocast(dtype=torch.float32): | |
| e = (self.modulation + e).chunk(6, dim=1) | |
| assert e[0].dtype == torch.float32 | |
| # self-attention | |
| y = self.self_attn( | |
| self.norm1(x).float() * (1 + e[1]) + e[0], seq_lens, **kwargs) | |
| with amp.autocast(dtype=torch.float32): | |
| x = x + y * e[2] | |
| # cross-attention & ffn function | |
| def cross_attn_ffn(x, context, context_lens, e): | |
| x = x + self.cross_attn(self.norm3(x), context, context_lens) | |
| y = self.ffn(self.norm2(x).float() * (1 + e[4]) + e[3]) | |
| with amp.autocast(dtype=torch.float32): | |
| x = x + y * e[5] | |
| return x | |
| x = cross_attn_ffn(x, context, context_lens, e) | |
| return x | |
| class Head(nn.Module): | |
| def __init__(self, dim, out_dim, patch_size, eps=1e-6): | |
| super().__init__() | |
| self.dim = dim | |
| self.out_dim = out_dim | |
| self.patch_size = patch_size | |
| self.eps = eps | |
| # layers | |
| out_dim = math.prod(patch_size) * out_dim | |
| self.norm = WanLayerNorm(dim, eps) | |
| self.head = nn.Linear(dim, out_dim) | |
| # modulation | |
| self.modulation = nn.Parameter(torch.randn(1, 2, dim) / dim**0.5) | |
| def forward(self, x, e): | |
| r""" | |
| Args: | |
| x(Tensor): Shape [B, L1, C] | |
| e(Tensor): Shape [B, C] | |
| """ | |
| assert e.dtype == torch.float32 | |
| with amp.autocast(dtype=torch.float32): | |
| e = (self.modulation + e.unsqueeze(1)).chunk(2, dim=1) | |
| x = (self.head(self.norm(x) * (1 + e[1]) + e[0])) | |
| return x | |
| class MLPProj(torch.nn.Module): | |
| def __init__(self, in_dim, out_dim, flf_pos_emb=False): | |
| super().__init__() | |
| self.proj = torch.nn.Sequential( | |
| torch.nn.LayerNorm(in_dim), torch.nn.Linear(in_dim, in_dim), | |
| torch.nn.GELU(), torch.nn.Linear(in_dim, out_dim), | |
| torch.nn.LayerNorm(out_dim)) | |
| if flf_pos_emb: # NOTE: we only use this for `flf2v` | |
| self.emb_pos = nn.Parameter( | |
| torch.zeros(1, FIRST_LAST_FRAME_CONTEXT_TOKEN_NUMBER, 1280)) | |
| def forward(self, image_embeds): | |
| if hasattr(self, 'emb_pos'): | |
| bs, n, d = image_embeds.shape | |
| image_embeds = image_embeds.view(-1, 2 * n, d) | |
| image_embeds = image_embeds + self.emb_pos | |
| clip_extra_context_tokens = self.proj(image_embeds) | |
| return clip_extra_context_tokens | |
| from einops import rearrange | |
| from functools import partial, reduce | |
| from operator import mul | |
| class SCAILModel(ModelMixin, ConfigMixin): | |
| r""" | |
| SCAIL diffusion backbone. | |
| """ | |
| ignore_for_config = [ | |
| 'patch_size', 'cross_attn_norm', 'qk_norm', 'text_dim', 'window_size' | |
| ] | |
| _no_split_modules = ['WanAttentionBlock'] | |
| def __init__(self, | |
| model_type='t2v', | |
| patch_size=(1, 2, 2), | |
| text_len=512, | |
| in_dim=16, | |
| dim=2048, | |
| ffn_dim=8192, | |
| freq_dim=256, | |
| text_dim=4096, | |
| out_dim=16, | |
| num_heads=16, | |
| num_layers=32, | |
| window_size=(-1, -1), | |
| qk_norm=True, | |
| cross_attn_norm=True, | |
| pose_rope_shift=[0,0,120], # shift in (t, h, w) for pose rope embedding | |
| eps=1e-6): | |
| r""" | |
| Initialize the diffusion model backbone. | |
| Args: | |
| model_type (`str`, *optional*, defaults to 't2v'): | |
| Model variant - 't2v' (text-to-video) or 'i2v' (image-to-video) or 'flf2v' (first-last-frame-to-video) or 'vace' | |
| patch_size (`tuple`, *optional*, defaults to (1, 2, 2)): | |
| 3D patch dimensions for video embedding (t_patch, h_patch, w_patch) | |
| text_len (`int`, *optional*, defaults to 512): | |
| Fixed length for text embeddings | |
| in_dim (`int`, *optional*, defaults to 16): | |
| Input video channels (C_in) | |
| dim (`int`, *optional*, defaults to 2048): | |
| Hidden dimension of the transformer | |
| ffn_dim (`int`, *optional*, defaults to 8192): | |
| Intermediate dimension in feed-forward network | |
| freq_dim (`int`, *optional*, defaults to 256): | |
| Dimension for sinusoidal time embeddings | |
| text_dim (`int`, *optional*, defaults to 4096): | |
| Input dimension for text embeddings | |
| out_dim (`int`, *optional*, defaults to 16): | |
| Output video channels (C_out) | |
| num_heads (`int`, *optional*, defaults to 16): | |
| Number of attention heads | |
| num_layers (`int`, *optional*, defaults to 32): | |
| Number of transformer blocks | |
| window_size (`tuple`, *optional*, defaults to (-1, -1)): | |
| Window size for local attention (-1 indicates global attention) | |
| qk_norm (`bool`, *optional*, defaults to True): | |
| Enable query/key normalization | |
| cross_attn_norm (`bool`, *optional*, defaults to False): | |
| Enable cross-attention normalization | |
| eps (`float`, *optional*, defaults to 1e-6): | |
| Epsilon value for normalization layers | |
| """ | |
| super().__init__() | |
| assert model_type in ['t2v', 'i2v', 'flf2v', 'vace'] | |
| self.model_type = model_type | |
| self.patch_size = patch_size | |
| self.text_len = text_len | |
| self.in_dim = in_dim | |
| self.dim = dim | |
| self.ffn_dim = ffn_dim | |
| self.freq_dim = freq_dim | |
| self.text_dim = text_dim | |
| self.out_dim = out_dim | |
| self.num_heads = num_heads | |
| self.num_layers = num_layers | |
| self.window_size = window_size | |
| self.qk_norm = qk_norm | |
| self.cross_attn_norm = cross_attn_norm | |
| self.pose_rope_shift = pose_rope_shift | |
| self.eps = eps | |
| # embeddings | |
| self.patch_embedding = nn.Conv3d( | |
| in_dim, dim, kernel_size=patch_size, stride=patch_size) | |
| self.patch_embedding_pose = nn.Conv3d( | |
| in_dim, dim, kernel_size=patch_size, stride=patch_size) | |
| self.text_embedding = nn.Sequential( | |
| nn.Linear(text_dim, dim), nn.GELU(approximate='tanh'), | |
| nn.Linear(dim, dim)) | |
| self.time_embedding = nn.Sequential( | |
| nn.Linear(freq_dim, dim), nn.SiLU(), nn.Linear(dim, dim)) | |
| self.time_projection = nn.Sequential(nn.SiLU(), nn.Linear(dim, dim * 6)) | |
| # blocks | |
| cross_attn_type = 't2v_cross_attn' if model_type == 't2v' else 'i2v_cross_attn' | |
| self.blocks = nn.ModuleList([ | |
| WanAttentionBlock(cross_attn_type, dim, ffn_dim, num_heads, | |
| window_size, qk_norm, cross_attn_norm, eps) | |
| for _ in range(num_layers) | |
| ]) | |
| # head | |
| self.head = Head(dim, out_dim, patch_size, eps) | |
| # buffers (don't use register_buffer otherwise dtype will be changed in to()) | |
| assert (dim % num_heads) == 0 and (dim // num_heads) % 2 == 0 | |
| d = dim // num_heads | |
| self.freqs = torch.cat([ | |
| rope_params(8192, d - 4 * (d // 6)), | |
| rope_params(8192, 2 * (d // 6)), | |
| rope_params(8192, 2 * (d // 6)) | |
| ], | |
| dim=1) | |
| self.hidden_size_head = d | |
| if model_type == 'i2v' or model_type == 'flf2v': | |
| self.img_emb = MLPProj(1280, dim, flf_pos_emb=model_type == 'flf2v') | |
| # initialize weights | |
| self.init_weights() | |
| def apply_i2v_ones_masks(self, inputs: torch.Tensor, mask_dim: int = 4): | |
| b, d, t, h, w= inputs.shape | |
| mask = torch.ones(b, mask_dim, t, h, w, device=inputs.device, dtype=inputs.dtype) | |
| inputs = torch.concat([inputs, mask], dim=1) | |
| return inputs | |
| def apply_i2v_zeros_masks(self, inputs: torch.Tensor, mask_dim: int = 4): | |
| b, d, t, h, w= inputs.shape | |
| mask = torch.zeros(b, mask_dim, t, h, w, device=inputs.device, dtype=inputs.dtype) | |
| inputs = torch.concat([inputs, mask], dim=1) | |
| return inputs | |
| def merge_list_of_tensors_to_batch(self, inputs: list[torch.Tensor]): | |
| return torch.cat([u.unsqueeze(0) for u in inputs], dim=0) | |
| def forward( | |
| self, | |
| x: list[torch.Tensor], | |
| pose_latents: list[torch.Tensor], | |
| ref_latents: list[torch.Tensor], | |
| t, | |
| context, | |
| seq_len, | |
| clip_fea=None, | |
| ): | |
| r""" | |
| Forward pass through the diffusion model | |
| Args: | |
| x (List[Tensor]): | |
| List of input video tensors, each with shape [C_in, F, H, W] | |
| ref_latents (list[Tensor]): | |
| list of reference latents, each with shape [C_in, 1, H, W] | |
| pose_latents (list[Tensor]): | |
| list of downsampled pose video latents, each with shape [C_in, F, H / 2, W / 2] | |
| t (Tensor): | |
| Diffusion timesteps tensor of shape [B] | |
| context (List[Tensor]): | |
| List of text embeddings each with shape [L, C] | |
| seq_len (`int`): | |
| Maximum sequence length for positional encoding | |
| clip_fea (Tensor, *optional*): | |
| CLIP image features | |
| Returns: | |
| List[Tensor]: | |
| List of denoised video tensors with original input shapes [C_out, F, H / 8, W / 8] | |
| """ | |
| assert clip_fea is not None | |
| # params | |
| device = self.patch_embedding.weight.device | |
| if self.freqs.device != device: | |
| self.freqs = self.freqs.to(device) | |
| # TODO: support misaligned inputs | |
| x = self.merge_list_of_tensors_to_batch(x) | |
| ref_latents = self.merge_list_of_tensors_to_batch(ref_latents) | |
| pose_latents = self.merge_list_of_tensors_to_batch(pose_latents) | |
| x = self.apply_i2v_zeros_masks(x) | |
| ref_latents = self.apply_i2v_ones_masks(ref_latents) | |
| pose_latents = self.apply_i2v_ones_masks(pose_latents) | |
| B, D, T, H, W = x.shape | |
| assert pose_latents.shape[3] == H//2 | |
| assert pose_latents.shape[4] == W//2 | |
| ref_length = 1 * H * W // reduce(mul, self.patch_size) | |
| seq_length = T * ref_length | |
| pose_length = T * (H // 2) * (W // 2) // reduce(mul, self.patch_size) | |
| # embeddings | |
| x = torch.cat([ref_latents, x], dim=2) | |
| x = self.patch_embedding(x) | |
| pose_emb = self.patch_embedding_pose(pose_latents) | |
| x = torch.cat( | |
| [ | |
| rearrange(x, "b c t h w -> b (t h w) c"), | |
| rearrange(pose_emb, "b c t h w -> b (t h w) c"), | |
| ], | |
| dim=1, | |
| ) | |
| seq_lens = torch.tensor([u.size(0) for u in x], dtype=torch.long) | |
| # seq_lens is used for flash attention k_lens | |
| # time embeddings | |
| with amp.autocast(dtype=torch.float32): | |
| e = self.time_embedding( | |
| sinusoidal_embedding_1d(self.freq_dim, t).float()) | |
| e0 = self.time_projection(e).unflatten(1, (6, self.dim)) | |
| assert e.dtype == torch.float32 and e0.dtype == torch.float32 | |
| # context | |
| context_lens = None | |
| context = self.text_embedding( | |
| torch.stack([ | |
| torch.cat( | |
| [u, u.new_zeros(self.text_len - u.size(0), u.size(1))]) | |
| for u in context | |
| ])) | |
| if clip_fea is not None: | |
| context_clip = self.img_emb(clip_fea) # bs x 257 (x2) x dim | |
| context = torch.concat([context_clip, context], dim=1) | |
| rope_t = T // self.patch_size[0] | |
| rope_h = H // self.patch_size[1] | |
| rope_w = W // self.patch_size[2] | |
| # grid_sizes: | |
| # Original spatial-temporal grid dimensions before patching, | |
| # shape [B, 3] (3 dimensions correspond to F_patches, H_patches, W_patches) | |
| grid_sizes = torch.stack([torch.tensor((rope_t, rope_h, rope_w), dtype=torch.long) for _ in range(B)]) | |
| # arguments | |
| kwargs = dict( | |
| e=e0, | |
| seq_lens=seq_lens, | |
| grid_sizes=grid_sizes, | |
| freqs=self.freqs, | |
| context=context, | |
| context_lens=context_lens, | |
| ref_length=ref_length, | |
| seq_length=seq_length, | |
| pose_length=pose_length, | |
| ) | |
| kwargs["rope_T"] = rope_t | |
| kwargs["rope_H"] = rope_h | |
| kwargs["rope_W"] = rope_w | |
| kwargs["hidden_size_head"] = self.hidden_size_head | |
| kwargs["global_rope_H"] = self.pose_rope_shift[1] | |
| kwargs["global_rope_W"] = self.pose_rope_shift[2] | |
| # TODO: add shift based on rank of sequence parallelism | |
| kwargs["rope_H_shift"] = 0 | |
| kwargs["rope_W_shift"] = 0 | |
| def apply_rope_scail(x): | |
| """ | |
| x: [b, s, n, d] | |
| """ | |
| y = rope_apply_scail(x, **kwargs) | |
| return y | |
| kwargs["rope_apply_func"] = apply_rope_scail | |
| for block in self.blocks: | |
| x = block(x, **kwargs) | |
| # head | |
| x = self.head(x, e) | |
| # unpatchify | |
| x = self.unpatchify(x, grid_sizes, offset=ref_length) | |
| return [u.float() for u in x] | |
| def unpatchify(self, x, grid_sizes, offset:int= 0): | |
| r""" | |
| Reconstruct video tensors from patch embeddings. | |
| Args: | |
| x (List[Tensor]): | |
| List of patchified features, each with shape [L, C_out * prod(patch_size)] | |
| grid_sizes (Tensor): | |
| Original spatial-temporal grid dimensions before patching, | |
| shape [B, 3] (3 dimensions correspond to F_patches, H_patches, W_patches) | |
| Returns: | |
| List[Tensor]: | |
| Reconstructed video tensors with shape [C_out, F, H / 8, W / 8] | |
| """ | |
| c = self.out_dim | |
| out = [] | |
| for u, v in zip(x, grid_sizes.tolist()): | |
| # only keep denoised part of u | |
| u = u[offset:offset+math.prod(v)].view(*v, *self.patch_size, c) | |
| u = torch.einsum('fhwpqrc->cfphqwr', u) | |
| u = u.reshape(c, *[i * j for i, j in zip(v, self.patch_size)]) | |
| out.append(u) | |
| return out | |
| def init_weights(self): | |
| r""" | |
| Initialize model parameters using Xavier initialization. | |
| """ | |
| # basic init | |
| for m in self.modules(): | |
| if isinstance(m, nn.Linear): | |
| nn.init.xavier_uniform_(m.weight) | |
| if m.bias is not None: | |
| nn.init.zeros_(m.bias) | |
| # init embeddings | |
| nn.init.xavier_uniform_(self.patch_embedding.weight.flatten(1)) | |
| for m in self.text_embedding.modules(): | |
| if isinstance(m, nn.Linear): | |
| nn.init.normal_(m.weight, std=.02) | |
| for m in self.time_embedding.modules(): | |
| if isinstance(m, nn.Linear): | |
| nn.init.normal_(m.weight, std=.02) | |
| # init output layer | |
| nn.init.zeros_(self.head.head.weight) | |