File size: 12,552 Bytes
742c169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import torch
import torch.nn as nn
import torch.nn.functional as F

from .utils import compile_wrapper


class RMSNorm(nn.RMSNorm):
    @compile_wrapper
    def forward(self, x):
        return F.rms_norm(
            x.to(self.weight), self.normalized_shape, self.weight, self.eps
        )


class SwiGLU(nn.Module):
    def __init__(self, hidden_size, intermediate_size):
        super(SwiGLU, self).__init__()
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.up = nn.Linear(hidden_size, intermediate_size * 2)
        self.down = nn.Linear(intermediate_size, hidden_size)

    @compile_wrapper
    def forward(self, x):
        x, gate = self.up(x).chunk(2, dim=-1)
        x = F.silu(x) * gate
        x = self.down(x)
        return x


class GELUMLP(nn.Module):
    def __init__(self, hidden_size, intermediate_size):
        super().__init__()
        self.fc1 = nn.Linear(hidden_size, intermediate_size)
        self.act = nn.GELU()
        self.fc2 = nn.Linear(intermediate_size, hidden_size)

    @compile_wrapper
    def forward(self, x):
        return self.fc2(self.act(self.fc1(x)))


def _qk_norm(q, k, scale, eps=1e-6):
    """Cosine-similarity attention with learnable temperature (DisMo-style).

    Logits = scale * cos(q, k).
    Computes in float32 for numerical stability, uses rsqrt(sum_sq + eps).
    Scale is clamped to min=eps to prevent NaN from sqrt of negative values.
    """
    dtype = q.dtype
    q, k, scale = q.float(), k.float(), scale.float()
    sum_sq_q = torch.sum(q ** 2, dim=-1, keepdim=True)
    sum_sq_k = torch.sum(k ** 2, dim=-1, keepdim=True)
    sqrt_scale = torch.sqrt(scale.clamp(min=eps))
    scale_q = sqrt_scale * torch.rsqrt(sum_sq_q + eps)
    scale_k = sqrt_scale * torch.rsqrt(sum_sq_k + eps)
    return (q * scale_q).to(dtype), (k * scale_k).to(dtype)


