Download deploy/diffsynth/models/ltx25_duration_head.py from Agnes-AI/Agnes-Video-H3-MoE: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/Agnes-AI/Agnes-Video-H3-MoE/resolve/main/deploy/diffsynth/models/ltx25_duration_head.py
- Command line
-
hf download hf://Agnes-AI/Agnes-Video-H3-MoE/deploy/diffsynth/models/ltx25_duration_head.py
-
curl -L -o ltx25_duration_head.py https://huggingface.co/Agnes-AI/Agnes-Video-H3-MoE/resolve/main/deploy/diffsynth/models/ltx25_duration_head.py
2.56 kB
| import torch | |
| from torch import nn | |
| class LTX25AttentionPooler(nn.Module): | |
| def __init__(self, hidden_dim: int = 256, num_queries: int = 1, num_heads: int = 4): | |
| super().__init__() | |
| self.hidden_dim = hidden_dim | |
| self.num_queries = num_queries | |
| self.query_tokens = nn.Parameter(torch.randn(num_queries, hidden_dim) * 0.02) | |
| self.cross_attn = nn.MultiheadAttention( | |
| embed_dim=hidden_dim, | |
| num_heads=num_heads, | |
| batch_first=True, | |
| ) | |
| def forward(self, tokens: torch.Tensor) -> torch.Tensor: | |
| queries = self.query_tokens.unsqueeze(0).expand(tokens.shape[0], -1, -1) | |
| pooled, _ = self.cross_attn(queries, tokens, tokens, need_weights=False) | |
| return pooled | |
| class LTX25DurationHead(nn.Module): | |
| def __init__( | |
| self, | |
| video_cross_attention_dim: int = 4096, | |
| audio_cross_attention_dim: int = 2048, | |
| pooler_hidden_dim: int = 256, | |
| num_queries: int = 1, | |
| num_pooler_heads: int = 4, | |
| mlp_hidden: int = 256, | |
| ): | |
| super().__init__() | |
| self.pooler_hidden_dim = pooler_hidden_dim | |
| self.video_input_proj = nn.Linear(video_cross_attention_dim, pooler_hidden_dim) | |
| self.video_modality_emb = nn.Parameter(torch.randn(pooler_hidden_dim) * 0.02) | |
| self.audio_input_proj = nn.Linear(audio_cross_attention_dim, pooler_hidden_dim) | |
| self.audio_modality_emb = nn.Parameter(torch.randn(pooler_hidden_dim) * 0.02) | |
| self.attention_pooler = LTX25AttentionPooler(pooler_hidden_dim, num_queries, num_pooler_heads) | |
| self.mlp_hidden = nn.Linear(pooler_hidden_dim * num_queries, mlp_hidden) | |
| self.mlp_out = nn.Linear(mlp_hidden, 1) | |
| def forward( | |
| self, | |
| video_tokens: torch.Tensor | None = None, | |
| audio_tokens: torch.Tensor | None = None, | |
| ) -> torch.Tensor: | |
| if video_tokens is None and audio_tokens is None: | |
| raise ValueError("LTX25DurationHead.forward requires video_tokens and/or audio_tokens.") | |
| token_groups = [] | |
| if video_tokens is not None: | |
| token_groups.append(self.video_input_proj(video_tokens) + self.video_modality_emb) | |
| if audio_tokens is not None: | |
| token_groups.append(self.audio_input_proj(audio_tokens) + self.audio_modality_emb) | |
| pooled = self.attention_pooler(torch.cat(token_groups, dim=1)) | |
| hidden = torch.nn.functional.gelu(self.mlp_hidden(pooled.reshape(pooled.shape[0], -1)), approximate="tanh") | |
| return self.mlp_out(hidden).squeeze(-1).exp() | |