|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +from dataclasses import dataclass, field |
| 3 | + |
| 4 | +from fastvideo.configs.models.dits.base import DiTArchConfig, DiTConfig |
| 5 | +from fastvideo.platforms import AttentionBackendEnum |
| 6 | + |
| 7 | + |
| 8 | +def _is_transformer_block(name: str, module) -> bool: |
| 9 | + del module |
| 10 | + return name.startswith("blocks.") and name.split(".")[-1].isdigit() |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class HeliosArchConfig(DiTArchConfig): |
| 15 | + """Architecture fields for Helios-Distilled's history-aware DiT.""" |
| 16 | + |
| 17 | + _fsdp_shard_conditions: list = field(default_factory=lambda: [_is_transformer_block]) |
| 18 | + _supported_attention_backends: tuple[AttentionBackendEnum, ...] = ( |
| 19 | + AttentionBackendEnum.FLASH_ATTN, |
| 20 | + AttentionBackendEnum.TORCH_SDPA, |
| 21 | + ) |
| 22 | + param_names_mapping: dict = field(default_factory=dict) |
| 23 | + reverse_param_names_mapping: dict = field(default_factory=dict) |
| 24 | + lora_param_names_mapping: dict = field(default_factory=dict) |
| 25 | + |
| 26 | + patch_size: tuple[int, int, int] = (1, 2, 2) |
| 27 | + num_attention_heads: int = 40 |
| 28 | + attention_head_dim: int = 128 |
| 29 | + in_channels: int = 16 |
| 30 | + out_channels: int = 16 |
| 31 | + text_dim: int = 4096 |
| 32 | + freq_dim: int = 256 |
| 33 | + ffn_dim: int = 13824 |
| 34 | + num_layers: int = 40 |
| 35 | + cross_attn_norm: bool = True |
| 36 | + qk_norm: str = "rms_norm_across_heads" |
| 37 | + eps: float = 1e-6 |
| 38 | + added_kv_proj_dim: int | None = None |
| 39 | + rope_dim: tuple[int, int, int] = (44, 42, 42) |
| 40 | + rope_theta: float = 10000.0 |
| 41 | + guidance_cross_attn: bool = True |
| 42 | + zero_history_timestep: bool = True |
| 43 | + has_multi_term_memory_patch: bool = True |
| 44 | + is_amplify_history: bool = False |
| 45 | + history_scale_mode: str = "per_head" |
| 46 | + |
| 47 | + def __post_init__(self) -> None: |
| 48 | + super().__post_init__() |
| 49 | + self.out_channels = self.out_channels or self.in_channels |
| 50 | + self.hidden_size = self.num_attention_heads * self.attention_head_dim |
| 51 | + self.num_channels_latents = self.out_channels |
| 52 | + if not self.cross_attn_norm: |
| 53 | + raise ValueError("Helios currently requires cross_attn_norm=True") |
| 54 | + if self.qk_norm != "rms_norm_across_heads": |
| 55 | + raise ValueError("Helios currently requires qk_norm='rms_norm_across_heads'") |
| 56 | + if self.added_kv_proj_dim is not None: |
| 57 | + raise ValueError("Helios added_kv_proj_dim variants are not supported") |
| 58 | + if not self.guidance_cross_attn: |
| 59 | + raise ValueError("Helios currently requires guidance_cross_attn=True") |
| 60 | + if not self.zero_history_timestep: |
| 61 | + raise ValueError("Helios currently requires zero_history_timestep=True") |
| 62 | + if not self.has_multi_term_memory_patch: |
| 63 | + raise ValueError("Helios currently requires has_multi_term_memory_patch=True") |
| 64 | + if self.is_amplify_history: |
| 65 | + raise ValueError("Helios is_amplify_history variants are not supported") |
| 66 | + if self.history_scale_mode != "per_head": |
| 67 | + raise ValueError("Helios currently requires history_scale_mode='per_head'") |
| 68 | + if sum(self.rope_dim) != self.attention_head_dim: |
| 69 | + raise ValueError( |
| 70 | + f"Helios rope_dim must sum to attention_head_dim, got {self.rope_dim} and {self.attention_head_dim}") |
| 71 | + if any(dim % 2 for dim in self.rope_dim): |
| 72 | + raise ValueError(f"Helios rope dimensions must be even: {self.rope_dim}") |
| 73 | + |
| 74 | + |
| 75 | +@dataclass |
| 76 | +class HeliosConfig(DiTConfig): |
| 77 | + arch_config: DiTArchConfig = field(default_factory=HeliosArchConfig) |
| 78 | + prefix: str = "Helios" |
0 commit comments