| """ |
| LiRA Core Modules: Gated State-Space Backbone (GS3B) |
| |
| Mathematical Foundation: |
| ======================== |
| Traditional transformers use self-attention: O_i = softmax(Q_i K^T / sqrt(d)) V |
| This is O(N^2) in sequence length - prohibitive for high-res images. |
| |
| Our approach combines three key innovations: |
| |
| 1. SELECTIVE STATE SPACE (from Mamba/S6): |
| State evolution: h_t = A_t * h_{t-1} + B_t * x_t |
| Output: y_t = C_t * h_t + D * x_t |
| Where A_t, B_t, C_t are INPUT-DEPENDENT (selective) - this is the key insight |
| from Mamba that makes SSMs competitive with attention. |
| |
| 2. BIDIRECTIONAL GATED SCANNING (from DiM + RWKV-7): |
| Images are 2D, not 1D. We scan in 4 directions: |
| - Horizontal L→R, R→L |
| - Vertical T→B, B→T |
| Each direction maintains its own state. A learned gate fuses them: |
| y = gate * [y_lr; y_rl; y_tb; y_bt] |
| |
| From RWKV-7 we take the generalized delta rule for state updates: |
| S_t = S_{t-1} * (diag(w_t) - k_t^T (a_t ⊗ k_t)) + v_t^T k_t |
| This gives us input-dependent decay with O(N) complexity. |
| |
| 3. FREQUENCY-AWARE PROCESSING (from DiMSUM): |
| We apply lightweight wavelet decomposition to separate structure from detail, |
| process each frequency band with appropriate granularity, then recombine. |
| Low-freq (structure) → fewer tokens, heavier processing |
| High-freq (detail) → more tokens, lighter processing |
| |
| Combined complexity: O(N * d * H) where N=tokens, d=state_dim, H=num_heads |
| For 1024px with f32 VAE: N = 32*32 = 1024 tokens → extremely efficient |
| """ |
|
|
| import torch |
| import torch.nn as nn |
| import torch.nn.functional as F |
| import math |
| from typing import Optional, Tuple |
| from einops import rearrange |
|
|
|
|
| |
| |
| |
|
|
| class SelectiveStateSpace(nn.Module): |
| """ |
| Selective State Space layer with input-dependent parameters. |
| |
| Mathematical formulation: |
| h_t = diag(exp(A_t)) * h_{t-1} + B_t * x_t (state transition) |
| y_t = C_t * h_t (output projection) |
| |
| Where A_t, B_t, C_t are all computed from the input (selective/data-dependent). |
| This selectivity is what allows SSMs to match transformer quality. |
| |
| Key insight: discretization of continuous dynamics means we can model |
| any timescale of dependencies by learning the step size Δ. |
| """ |
| |
| def __init__(self, d_model: int, d_state: int = 16, d_conv: int = 4): |
| super().__init__() |
| self.d_model = d_model |
| self.d_state = d_state |
| self.d_conv = d_conv |
| |
| |
| |
| self.in_proj = nn.Linear(d_model, 2 * d_model, bias=False) |
| |
| |
| self.conv1d = nn.Conv1d( |
| d_model, d_model, kernel_size=d_conv, |
| padding=d_conv - 1, groups=d_model, bias=True |
| ) |
| |
| |
| |
| self.A_log = nn.Parameter(torch.log(torch.arange(1, d_state + 1, dtype=torch.float32).repeat(d_model, 1))) |
| self.D = nn.Parameter(torch.ones(d_model)) |
| |
| |
| self.dt_proj = nn.Linear(d_model, d_model, bias=True) |
| self.B_proj = nn.Linear(d_model, d_state, bias=False) |
| self.C_proj = nn.Linear(d_model, d_state, bias=False) |
| |
| |
| self.out_proj = nn.Linear(d_model, d_model, bias=False) |
| |
| |
| dt_init_std = d_model ** -0.5 |
| nn.init.uniform_(self.dt_proj.bias, -4.0, -2.0) |
| |
| def forward(self, x: torch.Tensor) -> torch.Tensor: |
| """ |
| x: (B, L, D) input sequence |
| Returns: (B, L, D) output sequence |
| """ |
| B, L, D = x.shape |
| |
| |
| xz = self.in_proj(x) |
| x_ssm, z = xz.chunk(2, dim=-1) |
| |
| |
| x_conv = x_ssm.transpose(1, 2) |
| x_conv = self.conv1d(x_conv)[:, :, :L] |
| x_conv = x_conv.transpose(1, 2) |
| x_conv = F.silu(x_conv) |
| |
| |
| dt = F.softplus(self.dt_proj(x_conv)) |
| B_sel = self.B_proj(x_conv) |
| C_sel = self.C_proj(x_conv) |
| |
| |
| A = -torch.exp(self.A_log) |
| |
| |
| y = self._selective_scan(x_conv, dt, A, B_sel, C_sel) |
| |
| |
| y = y + self.D.unsqueeze(0).unsqueeze(0) * x_conv |
| |
| |
| y = y * F.silu(z) |
| |
| return self.out_proj(y) |
| |
| def _selective_scan(self, x, dt, A, B, C): |
| """ |
| Parallel selective scan using cumulative operations. |
| |
| For training, we use the parallel form: |
| h_t = exp(A * dt_t) * h_{t-1} + dt_t * B_t * x_t |
| y_t = C_t * h_t |
| |
| We compute this via log-space cumsum for numerical stability. |
| """ |
| B_batch, L, D = x.shape |
| N = A.shape[1] |
| |
| |
| |
| dt_expanded = dt.unsqueeze(-1) |
| A_expanded = A.unsqueeze(0).unsqueeze(0) |
| dA = torch.exp(dt_expanded * A_expanded) |
| |
| |
| dBx = dt_expanded * B.unsqueeze(2) * x.unsqueeze(-1) |
| |
| |
| |
| h = torch.zeros(B_batch, D, N, device=x.device, dtype=x.dtype) |
| ys = [] |
| |
| |
| chunk_size = min(64, L) |
| for i in range(0, L, chunk_size): |
| end = min(i + chunk_size, L) |
| chunk_len = end - i |
| |
| chunk_ys = [] |
| for t in range(chunk_len): |
| idx = i + t |
| h = dA[:, idx] * h + dBx[:, idx] |
| y_t = (h * C[:, idx].unsqueeze(1)).sum(-1) |
| chunk_ys.append(y_t) |
| |
| ys.extend(chunk_ys) |
| |
| y = torch.stack(ys, dim=1) |
| return y |
|
|
|
|
| |
| |
| |
|
|
| class BidirectionalSpatialScanner(nn.Module): |
| """ |
| Scans 2D spatial features in 4 directions to capture full spatial context. |
| |
| Innovation: Instead of 4 separate SSMs (expensive), we use 2 SSMs with |
| input reversal, and fuse with a learned spatial gate. |
| |
| Directions: |
| 1. Row-major L→R (horizontal forward) |
| 2. Row-major R→L (horizontal backward) |
| 3. Col-major T→B (vertical forward) |
| 4. Col-major B→T (vertical backward) |
| |
| The gate learns to weight each direction based on spatial position and content. |
| """ |
| |
| def __init__(self, d_model: int, d_state: int = 16): |
| super().__init__() |
| |
| |
| self.ssm_horizontal = SelectiveStateSpace(d_model, d_state) |
| self.ssm_vertical = SelectiveStateSpace(d_model, d_state) |
| |
| |
| self.fusion_gate = nn.Sequential( |
| nn.Linear(d_model, d_model, bias=False), |
| nn.Sigmoid() |
| ) |
| |
| |
| self.norm = nn.LayerNorm(d_model) |
| |
| def forward(self, x: torch.Tensor, H: int, W: int) -> torch.Tensor: |
| """ |
| x: (B, H*W, D) flattened spatial features |
| Returns: (B, H*W, D) with full spatial context |
| """ |
| B, L, D = x.shape |
| |
| |
| x_fwd = self.ssm_horizontal(x) |
| x_bwd = self._reverse_scan(x, self.ssm_horizontal, H, W, reverse_dim='horizontal') |
| |
| |
| x_col = rearrange(x, 'b (h w) d -> b (w h) d', h=H, w=W) |
| x_top_down = self.ssm_vertical(x_col) |
| x_top_down = rearrange(x_top_down, 'b (w h) d -> b (h w) d', h=H, w=W) |
| |
| x_bot_up = self._reverse_scan(x_col, self.ssm_vertical, W, H, reverse_dim='vertical') |
| x_bot_up = rearrange(x_bot_up, 'b (w h) d -> b (h w) d', h=H, w=W) |
| |
| |
| combined = (x_fwd + x_bwd + x_top_down + x_bot_up) / 4.0 |
| gate = self.fusion_gate(x) |
| |
| out = gate * combined + (1 - gate) * x |
| return self.norm(out) |
| |
| def _reverse_scan(self, x, ssm, H, W, reverse_dim): |
| """Scan in reverse direction""" |
| x_rev = x.flip(dims=[1]) |
| y_rev = ssm(x_rev) |
| return y_rev.flip(dims=[1]) |
|
|
|
|
| |
| |
| |
|
|
| class MixFFN(nn.Module): |
| """ |
| Feed-forward network with depthwise convolution for local feature mixing. |
| |
| From SANA: "depth-wise convolution enhances the model's ability to capture |
| local information, compensating for the weaker local information-capturing |
| ability of linear attention" |
| |
| Architecture: Linear → DWConv3x3 → GELU → Gate → Linear |
| This is an inverted bottleneck with gating. |
| """ |
| |
| def __init__(self, d_model: int, expand_ratio: float = 2.5): |
| super().__init__() |
| d_inner = int(d_model * expand_ratio) |
| |
| |
| self.fc1 = nn.Linear(d_model, d_inner * 2) |
| self.dwconv = nn.Conv2d( |
| d_inner, d_inner, kernel_size=3, padding=1, |
| groups=d_inner, bias=True |
| ) |
| self.fc2 = nn.Linear(d_inner, d_model) |
| self.norm = nn.LayerNorm(d_inner) |
| |
| def forward(self, x: torch.Tensor, H: int, W: int) -> torch.Tensor: |
| """ |
| x: (B, H*W, D) |
| Returns: (B, H*W, D) |
| """ |
| B, L, D = x.shape |
| |
| |
| xg = self.fc1(x) |
| x_val, x_gate = xg.chunk(2, dim=-1) |
| |
| |
| x_val = rearrange(x_val, 'b (h w) d -> b d h w', h=H, w=W) |
| x_val = self.dwconv(x_val) |
| x_val = rearrange(x_val, 'b d h w -> b (h w) d') |
| |
| |
| x_val = self.norm(x_val) |
| x_out = x_val * F.gelu(x_gate) |
| |
| return self.fc2(x_out) |
|
|
|
|
| |
| |
| |
|
|
| class HyperConnection(nn.Module): |
| """ |
| Hyper-connections generalize residual connections. |
| |
| Instead of fixed: y = x + F(x) |
| We learn a connection matrix HC that can represent any blend of |
| sequential and parallel layer arrangements. |
| |
| For expansion rate n: |
| Input: split x into n copies [x_1, ..., x_n] |
| HC matrix is (n+1) x (n+1), learnable |
| [input_to_layer, output_1, ..., output_n] = HC @ [F(input_to_layer), x_1, ..., x_n] |
| |
| This subsumes both Pre-Norm and Post-Norm residual connections, |
| and can learn arrangements that are neither purely sequential nor parallel. |
| """ |
| |
| def __init__(self, d_model: int, expansion_rate: int = 2): |
| super().__init__() |
| self.n = expansion_rate |
| self.d_model = d_model |
| |
| |
| |
| init_matrix = torch.zeros(self.n + 1, self.n + 1) |
| |
| init_matrix[0, 1] = 1.0 |
| for i in range(1, self.n + 1): |
| init_matrix[i, i] = 1.0 |
| init_matrix[i, 0] = 1.0 / self.n |
| |
| self.hc_matrix = nn.Parameter(init_matrix) |
| self.norm = nn.LayerNorm(d_model) |
| |
| def pre_forward(self, x_streams: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: |
| """ |
| x_streams: (B, L, n*D) - n parallel streams concatenated |
| Returns: (layer_input, x_streams) |
| """ |
| B, L, _ = x_streams.shape |
| |
| |
| streams = x_streams.chunk(self.n, dim=-1) |
| |
| |
| layer_input = sum(self.hc_matrix[0, i + 1] * streams[i] for i in range(self.n)) |
| layer_input = self.norm(layer_input) |
| |
| return layer_input, x_streams |
| |
| def post_forward(self, layer_output: torch.Tensor, x_streams: torch.Tensor) -> torch.Tensor: |
| """ |
| Combine layer output with streams using HC matrix. |
| """ |
| streams = x_streams.chunk(self.n, dim=-1) |
| |
| new_streams = [] |
| for i in range(self.n): |
| new_stream = self.hc_matrix[i + 1, 0] * layer_output |
| for j in range(self.n): |
| new_stream = new_stream + self.hc_matrix[i + 1, j + 1] * streams[j] |
| new_streams.append(new_stream) |
| |
| return torch.cat(new_streams, dim=-1) |
| |
| def init_streams(self, x: torch.Tensor) -> torch.Tensor: |
| """Initialize n streams from single input""" |
| return x.repeat(1, 1, self.n) |
|
|
|
|
| |
| |
| |
|
|
| class AdaLNZero(nn.Module): |
| """ |
| Adaptive Layer Normalization with zero initialization. |
| |
| Conditions each layer on timestep and text embeddings. |
| From DiT: "regresses dimensionwise scale and shift parameters |
| from the sum of the embedding vectors" |
| |
| Zero initialization ensures the network acts as identity at init, |
| critical for training stability. |
| """ |
| |
| def __init__(self, d_model: int, d_cond: int): |
| super().__init__() |
| self.norm = nn.LayerNorm(d_model, elementwise_affine=False) |
| |
| |
| self.proj = nn.Sequential( |
| nn.SiLU(), |
| nn.Linear(d_cond, 6 * d_model) |
| ) |
| |
| |
| nn.init.zeros_(self.proj[1].weight) |
| nn.init.zeros_(self.proj[1].bias) |
| |
| def forward(self, x: torch.Tensor, cond: torch.Tensor): |
| """ |
| x: (B, L, D) |
| cond: (B, d_cond) |
| Returns: shift1, scale1, gate1, shift2, scale2, gate2 |
| """ |
| params = self.proj(cond) |
| params = params.unsqueeze(1) |
| shift1, scale1, gate1, shift2, scale2, gate2 = params.chunk(6, dim=-1) |
| return shift1, scale1, gate1, shift2, scale2, gate2 |
| |
| def modulate(self, x: torch.Tensor, shift: torch.Tensor, scale: torch.Tensor): |
| return self.norm(x) * (1 + scale) + shift |
|
|
|
|
| |
| |
| |
|
|
| class LiRABlock(nn.Module): |
| """ |
| One LiRA block = Bidirectional SSM + Mix-FFN, with: |
| - AdaLN-Zero conditioning |
| - Hyper-connections for dynamic layer arrangement |
| |
| This replaces transformer blocks with O(N) complexity while maintaining |
| the quality of O(N^2) attention through: |
| 1. Selective state spaces (content-aware) |
| 2. Bidirectional scanning (full spatial context) |
| 3. Mix-FFN (local feature enhancement via DWConv) |
| """ |
| |
| def __init__(self, d_model: int, d_cond: int, d_state: int = 16, |
| ffn_expand: float = 2.5, hc_expansion: int = 2): |
| super().__init__() |
| |
| |
| self.adaln = AdaLNZero(d_model, d_cond) |
| |
| |
| self.scanner = BidirectionalSpatialScanner(d_model, d_state) |
| |
| |
| self.ffn = MixFFN(d_model, ffn_expand) |
| |
| |
| self.norm1 = nn.LayerNorm(d_model) |
| self.norm2 = nn.LayerNorm(d_model) |
| |
| def forward(self, x: torch.Tensor, cond: torch.Tensor, H: int, W: int) -> torch.Tensor: |
| """ |
| x: (B, H*W, D) |
| cond: (B, d_cond) - conditioning vector (timestep + text) |
| Returns: (B, H*W, D) |
| """ |
| |
| shift1, scale1, gate1, shift2, scale2, gate2 = self.adaln(x, cond) |
| |
| |
| x_mod = self.adaln.modulate(x, shift1, scale1) |
| x_ssm = self.scanner(x_mod, H, W) |
| x = x + gate1 * x_ssm |
| |
| |
| x_mod = self.adaln.modulate(x, shift2, scale2) |
| x_ffn = self.ffn(x_mod, H, W) |
| x = x + gate2 * x_ffn |
| |
| return x |
|
|
|
|
| |
| |
| |
|
|
| class GatedCrossStateFusion(nn.Module): |
| """ |
| Novel cross-modal fusion inspired by CrossWKV (from RWKV-7 paper). |
| |
| Instead of expensive cross-attention (O(N*M) where N=image, M=text tokens), |
| we use a state-based cross-modal mechanism: |
| |
| 1. Compress text into a fixed-size state matrix S_text via SSM over text tokens |
| 2. Inject S_text into image SSM states via gated addition |
| 3. This gives O(M + N) complexity instead of O(N*M) |
| |
| Mathematical formulation: |
| S_text = SSM_text(text_tokens) → (D, d_state) state matrix |
| For each image token x_i: |
| h_i = A_i * h_{i-1} + B_i * x_i + G_i * S_text * r_i |
| Where G_i is a learned gate and r_i is a receptance vector. |
| """ |
| |
| def __init__(self, d_model: int, d_text: int, d_state: int = 16, num_heads: int = 8): |
| super().__init__() |
| self.d_model = d_model |
| self.d_state = d_state |
| self.num_heads = num_heads |
| self.head_dim = d_model // num_heads |
| |
| |
| self.text_proj = nn.Linear(d_text, d_model) |
| self.text_key = nn.Linear(d_model, d_model, bias=False) |
| self.text_value = nn.Linear(d_model, d_model, bias=False) |
| |
| |
| self.image_query = nn.Linear(d_model, d_model, bias=False) |
| |
| |
| self.gate = nn.Sequential( |
| nn.Linear(d_model * 2, d_model), |
| nn.Sigmoid() |
| ) |
| |
| |
| self.out_proj = nn.Linear(d_model, d_model, bias=False) |
| self.norm = nn.LayerNorm(d_model) |
| |
| def forward(self, x_image: torch.Tensor, x_text: torch.Tensor) -> torch.Tensor: |
| """ |
| x_image: (B, N, D) - image features |
| x_text: (B, M, D_text) - text features |
| Returns: (B, N, D) - text-conditioned image features |
| """ |
| B, N, D = x_image.shape |
| |
| |
| text_feat = self.text_proj(x_text) |
| |
| |
| |
| text_k = self.text_key(text_feat) |
| text_v = self.text_value(text_feat) |
| |
| |
| text_k = rearrange(text_k, 'b m (h d) -> b h m d', h=self.num_heads) |
| text_v = rearrange(text_v, 'b m (h d) -> b h m d', h=self.num_heads) |
| |
| |
| |
| text_state = torch.einsum('bhmd,bhmk->bhdk', text_k, text_v) / text_k.shape[2] |
| |
| |
| img_q = self.image_query(x_image) |
| img_q = rearrange(img_q, 'b n (h d) -> b h n d', h=self.num_heads) |
| |
| |
| cross_out = torch.einsum('bhnd,bhdk->bhnk', img_q, text_state) |
| cross_out = rearrange(cross_out, 'b h n d -> b n (h d)') |
| |
| |
| gate = self.gate(torch.cat([x_image, cross_out], dim=-1)) |
| out = x_image + gate * cross_out |
| |
| return self.norm(out) |
|
|
|
|
| |
| |
| |
|
|
| class LatentReasoningLoop(nn.Module): |
| """ |
| NOVEL CONTRIBUTION: Iterative reasoning in latent space for image generation. |
| |
| Inspired by Liquid Reasoning Transformer (LRT), but adapted for generative models. |
| |
| Key insight: Image generation benefits from iterative refinement. Instead of |
| a fixed number of denoising steps (expensive), we add a CHEAP inner reasoning |
| loop that refines the latent representation before final prediction. |
| |
| How it works: |
| 1. A "reasoning state" r_t evolves over T_think iterations |
| 2. Each iteration applies a lightweight SSM + FFN to refine r_t |
| 3. A DISCARD GATE filters bad updates (prevents error accumulation) |
| 4. A STOP GATE halts early for easy inputs (adaptive compute) |
| 5. The final r_T is used to condition the denoising prediction |
| |
| This gives the model "thinking time" proportional to input difficulty: |
| - Simple prompts / high noise levels → few reasoning steps |
| - Complex prompts / fine detail refinement → more reasoning steps |
| |
| Mathematical formulation: |
| r_0 = MLP(concat(z_t, c_text, t_embed)) |
| For t in 1..T_max: |
| r_proposal = SSM_think(concat(z_tokens, r_t)) |
| u_t = MLP(r_proposal) # candidate update |
| d_t = σ(W_d [r_{t-1}; u_t]) # discard gate |
| r_t = (1-d_t) * u_t + d_t * r_{t-1} # filtered update |
| s_t = σ(W_s r_t) # stop gate |
| if s_t > τ: break |
| |
| Cost: T_think iterations of a SMALL network (1/10th of main backbone) |
| Typical T_think: 2-8 steps (learned, not fixed) |
| """ |
| |
| def __init__(self, d_model: int, d_reason: int = 128, max_steps: int = 8): |
| super().__init__() |
| self.d_reason = d_reason |
| self.max_steps = max_steps |
| |
| |
| self.state_init = nn.Sequential( |
| nn.Linear(d_model, d_reason * 2), |
| nn.GELU(), |
| nn.Linear(d_reason * 2, d_reason) |
| ) |
| |
| |
| self.reason_ssm = SelectiveStateSpace(d_reason, d_state=8, d_conv=3) |
| self.reason_ffn = nn.Sequential( |
| nn.Linear(d_reason, d_reason * 2), |
| nn.GELU(), |
| nn.Linear(d_reason * 2, d_reason) |
| ) |
| self.reason_norm = nn.LayerNorm(d_reason) |
| |
| |
| self.discard_gate = nn.Sequential( |
| nn.Linear(d_reason * 2, d_reason), |
| nn.Sigmoid() |
| ) |
| |
| |
| self.stop_gate = nn.Sequential( |
| nn.Linear(d_reason, 1), |
| nn.Sigmoid() |
| ) |
| self.stop_threshold = 0.8 |
| |
| |
| self.reason_proj = nn.Linear(d_reason, d_model) |
| |
| def forward(self, x: torch.Tensor, return_steps: bool = False) -> Tuple[torch.Tensor, dict]: |
| """ |
| x: (B, L, D) - input features (latent tokens + conditioning) |
| Returns: (B, D_model) reasoning conditioning vector, info dict |
| """ |
| B = x.shape[0] |
| |
| |
| x_global = x.mean(dim=1) |
| r = self.state_init(x_global) |
| |
| info = {'steps': [], 'discard_rates': [], 'stop_values': []} |
| |
| |
| total_steps = 0 |
| for step in range(self.max_steps): |
| |
| r_expanded = r.unsqueeze(1).expand(-1, x.shape[1], -1) |
| |
| |
| r_processed = self.reason_ssm(self.reason_norm(r_expanded)) |
| r_proposal = self.reason_ffn(r_processed.mean(dim=1)) |
| |
| |
| d = self.discard_gate(torch.cat([r, r_proposal], dim=-1)) |
| r_new = d * r + (1 - d) * r_proposal |
| |
| |
| s = self.stop_gate(r_new).squeeze(-1) |
| |
| info['discard_rates'].append(d.mean().item()) |
| info['stop_values'].append(s.mean().item()) |
| |
| r = r_new |
| total_steps += 1 |
| |
| |
| if not self.training and (s > self.stop_threshold).all(): |
| break |
| |
| info['total_steps'] = total_steps |
| |
| |
| cond = self.reason_proj(r) |
| return cond, info |
|
|
|
|
| |
| |
| |
|
|
| class TimestepEmbedding(nn.Module): |
| """ |
| Sinusoidal timestep embedding with MLP projection. |
| Standard approach from DDPM, with the addition of frequency scaling |
| for better coverage of the continuous [0,1] range used in flow matching. |
| """ |
| |
| def __init__(self, d_model: int, max_period: int = 10000): |
| super().__init__() |
| self.d_model = d_model |
| self.max_period = max_period |
| |
| self.mlp = nn.Sequential( |
| nn.Linear(d_model, d_model * 4), |
| nn.SiLU(), |
| nn.Linear(d_model * 4, d_model) |
| ) |
| |
| def forward(self, t: torch.Tensor) -> torch.Tensor: |
| """ |
| t: (B,) timestep values in [0, 1] |
| Returns: (B, d_model) |
| """ |
| half_dim = self.d_model // 2 |
| freqs = torch.exp( |
| -math.log(self.max_period) * torch.arange(half_dim, device=t.device).float() / half_dim |
| ) |
| args = t.unsqueeze(1) * freqs.unsqueeze(0) * 1000 |
| embedding = torch.cat([torch.sin(args), torch.cos(args)], dim=-1) |
| |
| if self.d_model % 2: |
| embedding = F.pad(embedding, (0, 1)) |
| |
| return self.mlp(embedding) |
|
|
|
|
| class TextProjection(nn.Module): |
| """ |
| Projects text encoder outputs to model dimension. |
| Supports variable-length text with a pooled global + per-token output. |
| """ |
| |
| def __init__(self, d_text: int, d_model: int): |
| super().__init__() |
| self.proj = nn.Linear(d_text, d_model) |
| self.pool_proj = nn.Linear(d_text, d_model) |
| self.norm = nn.LayerNorm(d_model) |
| |
| def forward(self, text_features: torch.Tensor, text_mask: Optional[torch.Tensor] = None): |
| """ |
| text_features: (B, M, D_text) |
| text_mask: (B, M) boolean mask |
| Returns: per_token (B, M, D), pooled (B, D) |
| """ |
| per_token = self.norm(self.proj(text_features)) |
| |
| if text_mask is not None: |
| |
| mask = text_mask.unsqueeze(-1).float() |
| pooled = (text_features * mask).sum(1) / mask.sum(1).clamp(min=1) |
| else: |
| pooled = text_features.mean(dim=1) |
| |
| pooled = self.pool_proj(pooled) |
| return per_token, pooled |
|
|