|
| 1 | +from dataclasses import dataclass, field |
| 2 | + |
| 3 | +import torch |
| 4 | +from fastvideo.configs.models.dits.wanvideo import WanVideoArchConfig, WanVideoConfig |
| 5 | + |
| 6 | + |
| 7 | +def _is_transformer_block(param_name: str, module: torch.nn.Module) -> bool: |
| 8 | + return bool("blocks" in param_name and param_name.split(".")[-1].isdigit()) |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class MatrixGame3WanVideoArchConfig(WanVideoArchConfig): |
| 13 | + param_names_mapping: dict = field( |
| 14 | + default_factory=lambda: { |
| 15 | + r"^patch_embedding\.(weight|bias)$": r"patch_embedding.proj.\1", |
| 16 | + r"^patch_embedding_wancamctrl\.(.*)$": r"camera_patch_embedding.proj.\1", |
| 17 | + r"^time_embedding\.0\.(.*)$": r"condition_embedder.time_embedder.mlp.fc_in.\1", |
| 18 | + r"^time_embedding\.2\.(.*)$": r"condition_embedder.time_embedder.mlp.fc_out.\1", |
| 19 | + r"^time_projection\.1\.(.*)$": r"condition_embedder.time_modulation.linear.\1", |
| 20 | + r"^head\.head\.(.*)$": r"proj_out.\1", |
| 21 | + r"^head\.modulation$": r"scale_shift_table", |
| 22 | + r"^blocks\.(\d+)\.self_attn\.q\.(.*)$": r"blocks.\1.to_q.\2", |
| 23 | + r"^blocks\.(\d+)\.self_attn\.k\.(.*)$": r"blocks.\1.to_k.\2", |
| 24 | + r"^blocks\.(\d+)\.self_attn\.v\.(.*)$": r"blocks.\1.to_v.\2", |
| 25 | + r"^blocks\.(\d+)\.self_attn\.o\.(.*)$": r"blocks.\1.to_out.\2", |
| 26 | + r"^blocks\.(\d+)\.self_attn\.norm_q\.(.*)$": r"blocks.\1.norm_q.\2", |
| 27 | + r"^blocks\.(\d+)\.self_attn\.norm_k\.(.*)$": r"blocks.\1.norm_k.\2", |
| 28 | + r"^blocks\.(\d+)\.cross_attn\.q\.(.*)$": r"blocks.\1.attn2.to_q.\2", |
| 29 | + r"^blocks\.(\d+)\.cross_attn\.k\.(.*)$": r"blocks.\1.attn2.to_k.\2", |
| 30 | + r"^blocks\.(\d+)\.cross_attn\.v\.(.*)$": r"blocks.\1.attn2.to_v.\2", |
| 31 | + r"^blocks\.(\d+)\.cross_attn\.o\.(.*)$": r"blocks.\1.attn2.to_out.\2", |
| 32 | + r"^blocks\.(\d+)\.cross_attn\.norm_q\.(.*)$": r"blocks.\1.attn2.norm_q.\2", |
| 33 | + r"^blocks\.(\d+)\.cross_attn\.norm_k\.(.*)$": r"blocks.\1.attn2.norm_k.\2", |
| 34 | + r"^blocks\.(\d+)\.ffn\.0\.(.*)$": r"blocks.\1.ffn.fc_in.\2", |
| 35 | + r"^blocks\.(\d+)\.ffn\.2\.(.*)$": r"blocks.\1.ffn.fc_out.\2", |
| 36 | + r"^blocks\.(\d+)\.norm3\.(.*)$": r"blocks.\1.self_attn_residual_norm.norm.\2", |
| 37 | + r"^blocks\.(\d+)\.modulation$": r"blocks.\1.scale_shift_table", |
| 38 | + }) |
| 39 | + patch_size: tuple[int, int, int] = (1, 2, 2) |
| 40 | + in_channels: int = 48 |
| 41 | + out_channels: int = 48 |
| 42 | + num_attention_heads: int = 24 |
| 43 | + attention_head_dim: int = 128 |
| 44 | + ffn_dim: int = 14336 |
| 45 | + num_layers: int = 30 |
| 46 | + text_len: int = 512 |
| 47 | + image_dim: int = 0 |
| 48 | + use_text_crossattn: bool = True |
| 49 | + use_memory: bool = True |
| 50 | + sigma_theta: float = 0.8 |
| 51 | + camera_embed_in_channels: int = 1536 |
| 52 | + action_config: dict = field( |
| 53 | + default_factory=lambda: { |
| 54 | + "blocks": list(range(15)), |
| 55 | + "enable_mouse": True, |
| 56 | + "enable_keyboard": True, |
| 57 | + "heads_num": 16, |
| 58 | + "hidden_size": 128, |
| 59 | + "img_hidden_size": 3072, |
| 60 | + "keyboard_dim_in": 6, |
| 61 | + "keyboard_hidden_dim": 1024, |
| 62 | + "mouse_dim_in": 2, |
| 63 | + "mouse_hidden_dim": 1024, |
| 64 | + "mouse_qk_dim_list": [8, 28, 28], |
| 65 | + "patch_size": [1, 2, 2], |
| 66 | + "qk_norm": True, |
| 67 | + "qkv_bias": False, |
| 68 | + "rope_dim_list": [8, 28, 28], |
| 69 | + "rope_theta": 256, |
| 70 | + "vae_time_compression_ratio": 4, |
| 71 | + "windows_size": 3, |
| 72 | + }) |
| 73 | + |
| 74 | + |
| 75 | +@dataclass |
| 76 | +class MatrixGame3WanVideoConfig(WanVideoConfig): |
| 77 | + arch_config: MatrixGame3WanVideoArchConfig = field(default_factory=MatrixGame3WanVideoArchConfig) |
| 78 | + prefix: str = "Wan" |
| 79 | + _compile_conditions: list = field(default_factory=lambda: [_is_transformer_block]) |
0 commit comments