Spaces:
Sleeping
Sleeping
File size: 9,671 Bytes
c388140 | 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 | from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Dict, Optional, Tuple
import torch
from torch import nn
def _extract(a: torch.Tensor, t: torch.Tensor, x_shape: Tuple[int, ...]) -> torch.Tensor:
"""
从 [T] 的一维张量 a 中按 batch 的 t 抽取,并 reshape 到 x 的维度用于广播。
"""
b = t.shape[0]
out = a.gather(0, t)
return out.reshape(b, *((1,) * (len(x_shape) - 1)))
@dataclass(frozen=True)
class DiffusionConfig:
"""
扩散配置。
"""
timesteps: int = 1000
beta_schedule: str = "cosine" # "linear" 或 "cosine"
class GaussianDiffusion:
"""
标准 Gaussian Diffusion:
- 训练:epsilon-prediction,loss 为 MSE
- 采样:支持 DDPM 与(可选)DDIM
训练循环/保存等不在这里做,以保证训练与采样逻辑解耦。
"""
def __init__(self, betas: torch.Tensor):
if betas.ndim != 1:
raise ValueError("betas 必须是一维张量")
self.device = betas.device
self.betas = betas.float()
self.num_timesteps = int(self.betas.shape[0])
alphas = 1.0 - self.betas
alphas_cumprod = torch.cumprod(alphas, dim=0)
alphas_cumprod_prev = torch.cat([torch.tensor([1.0], device=betas.device), alphas_cumprod[:-1]], dim=0)
self.alphas = alphas
self.alphas_cumprod = alphas_cumprod
self.alphas_cumprod_prev = alphas_cumprod_prev
self.sqrt_alphas_cumprod = torch.sqrt(alphas_cumprod)
self.sqrt_one_minus_alphas_cumprod = torch.sqrt(1.0 - alphas_cumprod)
self.sqrt_recip_alphas_cumprod = torch.sqrt(1.0 / alphas_cumprod)
self.sqrt_recipm1_alphas_cumprod = torch.sqrt(1.0 / alphas_cumprod - 1.0)
# q(x_{t-1}|x_t,x_0) 的 posterior
self.posterior_variance = (
self.betas * (1.0 - alphas_cumprod_prev) / (1.0 - alphas_cumprod)
).clamp(min=1e-20)
self.posterior_log_variance_clipped = torch.log(self.posterior_variance)
self.posterior_mean_coef1 = self.betas * torch.sqrt(alphas_cumprod_prev) / (1.0 - alphas_cumprod)
self.posterior_mean_coef2 = (1.0 - alphas_cumprod_prev) * torch.sqrt(alphas) / (1.0 - alphas_cumprod)
def q_sample(self, x_start: torch.Tensor, t: torch.Tensor, noise: Optional[torch.Tensor] = None) -> torch.Tensor:
"""
前向加噪:q(x_t | x_0)
"""
if noise is None:
noise = torch.randn_like(x_start)
return _extract(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start + _extract(
self.sqrt_one_minus_alphas_cumprod, t, x_start.shape
) * noise
def predict_x0_from_eps(self, x_t: torch.Tensor, t: torch.Tensor, eps: torch.Tensor) -> torch.Tensor:
return _extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t - _extract(
self.sqrt_recipm1_alphas_cumprod, t, x_t.shape
) * eps
def q_posterior(self, x_start: torch.Tensor, x_t: torch.Tensor, t: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""
返回 q(x_{t-1}|x_t,x_0) 的 posterior mean 与 variance。
"""
posterior_mean = _extract(self.posterior_mean_coef1, t, x_t.shape) * x_start + _extract(
self.posterior_mean_coef2, t, x_t.shape
) * x_t
posterior_variance = _extract(self.posterior_variance, t, x_t.shape)
return posterior_mean, posterior_variance
def training_losses(
self,
model: nn.Module,
x_start: torch.Tensor,
t: torch.Tensor,
model_kwargs: Optional[Dict[str, Any]] = None,
cond_drop_prob: float = 0.0,
snr_gamma: float = 0.0,
) -> Dict[str, torch.Tensor]:
"""
训练损失:MSE(eps_pred, eps)。
可选:SNR 加权(snr_gamma>0),通常能提升采样质量/稳定性。
"""
model_kwargs = dict(model_kwargs or {})
noise = torch.randn_like(x_start)
x_t = self.q_sample(x_start, t, noise=noise)
# Classifier-Free Guidance 的训练方式:以一定概率丢弃条件(mask 全 0 => 等价无条件)
if cond_drop_prob and cond_drop_prob > 0 and ("mask" in model_kwargs):
mask = model_kwargs.get("mask", None)
if mask is not None:
# mask: (B,L);drop: (B,1) 广播到 (B,L)
drop = torch.rand((x_start.shape[0], 1), device=x_start.device) < float(cond_drop_prob)
if mask.dtype == torch.bool:
model_kwargs["mask"] = mask & (~drop)
else:
drop_f = drop.to(dtype=mask.dtype)
model_kwargs["mask"] = mask * (1 - drop_f)
eps_pred = model(x_t, t, **model_kwargs)
mse = (eps_pred - noise) ** 2 # (B,C,H,W)
loss_per = mse.mean(dim=(1, 2, 3)) # (B,)
if snr_gamma and snr_gamma > 0:
# snr = alpha_bar / (1-alpha_bar)
alpha_bar = _extract(self.alphas_cumprod, t, x_start.shape).reshape(x_start.shape[0])
snr = alpha_bar / (1.0 - alpha_bar).clamp(min=1e-12)
# 经典做法:w = min(snr, gamma) / snr
w = torch.minimum(snr, torch.full_like(snr, float(snr_gamma))) / snr.clamp(min=1e-12)
loss_per = loss_per * w
loss = loss_per.mean()
return {"loss": loss}
@torch.no_grad()
def p_mean_variance(
self,
model: nn.Module,
x_t: torch.Tensor,
t: torch.Tensor,
model_kwargs: Optional[Dict[str, Any]] = None,
) -> Dict[str, torch.Tensor]:
"""
DDPM 反向一步:根据 eps_pred 推出 x0_pred,再得到 posterior。
"""
model_kwargs = dict(model_kwargs or {})
eps_pred = model(x_t, t, **model_kwargs)
x0_pred = self.predict_x0_from_eps(x_t, t, eps_pred).clamp(-1.0, 1.0)
model_mean, model_var = self.q_posterior(x0_pred, x_t, t)
return {"mean": model_mean, "variance": model_var, "pred_x0": x0_pred, "pred_eps": eps_pred}
@torch.no_grad()
def p_sample(
self,
model: nn.Module,
x_t: torch.Tensor,
t: torch.Tensor,
model_kwargs: Optional[Dict[str, Any]] = None,
) -> torch.Tensor:
"""
采样一步:x_t -> x_{t-1}(DDPM)。
"""
out = self.p_mean_variance(model, x_t, t, model_kwargs=model_kwargs)
mean, var = out["mean"], out["variance"]
noise = torch.randn_like(x_t)
# t==0 时不再加噪声
nonzero_mask = (t != 0).float().reshape(x_t.shape[0], *((1,) * (x_t.ndim - 1)))
return mean + nonzero_mask * torch.sqrt(var) * noise
@torch.no_grad()
def p_sample_loop(
self,
model: nn.Module,
shape: Tuple[int, int, int, int],
device: torch.device,
model_kwargs: Optional[Dict[str, Any]] = None,
progress: bool = True,
) -> torch.Tensor:
"""
从纯噪声开始逐步采样到 x_0(DDPM)。
"""
img = torch.randn(shape, device=device)
timesteps = range(self.num_timesteps - 1, -1, -1)
if progress:
try:
from tqdm import tqdm # 延迟导入,避免强依赖
except Exception:
tqdm = None
if tqdm is not None:
timesteps = tqdm(timesteps, desc="DDPM Sampling", total=self.num_timesteps)
for i in timesteps:
t = torch.full((shape[0],), i, device=device, dtype=torch.long)
img = self.p_sample(model, img, t, model_kwargs=model_kwargs)
return img
@torch.no_grad()
def ddim_sample_loop(
self,
model: nn.Module,
shape: Tuple[int, int, int, int],
device: torch.device,
steps: int = 50,
eta: float = 0.0,
model_kwargs: Optional[Dict[str, Any]] = None,
progress: bool = True,
) -> torch.Tensor:
"""
DDIM 采样(更少步数、更快)。eta=0 为确定性采样。
"""
if steps <= 1:
raise ValueError("steps 必须 > 1")
img = torch.randn(shape, device=device)
# 选择等间隔的时间序列(从 T-1 到 0)
# 注意:这里用 Python int 列表,避免在 CUDA Tensor 上做 int() 转换导致报错。
times = torch.linspace(self.num_timesteps - 1, 0, steps, dtype=torch.float32).long().tolist()
if progress:
try:
from tqdm import tqdm
except Exception:
tqdm = None
if tqdm is not None:
times = tqdm(list(times), desc="DDIM Sampling", total=steps)
for idx, t_i in enumerate(times):
t = torch.full((shape[0],), int(t_i), device=device, dtype=torch.long)
out = self.p_mean_variance(model, img, t, model_kwargs=model_kwargs)
eps = out["pred_eps"]
x0 = out["pred_x0"]
if idx == len(times) - 1:
img = x0
break
t_next = torch.full((shape[0],), int(times[idx + 1]), device=device, dtype=torch.long)
a_t = _extract(self.alphas_cumprod, t, img.shape)
a_next = _extract(self.alphas_cumprod, t_next, img.shape)
# DDIM 更新公式
sigma = (
eta
* torch.sqrt((1.0 - a_next) / (1.0 - a_t))
* torch.sqrt(1.0 - a_t / a_next)
)
noise = torch.randn_like(img)
dir_xt = torch.sqrt(1.0 - a_next - sigma**2) * eps
img = torch.sqrt(a_next) * x0 + dir_xt + sigma * noise
return img
|