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