azula.sample¶
Reverse diffusion samplers.
For a distribution \(p(X)\) over \(\mathbb{R}^D\), the perturbation kernel (see
azula.noise)
defines a series of marginal distributions
The goal of diffusion models is to generate samples from \(p(X_0)\). To this end, reverse transition kernels \(q(X_s \mid X_t)\) from \(t\) to \(s < t\) are chosen. Then, starting from \(x_1 \sim p(X_1)\), \(T\) transitions \(x_{t_{i-1}} \sim q(X_{t_{i-1}} \mid x_{t_i})\) are simulated from \(t_T = 1\) to \(t_0 = 0\). If the kernels are consistent with the marginals \(p(X_t)\), that is if
for all \(i = 1, \dots, T\), the vectors \(x_{t_i}\) are distributed according to \(p(X_{t_i})\), including the last one \(x_{t_0} = x_0\).
Classes¶
Abstract reverse diffusion sampler. |
|
Creates an DDPM sampler. |
|
Creates a DDIM sampler. |
|
Creates a deterministic Euler (1st order) sampler. |
|
Creates a deterministic Heun (2nd order) sampler. |
|
Creates a linear multi-step (LMS) sampler. |
|
Creates a predictor-corrector (PC) sampler. |
Descriptions¶
- class azula.sample.Sampler(steps)¶
Abstract reverse diffusion sampler.
- Parameters:
steps (int) – The number of discretization steps \(T\). By default, the step size \(t - s\) is constant.
- init(shape, mean=None, var=None, **kwargs)¶
Draws an initial noisy vector \(x_1\).
\[x_1 \sim \mathcal{N}(\alpha_1 \mathbb{E}[X], \alpha_1^2 \mathbb{V}[X] + \sigma_1^2 I)\]- Parameters:
mean (Tensor | None) – The mean \(\mathbb{E}[X]\) of \(p(X)\), with shape \(()\) or \((*, D)\). If
None, use 0 instead.var (Tensor | None) – The variance \(\mathbb{V}[X]\) of \(p(X)\), with shape \(()\) or \((*, D)\). If
None, use 1 instead.kwargs – Keyword arguments passed to
torch.randn.
- Returns:
A noisy vector \(x_1\), with shape \((*, D)\).
- Return type:
- forward(x_1, **kwargs)¶
Simulates the reverse process from \(t = 1\) to \(0\).
- step(x_t, t, s, **kwargs)¶
Simulates the reverse process from \(t\) to \(s \leq t\).
- Parameters:
- Returns:
The new vector \(x_s \sim q(X_s \mid x_t)\), with shape \((*, D)\).
- Return type:
- class azula.sample.DDPMSampler(denoiser, **kwargs)¶
Creates an DDPM sampler.
\[x_s = \alpha_s \mu_\phi(x_t) + \sigma_s \, \sqrt{1 - \tau} \, \frac{x_t - \alpha_t \mu_\phi(x_t)}{\sigma_t} + \sigma_s \, \sqrt{\tau} \, \epsilon\]where \(\epsilon \sim \mathcal{N}(0, I)\) and
\[\tau = 1 - \frac{\alpha_t^2}{\alpha_s^2} \frac{\sigma_s^2}{\sigma_t^2} \, .\]References
Denoising Diffusion Probabilistic Models (Ho et al., 2020)- Parameters:
denoiser (GaussianDenoiser) – A Gaussian denoiser.
kwargs – Keyword arguments passed to
Sampler.
- class azula.sample.DDIMSampler(denoiser, eta=0.0, **kwargs)¶
Creates a DDIM sampler.
\[x_s = \alpha_s \mu_\phi(x_t) + \sigma_s \, \sqrt{1 - \eta \, \tau} \, \frac{x_t - \alpha_t \mu_\phi(x_t)}{\sigma_t} + \sigma_s \, \sqrt{\eta \, \tau} \, \epsilon\]where \(\epsilon \sim \mathcal{N}(0, I)\) and
\[\tau = 1 - \frac{\alpha_t^2}{\alpha_s^2} \frac{\sigma_s^2}{\sigma_t^2} \, .\]References
Denoising Diffusion Implicit Models (Song et al., 2021)- Parameters:
denoiser (GaussianDenoiser) – A Gaussian denoiser.
eta (float) – The stochasticity hyperparameter \(\eta \in \mathbb{R}_+\). If \(\eta = 1\),
DDIMSampleris equivalent toDDPMSampler. If \(\eta = 0\),DDIMSampleris equivalent toEulerSampler.kwargs – Keyword arguments passed to
Sampler.
- class azula.sample.EulerSampler(denoiser, **kwargs)¶
Creates a deterministic Euler (1st order) sampler.
The integration is carried with respect to the noise-to-signal ratio \(\frac{\sigma_t}{\alpha_t}\) rather than the time \(t\).
Wikipedia
https://wikipedia.org/wiki/Euler_method
- Parameters:
denoiser (GaussianDenoiser) – A Gaussian denoiser.
kwargs – Keyword arguments passed to
Sampler.
- class azula.sample.HeunSampler(denoiser, **kwargs)¶
Creates a deterministic Heun (2nd order) sampler.
The integration is carried with respect to the noise-to-signal ratio \(\frac{\sigma_t}{\alpha_t}\) rather than the time \(t\).
Wikipedia
https://wikipedia.org/wiki/Heun%27s_method
- Parameters:
denoiser (GaussianDenoiser) – A Gaussian denoiser.
kwargs – Keyword arguments passed to
Sampler.
- class azula.sample.LMSSampler(denoiser, order=3, **kwargs)¶
Creates a linear multi-step (LMS) sampler.
References
k-diffusion (Katherine Crowson)- Parameters:
denoiser (GaussianDenoiser) – A Gaussian denoiser.
order (int) – The order of the multi-step method.
kwargs – Keyword arguments passed to
Sampler.
- static adams_bashforth(t, i, order=3)¶
Returns the coefficients of the \(N\)-th order Adams-Bashforth method.
Wikipedia
- class azula.sample.PCSampler(denoiser, corrections=1, delta=0.01, **kwargs)¶
Creates a predictor-corrector (PC) sampler.
References
Score-Based Generative Modeling through Stochastic Differential Equations (Song et al., 2021)- Parameters:
denoiser (GaussianDenoiser) – A Gaussian denoiser.
corrections (int) – The number of Langevin corrections per step.
delta (float) – The amplitude of Langevin corrections.
kwargs – Keyword arguments passed to
Sampler.