class Attention(nn.Module):
    def __init__(self, hidden_size, num_heads, qk_norm=False, bias=True):
        super(Attention, self).__init__()
        self.hidden_size = hidden_size
        self.num_heads = num_heads
        self.head_dim = hidden_size // num_heads
        self.qk_norm = qk_norm
        self.q_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        self.k_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        self.v_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        self.out_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        if qk_norm:
            self.qk_scale = nn.Parameter(torch.full([num_heads, 1, 1], 10.0))

    @compile_wrapper
    def forward(self, x, rope=None, positions=None):
        """
        Args:
            x: [B, T, SL, D] or [B, SL, D]
            rope: optional RoPE3D module (from tt3d.py)
            positions: optional [SL, 3] positions for 3D RoPE
        """
        # [B, T, SL, D]
        *b, seq_len, dim = x.shape
        x = x.flatten(1, 2)
        q = self.q_proj(x)
        k = self.k_proj(x)
        v = self.v_proj(x)
        q = q.view(-1, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
        k = k.view(-1, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
        v = v.view(-1, seq_len, self.num_heads, self.head_dim).transpose(1, 2)

        # Optional 3D RoPE
        if rope is not None and positions is not None:
            q, k = rope(q, k, positions)

        if self.qk_norm:
            q, k = _qk_norm(q, k, self.qk_scale)
            attn = F.scaled_dot_product_attention(q, k, v, scale=1.0)
        else:
            attn = F.scaled_dot_product_attention(q, k, v)

        x = self.out_proj(attn.transpose(1, 2).flatten(2, 3))
        return x.reshape(*b, seq_len, dim)


class TransformerBlock(nn.Module):
    """Standard pre-norm transformer block: RMSNorm + Attention + FFN (SwiGLU or GELU)."""

    def __init__(self, hidden_size: int, intermediate_size: int, num_heads: int,
                 ffn_type: str = "swiglu", qk_norm: bool = False):
        super().__init__()
        self.norm1 = RMSNorm(hidden_size)
        self.attn = Attention(hidden_size, num_heads, qk_norm=qk_norm)
        self.norm2 = RMSNorm(hidden_size)
        if ffn_type == "gelu":
            self.mlp = GELUMLP(hidden_size, intermediate_size)
        else:
            self.mlp = SwiGLU(hidden_size, intermediate_size)

    def forward(self, x: torch.Tensor, rope=None, positions=None) -> torch.Tensor:
        x = x + self.attn(self.norm1(x), rope=rope, positions=positions)
        x = x + self.mlp(self.norm2(x))
        return x


class AdaRMSNorm(nn.Module):
    """Adaptive RMSNorm: scale-only modulation from conditioning signal (DisMo-style)."""

    def __init__(self, hidden_size: int, cond_size: int):
        super().__init__()
        self.norm = nn.RMSNorm(hidden_size)
        self.linear = nn.Linear(cond_size, hidden_size, bias=False)
        nn.init.zeros_(self.linear.weight)

    @compile_wrapper
    def forward(self, x: torch.Tensor, c: torch.Tensor) -> torch.Tensor:
        """
        Args:
            x: [..., D] input tensor
            c: [..., C] conditioning tensor (broadcastable to x batch dims)
        Returns:
            [..., D] modulated tensor
        """
        return (self.linear(F.silu(c)) + 1) * self.norm(x.to(self.norm.weight))


class CrossAttention(nn.Module):
    """Cross-attention: Q from main stream, K/V from context."""

    def __init__(self, hidden_size: int, num_heads: int, context_size: int | None = None, qk_norm: bool = False, bias: bool = True):
        super().__init__()
        self.hidden_size = hidden_size
        self.num_heads = num_heads
        self.head_dim = hidden_size // num_heads
        self.qk_norm = qk_norm
        context_size = context_size or hidden_size
        self.q_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        self.k_proj = nn.Linear(context_size, hidden_size, bias=bias)
        self.v_proj = nn.Linear(context_size, hidden_size, bias=bias)
        self.out_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        if qk_norm:
            self.qk_scale = nn.Parameter(torch.full([num_heads, 1, 1], 10.0))

    @compile_wrapper
    def forward(self, x: torch.Tensor, context: torch.Tensor) -> torch.Tensor:
        """
        Args:
            x: [B, T, S, D] main stream (query)
            context: [B, T, Sc, Dc] context (key/value)
        Returns:
            [B, T, S, D]
        """
        bt = x.shape[0] * x.shape[1]
        seq_q = x.shape[2]
        seq_kv = context.shape[2]

        x_flat = x.reshape(bt, seq_q, self.hidden_size)
        ctx_flat = context.reshape(bt, seq_kv, -1)

        q = self.q_proj(x_flat).view(bt, seq_q, self.num_heads, self.head_dim).transpose(1, 2)
        k = self.k_proj(ctx_flat).view(bt, seq_kv, self.num_heads, self.head_dim).transpose(1, 2)
        v = self.v_proj(ctx_flat).view(bt, seq_kv, self.num_heads, self.head_dim).transpose(1, 2)

        if self.qk_norm:
            q, k = _qk_norm(q, k, self.qk_scale)
            attn = F.scaled_dot_product_attention(q, k, v, scale=1.0)
        else:
            attn = F.scaled_dot_product_attention(q, k, v)
        out = self.out_proj(attn.transpose(1, 2).flatten(2, 3))
        return out.view(x.shape)


class AttentivePooling(nn.Module):
    """Learnable query token attends to M tokens to produce a CLS-like token."""

    def __init__(self, hidden_size: int, num_heads: int = 1):
        super().__init__()
        self.hidden_size = hidden_size
        self.num_heads = num_heads
        self.head_dim = hidden_size // num_heads
        self.query = nn.Parameter(torch.randn(1, 1, 1, hidden_size) * 0.02)
        self.k_proj = nn.Linear(hidden_size, hidden_size)
        self.v_proj = nn.Linear(hidden_size, hidden_size)
        self.out_proj = nn.Linear(hidden_size, hidden_size)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Args:
            x: [B, T, M, D] motion tokens
        Returns:
            [B, T, D] pooled CLS-like token
        """
        B, T, M, D = x.shape
        bt = B * T

        q = self.query.expand(B, T, -1, -1).reshape(bt, 1, self.num_heads, self.head_dim).transpose(1, 2)
        x_flat = x.reshape(bt, M, D)
        k = self.k_proj(x_flat).view(bt, M, self.num_heads, self.head_dim).transpose(1, 2)
        v = self.v_proj(x_flat).view(bt, M, self.num_heads, self.head_dim).transpose(1, 2)

        attn = F.scaled_dot_product_attention(q, k, v)  # [bt, H, 1, head_dim]
        out = self.out_proj(attn.transpose(1, 2).flatten(2, 3))  # [bt, 1, D]
        return out.squeeze(2).view(B, T, D)


class DiTTransformerBlock(nn.Module):
    """DiT-style transformer block with AdaRMSNorm conditioning and optional cross-attention."""

    def __init__(
        self,
        hidden_size: int,
        intermediate_size: int,
        num_heads: int,
        cond_size: int,
        use_cross_attn: bool = False,
        context_size: int | None = None,
        use_rope: bool = False,
        qk_norm: bool = False,
        bias: bool = True,
    ):
        super().__init__()
        from .pos_embed import RoPE2DAttention

        # Self-attention sub-layer (with optional 2D RoPE)
        self.ada_norm1 = AdaRMSNorm(hidden_size, cond_size)
        self.use_rope = use_rope
        if use_rope:
            self.attn = RoPE2DAttention(hidden_size, num_heads, qk_norm=qk_norm, bias=bias)
        else:
            self.attn = Attention(hidden_size, num_heads, qk_norm=qk_norm, bias=bias)

        # Optional cross-attention sub-layer
        self.use_cross_attn = use_cross_attn
        if use_cross_attn:
            self.ada_norm_cross = AdaRMSNorm(hidden_size, cond_size)
            self.cross_attn = CrossAttention(hidden_size, num_heads, context_size, qk_norm=qk_norm, bias=bias)

        # FFN sub-layer
        self.ada_norm2 = AdaRMSNorm(hidden_size, cond_size)
        self.mlp = SwiGLU(hidden_size, intermediate_size)

    def forward(
        self,
        x: torch.Tensor,
        c: torch.Tensor,
        context: torch.Tensor | None = None,
        h: int | None = None,
        w: int | None = None,
    ) -> torch.Tensor:
        """
        Args:
            x: [B, T, S, D] main stream
            c: [B, T, 1, Dc] conditioning (broadcast over S)
            context: [B, T, Sc, Dc_ctx] optional cross-attn context
            h, w: spatial dims (required when use_rope=True)
        Returns:
            [B, T, S, D]
        """
        if self.use_rope:
            x = x + self.attn(self.ada_norm1(x, c), h, w)
        else:
            x = x + self.attn(self.ada_norm1(x, c))
        if self.use_cross_attn and context is not None:
            x = x + self.cross_attn(self.ada_norm_cross(x, c), context)
        x = x + self.mlp(self.ada_norm2(x, c))
        return x


class LatentPatchEmbed(nn.Module):
    """
    Patch embedding for latent-space video input.

    Expects input of shape [B, T, C_in, H, W] where the spatial dimensions are
    already at patch resolution (e.g. from a VAE encoder).  If the input channel
    dimension differs from hidden_size, a linear projection is applied per-patch.

    When spatial_patch_size > 1 the spatial grid is further down-sampled via a
    Conv2d with kernel_size = stride = spatial_patch_size.
    """

    def __init__(
        self,
        in_channels: int,
        hidden_size: int,
        spatial_patch_size: int = 1,
    ):
        super().__init__()
        self.in_channels = in_channels
        self.hidden_size = hidden_size
        self.spatial_patch_size = spatial_patch_size

        if spatial_patch_size > 1:
            self.proj = nn.Conv2d(
                in_channels,
                hidden_size,
                kernel_size=spatial_patch_size,
                stride=spatial_patch_size,
            )
        else:
            self.proj = nn.Linear(in_channels, hidden_size)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Args:
            x: [B, T, C, H, W]
        Returns:
            [B, T, H'*W', D]  where H' = H // spatial_patch_size, etc.
        """
        b, t, c, h, w = x.shape
        if self.spatial_patch_size > 1:
            x = x.reshape(b * t, c, h, w)
            x = self.proj(x)  # [B*T, D, H', W']
            _, d, hp, wp = x.shape
            x = x.flatten(2).transpose(1, 2)  # [B*T, H'*W', D]
            x = x.reshape(b, t, hp * wp, d)
        else:
            # x: [B, T, C, H, W] -> [B, T, H*W, C] -> Linear -> [B, T, H*W, D]
            x = x.permute(0, 1, 3, 4, 2).reshape(b, t, h * w, c)
            x = self.proj(x)
        return x