Spaces:
Sleeping
Sleeping
File size: 1,137 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 | from __future__ import annotations
from typing import Any, Dict, Optional
import torch
from torch import nn
class CFGuidanceModel(nn.Module):
"""
Classifier-Free Guidance 采样包装器:
eps = eps_uncond + scale * (eps_cond - eps_uncond)
说明:
- base_model 必须支持 forward(x, t, **model_kwargs)
- cond_kwargs/uncond_kwargs 用于传 tokens/mask 等条件信息
"""
def __init__(
self,
base_model: nn.Module,
cond_kwargs: Optional[Dict[str, Any]] = None,
uncond_kwargs: Optional[Dict[str, Any]] = None,
scale: float = 3.0,
):
super().__init__()
self.base_model = base_model
self.cond_kwargs = dict(cond_kwargs or {})
self.uncond_kwargs = dict(uncond_kwargs or {})
self.scale = float(scale)
def forward(self, x: torch.Tensor, t: torch.Tensor, **_: Any) -> torch.Tensor:
eps_uncond = self.base_model(x, t, **self.uncond_kwargs)
eps_cond = self.base_model(x, t, **self.cond_kwargs)
return eps_uncond + self.scale * (eps_cond - eps_uncond)
|