Spaces:
Sleeping
Sleeping
Download blockdiffusion/diffusion/gaussian_diffusion.py from wkea/blockdiffusion-api: direct link, hf CLI and curl.
- Browser
- Download file 9.67 kB
-
https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/b8d20780a64fd01197c47f0ec9fc2a3fc23499bf/blockdiffusion/diffusion/gaussian_diffusion.py
- Command line
-
hf download hf://spaces/wkea/blockdiffusion-api@b8d20780a64fd01197c47f0ec9fc2a3fc23499bf/blockdiffusion/diffusion/gaussian_diffusion.py
-
curl -L -o gaussian_diffusion.py https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/b8d20780a64fd01197c47f0ec9fc2a3fc23499bf/blockdiffusion/diffusion/gaussian_diffusion.py
9.67 kB
| 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))) | |
| 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} | |
| 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} | |
| 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 | |
| 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 | |
| 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 | |