Instructions to use diffusers/matrix-game-2-modular with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use diffusers/matrix-game-2-modular with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("diffusers/matrix-game-2-modular", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
| # Copyright 2024-2025 The Alibaba MatrixGameWan Team Authors. All rights reserved. | |
| import math | |
| import numpy as np | |
| import torch | |
| import torch.amp as amp | |
| import torch.nn as nn | |
| from diffusers.configuration_utils import ConfigMixin, register_to_config | |
| from diffusers.loaders import FromOriginalModelMixin, PeftAdapterMixin | |
| from diffusers.models.modeling_utils import ModelMixin | |
| from einops import repeat, rearrange | |
| from .action_module import ActionModule | |
| from .attention import flash_attention | |
| DISABLE_COMPILE = False # get os env | |
| __all__ = ["MatrixGameWanModel"] | |
| 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 | |
| # @amp.autocast(enabled=False) | |
| 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 | |
| # @amp.autocast(enabled=False) | |
| def rope_apply(x, grid_sizes, freqs): | |
| 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 = [] | |
| # print(grid_sizes.shape, len(grid_sizes.tolist()), grid_sizes.tolist()[0]) | |
| f, h, w = grid_sizes.tolist() | |
| for i in range(len(x)): | |
| seq_len = f * h * w | |
| # 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][:f].view(f, 1, 1, -1).expand(f, h, w, -1), | |
| freqs[1][:h].view(1, h, 1, -1).expand(f, h, w, -1), | |
| freqs[2][: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).type_as(x) | |
| class MatrixGameWanRMSNorm(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 MatrixGameWanLayerNorm(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).type_as(x) | |
| class MatrixGameWanSelfAttention(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 = MatrixGameWanRMSNorm(dim, eps=eps) if qk_norm else nn.Identity() | |
| self.norm_k = MatrixGameWanRMSNorm(dim, eps=eps) if qk_norm else nn.Identity() | |
| def forward(self, x, seq_lens, grid_sizes, freqs): | |
| 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) | |
| # print(k.shape, seq_lens) | |
| x = flash_attention( | |
| q=rope_apply(q, grid_sizes, freqs), | |
| k=rope_apply(k, grid_sizes, freqs), | |
| v=v, | |
| k_lens=seq_lens, | |
| window_size=self.window_size, | |
| ) | |
| # output | |
| x = x.flatten(2) | |
| x = self.o(x) | |
| return x | |
| # class MatrixGameWanT2VCrossAttention(MatrixGameWanSelfAttention): | |
| # def forward(self, x, context, context_lens, crossattn_cache=None): | |
| # r""" | |
| # Args: | |
| # x(Tensor): Shape [B, L1, C] | |
| # context(Tensor): Shape [B, L2, C] | |
| # context_lens(Tensor): Shape [B] | |
| # crossattn_cache (List[dict], *optional*): Contains the cached key and value tensors for context embedding. | |
| # """ | |
| # 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) | |
| # if crossattn_cache is not None: | |
| # if not crossattn_cache["is_init"]: | |
| # crossattn_cache["is_init"] = True | |
| # k = self.norm_k(self.k(context)).view(b, -1, n, d) | |
| # v = self.v(context).view(b, -1, n, d) | |
| # crossattn_cache["k"] = k | |
| # crossattn_cache["v"] = v | |
| # else: | |
| # k = crossattn_cache["k"] | |
| # v = crossattn_cache["v"] | |
| # else: | |
| # 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 MatrixGameWanGanCrossAttention(MatrixGameWanSelfAttention): | |
| # def forward(self, x, context, crossattn_cache=None): | |
| # r""" | |
| # Args: | |
| # x(Tensor): Shape [B, L1, C] | |
| # context(Tensor): Shape [B, L2, C] | |
| # context_lens(Tensor): Shape [B] | |
| # crossattn_cache (List[dict], *optional*): Contains the cached key and value tensors for context embedding. | |
| # """ | |
| # b, n, d = x.size(0), self.num_heads, self.head_dim | |
| # # compute query, key, value | |
| # qq = self.norm_q(self.q(context)).view(b, 1, -1, d) | |
| # kk = self.norm_k(self.k(x)).view(b, -1, n, d) | |
| # vv = self.v(x).view(b, -1, n, d) | |
| # # compute attention | |
| # x = flash_attention(qq, kk, vv) | |
| # # output | |
| # x = x.flatten(2) | |
| # x = self.o(x) | |
| # return x | |
| class MatrixGameWanI2VCrossAttention(MatrixGameWanSelfAttention): | |
| def forward(self, x, context, crossattn_cache=None): | |
| 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) | |
| if crossattn_cache is not None: | |
| if not crossattn_cache["is_init"]: | |
| crossattn_cache["is_init"] = True | |
| k = self.norm_k(self.k(context)).view(b, -1, n, d) | |
| v = self.v(context).view(b, -1, n, d) | |
| crossattn_cache["k"] = k | |
| crossattn_cache["v"] = v | |
| else: | |
| k = crossattn_cache["k"] | |
| v = crossattn_cache["v"] | |
| else: | |
| 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=None) | |
| # output | |
| x = x.flatten(2) | |
| x = self.o(x) | |
| return x | |
| MatrixGameWan_CROSSATTENTION_CLASSES = { | |
| "i2v_cross_attn": MatrixGameWanI2VCrossAttention, | |
| } | |
| def mul_add(x, y, z): | |
| return x.float() + y.float() * z.float() | |
| def mul_add_add(x, y, z): | |
| return x.float() * (1 + y) + z | |
| class MatrixGameWanAttentionBlock(nn.Module): | |
| def __init__( | |
| self, | |
| cross_attn_type, | |
| dim, | |
| ffn_dim, | |
| num_heads, | |
| window_size=(-1, -1), | |
| qk_norm=True, | |
| cross_attn_norm=False, | |
| action_config={}, | |
| 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 | |
| if len(action_config) != 0: | |
| self.action_model = ActionModule(**action_config) | |
| else: | |
| self.action_model = None | |
| # layers | |
| self.norm1 = MatrixGameWanLayerNorm(dim, eps) | |
| self.self_attn = MatrixGameWanSelfAttention(dim, num_heads, window_size, qk_norm, eps) | |
| self.norm3 = ( | |
| MatrixGameWanLayerNorm(dim, eps, elementwise_affine=True) | |
| if cross_attn_norm | |
| else nn.Identity() | |
| ) | |
| self.cross_attn = MatrixGameWan_CROSSATTENTION_CLASSES[cross_attn_type]( | |
| dim, num_heads, (-1, -1), qk_norm, eps | |
| ) | |
| self.norm2 = MatrixGameWanLayerNorm(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, | |
| grid_sizes, | |
| freqs, | |
| context, | |
| mouse_cond=None, | |
| keyboard_cond=None, | |
| # context_lens, | |
| ): | |
| 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 | |
| if e.dim() == 3: | |
| modulation = self.modulation | |
| # with amp.autocast(dtype=torch.float32): | |
| e = (self.modulation + e).chunk(6, dim=1) | |
| elif e.dim() == 4: | |
| modulation = self.modulation.unsqueeze(2) # 1, 6, 1, dim | |
| # with amp.autocast("cuda", dtype=torch.float32): | |
| e = (modulation + e).chunk(6, dim=1) | |
| e = [ei.squeeze(1) for ei in e] | |
| # assert e[0].dtype == torch.float32 | |
| # self-attention | |
| y = self.self_attn( | |
| self.norm1(x) * (1 + e[1]) + e[0], seq_lens, grid_sizes, freqs | |
| ) | |
| # with amp.autocast(dtype=torch.float32): | |
| x = x + y * e[2] | |
| # cross-attention & ffn function | |
| def cross_attn_ffn(x, context, e, mouse_cond, keyboard_cond): | |
| dtype = context.dtype | |
| x = x + self.cross_attn(self.norm3(x.to(dtype)), context) | |
| if self.action_model is not None: | |
| assert mouse_cond is not None or keyboard_cond is not None | |
| x = self.action_model( | |
| x.to(dtype), | |
| grid_sizes[0], | |
| grid_sizes[1], | |
| grid_sizes[2], | |
| mouse_cond, | |
| keyboard_cond, | |
| ) | |
| y = self.ffn(self.norm2(x) * (1 + e[4]) + e[3]) | |
| # with amp.autocast(dtype=torch.float32): | |
| x = x + y * e[5] | |
| return x | |
| x = cross_attn_ffn(x, context, e, mouse_cond, keyboard_cond) | |
| 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 = MatrixGameWanLayerNorm(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): | |
| if e.dim() == 2: | |
| modulation = self.modulation # 1, 2, dim | |
| e = (modulation + e.unsqueeze(1)).chunk(2, dim=1) | |
| elif e.dim() == 3: | |
| modulation = self.modulation.unsqueeze(2) # 1, 2, seq, dim | |
| e = (modulation + e.unsqueeze(1)).chunk(2, dim=1) | |
| e = [ei.squeeze(1) for ei in e] | |
| 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): | |
| 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), | |
| ) | |
| def forward(self, image_embeds): | |
| clip_extra_context_tokens = self.proj(image_embeds) | |
| return clip_extra_context_tokens | |
| # class RegisterTokens(nn.Module): | |
| # def __init__(self, num_registers: int, dim: int): | |
| # super().__init__() | |
| # self.register_tokens = nn.Parameter(torch.randn(num_registers, dim) * 0.02) | |
| # self.rms_norm = MatrixGameWanRMSNorm(dim, eps=1e-6) | |
| # def forward(self): | |
| # return self.rms_norm(self.register_tokens) | |
| # def reset_parameters(self): | |
| # nn.init.normal_(self.register_tokens, std=0.02) | |
| class MatrixGameWanModel(ModelMixin, ConfigMixin, FromOriginalModelMixin, PeftAdapterMixin): | |
| r""" | |
| MatrixGameWan diffusion backbone supporting both text-to-video and image-to-video. | |
| """ | |
| ignore_for_config = [ | |
| "patch_size", | |
| "cross_attn_norm", | |
| "qk_norm", | |
| "text_dim", | |
| "window_size", | |
| ] | |
| _no_split_modules = ["MatrixGameWanAttentionBlock"] | |
| _supports_gradient_checkpointing = True | |
| def __init__( | |
| self, | |
| model_type="i2v", | |
| patch_size=(1, 2, 2), | |
| text_len=512, | |
| in_dim=36, | |
| dim=1536, | |
| ffn_dim=8960, | |
| freq_dim=256, | |
| text_dim=4096, | |
| out_dim=16, | |
| num_heads=12, | |
| num_layers=30, | |
| window_size=(-1, -1), | |
| qk_norm=True, | |
| cross_attn_norm=True, | |
| inject_sample_info=False, | |
| action_config={}, | |
| 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) | |
| 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 ["i2v"] | |
| self.model_type = model_type | |
| self.use_action_module = len(action_config) > 0 | |
| assert self.use_action_module == True | |
| 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.eps = eps | |
| self.local_attn_size = -1 | |
| # embeddings | |
| self.patch_embedding = 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 = "i2v_cross_attn" | |
| self.blocks = nn.ModuleList( | |
| [ | |
| MatrixGameWanAttentionBlock( | |
| cross_attn_type, | |
| dim, | |
| ffn_dim, | |
| num_heads, | |
| window_size, | |
| qk_norm, | |
| cross_attn_norm, | |
| eps=eps, | |
| action_config=action_config, | |
| ) | |
| 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(1024, d - 4 * (d // 6)), | |
| rope_params(1024, 2 * (d // 6)), | |
| rope_params(1024, 2 * (d // 6)), | |
| ], | |
| dim=1, | |
| ) | |
| if model_type == "i2v": | |
| self.img_emb = MLPProj(1280, dim) | |
| # initialize weights | |
| self.init_weights() | |
| self.gradient_checkpointing = False | |
| def _set_gradient_checkpointing(self, module, value=False): | |
| self.gradient_checkpointing = value | |
| def forward(self, *args, **kwargs): | |
| # if kwargs.get('classify_mode', False) is True: | |
| # kwargs.pop('classify_mode') | |
| # return self._forward_classify(*args, **kwargs) | |
| # else: | |
| return self._forward(*args, **kwargs) | |
| def _forward( | |
| self, | |
| x, | |
| t, | |
| visual_context, | |
| cond_concat, | |
| mouse_cond=None, | |
| keyboard_cond=None, | |
| fps=None, | |
| # seq_len, | |
| # classify_mode=False, | |
| # concat_time_embeddings=False, | |
| # register_tokens=None, | |
| # cls_pred_branch=None, | |
| # gan_ca_blocks=None, | |
| # clip_fea=None, | |
| # y=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] | |
| 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 for image-to-video mode | |
| y (List[Tensor], *optional*): | |
| Conditional video inputs for image-to-video mode, same shape as x | |
| Returns: | |
| List[Tensor]: | |
| List of denoised video tensors with original input shapes [C_out, F, H / 8, W / 8] | |
| """ | |
| # params | |
| if mouse_cond is not None or keyboard_cond is not None: | |
| assert self.use_action_module == True | |
| device = self.patch_embedding.weight.device | |
| if self.freqs.device != device: | |
| self.freqs = self.freqs.to(device) | |
| x = torch.cat([x, cond_concat], dim=1) | |
| # embeddings | |
| x = self.patch_embedding(x) | |
| grid_sizes = torch.tensor(x.shape[2:], dtype=torch.long) | |
| x = x.flatten(2).transpose(1, 2) | |
| seq_lens = torch.tensor([u.size(0) for u in x], dtype=torch.long) | |
| # seq_len = seq_lens.max() | |
| # # assert seq_lens.max() <= seq_len | |
| # x = torch.cat([ | |
| # torch.cat([u, u.new_zeros(1, seq_len - u.size(1), u.size(2))], | |
| # dim=1) for u in x | |
| # ]) | |
| # time embeddings | |
| # with amp.autocast(dtype=torch.float32): | |
| # assert t.ndim == 1 | |
| e = self.time_embedding( | |
| sinusoidal_embedding_1d(self.freq_dim, t).type_as(x) | |
| ) # TODO: check if t ndim == 1 | |
| 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 x dim | |
| context = self.img_emb(visual_context) | |
| # arguments | |
| # kwargs = dict( | |
| # e=e0, | |
| # seq_lens=seq_lens, | |
| # grid_sizes=grid_sizes, | |
| # freqs=self.freqs, | |
| # context=context, | |
| # context_lens=context_lens) | |
| kwargs = dict( | |
| e=e0, | |
| grid_sizes=grid_sizes, | |
| seq_lens=seq_lens, | |
| freqs=self.freqs, | |
| context=context, | |
| mouse_cond=mouse_cond, | |
| # context_lens=context_lens, | |
| keyboard_cond=keyboard_cond, | |
| ) | |
| def create_custom_forward(module): | |
| def custom_forward(*inputs, **kwargs): | |
| return module(*inputs, **kwargs) | |
| return custom_forward | |
| for ii, block in enumerate(self.blocks): | |
| if torch.is_grad_enabled() and self.gradient_checkpointing: | |
| x = torch.utils.checkpoint.checkpoint( | |
| create_custom_forward(block), | |
| x, | |
| **kwargs, | |
| use_reentrant=False, | |
| ) | |
| else: | |
| x = block(x, **kwargs) | |
| # head | |
| x = self.head(x, e) | |
| # unpatchify | |
| x = self.unpatchify(x, grid_sizes) | |
| return x.float() | |
| def unpatchify(self, x, grid_sizes): # TODO check grid sizes | |
| 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 [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 | |
| bs = x.shape[0] | |
| x = x.view(bs, *grid_sizes, *self.patch_size, c) | |
| x = torch.einsum("bfhwpqrc->bcfphqwr", x) | |
| x = x.reshape(bs, c, *[i * j for i, j in zip(grid_sizes, self.patch_size)]) | |
| return x | |
| 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.time_embedding.modules(): | |
| if isinstance(m, nn.Linear): | |
| nn.init.normal_(m.weight, std=0.02) | |
| # init output layer | |
| nn.init.zeros_(self.head.head.weight) | |
| if self.use_action_module == True: | |
| for m in self.blocks: | |
| nn.init.zeros_(m.action_model.proj_mouse.weight) | |
| if m.action_model.proj_mouse.bias is not None: | |
| nn.init.zeros_(m.action_model.proj_mouse.bias) | |
| nn.init.zeros_(m.action_model.proj_keyboard.weight) | |
| if m.action_model.proj_keyboard.bias is not None: | |
| nn.init.zeros_(m.action_model.proj_keyboard.bias) | |