Spaces:
Running on Zero
Running on Zero
multimodalart HF Staff
fwDiT demo: REPA-256 + SiT-512 checkpoints, authors' sampler, presets/knobs, examples
dd43bb2 verified Download fwdit/backbones/__init__.py from hugging-apps/fwdit-class-conditional-imagegen: direct link, hf CLI and curl.
- Browser
- Download file 3.01 kB
-
https://huggingface.co/spaces/hugging-apps/fwdit-class-conditional-imagegen/resolve/main/fwdit/backbones/__init__.py
- Command line
-
hf download hf://spaces/hugging-apps/fwdit-class-conditional-imagegen/fwdit/backbones/__init__.py
-
curl -L -o __init__.py https://huggingface.co/spaces/hugging-apps/fwdit-class-conditional-imagegen/resolve/main/fwdit/backbones/__init__.py
3.01 kB
| """The three pre-trained backbones and the few facts fwDiT needs about each. | |
| dit.py, sit.py and repa.py are the upstream model definitions, verbatim. All three expose the same pieces: | |
| x_embedder + pos_embed (E), t_embedder + y_embedder (the conditioning c), blocks, final_layer + unpatchify (the head). | |
| What differs is collected in `Backbone`: | |
| out_channels channels of the head's output that the solver reads (DiT: noise and learned variance; SiT: velocity | |
| [the learned-sigma half is dropped]; REPA: velocity) | |
| solver the sampler the backbone was trained / evaluated with (fwdit.sampling) | |
| progress(t) the solver's time t mapped to the paper's convention (0 = noise, 1 = data), for the F-CFG interval | |
| """ | |
| import math | |
| import numpy as np | |
| import torch | |
| import torch.nn as nn | |
| def ddpm_alphas_cumprod(T=1000): | |
| """DiT's linear beta schedule (betas 1e-4 .. 0.02 over T = 1000 steps), cumulative product of 1 - beta (float64).""" | |
| betas = np.linspace(1000 / T * 0.0001, 1000 / T * 0.02, T, dtype=np.float64) | |
| return np.cumprod(1.0 - betas) | |
| class Backbone: | |
| def __init__(self, name, image_size, num_classes, learn_sigma): | |
| family, arch = name.split("-", 1) if name.startswith("REPA-") else (name.split("-")[0], name) | |
| self.family, size = family, image_size // 8 # SD-VAE latents: 8x downsampling, 4 channels | |
| if family == "DiT": | |
| from .dit import DiT_models | |
| self.net = DiT_models[arch](input_size=size, num_classes=num_classes) | |
| self.solver, self.out_channels = "ddpm", 2 * self.net.in_channels | |
| abar = torch.tensor(ddpm_alphas_cumprod()) | |
| self._t_of_step = abar.sqrt() / (abar.sqrt() + (1 - abar).sqrt()) # flow-matching t with the same SNR | |
| elif family == "SiT": | |
| from .sit import SiT_models | |
| self.net = SiT_models[arch](input_size=size, num_classes=num_classes, learn_sigma=learn_sigma) | |
| self.solver, self.out_channels = "sde-sit", self.net.in_channels | |
| elif family == "REPA": | |
| from .repa import SiT_models | |
| # REPA's alignment projectors (z_dims) are a training-time head; without them the network is unchanged | |
| self.net = SiT_models[arch](input_size=size, num_classes=num_classes, use_cfg=True, z_dims=[], | |
| fused_attn=False, qk_norm=False) | |
| self.solver, self.out_channels = "sde-repa", self.net.in_channels | |
| else: | |
| raise ValueError(f"unknown backbone {name!r}: DiT-*, SiT-* or REPA-SiT-*") | |
| self.null_label = num_classes | |
| def progress(self, t): | |
| """The paper's time (0 = noise, 1 = data). DiT: the flow-matching t with the same SNR as DDPM step t | |
| (SNR = abar / (1 - abar) = (t / (1 - t))^2, so t >= 0.5 <=> abar >= 0.5); SiT: t; REPA: 1 - t.""" | |
| if self.family == "DiT": | |
| return self._t_of_step.to(t.device)[t.long()] | |
| return t.double() if self.family == "SiT" else 1 - t.double() | |