from __future__ import annotations import math from dataclasses import dataclass from typing import Iterable, List, Sequence, Tuple import torch from torch import nn from torch.nn import functional as F def _num_groups(channels: int, max_groups: int = 32) -> int: """ GroupNorm 组数必须能整除 channels;这里自动选择合适组数。 """ for g in range(min(max_groups, channels), 0, -1): if channels % g == 0: return g return 1 class SinusoidalTimeEmbedding(nn.Module): """ 将离散 timestep 映射到连续 embedding(sin/cos)。 """ def __init__(self, dim: int): super().__init__() self.dim = dim def forward(self, t: torch.Tensor) -> torch.Tensor: half = self.dim // 2 # 频率:exp(-log(10000) * i/(half-1)) freqs = torch.exp( -math.log(10000.0) * torch.arange(0, half, device=t.device, dtype=torch.float32) / max(half - 1, 1) ) args = t.float().unsqueeze(1) * freqs.unsqueeze(0) emb = torch.cat([torch.sin(args), torch.cos(args)], dim=1) if self.dim % 2 == 1: emb = F.pad(emb, (0, 1)) return emb class ResBlock(nn.Module): """ ResNet block(带 time embedding 注入),避免对 16×16 不友好的过深/过度下采样。 """ def __init__(self, in_ch: int, out_ch: int, time_emb_dim: int, dropout: float = 0.0): super().__init__() self.in_ch = in_ch self.out_ch = out_ch self.norm1 = nn.GroupNorm(_num_groups(in_ch), in_ch) self.conv1 = nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1) self.time_proj = nn.Sequential( nn.SiLU(), nn.Linear(time_emb_dim, out_ch), ) self.norm2 = nn.GroupNorm(_num_groups(out_ch), out_ch) self.dropout = nn.Dropout(dropout) if dropout > 0 else nn.Identity() self.conv2 = nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1) self.skip = nn.Identity() if in_ch == out_ch else nn.Conv2d(in_ch, out_ch, kernel_size=1) def forward(self, x: torch.Tensor, t_emb: torch.Tensor) -> torch.Tensor: h = self.conv1(F.silu(self.norm1(x))) # time embedding 以 bias 的形式注入到 feature map h = h + self.time_proj(t_emb).unsqueeze(-1).unsqueeze(-1) h = self.conv2(self.dropout(F.silu(self.norm2(h)))) return h + self.skip(x) class AttentionBlock(nn.Module): """ 轻量自注意力(用于 8×8 / 4×4 等小分辨率特征图)。 """ def __init__(self, channels: int, num_heads: int = 4): super().__init__() if channels % num_heads != 0: # 保守处理:无法整除就退化为 1 head,避免运行时报错 num_heads = 1 self.channels = channels self.num_heads = num_heads self.head_dim = channels // num_heads self.norm = nn.GroupNorm(_num_groups(channels), channels) self.qkv = nn.Conv2d(channels, channels * 3, kernel_size=1) self.proj = nn.Conv2d(channels, channels, kernel_size=1) def forward(self, x: torch.Tensor) -> torch.Tensor: b, c, h, w = x.shape n = h * w qkv = self.qkv(self.norm(x)) q, k, v = qkv.chunk(3, dim=1) # (b, heads, head_dim, n) q = q.reshape(b, self.num_heads, self.head_dim, n) k = k.reshape(b, self.num_heads, self.head_dim, n) v = v.reshape(b, self.num_heads, self.head_dim, n) # 注意力:softmax(q^T k / sqrt(d)) scale = 1.0 / math.sqrt(self.head_dim) attn = torch.einsum("bhdn,bhdm->bhnm", q * scale, k) # (b, heads, n, n) attn = torch.softmax(attn, dim=-1) out = torch.einsum("bhnm,bhdm->bhdn", attn, v) # (b, heads, head_dim, n) out = out.reshape(b, c, h, w) out = self.proj(out) return x + out class Downsample(nn.Module): """ stride=2 卷积下采样(对小分辨率更友好、可学习)。 """ def __init__(self, channels: int): super().__init__() self.conv = nn.Conv2d(channels, channels, kernel_size=3, stride=2, padding=1) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.conv(x) class Upsample(nn.Module): """ 最近邻上采样 + 卷积。 """ def __init__(self, channels: int): super().__init__() self.conv = nn.Conv2d(channels, channels, kernel_size=3, padding=1) def forward(self, x: torch.Tensor) -> torch.Tensor: x = F.interpolate(x, scale_factor=2.0, mode="nearest") return self.conv(x) @dataclass(frozen=True) class UNet16Config: """ 16×16 专用 UNet 配置。 """ in_channels: int = 3 out_channels: int = 3 # 预测 eps,通道数与输入一致 base_channels: int = 64 channel_mults: Tuple[int, ...] = (1, 2, 2) # 16->8->4,避免过度下采样 num_res_blocks: int = 2 attn_resolutions: Tuple[int, ...] = (8, 4) # 小分辨率加注意力更划算 dropout: float = 0.0 time_emb_dim: int = 256 # 文本条件向量维度(与 time_emb_dim 对齐最简单) cond_dim: int = 256 class UNet16(nn.Module): """ 小型 UNet(epsilon-predictor),适配 16×16 输入。 """ def __init__(self, cfg: UNet16Config): super().__init__() self.cfg = cfg self.time_embed = nn.Sequential( SinusoidalTimeEmbedding(cfg.time_emb_dim), nn.Linear(cfg.time_emb_dim, cfg.time_emb_dim * 4), nn.SiLU(), nn.Linear(cfg.time_emb_dim * 4, cfg.time_emb_dim), ) self.cond_proj = nn.Identity() if cfg.cond_dim == cfg.time_emb_dim else nn.Linear(cfg.cond_dim, cfg.time_emb_dim) self.in_conv = nn.Conv2d(cfg.in_channels, cfg.base_channels, kernel_size=3, padding=1) # Down path(按 level 显式组织,避免小分辨率下采样/上采样位置出错) ch = cfg.base_channels self.down_resblocks = nn.ModuleList() self.down_attn = nn.ModuleList() self.downsamples = nn.ModuleList() skip_chs: List[int] = [] resolution = 16 # 纹理固定为 16×16 for level, mult in enumerate(cfg.channel_mults): out_ch = cfg.base_channels * mult level_res = nn.ModuleList() level_attn = nn.ModuleList() for _ in range(cfg.num_res_blocks): level_res.append(ResBlock(ch, out_ch, cfg.time_emb_dim, dropout=cfg.dropout)) ch = out_ch level_attn.append(AttentionBlock(ch) if resolution in cfg.attn_resolutions else nn.Identity()) skip_chs.append(ch) self.down_resblocks.append(level_res) self.down_attn.append(level_attn) if level != len(cfg.channel_mults) - 1: self.downsamples.append(Downsample(ch)) resolution //= 2 # Mid self.mid_block1 = ResBlock(ch, ch, cfg.time_emb_dim, dropout=cfg.dropout) self.mid_attn = AttentionBlock(ch) self.mid_block2 = ResBlock(ch, ch, cfg.time_emb_dim, dropout=cfg.dropout) # Up path(从最深层开始逐级上采样) self.up_resblocks = nn.ModuleList() self.up_attn = nn.ModuleList() self.upsamples = nn.ModuleList() # 当前 resolution 对应最深层(例如 4×4) # down 部分循环结束后,resolution 已被更新到最深层 for level, mult in reversed(list(enumerate(cfg.channel_mults))): out_ch = cfg.base_channels * mult level_res = nn.ModuleList() level_attn = nn.ModuleList() for _ in range(cfg.num_res_blocks): skip_ch = skip_chs.pop() level_res.append(ResBlock(ch + skip_ch, out_ch, cfg.time_emb_dim, dropout=cfg.dropout)) ch = out_ch level_attn.append(AttentionBlock(ch) if resolution in cfg.attn_resolutions else nn.Identity()) self.up_resblocks.append(level_res) self.up_attn.append(level_attn) if level != 0: self.upsamples.append(Upsample(ch)) resolution *= 2 self.out_norm = nn.GroupNorm(_num_groups(ch), ch) self.out_conv = nn.Conv2d(ch, cfg.out_channels, kernel_size=3, padding=1) def forward(self, x: torch.Tensor, t: torch.Tensor, cond: torch.Tensor | None = None) -> torch.Tensor: """ 输入: - x: (B, C, 16, 16) 的噪声图 - t: (B,) 的 timestep(int64) - cond: (B, cond_dim) 的条件向量(可选;为空则无条件) 输出: - eps_pred: (B, C, 16, 16) """ t_emb = self.time_embed(t) if cond is not None: t_emb = t_emb + self.cond_proj(cond) h = self.in_conv(x) skips: List[torch.Tensor] = [] for level in range(len(self.cfg.channel_mults)): for i in range(self.cfg.num_res_blocks): h = self.down_resblocks[level][i](h, t_emb) h = self.down_attn[level][i](h) skips.append(h) if level < len(self.downsamples): h = self.downsamples[level](h) h = self.mid_block1(h, t_emb) h = self.mid_attn(h) h = self.mid_block2(h, t_emb) # up_resblocks/up_attn 的顺序是从最深层到最浅层 for level in range(len(self.cfg.channel_mults)): for i in range(self.cfg.num_res_blocks): skip = skips.pop() h = torch.cat([h, skip], dim=1) h = self.up_resblocks[level][i](h, t_emb) h = self.up_attn[level][i](h) if level < len(self.upsamples): h = self.upsamples[level](h) h = self.out_conv(F.silu(self.out_norm(h))) return h