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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ArtFlow: Reasoning-Native Artistic Image Generation for Mobile Devices
|
| 3 |
+
===========================================================================
|
| 4 |
+
Complete prototype implementation for architecture validation.
|
| 5 |
+
This code validates:
|
| 6 |
+
1. All tensor shapes are correct through the full pipeline
|
| 7 |
+
2. Memory usage is within mobile budget
|
| 8 |
+
3. Forward/backward pass works correctly
|
| 9 |
+
4. FLOPs and parameter counts match specification
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
import torch.nn.functional as F
|
| 15 |
+
import math
|
| 16 |
+
from typing import Optional, Tuple
|
| 17 |
+
from dataclasses import dataclass
|
| 18 |
+
|
| 19 |
+
# ============================================================================
|
| 20 |
+
# Configuration
|
| 21 |
+
# ============================================================================
|
| 22 |
+
|
| 23 |
+
@dataclass
|
| 24 |
+
class ArtFlowConfig:
|
| 25 |
+
"""Complete model configuration."""
|
| 26 |
+
# Latent space (assuming DC-AE f32 or similar)
|
| 27 |
+
latent_channels: int = 32
|
| 28 |
+
latent_size: int = 32 # For 1024px with f32 compression
|
| 29 |
+
|
| 30 |
+
# UNet channels per stage
|
| 31 |
+
stage_channels: Tuple[int, ...] = (256, 512, 768)
|
| 32 |
+
|
| 33 |
+
# WaveMamba settings
|
| 34 |
+
mamba_state_dim: int = 16 # SSM state dimension N
|
| 35 |
+
mamba_expand: int = 2 # Expansion factor in Mamba
|
| 36 |
+
|
| 37 |
+
# Blocks per stage
|
| 38 |
+
blocks_per_stage: Tuple[int, ...] = (2, 2, 2)
|
| 39 |
+
bottleneck_blocks: int = 4
|
| 40 |
+
|
| 41 |
+
# Reasoning
|
| 42 |
+
reasoning_recursions: int = 2 # R in RLR
|
| 43 |
+
|
| 44 |
+
# ArtStyle Matrix
|
| 45 |
+
num_styles: int = 256
|
| 46 |
+
style_dim: int = 512
|
| 47 |
+
|
| 48 |
+
# Mood Controller
|
| 49 |
+
mood_dim: int = 128
|
| 50 |
+
num_moods: int = 32
|
| 51 |
+
|
| 52 |
+
# Text
|
| 53 |
+
text_dim: int = 768
|
| 54 |
+
text_length: int = 77
|
| 55 |
+
|
| 56 |
+
# Attention
|
| 57 |
+
num_heads: int = 8
|
| 58 |
+
num_kv_heads: int = 1 # MQA
|
| 59 |
+
|
| 60 |
+
# General
|
| 61 |
+
dropout: float = 0.0
|
| 62 |
+
|
| 63 |
+
# Concept Reasoning
|
| 64 |
+
num_concept_nodes: int = 16
|
| 65 |
+
concept_dim: int = 256
|
| 66 |
+
kan_grid_size: int = 5
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# ============================================================================
|
| 70 |
+
# Utility Layers
|
| 71 |
+
# ============================================================================
|
| 72 |
+
|
| 73 |
+
class RMSNorm(nn.Module):
|
| 74 |
+
"""Root Mean Square Layer Normalization."""
|
| 75 |
+
def __init__(self, dim: int, eps: float = 1e-6):
|
| 76 |
+
super().__init__()
|
| 77 |
+
self.eps = eps
|
| 78 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 79 |
+
|
| 80 |
+
def forward(self, x):
|
| 81 |
+
rms = torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 82 |
+
return x * rms * self.weight
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class SinusoidalPositionEmbedding(nn.Module):
|
| 86 |
+
"""Sinusoidal timestep embedding."""
|
| 87 |
+
def __init__(self, dim: int):
|
| 88 |
+
super().__init__()
|
| 89 |
+
self.dim = dim
|
| 90 |
+
|
| 91 |
+
def forward(self, t: torch.Tensor) -> torch.Tensor:
|
| 92 |
+
half_dim = self.dim // 2
|
| 93 |
+
emb = math.log(10000) / (half_dim - 1)
|
| 94 |
+
emb = torch.exp(torch.arange(half_dim, device=t.device) * -emb)
|
| 95 |
+
emb = t[:, None] * emb[None, :]
|
| 96 |
+
return torch.cat([emb.sin(), emb.cos()], dim=-1)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class AdaLNZero(nn.Module):
|
| 100 |
+
"""Adaptive Layer Normalization with Zero initialization."""
|
| 101 |
+
def __init__(self, dim: int, cond_dim: int):
|
| 102 |
+
super().__init__()
|
| 103 |
+
self.norm = RMSNorm(dim)
|
| 104 |
+
self.proj = nn.Linear(cond_dim, dim * 3)
|
| 105 |
+
nn.init.zeros_(self.proj.weight)
|
| 106 |
+
nn.init.zeros_(self.proj.bias)
|
| 107 |
+
|
| 108 |
+
def forward(self, x: torch.Tensor, cond: torch.Tensor) -> torch.Tensor:
|
| 109 |
+
gamma, beta, alpha = self.proj(cond).chunk(3, dim=-1)
|
| 110 |
+
# Reshape for spatial tensors if needed
|
| 111 |
+
while gamma.dim() < x.dim():
|
| 112 |
+
gamma = gamma.unsqueeze(-2)
|
| 113 |
+
beta = beta.unsqueeze(-2)
|
| 114 |
+
alpha = alpha.unsqueeze(-2)
|
| 115 |
+
return alpha * (gamma * self.norm(x) + beta)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# ============================================================================
|
| 119 |
+
# Wavelet Transform (Parameter-free, O(n))
|
| 120 |
+
# ============================================================================
|
| 121 |
+
|
| 122 |
+
class HaarWavelet2D(nn.Module):
|
| 123 |
+
"""2D Haar Wavelet Transform - parameter free, O(n) complexity."""
|
| 124 |
+
|
| 125 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, ...]:
|
| 126 |
+
"""
|
| 127 |
+
x: (B, C, H, W) -> (LL, LH, HL, HH) each (B, C, H/2, W/2)
|
| 128 |
+
"""
|
| 129 |
+
# Ensure even dimensions
|
| 130 |
+
B, C, H, W = x.shape
|
| 131 |
+
assert H % 2 == 0 and W % 2 == 0, f"Dimensions must be even, got {H}x{W}"
|
| 132 |
+
|
| 133 |
+
# Vectorized Haar wavelet (no loops!)
|
| 134 |
+
x_00 = x[:, :, 0::2, 0::2] # Even rows, even cols
|
| 135 |
+
x_01 = x[:, :, 0::2, 1::2] # Even rows, odd cols
|
| 136 |
+
x_10 = x[:, :, 1::2, 0::2] # Odd rows, even cols
|
| 137 |
+
x_11 = x[:, :, 1::2, 1::2] # Odd rows, odd cols
|
| 138 |
+
|
| 139 |
+
LL = (x_00 + x_01 + x_10 + x_11) * 0.5
|
| 140 |
+
LH = (x_00 + x_01 - x_10 - x_11) * 0.5
|
| 141 |
+
HL = (x_00 - x_01 + x_10 - x_11) * 0.5
|
| 142 |
+
HH = (x_00 - x_01 - x_10 + x_11) * 0.5
|
| 143 |
+
|
| 144 |
+
return LL, LH, HL, HH
|
| 145 |
+
|
| 146 |
+
def inverse(self, LL, LH, HL, HH) -> torch.Tensor:
|
| 147 |
+
"""Inverse wavelet: (B, C, H/2, W/2) ร 4 -> (B, C, H, W)"""
|
| 148 |
+
B, C, H2, W2 = LL.shape
|
| 149 |
+
|
| 150 |
+
x_00 = (LL + LH + HL + HH) * 0.5
|
| 151 |
+
x_01 = (LL + LH - HL - HH) * 0.5
|
| 152 |
+
x_10 = (LL - LH + HL - HH) * 0.5
|
| 153 |
+
x_11 = (LL - LH - HL + HH) * 0.5
|
| 154 |
+
|
| 155 |
+
x = torch.zeros(B, C, H2 * 2, W2 * 2, device=LL.device, dtype=LL.dtype)
|
| 156 |
+
x[:, :, 0::2, 0::2] = x_00
|
| 157 |
+
x[:, :, 0::2, 1::2] = x_01
|
| 158 |
+
x[:, :, 1::2, 0::2] = x_10
|
| 159 |
+
x[:, :, 1::2, 1::2] = x_11
|
| 160 |
+
|
| 161 |
+
return x
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# ============================================================================
|
| 165 |
+
# Zigzag Scan (from ZigMa paper, maintains spatial continuity)
|
| 166 |
+
# ============================================================================
|
| 167 |
+
|
| 168 |
+
def create_zigzag_indices(H: int, W: int) -> torch.Tensor:
|
| 169 |
+
"""Create zigzag scan indices for HรW grid."""
|
| 170 |
+
indices = []
|
| 171 |
+
for i in range(H):
|
| 172 |
+
if i % 2 == 0:
|
| 173 |
+
for j in range(W):
|
| 174 |
+
indices.append(i * W + j)
|
| 175 |
+
else:
|
| 176 |
+
for j in range(W - 1, -1, -1):
|
| 177 |
+
indices.append(i * W + j)
|
| 178 |
+
return torch.tensor(indices, dtype=torch.long)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def zigzag_flatten(x: torch.Tensor) -> torch.Tensor:
|
| 182 |
+
"""Flatten 2D feature map using zigzag scan. x: (B, C, H, W) -> (B, H*W, C)"""
|
| 183 |
+
B, C, H, W = x.shape
|
| 184 |
+
x_flat = x.permute(0, 2, 3, 1).reshape(B, H * W, C) # (B, HW, C)
|
| 185 |
+
indices = create_zigzag_indices(H, W).to(x.device)
|
| 186 |
+
return x_flat[:, indices, :]
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def zigzag_unflatten(x: torch.Tensor, H: int, W: int) -> torch.Tensor:
|
| 190 |
+
"""Unflatten zigzag-scanned sequence back to 2D. x: (B, H*W, C) -> (B, C, H, W)"""
|
| 191 |
+
B, N, C = x.shape
|
| 192 |
+
indices = create_zigzag_indices(H, W).to(x.device)
|
| 193 |
+
# Create inverse mapping
|
| 194 |
+
inv_indices = torch.empty_like(indices)
|
| 195 |
+
inv_indices[indices] = torch.arange(N, device=x.device)
|
| 196 |
+
x_unscanned = x[:, inv_indices, :]
|
| 197 |
+
return x_unscanned.reshape(B, H, W, C).permute(0, 3, 1, 2)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# ============================================================================
|
| 201 |
+
# Selective State Space Model (Mamba-style, simplified)
|
| 202 |
+
# ============================================================================
|
| 203 |
+
|
| 204 |
+
class SelectiveSSM(nn.Module):
|
| 205 |
+
"""
|
| 206 |
+
Simplified Selective State Space Model (Mamba-style).
|
| 207 |
+
O(n) complexity in sequence length.
|
| 208 |
+
"""
|
| 209 |
+
def __init__(self, d_model: int, state_dim: int = 16, expand: int = 2):
|
| 210 |
+
super().__init__()
|
| 211 |
+
d_inner = d_model * expand
|
| 212 |
+
|
| 213 |
+
# Input projection
|
| 214 |
+
self.in_proj = nn.Linear(d_model, d_inner * 2, bias=False)
|
| 215 |
+
|
| 216 |
+
# SSM parameters
|
| 217 |
+
self.conv1d = nn.Conv1d(d_inner, d_inner, kernel_size=3, padding=1, groups=d_inner)
|
| 218 |
+
|
| 219 |
+
# Selective projections (input-dependent B, C, ฮ)
|
| 220 |
+
self.x_proj = nn.Linear(d_inner, state_dim * 2 + 1, bias=False) # B, C, dt
|
| 221 |
+
|
| 222 |
+
# A parameter (log-space for stability)
|
| 223 |
+
A = torch.arange(1, state_dim + 1, dtype=torch.float32).unsqueeze(0).expand(d_inner, -1)
|
| 224 |
+
self.A_log = nn.Parameter(torch.log(A))
|
| 225 |
+
|
| 226 |
+
# D parameter (skip connection)
|
| 227 |
+
self.D = nn.Parameter(torch.ones(d_inner))
|
| 228 |
+
|
| 229 |
+
# Output projection
|
| 230 |
+
self.out_proj = nn.Linear(d_inner, d_model, bias=False)
|
| 231 |
+
|
| 232 |
+
self.d_inner = d_inner
|
| 233 |
+
self.state_dim = state_dim
|
| 234 |
+
|
| 235 |
+
def forward(self, x: torch.Tensor, style_mod: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 236 |
+
"""
|
| 237 |
+
x: (B, L, D) - input sequence
|
| 238 |
+
style_mod: (B, D) optional style modulation
|
| 239 |
+
"""
|
| 240 |
+
B, L, D = x.shape
|
| 241 |
+
|
| 242 |
+
# Input projection with gating
|
| 243 |
+
xz = self.in_proj(x) # (B, L, 2*d_inner)
|
| 244 |
+
x_inner, z = xz.chunk(2, dim=-1) # Each (B, L, d_inner)
|
| 245 |
+
|
| 246 |
+
# Depthwise conv for local context
|
| 247 |
+
x_inner = self.conv1d(x_inner.transpose(1, 2)).transpose(1, 2)
|
| 248 |
+
x_inner = F.silu(x_inner)
|
| 249 |
+
|
| 250 |
+
# Selective SSM parameters (input-dependent)
|
| 251 |
+
x_params = self.x_proj(x_inner) # (B, L, 2*N + 1)
|
| 252 |
+
B_sel = x_params[..., :self.state_dim] # (B, L, N)
|
| 253 |
+
C_sel = x_params[..., self.state_dim:2*self.state_dim] # (B, L, N)
|
| 254 |
+
dt = F.softplus(x_params[..., -1:]) # (B, L, 1)
|
| 255 |
+
|
| 256 |
+
# Style modulation of SSM parameters
|
| 257 |
+
if style_mod is not None:
|
| 258 |
+
# Project style to modulation signals
|
| 259 |
+
# (B, d_style) -> (B, 1, N) for B and C bias
|
| 260 |
+
style_B = style_mod[:, :self.state_dim].unsqueeze(1)
|
| 261 |
+
style_C = style_mod[:, self.state_dim:2*self.state_dim].unsqueeze(1)
|
| 262 |
+
B_sel = B_sel + style_B
|
| 263 |
+
C_sel = C_sel + style_C
|
| 264 |
+
|
| 265 |
+
# Discretize A
|
| 266 |
+
A = -torch.exp(self.A_log) # (d_inner, N)
|
| 267 |
+
|
| 268 |
+
# Sequential scan (vectorized as much as possible)
|
| 269 |
+
# For prototype: simple sequential implementation
|
| 270 |
+
# Production: use parallel scan / Mamba CUDA kernel
|
| 271 |
+
dt_expanded = dt.expand(-1, -1, self.d_inner) # (B, L, d_inner)
|
| 272 |
+
|
| 273 |
+
# Simplified SSM: use cumulative operations instead of true recurrence
|
| 274 |
+
# This is mathematically equivalent for the linear SSM part
|
| 275 |
+
dA = torch.exp(dt_expanded.unsqueeze(-1) * A.unsqueeze(0).unsqueeze(0)) # (B, L, d_inner, N)
|
| 276 |
+
dB = dt_expanded.unsqueeze(-1) * B_sel.unsqueeze(2) # (B, L, d_inner, N)
|
| 277 |
+
|
| 278 |
+
# Compute output via scan (simplified: chunk-based for efficiency)
|
| 279 |
+
# For the prototype, we use a simple loop over chunks
|
| 280 |
+
chunk_size = min(64, L)
|
| 281 |
+
y = torch.zeros_like(x_inner)
|
| 282 |
+
h = torch.zeros(B, self.d_inner, self.state_dim, device=x.device, dtype=x.dtype)
|
| 283 |
+
|
| 284 |
+
for i in range(0, L, chunk_size):
|
| 285 |
+
end = min(i + chunk_size, L)
|
| 286 |
+
for j in range(i, end):
|
| 287 |
+
h = h * dA[:, j] + dB[:, j] * x_inner[:, j:j+1, :].transpose(1, 2)
|
| 288 |
+
y_j = (h * C_sel[:, j].unsqueeze(1)).sum(-1) # (B, d_inner)
|
| 289 |
+
y[:, j] = y_j
|
| 290 |
+
|
| 291 |
+
# Skip connection
|
| 292 |
+
y = y + x_inner * self.D.unsqueeze(0).unsqueeze(0)
|
| 293 |
+
|
| 294 |
+
# Gate
|
| 295 |
+
y = y * F.silu(z)
|
| 296 |
+
|
| 297 |
+
# Output projection
|
| 298 |
+
return self.out_proj(y)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
# ============================================================================
|
| 302 |
+
# WaveMamba Block
|
| 303 |
+
# ============================================================================
|
| 304 |
+
|
| 305 |
+
class WaveMambaBlock(nn.Module):
|
| 306 |
+
"""
|
| 307 |
+
Wavelet-decomposed Mamba block. Core innovation of ArtFlow.
|
| 308 |
+
Decomposes input into frequency subbands, processes each with Mamba,
|
| 309 |
+
then reconstructs. O(n) complexity with frequency awareness.
|
| 310 |
+
"""
|
| 311 |
+
def __init__(self, channels: int, config: ArtFlowConfig):
|
| 312 |
+
super().__init__()
|
| 313 |
+
self.wavelet = HaarWavelet2D()
|
| 314 |
+
|
| 315 |
+
# One Mamba per subband (shared weights for LL and detail bands)
|
| 316 |
+
self.mamba_low = SelectiveSSM(channels, config.mamba_state_dim, config.mamba_expand)
|
| 317 |
+
self.mamba_high = SelectiveSSM(channels, config.mamba_state_dim, config.mamba_expand)
|
| 318 |
+
|
| 319 |
+
# Pre/post norms
|
| 320 |
+
self.norm_pre = RMSNorm(channels)
|
| 321 |
+
self.norm_post = RMSNorm(channels)
|
| 322 |
+
|
| 323 |
+
# AdaLN for conditioning
|
| 324 |
+
self.adaln = AdaLNZero(channels, config.style_dim + config.text_dim)
|
| 325 |
+
|
| 326 |
+
# Style projection for Mamba modulation
|
| 327 |
+
self.style_proj = nn.Linear(config.style_dim, config.mamba_state_dim * 2)
|
| 328 |
+
|
| 329 |
+
def forward(self, x: torch.Tensor, cond: torch.Tensor,
|
| 330 |
+
style_mod: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 331 |
+
"""
|
| 332 |
+
x: (B, C, H, W)
|
| 333 |
+
cond: (B, cond_dim) - combined conditioning
|
| 334 |
+
style_mod: (B, style_dim) - style modulation
|
| 335 |
+
"""
|
| 336 |
+
residual = x
|
| 337 |
+
B, C, H, W = x.shape
|
| 338 |
+
|
| 339 |
+
# Pre-norm
|
| 340 |
+
x_flat = x.permute(0, 2, 3, 1).reshape(B * H * W, C)
|
| 341 |
+
x_flat = self.norm_pre(x_flat).reshape(B, H, W, C).permute(0, 3, 1, 2)
|
| 342 |
+
|
| 343 |
+
# Wavelet decomposition
|
| 344 |
+
LL, LH, HL, HH = self.wavelet(x_flat)
|
| 345 |
+
H2, W2 = H // 2, W // 2
|
| 346 |
+
|
| 347 |
+
# Style modulation signal
|
| 348 |
+
ssm_style = self.style_proj(style_mod) if style_mod is not None else None
|
| 349 |
+
|
| 350 |
+
# Zigzag flatten each subband
|
| 351 |
+
seq_LL = zigzag_flatten(LL) # (B, H2*W2, C)
|
| 352 |
+
seq_LH = zigzag_flatten(LH)
|
| 353 |
+
seq_HL = zigzag_flatten(HL)
|
| 354 |
+
seq_HH = zigzag_flatten(HH)
|
| 355 |
+
|
| 356 |
+
# Process with Mamba
|
| 357 |
+
out_LL = self.mamba_low(seq_LL, ssm_style)
|
| 358 |
+
out_LH = self.mamba_high(seq_LH, ssm_style)
|
| 359 |
+
out_HL = self.mamba_high(seq_HL, ssm_style)
|
| 360 |
+
out_HH = self.mamba_high(seq_HH, ssm_style)
|
| 361 |
+
|
| 362 |
+
# Zigzag unflatten
|
| 363 |
+
out_LL = zigzag_unflatten(out_LL, H2, W2)
|
| 364 |
+
out_LH = zigzag_unflatten(out_LH, H2, W2)
|
| 365 |
+
out_HL = zigzag_unflatten(out_HL, H2, W2)
|
| 366 |
+
out_HH = zigzag_unflatten(out_HH, H2, W2)
|
| 367 |
+
|
| 368 |
+
# Inverse wavelet reconstruction
|
| 369 |
+
y = self.wavelet.inverse(out_LL, out_LH, out_HL, out_HH)
|
| 370 |
+
|
| 371 |
+
# AdaLN + residual
|
| 372 |
+
y_flat = y.permute(0, 2, 3, 1).reshape(B, H * W, C)
|
| 373 |
+
y_flat = self.adaln(y_flat, cond)
|
| 374 |
+
y = y_flat.reshape(B, H, W, C).permute(0, 3, 1, 2)
|
| 375 |
+
|
| 376 |
+
return residual + y
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
# ============================================================================
|
| 380 |
+
# Expanded Separable Convolution Block (for high-res stages)
|
| 381 |
+
# ============================================================================
|
| 382 |
+
|
| 383 |
+
class SepConvBlock(nn.Module):
|
| 384 |
+
"""Expanded separable convolution block (UIB-inspired, from SnapGen)."""
|
| 385 |
+
def __init__(self, channels: int, expansion: int = 2):
|
| 386 |
+
super().__init__()
|
| 387 |
+
expanded = channels * expansion
|
| 388 |
+
|
| 389 |
+
self.norm = nn.GroupNorm(32, channels)
|
| 390 |
+
self.dw_conv = nn.Conv2d(channels, channels, 3, padding=1, groups=channels)
|
| 391 |
+
self.pw_expand = nn.Conv2d(channels, expanded, 1)
|
| 392 |
+
self.act = nn.SiLU()
|
| 393 |
+
self.pw_reduce = nn.Conv2d(expanded, channels, 1)
|
| 394 |
+
|
| 395 |
+
# Zero-init for residual stability
|
| 396 |
+
nn.init.zeros_(self.pw_reduce.weight)
|
| 397 |
+
nn.init.zeros_(self.pw_reduce.bias)
|
| 398 |
+
|
| 399 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 400 |
+
residual = x
|
| 401 |
+
x = self.norm(x)
|
| 402 |
+
x = self.dw_conv(x)
|
| 403 |
+
x = self.pw_expand(x)
|
| 404 |
+
x = self.act(x)
|
| 405 |
+
x = self.pw_reduce(x)
|
| 406 |
+
return residual + x
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
# ============================================================================
|
| 410 |
+
# Multi-Query Cross Attention
|
| 411 |
+
# ============================================================================
|
| 412 |
+
|
| 413 |
+
class MultiQueryCrossAttention(nn.Module):
|
| 414 |
+
"""Multi-Query Attention for text conditioning (from SnapGen)."""
|
| 415 |
+
def __init__(self, dim: int, text_dim: int, num_heads: int = 8, num_kv_heads: int = 1):
|
| 416 |
+
super().__init__()
|
| 417 |
+
self.num_heads = num_heads
|
| 418 |
+
self.num_kv_heads = num_kv_heads
|
| 419 |
+
self.head_dim = dim // num_heads
|
| 420 |
+
|
| 421 |
+
self.q_proj = nn.Linear(dim, dim)
|
| 422 |
+
self.k_proj = nn.Linear(text_dim, self.head_dim * num_kv_heads)
|
| 423 |
+
self.v_proj = nn.Linear(text_dim, self.head_dim * num_kv_heads)
|
| 424 |
+
self.out_proj = nn.Linear(dim, dim)
|
| 425 |
+
|
| 426 |
+
# QK RMSNorm for training stability
|
| 427 |
+
self.q_norm = RMSNorm(self.head_dim)
|
| 428 |
+
self.k_norm = RMSNorm(self.head_dim)
|
| 429 |
+
|
| 430 |
+
self.norm = RMSNorm(dim)
|
| 431 |
+
|
| 432 |
+
def forward(self, x: torch.Tensor, text_emb: torch.Tensor) -> torch.Tensor:
|
| 433 |
+
"""
|
| 434 |
+
x: (B, N, D) - image features (flattened spatial)
|
| 435 |
+
text_emb: (B, L, text_dim) - text embeddings
|
| 436 |
+
"""
|
| 437 |
+
B, N, D = x.shape
|
| 438 |
+
residual = x
|
| 439 |
+
x = self.norm(x)
|
| 440 |
+
|
| 441 |
+
Q = self.q_proj(x).reshape(B, N, self.num_heads, self.head_dim).transpose(1, 2)
|
| 442 |
+
K = self.k_proj(text_emb).reshape(B, -1, self.num_kv_heads, self.head_dim).transpose(1, 2)
|
| 443 |
+
V = self.v_proj(text_emb).reshape(B, -1, self.num_kv_heads, self.head_dim).transpose(1, 2)
|
| 444 |
+
|
| 445 |
+
# QK Normalization
|
| 446 |
+
Q = self.q_norm(Q)
|
| 447 |
+
K = self.k_norm(K)
|
| 448 |
+
|
| 449 |
+
# Expand KV heads to match Q heads
|
| 450 |
+
if self.num_kv_heads < self.num_heads:
|
| 451 |
+
repeat = self.num_heads // self.num_kv_heads
|
| 452 |
+
K = K.repeat(1, repeat, 1, 1)
|
| 453 |
+
V = V.repeat(1, repeat, 1, 1)
|
| 454 |
+
|
| 455 |
+
# Attention
|
| 456 |
+
scale = self.head_dim ** -0.5
|
| 457 |
+
attn = torch.matmul(Q, K.transpose(-2, -1)) * scale
|
| 458 |
+
attn = F.softmax(attn, dim=-1)
|
| 459 |
+
|
| 460 |
+
out = torch.matmul(attn, V)
|
| 461 |
+
out = out.transpose(1, 2).reshape(B, N, D)
|
| 462 |
+
out = self.out_proj(out)
|
| 463 |
+
|
| 464 |
+
return residual + out
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
# ============================================================================
|
| 468 |
+
# ArtStyle Matrix Module
|
| 469 |
+
# ============================================================================
|
| 470 |
+
|
| 471 |
+
class ArtStyleMatrix(nn.Module):
|
| 472 |
+
"""Learnable art style matrix with continuous interpolation."""
|
| 473 |
+
def __init__(self, config: ArtFlowConfig):
|
| 474 |
+
super().__init__()
|
| 475 |
+
self.style_matrix = nn.Parameter(torch.randn(config.num_styles, config.style_dim) * 0.02)
|
| 476 |
+
self.style_mlp = nn.Sequential(
|
| 477 |
+
nn.Linear(config.style_dim, config.style_dim * 4),
|
| 478 |
+
nn.SiLU(),
|
| 479 |
+
nn.Linear(config.style_dim * 4, config.style_dim * 4),
|
| 480 |
+
nn.SiLU(),
|
| 481 |
+
nn.Linear(config.style_dim * 4, config.style_dim),
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
def forward(self, style_ids: Optional[torch.Tensor] = None,
|
| 485 |
+
style_weights: Optional[torch.Tensor] = None,
|
| 486 |
+
custom_style: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 487 |
+
"""
|
| 488 |
+
Three modes:
|
| 489 |
+
1. style_ids: (B,) integer IDs -> lookup
|
| 490 |
+
2. style_weights: (B, K) weights for weighted combination
|
| 491 |
+
3. custom_style: (B, d) custom style vector
|
| 492 |
+
"""
|
| 493 |
+
if custom_style is not None:
|
| 494 |
+
style_vec = custom_style
|
| 495 |
+
elif style_weights is not None:
|
| 496 |
+
style_vec = torch.matmul(style_weights, self.style_matrix)
|
| 497 |
+
elif style_ids is not None:
|
| 498 |
+
style_vec = self.style_matrix[style_ids]
|
| 499 |
+
else:
|
| 500 |
+
# Default: mean of all styles (neutral)
|
| 501 |
+
style_vec = self.style_matrix.mean(0, keepdim=True)
|
| 502 |
+
|
| 503 |
+
return self.style_mlp(style_vec)
|
| 504 |
+
|
| 505 |
+
|
| 506 |
+
# ============================================================================
|
| 507 |
+
# Mood Controller (Liquid Dynamics)
|
| 508 |
+
# ============================================================================
|
| 509 |
+
|
| 510 |
+
class MoodController(nn.Module):
|
| 511 |
+
"""Mood controller with liquid neural network-inspired dynamics."""
|
| 512 |
+
def __init__(self, config: ArtFlowConfig):
|
| 513 |
+
super().__init__()
|
| 514 |
+
self.mood_embedding = nn.Embedding(config.num_moods, config.mood_dim)
|
| 515 |
+
|
| 516 |
+
# Liquid time constant network
|
| 517 |
+
self.tau_net = nn.Sequential(
|
| 518 |
+
nn.Linear(config.mood_dim, config.mood_dim * 2),
|
| 519 |
+
nn.SiLU(),
|
| 520 |
+
nn.Linear(config.mood_dim * 2, config.style_dim),
|
| 521 |
+
nn.Sigmoid(), # ฯ โ (0, 1) โ controls dynamics speed
|
| 522 |
+
)
|
| 523 |
+
|
| 524 |
+
# Mood to modulation
|
| 525 |
+
self.mood_proj = nn.Sequential(
|
| 526 |
+
nn.Linear(config.mood_dim, config.style_dim),
|
| 527 |
+
nn.SiLU(),
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
def forward(self, mood_ids: Optional[torch.Tensor] = None,
|
| 531 |
+
mood_vector: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 532 |
+
"""
|
| 533 |
+
Returns mood modulation signal with liquid dynamics.
|
| 534 |
+
"""
|
| 535 |
+
if mood_vector is not None:
|
| 536 |
+
m = mood_vector
|
| 537 |
+
elif mood_ids is not None:
|
| 538 |
+
m = self.mood_embedding(mood_ids)
|
| 539 |
+
else:
|
| 540 |
+
m = torch.zeros(1, self.mood_embedding.embedding_dim,
|
| 541 |
+
device=self.mood_embedding.weight.device)
|
| 542 |
+
|
| 543 |
+
tau = self.tau_net(m) + 0.1 # Avoid division by zero
|
| 544 |
+
mood_signal = self.mood_proj(m) / tau # Signal scaled by dynamics
|
| 545 |
+
|
| 546 |
+
return mood_signal
|
| 547 |
+
|
| 548 |
+
|
| 549 |
+
# ============================================================================
|
| 550 |
+
# Concept Reasoning Engine (with KAN-inspired composition)
|
| 551 |
+
# ============================================================================
|
| 552 |
+
|
| 553 |
+
class BSplineBasis(nn.Module):
|
| 554 |
+
"""B-spline basis for KAN-style learnable activations."""
|
| 555 |
+
def __init__(self, grid_size: int = 5, degree: int = 3):
|
| 556 |
+
super().__init__()
|
| 557 |
+
self.grid_size = grid_size
|
| 558 |
+
self.degree = degree
|
| 559 |
+
# Uniform grid
|
| 560 |
+
grid = torch.linspace(-1, 1, grid_size + degree + 1)
|
| 561 |
+
self.register_buffer('grid', grid)
|
| 562 |
+
|
| 563 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 564 |
+
"""Evaluate B-spline basis functions at x. Returns (*, grid_size) tensor."""
|
| 565 |
+
# Simplified: use RBF-like basis instead of true B-splines for efficiency
|
| 566 |
+
centers = torch.linspace(-1, 1, self.grid_size, device=x.device)
|
| 567 |
+
width = 2.0 / (self.grid_size - 1)
|
| 568 |
+
return torch.exp(-((x.unsqueeze(-1) - centers) ** 2) / (2 * width ** 2))
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
class KANLayer(nn.Module):
|
| 572 |
+
"""Kolmogorov-Arnold Network layer with learnable activation functions."""
|
| 573 |
+
def __init__(self, d_in: int, d_out: int, grid_size: int = 5):
|
| 574 |
+
super().__init__()
|
| 575 |
+
self.d_in = d_in
|
| 576 |
+
self.d_out = d_out
|
| 577 |
+
self.basis = BSplineBasis(grid_size)
|
| 578 |
+
self.coeffs = nn.Parameter(torch.randn(d_in, d_out, grid_size) * 0.1)
|
| 579 |
+
|
| 580 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 581 |
+
"""x: (B, d_in) -> (B, d_out)"""
|
| 582 |
+
# Normalize input to [-1, 1]
|
| 583 |
+
x_norm = torch.tanh(x)
|
| 584 |
+
basis_vals = self.basis(x_norm) # (B, d_in, grid_size)
|
| 585 |
+
# Efficient einsum: (B, d_in, grid) ร (d_in, d_out, grid) -> (B, d_out)
|
| 586 |
+
return torch.einsum('big,iog->bo', basis_vals, self.coeffs)
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
class ConceptReasoningEngine(nn.Module):
|
| 590 |
+
"""Graph-based concept reasoning with KAN composition rules."""
|
| 591 |
+
def __init__(self, config: ArtFlowConfig):
|
| 592 |
+
super().__init__()
|
| 593 |
+
# Concept extraction from text
|
| 594 |
+
self.concept_proj = nn.Linear(config.text_dim, config.concept_dim)
|
| 595 |
+
|
| 596 |
+
# Graph attention layers
|
| 597 |
+
self.graph_layers = nn.ModuleList([
|
| 598 |
+
nn.MultiheadAttention(config.concept_dim, num_heads=4, batch_first=True)
|
| 599 |
+
for _ in range(3)
|
| 600 |
+
])
|
| 601 |
+
self.graph_norms = nn.ModuleList([
|
| 602 |
+
RMSNorm(config.concept_dim) for _ in range(3)
|
| 603 |
+
])
|
| 604 |
+
|
| 605 |
+
# KAN composition layer
|
| 606 |
+
self.composition_kan = KANLayer(config.concept_dim, config.concept_dim, config.kan_grid_size)
|
| 607 |
+
|
| 608 |
+
# Layout generation
|
| 609 |
+
self.layout_mlp = nn.Sequential(
|
| 610 |
+
nn.Linear(config.concept_dim, config.concept_dim),
|
| 611 |
+
nn.SiLU(),
|
| 612 |
+
nn.Linear(config.concept_dim, config.latent_size * config.latent_size),
|
| 613 |
+
)
|
| 614 |
+
|
| 615 |
+
def forward(self, text_emb: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 616 |
+
"""
|
| 617 |
+
text_emb: (B, L, text_dim)
|
| 618 |
+
Returns:
|
| 619 |
+
concept_emb: (B, M, concept_dim)
|
| 620 |
+
spatial_bias: (B, 1, H, W) soft layout
|
| 621 |
+
"""
|
| 622 |
+
B = text_emb.shape[0]
|
| 623 |
+
|
| 624 |
+
# Extract concept nodes (take first M tokens as concepts)
|
| 625 |
+
concepts = self.concept_proj(text_emb[:, :16, :]) # (B, M, concept_dim)
|
| 626 |
+
|
| 627 |
+
# Graph attention
|
| 628 |
+
for layer, norm in zip(self.graph_layers, self.graph_norms):
|
| 629 |
+
residual = concepts
|
| 630 |
+
concepts = norm(concepts)
|
| 631 |
+
concepts, _ = layer(concepts, concepts, concepts)
|
| 632 |
+
concepts = residual + concepts
|
| 633 |
+
|
| 634 |
+
# KAN composition for spatial rules
|
| 635 |
+
concept_pooled = concepts.mean(dim=1) # (B, concept_dim)
|
| 636 |
+
composition = self.composition_kan(concept_pooled) # (B, concept_dim)
|
| 637 |
+
|
| 638 |
+
# Generate spatial layout
|
| 639 |
+
layout = self.layout_mlp(composition) # (B, H*W)
|
| 640 |
+
H = W = int(math.sqrt(layout.shape[-1]))
|
| 641 |
+
spatial_bias = layout.reshape(B, 1, H, W)
|
| 642 |
+
spatial_bias = torch.sigmoid(spatial_bias) # Soft mask [0, 1]
|
| 643 |
+
|
| 644 |
+
return concepts, spatial_bias
|
| 645 |
+
|
| 646 |
+
|
| 647 |
+
# ============================================================================
|
| 648 |
+
# Recursive Latent Reasoning (RLR) Module
|
| 649 |
+
# ============================================================================
|
| 650 |
+
|
| 651 |
+
class RecursiveLatentReasoner(nn.Module):
|
| 652 |
+
"""
|
| 653 |
+
Implements TRM/HRM-style recursive reasoning for image generation.
|
| 654 |
+
z_L: working memory (reasoning scratchpad)
|
| 655 |
+
z_H: current solution (directly supervised)
|
| 656 |
+
"""
|
| 657 |
+
def __init__(self, channels: int, config: ArtFlowConfig):
|
| 658 |
+
super().__init__()
|
| 659 |
+
self.R = config.reasoning_recursions
|
| 660 |
+
|
| 661 |
+
# Shared reasoning blocks (f_L and f_H share parameters, different inputs)
|
| 662 |
+
self.reason_block = nn.Sequential(
|
| 663 |
+
RMSNorm(channels),
|
| 664 |
+
nn.Linear(channels, channels * 2),
|
| 665 |
+
nn.SiLU(),
|
| 666 |
+
nn.Linear(channels * 2, channels),
|
| 667 |
+
)
|
| 668 |
+
|
| 669 |
+
# Input injection
|
| 670 |
+
self.inject_proj = nn.Linear(channels, channels)
|
| 671 |
+
|
| 672 |
+
# Gate for controlling update magnitude
|
| 673 |
+
self.gate = nn.Sequential(
|
| 674 |
+
nn.Linear(channels * 2, channels),
|
| 675 |
+
nn.Sigmoid(),
|
| 676 |
+
)
|
| 677 |
+
|
| 678 |
+
def forward(self, x: torch.Tensor, inject: torch.Tensor) -> torch.Tensor:
|
| 679 |
+
"""
|
| 680 |
+
x: (B, N, C) - current features
|
| 681 |
+
inject: (B, N, C) - input injection signal (from skip connections)
|
| 682 |
+
|
| 683 |
+
Returns: refined features after R recursions
|
| 684 |
+
"""
|
| 685 |
+
B, N, C = x.shape
|
| 686 |
+
z_H = x # Current solution
|
| 687 |
+
z_L = torch.zeros_like(x) # Working memory (starts empty)
|
| 688 |
+
|
| 689 |
+
for r in range(self.R):
|
| 690 |
+
# Update working memory: z_L = f(z_L + inject + z_H)
|
| 691 |
+
z_L_input = z_L + self.inject_proj(inject) + z_H
|
| 692 |
+
z_L_new = self.reason_block(z_L_input)
|
| 693 |
+
|
| 694 |
+
# Gated update
|
| 695 |
+
gate_val = self.gate(torch.cat([z_L, z_L_new], dim=-1))
|
| 696 |
+
z_L = z_L + gate_val * z_L_new
|
| 697 |
+
|
| 698 |
+
# Update solution: z_H = g(z_L + z_H)
|
| 699 |
+
z_H_input = z_L + z_H
|
| 700 |
+
z_H_new = self.reason_block(z_H_input)
|
| 701 |
+
|
| 702 |
+
gate_val = self.gate(torch.cat([z_H, z_H_new], dim=-1))
|
| 703 |
+
z_H = z_H + gate_val * z_H_new
|
| 704 |
+
|
| 705 |
+
return z_H
|
| 706 |
+
|
| 707 |
+
|
| 708 |
+
# ============================================================================
|
| 709 |
+
# UNet Stages
|
| 710 |
+
# ============================================================================
|
| 711 |
+
|
| 712 |
+
class DownBlock(nn.Module):
|
| 713 |
+
"""Downsampling block."""
|
| 714 |
+
def __init__(self, in_ch: int, out_ch: int):
|
| 715 |
+
super().__init__()
|
| 716 |
+
self.conv = nn.Conv2d(in_ch, out_ch, 3, stride=2, padding=1)
|
| 717 |
+
self.norm = nn.GroupNorm(32, out_ch)
|
| 718 |
+
|
| 719 |
+
def forward(self, x):
|
| 720 |
+
return self.norm(self.conv(x))
|
| 721 |
+
|
| 722 |
+
|
| 723 |
+
class UpBlock(nn.Module):
|
| 724 |
+
"""Upsampling block."""
|
| 725 |
+
def __init__(self, in_ch: int, out_ch: int, skip_ch: int):
|
| 726 |
+
super().__init__()
|
| 727 |
+
self.up = nn.Upsample(scale_factor=2, mode='nearest')
|
| 728 |
+
self.conv = nn.Conv2d(in_ch + skip_ch, out_ch, 3, padding=1)
|
| 729 |
+
self.norm = nn.GroupNorm(32, out_ch)
|
| 730 |
+
|
| 731 |
+
def forward(self, x, skip):
|
| 732 |
+
x = self.up(x)
|
| 733 |
+
x = torch.cat([x, skip], dim=1)
|
| 734 |
+
return self.norm(F.silu(self.conv(x)))
|
| 735 |
+
|
| 736 |
+
|
| 737 |
+
# ============================================================================
|
| 738 |
+
# Complete ArtFlow Model
|
| 739 |
+
# ============================================================================
|
| 740 |
+
|
| 741 |
+
class ArtFlow(nn.Module):
|
| 742 |
+
"""
|
| 743 |
+
ArtFlow: Complete image generation model.
|
| 744 |
+
Combines WaveMamba denoising, recursive reasoning, style control, and mood modulation.
|
| 745 |
+
"""
|
| 746 |
+
def __init__(self, config: ArtFlowConfig):
|
| 747 |
+
super().__init__()
|
| 748 |
+
self.config = config
|
| 749 |
+
|
| 750 |
+
# ---- Conditioning modules ----
|
| 751 |
+
self.art_style = ArtStyleMatrix(config)
|
| 752 |
+
self.mood_ctrl = MoodController(config)
|
| 753 |
+
self.concept_engine = ConceptReasoningEngine(config)
|
| 754 |
+
|
| 755 |
+
# ---- Timestep embedding ----
|
| 756 |
+
self.time_embed = nn.Sequential(
|
| 757 |
+
SinusoidalPositionEmbedding(config.style_dim),
|
| 758 |
+
nn.Linear(config.style_dim, config.style_dim * 4),
|
| 759 |
+
nn.SiLU(),
|
| 760 |
+
nn.Linear(config.style_dim * 4, config.style_dim),
|
| 761 |
+
)
|
| 762 |
+
|
| 763 |
+
# ---- Input projection ----
|
| 764 |
+
self.input_proj = nn.Conv2d(config.latent_channels, config.stage_channels[0], 3, padding=1)
|
| 765 |
+
|
| 766 |
+
# ---- Encoder ----
|
| 767 |
+
ch = config.stage_channels
|
| 768 |
+
|
| 769 |
+
# Stage 1 (32ร32): SepConv + CrossAttn
|
| 770 |
+
self.enc_stage1 = nn.ModuleList([
|
| 771 |
+
SepConvBlock(ch[0]) for _ in range(config.blocks_per_stage[0])
|
| 772 |
+
])
|
| 773 |
+
self.enc_ca1 = MultiQueryCrossAttention(ch[0], config.text_dim, config.num_heads, config.num_kv_heads)
|
| 774 |
+
self.down1 = DownBlock(ch[0], ch[1])
|
| 775 |
+
|
| 776 |
+
# Stage 2 (16ร16): WaveMamba + CrossAttn
|
| 777 |
+
self.enc_stage2 = nn.ModuleList([
|
| 778 |
+
WaveMambaBlock(ch[1], config) for _ in range(config.blocks_per_stage[1])
|
| 779 |
+
])
|
| 780 |
+
self.enc_ca2 = MultiQueryCrossAttention(ch[1], config.text_dim, config.num_heads, config.num_kv_heads)
|
| 781 |
+
self.down2 = DownBlock(ch[1], ch[2])
|
| 782 |
+
|
| 783 |
+
# Stage 3 (8ร8): WaveMamba + CrossAttn
|
| 784 |
+
self.enc_stage3 = nn.ModuleList([
|
| 785 |
+
WaveMambaBlock(ch[2], config) for _ in range(config.blocks_per_stage[2])
|
| 786 |
+
])
|
| 787 |
+
self.enc_ca3 = MultiQueryCrossAttention(ch[2], config.text_dim, config.num_heads, config.num_kv_heads)
|
| 788 |
+
|
| 789 |
+
# ---- Bottleneck (8ร8) ----
|
| 790 |
+
self.bottleneck = nn.ModuleList([
|
| 791 |
+
WaveMambaBlock(ch[2], config) for _ in range(config.bottleneck_blocks)
|
| 792 |
+
])
|
| 793 |
+
self.bottleneck_ca = MultiQueryCrossAttention(ch[2], config.text_dim, config.num_heads, config.num_kv_heads)
|
| 794 |
+
self.reasoner = RecursiveLatentReasoner(ch[2], config)
|
| 795 |
+
|
| 796 |
+
# ---- Decoder ----
|
| 797 |
+
self.up2 = UpBlock(ch[2], ch[1], ch[1]) # 8โ16, skip from enc_stage2
|
| 798 |
+
self.dec_stage2 = nn.ModuleList([
|
| 799 |
+
WaveMambaBlock(ch[1], config) for _ in range(config.blocks_per_stage[1])
|
| 800 |
+
])
|
| 801 |
+
self.dec_ca2 = MultiQueryCrossAttention(ch[1], config.text_dim, config.num_heads, config.num_kv_heads)
|
| 802 |
+
|
| 803 |
+
self.up1 = UpBlock(ch[1], ch[0], ch[0]) # 16โ32, skip from enc_stage1
|
| 804 |
+
self.dec_stage1 = nn.ModuleList([
|
| 805 |
+
SepConvBlock(ch[0]) for _ in range(config.blocks_per_stage[0])
|
| 806 |
+
])
|
| 807 |
+
self.dec_ca1 = MultiQueryCrossAttention(ch[0], config.text_dim, config.num_heads, config.num_kv_heads)
|
| 808 |
+
|
| 809 |
+
# ---- Output ----
|
| 810 |
+
self.output_norm = nn.GroupNorm(32, ch[0])
|
| 811 |
+
self.output_proj = nn.Conv2d(ch[0], config.latent_channels, 3, padding=1)
|
| 812 |
+
nn.init.zeros_(self.output_proj.weight)
|
| 813 |
+
nn.init.zeros_(self.output_proj.bias)
|
| 814 |
+
|
| 815 |
+
def forward(self,
|
| 816 |
+
z_t: torch.Tensor, # (B, C, H, W) noisy latent
|
| 817 |
+
t: torch.Tensor, # (B,) timesteps
|
| 818 |
+
text_emb: torch.Tensor, # (B, L, text_dim)
|
| 819 |
+
style_ids: Optional[torch.Tensor] = None,
|
| 820 |
+
mood_ids: Optional[torch.Tensor] = None,
|
| 821 |
+
style_vec: Optional[torch.Tensor] = None,
|
| 822 |
+
mood_vec: Optional[torch.Tensor] = None,
|
| 823 |
+
) -> torch.Tensor:
|
| 824 |
+
"""Forward pass: predict velocity v for flow matching."""
|
| 825 |
+
B = z_t.shape[0]
|
| 826 |
+
|
| 827 |
+
# ---- Get conditioning signals ----
|
| 828 |
+
t_emb = self.time_embed(t) # (B, d)
|
| 829 |
+
style_mod = self.art_style(style_ids=style_ids, custom_style=style_vec) # (B, d)
|
| 830 |
+
mood_mod = self.mood_ctrl(mood_ids=mood_ids, mood_vector=mood_vec) # (B, d)
|
| 831 |
+
|
| 832 |
+
# Combined condition for AdaLN
|
| 833 |
+
cond = t_emb + style_mod + mood_mod # (B, d)
|
| 834 |
+
|
| 835 |
+
# Concept reasoning
|
| 836 |
+
concepts, spatial_bias = self.concept_engine(text_emb)
|
| 837 |
+
|
| 838 |
+
# Combine cond with text info for AdaLN
|
| 839 |
+
cond_for_adaln = torch.cat([cond, text_emb.mean(dim=1)], dim=-1) # (B, d + text_dim)
|
| 840 |
+
|
| 841 |
+
# ---- Input ----
|
| 842 |
+
x = self.input_proj(z_t) # (B, ch[0], 32, 32)
|
| 843 |
+
|
| 844 |
+
# Apply spatial bias from concept reasoning
|
| 845 |
+
x = x * (1 + spatial_bias)
|
| 846 |
+
|
| 847 |
+
# ---- Encoder Stage 1 (32ร32, SepConv) ----
|
| 848 |
+
for block in self.enc_stage1:
|
| 849 |
+
x = block(x)
|
| 850 |
+
x_flat = x.flatten(2).transpose(1, 2) # (B, H*W, C)
|
| 851 |
+
x_flat = self.enc_ca1(x_flat, text_emb)
|
| 852 |
+
x = x_flat.transpose(1, 2).reshape(B, -1, x.shape[2], x.shape[3])
|
| 853 |
+
skip1 = x
|
| 854 |
+
|
| 855 |
+
# ---- Downsample 1 ----
|
| 856 |
+
x = self.down1(x) # (B, ch[1], 16, 16)
|
| 857 |
+
|
| 858 |
+
# ---- Encoder Stage 2 (16ร16, WaveMamba) ----
|
| 859 |
+
for block in self.enc_stage2:
|
| 860 |
+
x = block(x, cond_for_adaln, style_mod)
|
| 861 |
+
x_flat = x.flatten(2).transpose(1, 2)
|
| 862 |
+
x_flat = self.enc_ca2(x_flat, text_emb)
|
| 863 |
+
x = x_flat.transpose(1, 2).reshape(B, -1, x.shape[2], x.shape[3])
|
| 864 |
+
skip2 = x
|
| 865 |
+
|
| 866 |
+
# ---- Downsample 2 ----
|
| 867 |
+
x = self.down2(x) # (B, ch[2], 8, 8)
|
| 868 |
+
|
| 869 |
+
# ---- Encoder Stage 3 (8ร8, WaveMamba) ----
|
| 870 |
+
for block in self.enc_stage3:
|
| 871 |
+
x = block(x, cond_for_adaln, style_mod)
|
| 872 |
+
x_flat = x.flatten(2).transpose(1, 2)
|
| 873 |
+
x_flat = self.enc_ca3(x_flat, text_emb)
|
| 874 |
+
x = x_flat.transpose(1, 2).reshape(B, -1, x.shape[2], x.shape[3])
|
| 875 |
+
|
| 876 |
+
# ---- Bottleneck (8ร8) ----
|
| 877 |
+
for block in self.bottleneck:
|
| 878 |
+
x = block(x, cond_for_adaln, style_mod)
|
| 879 |
+
|
| 880 |
+
# Cross attention in bottleneck
|
| 881 |
+
x_flat = x.flatten(2).transpose(1, 2)
|
| 882 |
+
x_flat = self.bottleneck_ca(x_flat, text_emb)
|
| 883 |
+
|
| 884 |
+
# Recursive Latent Reasoning!
|
| 885 |
+
inject = x_flat # Input injection for reasoning
|
| 886 |
+
x_flat = self.reasoner(x_flat, inject)
|
| 887 |
+
|
| 888 |
+
x = x_flat.transpose(1, 2).reshape(B, -1, x.shape[2], x.shape[3])
|
| 889 |
+
|
| 890 |
+
# ---- Decoder ----
|
| 891 |
+
x = self.up2(x, skip2) # (B, ch[1], 16, 16)
|
| 892 |
+
for block in self.dec_stage2:
|
| 893 |
+
x = block(x, cond_for_adaln, style_mod)
|
| 894 |
+
x_flat = x.flatten(2).transpose(1, 2)
|
| 895 |
+
x_flat = self.dec_ca2(x_flat, text_emb)
|
| 896 |
+
x = x_flat.transpose(1, 2).reshape(B, -1, x.shape[2], x.shape[3])
|
| 897 |
+
|
| 898 |
+
x = self.up1(x, skip1) # (B, ch[0], 32, 32)
|
| 899 |
+
for block in self.dec_stage1:
|
| 900 |
+
x = block(x)
|
| 901 |
+
x_flat = x.flatten(2).transpose(1, 2)
|
| 902 |
+
x_flat = self.dec_ca1(x_flat, text_emb)
|
| 903 |
+
x = x_flat.transpose(1, 2).reshape(B, -1, x.shape[2], x.shape[3])
|
| 904 |
+
|
| 905 |
+
# ---- Output ----
|
| 906 |
+
x = self.output_norm(x)
|
| 907 |
+
x = F.silu(x)
|
| 908 |
+
v_pred = self.output_proj(x) # (B, latent_channels, H, W)
|
| 909 |
+
|
| 910 |
+
return v_pred
|
| 911 |
+
|
| 912 |
+
|
| 913 |
+
# ============================================================================
|
| 914 |
+
# Flow Matching Training Utilities
|
| 915 |
+
# ============================================================================
|
| 916 |
+
|
| 917 |
+
class ArtAwareFlowMatchingLoss(nn.Module):
|
| 918 |
+
"""
|
| 919 |
+
Flow matching loss with art-aware frequency weighting.
|
| 920 |
+
Weighs line work (high-frequency) more than composition (low-frequency).
|
| 921 |
+
"""
|
| 922 |
+
def __init__(self, w_LL=1.0, w_LH=2.0, w_HL=2.0, w_HH=1.5):
|
| 923 |
+
super().__init__()
|
| 924 |
+
self.wavelet = HaarWavelet2D()
|
| 925 |
+
self.weights = {'LL': w_LL, 'LH': w_LH, 'HL': w_HL, 'HH': w_HH}
|
| 926 |
+
|
| 927 |
+
def forward(self, v_pred: torch.Tensor, v_target: torch.Tensor) -> torch.Tensor:
|
| 928 |
+
"""
|
| 929 |
+
Frequency-weighted MSE loss.
|
| 930 |
+
v_pred, v_target: (B, C, H, W)
|
| 931 |
+
"""
|
| 932 |
+
error = v_pred - v_target
|
| 933 |
+
|
| 934 |
+
# Check if dimensions are even (needed for wavelet)
|
| 935 |
+
if error.shape[2] % 2 == 0 and error.shape[3] % 2 == 0:
|
| 936 |
+
LL, LH, HL, HH = self.wavelet(error)
|
| 937 |
+
loss = (
|
| 938 |
+
self.weights['LL'] * LL.pow(2).mean() +
|
| 939 |
+
self.weights['LH'] * LH.pow(2).mean() +
|
| 940 |
+
self.weights['HL'] * HL.pow(2).mean() +
|
| 941 |
+
self.weights['HH'] * HH.pow(2).mean()
|
| 942 |
+
)
|
| 943 |
+
else:
|
| 944 |
+
# Fallback to standard MSE
|
| 945 |
+
loss = error.pow(2).mean()
|
| 946 |
+
|
| 947 |
+
return loss
|
| 948 |
+
|
| 949 |
+
|
| 950 |
+
def logit_normal_timestep(batch_size: int, device: torch.device,
|
| 951 |
+
mu: float = 0.0, sigma: float = 1.0) -> torch.Tensor:
|
| 952 |
+
"""Sample timesteps from logit-normal distribution (from FLUX/SD3)."""
|
| 953 |
+
u = torch.randn(batch_size, device=device)
|
| 954 |
+
t = torch.sigmoid(mu + sigma * u)
|
| 955 |
+
return t
|
| 956 |
+
|
| 957 |
+
|
| 958 |
+
# ============================================================================
|
| 959 |
+
# Complete Training Step
|
| 960 |
+
# ============================================================================
|
| 961 |
+
|
| 962 |
+
def training_step(model: ArtFlow, x_0: torch.Tensor, text_emb: torch.Tensor,
|
| 963 |
+
loss_fn: ArtAwareFlowMatchingLoss,
|
| 964 |
+
style_ids=None, mood_ids=None) -> torch.Tensor:
|
| 965 |
+
"""
|
| 966 |
+
Single training step for flow matching.
|
| 967 |
+
x_0: (B, C, H, W) clean latent
|
| 968 |
+
text_emb: (B, L, D) text embeddings
|
| 969 |
+
"""
|
| 970 |
+
B = x_0.shape[0]
|
| 971 |
+
device = x_0.device
|
| 972 |
+
|
| 973 |
+
# Sample timestep (logit-normal)
|
| 974 |
+
t = logit_normal_timestep(B, device)
|
| 975 |
+
|
| 976 |
+
# Sample noise
|
| 977 |
+
eps = torch.randn_like(x_0)
|
| 978 |
+
|
| 979 |
+
# Create noisy sample: x_t = (1-t)*x_0 + t*eps
|
| 980 |
+
t_expand = t[:, None, None, None]
|
| 981 |
+
x_t = (1 - t_expand) * x_0 + t_expand * eps
|
| 982 |
+
|
| 983 |
+
# Target velocity: v = eps - x_0
|
| 984 |
+
v_target = eps - x_0
|
| 985 |
+
|
| 986 |
+
# Predict velocity
|
| 987 |
+
v_pred = model(x_t, t, text_emb, style_ids=style_ids, mood_ids=mood_ids)
|
| 988 |
+
|
| 989 |
+
# Art-aware loss
|
| 990 |
+
loss = loss_fn(v_pred, v_target)
|
| 991 |
+
|
| 992 |
+
return loss
|
| 993 |
+
|
| 994 |
+
|
| 995 |
+
# ============================================================================
|
| 996 |
+
# Validation & Testing
|
| 997 |
+
# ============================================================================
|
| 998 |
+
|
| 999 |
+
def validate_architecture():
|
| 1000 |
+
"""Validate the complete architecture: shapes, parameters, memory."""
|
| 1001 |
+
print("=" * 70)
|
| 1002 |
+
print("ArtFlow Architecture Validation")
|
| 1003 |
+
print("=" * 70)
|
| 1004 |
+
|
| 1005 |
+
config = ArtFlowConfig()
|
| 1006 |
+
model = ArtFlow(config)
|
| 1007 |
+
|
| 1008 |
+
# Count parameters
|
| 1009 |
+
total_params = sum(p.numel() for p in model.parameters())
|
| 1010 |
+
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 1011 |
+
|
| 1012 |
+
print(f"\n๐ Parameter Count:")
|
| 1013 |
+
print(f" Total: {total_params:,} ({total_params/1e6:.1f}M)")
|
| 1014 |
+
print(f" Trainable: {trainable_params:,} ({trainable_params/1e6:.1f}M)")
|
| 1015 |
+
|
| 1016 |
+
# Per-module breakdown
|
| 1017 |
+
modules = {
|
| 1018 |
+
'ArtStyle Matrix': model.art_style,
|
| 1019 |
+
'Mood Controller': model.mood_ctrl,
|
| 1020 |
+
'Concept Engine': model.concept_engine,
|
| 1021 |
+
'Time Embedding': model.time_embed,
|
| 1022 |
+
'Encoder Stage 1': nn.ModuleList([model.enc_stage1, model.enc_ca1]),
|
| 1023 |
+
'Encoder Stage 2': nn.ModuleList([model.enc_stage2, model.enc_ca2]),
|
| 1024 |
+
'Encoder Stage 3': nn.ModuleList([model.enc_stage3, model.enc_ca3]),
|
| 1025 |
+
'Bottleneck': nn.ModuleList([model.bottleneck, model.bottleneck_ca, model.reasoner]),
|
| 1026 |
+
'Decoder Stage 2': nn.ModuleList([model.dec_stage2, model.dec_ca2, model.up2]),
|
| 1027 |
+
'Decoder Stage 1': nn.ModuleList([model.dec_stage1, model.dec_ca1, model.up1]),
|
| 1028 |
+
}
|
| 1029 |
+
|
| 1030 |
+
print(f"\n๐ฆ Per-Module Breakdown:")
|
| 1031 |
+
for name, module in modules.items():
|
| 1032 |
+
params = sum(p.numel() for p in module.parameters())
|
| 1033 |
+
print(f" {name:25s}: {params:>10,} ({params/1e6:.2f}M)")
|
| 1034 |
+
|
| 1035 |
+
# Memory estimation
|
| 1036 |
+
fp16_bytes = total_params * 2
|
| 1037 |
+
fp32_bytes = total_params * 4
|
| 1038 |
+
print(f"\n๐พ Model Memory:")
|
| 1039 |
+
print(f" FP16: {fp16_bytes/1e6:.1f} MB")
|
| 1040 |
+
print(f" FP32: {fp32_bytes/1e6:.1f} MB")
|
| 1041 |
+
print(f" INT8: {total_params/1e6:.1f} MB")
|
| 1042 |
+
|
| 1043 |
+
# Forward pass validation
|
| 1044 |
+
print(f"\n๐ Forward Pass Validation:")
|
| 1045 |
+
B = 2
|
| 1046 |
+
z_t = torch.randn(B, config.latent_channels, config.latent_size, config.latent_size)
|
| 1047 |
+
t = torch.rand(B)
|
| 1048 |
+
text_emb = torch.randn(B, config.text_length, config.text_dim)
|
| 1049 |
+
style_ids = torch.randint(0, config.num_styles, (B,))
|
| 1050 |
+
mood_ids = torch.randint(0, config.num_moods, (B,))
|
| 1051 |
+
|
| 1052 |
+
print(f" Input z_t shape: {z_t.shape}")
|
| 1053 |
+
print(f" Timestep shape: {t.shape}")
|
| 1054 |
+
print(f" Text emb shape: {text_emb.shape}")
|
| 1055 |
+
|
| 1056 |
+
with torch.no_grad():
|
| 1057 |
+
v_pred = model(z_t, t, text_emb, style_ids=style_ids, mood_ids=mood_ids)
|
| 1058 |
+
|
| 1059 |
+
print(f" Output v_pred shape: {v_pred.shape}")
|
| 1060 |
+
assert v_pred.shape == z_t.shape, f"Shape mismatch! {v_pred.shape} vs {z_t.shape}"
|
| 1061 |
+
print(f" โ
Shape check PASSED")
|
| 1062 |
+
|
| 1063 |
+
# Backward pass validation
|
| 1064 |
+
print(f"\n๐ Backward Pass Validation:")
|
| 1065 |
+
loss_fn = ArtAwareFlowMatchingLoss()
|
| 1066 |
+
loss = training_step(model, z_t, text_emb, loss_fn, style_ids, mood_ids)
|
| 1067 |
+
print(f" Loss value: {loss.item():.4f}")
|
| 1068 |
+
loss.backward()
|
| 1069 |
+
|
| 1070 |
+
# Check gradients exist
|
| 1071 |
+
grad_count = sum(1 for p in model.parameters() if p.grad is not None)
|
| 1072 |
+
total_count = sum(1 for p in model.parameters())
|
| 1073 |
+
print(f" Gradients computed: {grad_count}/{total_count}")
|
| 1074 |
+
print(f" โ
Backward pass PASSED")
|
| 1075 |
+
|
| 1076 |
+
# Check for NaN/Inf
|
| 1077 |
+
has_nan = any(torch.isnan(p.grad).any() for p in model.parameters() if p.grad is not None)
|
| 1078 |
+
has_inf = any(torch.isinf(p.grad).any() for p in model.parameters() if p.grad is not None)
|
| 1079 |
+
print(f" NaN in gradients: {'โ YES' if has_nan else 'โ
No'}")
|
| 1080 |
+
print(f" Inf in gradients: {'โ YES' if has_inf else 'โ
No'}")
|
| 1081 |
+
|
| 1082 |
+
# Activation memory estimation (inference)
|
| 1083 |
+
print(f"\n๐ฑ Mobile Inference Memory Estimate:")
|
| 1084 |
+
# Peak activations during forward pass
|
| 1085 |
+
activation_sizes = [
|
| 1086 |
+
(B, 256, 32, 32), # Stage 1
|
| 1087 |
+
(B, 512, 16, 16), # Stage 2
|
| 1088 |
+
(B, 768, 8, 8), # Stage 3 + bottleneck
|
| 1089 |
+
]
|
| 1090 |
+
total_activation_bytes = sum(
|
| 1091 |
+
math.prod(s) * 2 for s in activation_sizes # fp16
|
| 1092 |
+
) * 3 # Rough multiplier for intermediate activations
|
| 1093 |
+
|
| 1094 |
+
total_inference_mb = (fp16_bytes + total_activation_bytes) / 1e6
|
| 1095 |
+
print(f" Model weights (FP16): {fp16_bytes/1e6:.1f} MB")
|
| 1096 |
+
print(f" Activation memory (est): {total_activation_bytes/1e6:.1f} MB")
|
| 1097 |
+
print(f" Total inference (est): {total_inference_mb:.1f} MB")
|
| 1098 |
+
|
| 1099 |
+
target_ok = total_inference_mb < 2000
|
| 1100 |
+
print(f" Under 2GB for mobile: {'โ
YES' if target_ok else 'โ NO'}")
|
| 1101 |
+
|
| 1102 |
+
# Wavelet correctness check
|
| 1103 |
+
print(f"\n๐ Wavelet Transform Validation:")
|
| 1104 |
+
wavelet = HaarWavelet2D()
|
| 1105 |
+
test_img = torch.randn(1, 3, 8, 8)
|
| 1106 |
+
LL, LH, HL, HH = wavelet(test_img)
|
| 1107 |
+
reconstructed = wavelet.inverse(LL, LH, HL, HH)
|
| 1108 |
+
recon_error = (test_img - reconstructed).abs().max().item()
|
| 1109 |
+
print(f" Reconstruction error: {recon_error:.2e}")
|
| 1110 |
+
print(f" Perfect reconstruction: {'โ
YES' if recon_error < 1e-5 else 'โ NO'}")
|
| 1111 |
+
|
| 1112 |
+
# Zigzag scan validation
|
| 1113 |
+
print(f"\n๐ Zigzag Scan Validation:")
|
| 1114 |
+
test_feat = torch.randn(1, 3, 4, 4)
|
| 1115 |
+
flat = zigzag_flatten(test_feat)
|
| 1116 |
+
unflat = zigzag_unflatten(flat, 4, 4)
|
| 1117 |
+
scan_error = (test_feat - unflat).abs().max().item()
|
| 1118 |
+
print(f" Round-trip error: {scan_error:.2e}")
|
| 1119 |
+
print(f" Perfect round-trip: {'โ
YES' if scan_error < 1e-5 else 'โ NO'}")
|
| 1120 |
+
|
| 1121 |
+
# Flow matching loss validation
|
| 1122 |
+
print(f"\n๐ Loss Function Validation:")
|
| 1123 |
+
v1 = torch.randn(2, 32, 32, 32)
|
| 1124 |
+
v2 = torch.randn(2, 32, 32, 32)
|
| 1125 |
+
standard_loss = F.mse_loss(v1, v2)
|
| 1126 |
+
art_loss = loss_fn(v1, v2)
|
| 1127 |
+
print(f" Standard MSE: {standard_loss.item():.4f}")
|
| 1128 |
+
print(f" Art-Aware loss: {art_loss.item():.4f}")
|
| 1129 |
+
print(f" Art-Aware > Standard (expected due to frequency weighting): {'โ
' if art_loss > standard_loss else 'โ ๏ธ'}")
|
| 1130 |
+
|
| 1131 |
+
# KAN layer validation
|
| 1132 |
+
print(f"\n๐งฎ KAN Layer Validation:")
|
| 1133 |
+
kan = KANLayer(64, 32, grid_size=5)
|
| 1134 |
+
test_input = torch.randn(4, 64)
|
| 1135 |
+
kan_output = kan(test_input)
|
| 1136 |
+
print(f" Input: {test_input.shape} โ Output: {kan_output.shape}")
|
| 1137 |
+
kan_params = sum(p.numel() for p in kan.parameters())
|
| 1138 |
+
mlp_equiv_params = 64 * 32 + 32 # Linear equivalent
|
| 1139 |
+
print(f" KAN params: {kan_params} vs MLP equiv: {mlp_equiv_params}")
|
| 1140 |
+
|
| 1141 |
+
print(f"\n{'='*70}")
|
| 1142 |
+
print(f"๐ ALL VALIDATIONS PASSED!")
|
| 1143 |
+
print(f"{'='*70}")
|
| 1144 |
+
|
| 1145 |
+
return model
|
| 1146 |
+
|
| 1147 |
+
|
| 1148 |
+
if __name__ == "__main__":
|
| 1149 |
+
model = validate_architecture()
|