Diffusion model
Diffusion models are a revolutionary class of AI that learn to generate incredibly realistic data, from stunning images to natural language. They achieve this by mimicking a fundamental process in nature: diffusion, where order gradually dissolves into chaos. But the true magic lies in reversing this journey, allowing them to sculpt intricate creations from pure randomness. Diffusion models learn to generate complex data by simulating the process of adding and then intelligently removing noise. Originally rooted in principles from non-equilibrium thermodynamics, these models transform random noise into structured information through a series of carefully learned denoising steps. They power many of today's most advanced AI applications, including popular text-to-image generators like DALL-E and Stable Diffusion.
AI Summary
Diffusion models are a revolutionary class of AI that learn to generate incredibly realistic data, from stunning images to natural language. They achieve this by mimicking a fundamental process in nature: diffusion, where order gradually dissolves into chaos. But the true magic lies in reversing this journey, allowing them to sculpt intricate creations from pure randomness.
- Diffusion models learn to generate complex data by simulating the process of adding and then intelligently removing noise.
- Originally rooted in principles from non-equilibrium thermodynamics, these models transform random noise into structured information through a series of carefully learned denoising steps.
- They power many of today's most advanced AI applications, including popular text-to-image generators like DALL-E and Stable Diffusion.
What are Diffusion Models?
At their core, diffusion models are a special kind of generative AI. Unlike models that try to build data from scratch, diffusion models learn to understand and reverse a 'diffusion process'—imagine a drop of ink slowly spreading out in water. Their goal is to generate new data, like images or text, that look remarkably similar to the original dataset they were trained on.
The process involves two main components: a forward diffusion, which progressively adds noise to data until it's pure static, and a reverse sampling process, which meticulously removes that noise to reveal new, coherent data. It's like learning how to perfectly scramble an image, then using that knowledge to unscramble new, random noise into a completely original picture.
Under the Hood: The Mechanics
These sophisticated models are often built upon neural networks, commonly U-nets or transformers, which act as the 'backbone' that learns to denoise. They are typically trained using advanced statistical techniques like variational inference, allowing them to grasp complex data distributions without direct supervision.
Different formalisms exist, including Denoising Diffusion Probabilistic Models (DDPMs) and Noise Conditional Score Networks (NCSNs), but they all share the fundamental idea of iteratively transforming noise into meaningful data. Think of them as different mathematical languages describing the same powerful generative process.
Shaping the Future of AI
As of 2024, diffusion models are primarily transforming the field of computer vision. They excel at tasks like restoring damaged images through inpainting, enhancing resolution, and perhaps most famously, generating stunning images from simple text prompts. They're taught to reverse the blurring effect of Gaussian noise, step by painstaking step.
The widespread commercial interest in these models is evident in AI powerhouses like Stable Diffusion and DALL-E. These groundbreaking tools often combine diffusion models with other neural network components, such as text encoders, to enable you to describe an image you want to see, and watch it materialize from nothing but random pixels.
But their influence isn't limited to visuals. Diffusion models are also making strides in natural language processing for text generation and summarization, in creating realistic soundscapes, and even in reinforcement learning, proving their versatility across diverse AI challenges.
The Scientific Roots: Non-Equilibrium Thermodynamics
The initial concept for diffusion models emerged in 2015, drawing inspiration from non-equilibrium thermodynamics. This field studies systems that are out of balance, observing how they naturally evolve towards a state of equilibrium. Diffusion, the gradual spreading of particles, is a key phenomenon in this area.
Imagine a dataset of all natural photographs as a dense 'cloud' of points in a vast image space. If you repeatedly add noise to these images, this cloud will eventually 'diffuse' and spread out, becoming an indistinguishable, uniform Gaussian distribution—pure static. The brilliance of diffusion models is in learning to reverse this scattering, starting from pure static and systematically 'compressing' it back into that original, meaningful cloud of natural images.
This process involves biased random steps. While randomness is crucial to explore the vast data space, a 'drift' component—like a gentle gradient descent—guides the process towards meaningful data. Without this careful balance, the generated data would either collapse to a single point or remain entirely random, never forming coherent images.
The Forward Diffusion Process
In a Denoising Diffusion Probabilistic Model (DDPM), the forward process begins with an original data point, let's call it $x0$, and gradually adds Gaussian noise over many small steps. Each step introduces a tiny amount of noise, transforming $x{t-1}$ into $xt$. Mathematically, this transformation can be described as:
Here, $zt$ is a random sample from a standard Gaussian distribution, and $\betat$ controls the amount of noise added at each step. This process ensures that after enough steps (say, $T$ steps), $xT$ becomes almost entirely pure Gaussian noise, completely obliterating any trace of the original $x0$.
x_t = \sqrt{1-\beta_t} x_{t-1} + \sqrt{\beta_t} z_tA remarkable property of this setup is that you don't actually need to go through all the intermediate steps to get a noisy version of $x0$. You can directly sample any $xt$ conditioned on $x0$ using a single equation, making the forward process surprisingly efficient for training purposes:
Here, $\bar{\alpha}t$ is a product of $(1-\betai)$ terms, which effectively measures how much of the original signal remains after $t$ steps. As $t$ increases, $\bar{\alpha}t$ approaches zero, meaning the influence of $x0$ diminishes, and $xt$ becomes dominated by noise.
q(x_t|x_0) = \mathcal{N}(x_t; \sqrt{\bar{\alpha}_t} x_0, (1-\bar{\alpha}_t) I)The Reverse Sampling Process: Bringing Order from Chaos
The real goal is the reverse process: starting from pure noise, $xT$, and incrementally denoise it back to a clear $x0$. This is where the neural network comes in. It learns to predict the subtle changes needed at each step to go from $xt$ to $x{t-1}$.
Specifically, the network learns to predict the noise component that was added during the forward step, often denoted as $\epsilon$. Since we know the forward process equations, if we can accurately estimate the noise $\epsilon$ at each step, we can effectively invert the process to get a slightly cleaner image.
From this, we can see that if the neural network, $\epsilon\theta(xt, t)$, predicts $\epsilon$, we can then estimate the original, denoised image $x0$. This estimation is critical for guiding the reverse steps towards coherent data.
x_t = \sqrt{\bar{\alpha}_t} x_0 + \sqrt{1-\bar{\alpha}_t} \epsilonThe Denoising Network and Loss Function
The neural network, often a U-Net, is trained to predict this noise $\epsilon$ at various noise levels (time steps $t$). The most common and effective training objective is surprisingly simple: minimize the difference between the actual noise added and the noise predicted by the network. This is often called the 'simple' loss function.
This equation states that the network tries to make its predicted noise, $\epsilon\theta(xt, t)$, as close as possible to the true noise, $\epsilon$, that was used to transform $x0$ into $xt$. If the network becomes good at this, it can then generate new images by accurately reversing the noise process.
L_{simple} = \mathbb{E}_{t, x_0, \epsilon} \left[\|\epsilon - \epsilon_\theta(x_t, t)\|^2\right]Score-Based Generative Models: An Equivalent View
Another powerful formalism for diffusion models is the 'score-based generative model,' also known as Noise Conditional Score Networks (NCSNs). Instead of predicting noise, these models learn the 'score function' of the data distribution—essentially, the gradient of the log-probability of an image.
Imagine you have an image, and you want to know which direction to nudge its pixels to make it 'more likely' to be a real image. That's what the score function tells you. It's like a compass pointing towards higher probability density in the vast space of all possible images, helping navigate the generated content towards realistic outcomes.
This score function, $s(x)$, is incredibly useful because it describes the relative likelihood of an image compared to its neighbors, without needing to know the absolute probability, which is often intractable. This allows for a more robust approach to learning complex data distributions.
s(x) = \nabla_x \ln q(x)
These models leverage the Langevin equation from physics, which describes the motion of particles in a potential field. By using the negative log-probability of the data as a 'potential energy' landscape, particles (our data points) can be simulated to eventually settle into a distribution that matches the real data.
The Grand Unification: Equivalence
Remarkably, DDPMs and score-based generative models are mathematically equivalent. A network trained to predict noise can effectively be used as a score network, and vice versa. This equivalence demonstrates a deep underlying connection in how these models learn to generate data.
This means that regardless of the exact mathematical framework, the core idea remains: systematically perturb data with noise and then learn to reverse that perturbation. The choice of formalism often comes down to computational efficiency or specific architectural preferences.
Optimizing the Journey: Noise Schedules
A 'noise schedule' dictates how much noise is added at each step of the forward diffusion and, consequently, how it's removed in the reverse. This is a crucial design choice. A linear schedule might add noise uniformly, while other schedules might vary the noise intensity over time, impacting the model's training and generation quality.
These schedules are often defined by parameters $\betat$ that control the variance of the Gaussian noise added at each step. By carefully engineering these schedules, researchers can significantly improve the quality and speed of generated images. Instead of just depending on the time step $t$, modern models often directly depend on the standard deviation of the noise, $\sigmat$, offering more flexible control.
Faster Generation: DDIMs
One challenge with the original DDPM approach is its speed. Since the forward diffusion can take hundreds or even thousands of steps to reach pure noise, the reverse generation process also requires the same number of steps. This can make sampling new images quite slow.
The Denoising Diffusion Implicit Model (DDIM) was developed to address this. Unlike DDPM, DDIM's reverse process is deterministic, meaning it doesn't involve adding extra random noise at each step. This allows for 'skipping' steps during sampling, drastically speeding up image generation without sacrificing much quality, making these models practical for real-world applications.
Article
Diffusion model
In machine learning, diffusion models, also known as diffusion-based generative models or score-based generative models, are a class of latent variable generative models. A diffusion model consists of two major components: the forward diffusion process, and the reverse sampling process. The goal of diffusion models is to learn a diffusion process for a given dataset, such that the process can generate new elements that are distributed similarly as the original dataset. A diffusion model models data as generated by a diffusion process, whereby a new datum performs a random walk with drift through the space of all possible data. A trained diffusion model can be sampled in many ways, with different efficiency and quality.
There are various equivalent formalisms, including Markov chains, denoising diffusion probabilistic models, noise conditioned score networks, and stochastic differential equations. They are typically trained using variational inference. The model responsible for denoising is typically called its "backbone". The backbone may be of any kind, but they are typically U-nets or transformers.
As of 2024, diffusion models are mainly used for computer vision tasks, including image denoising, inpainting, super-resolution, image generation, and video generation. These typically involve training a neural network to sequentially denoise images blurred with Gaussian noise. The model is trained to reverse the process of adding noise to an image. After training to convergence, it can be used for image generation by starting with an image composed of random noise, and applying the network iteratively to denoise the image.
Diffusion-based image generators have seen widespread commercial interest, such as Stable Diffusion and DALL-E. These models typically combine diffusion models with other models, such as text-encoders and cross-attention modules to allow text-conditioned generation.
Other than computer vision, diffusion models have also found applications in natural language processing such as text generation and summarization, sound generation, and reinforcement learning.
Denoising diffusion model
Non-equilibrium thermodynamics
Diffusion models were introduced in 2015 as a method to train a model that can sample from a highly complex probability distribution. They used techniques from non-equilibrium thermodynamics, especially diffusion.
Consider, for example, how one might model the distribution of all naturally occurring photos. Each image is a point in the space of all images, and the distribution of naturally occurring photos is a "cloud" in space, which, by repeatedly adding noise to the images, diffuses out to the rest of the image space, until the cloud becomes all but indistinguishable from a Gaussian distribution ${\displaystyle {\mathcal {N}}(0,I)}$. A model that can approximately undo the diffusion can then be used to sample from the original distribution. This is studied in "non-equilibrium" thermodynamics, as the starting distribution is not in equilibrium, unlike the final distribution.
The equilibrium distribution is the Gaussian distribution ${\displaystyle {\mathcal {N}}(0,I)}$, with pdf ${\displaystyle \rho (x)\propto e^{-{\frac {1}{2}}\|x\|^{2}}}$. This is just the Maxwell–Boltzmann distribution of particles in a potential well ${\displaystyle V(x)={\frac {1}{2}}\|x\|^{2}}$ at temperature 1. The initial distribution, being very much out of equilibrium, would diffuse towards the equilibrium distribution, making biased random steps that are a sum of pure randomness (like a Brownian walker) and gradient descent down the potential well. The randomness is necessary: if the particles were to undergo only gradient descent, then they will all fall to the origin, collapsing the distribution.
Denoising Diffusion Probabilistic Model (DDPM)
The 2020 paper proposed the Denoising Diffusion Probabilistic Model (DDPM), which improves upon the previous method by variational inference.
Forward diffusion
• ${\displaystyle \beta {1},...,\beta {T}\in (0,1)}$ are fixed constants. • ${\displaystyle \alpha {t}:=1-\beta {t}}$ • ${\displaystyle {\bar {\alpha }}{t}:=\alpha {1}\cdots \alpha {t}}$ • ${\displaystyle \sigma {t}:={\sqrt {1-{\bar {\alpha }}{t}}}}$ • ${\displaystyle {\tilde {\sigma }}{t}:={\frac {\sigma {t-1}}{\sigma {t}}}{\sqrt {\beta {t}}}}$ • ${\displaystyle {\tilde {\mu }}{t}(x{t},x{0}):={\frac {{\sqrt {\alpha {t}}}(1-{\bar {\alpha }}{t-1})x{t}+{\sqrt {{\bar {\alpha }}{t-1}}}(1-\alpha {t})x{0}}{\sigma {t}^{2}}}}$ • ${\displaystyle {\mathcal {N}}(\mu ,\Sigma )}$ is the normal distribution with mean ${\displaystyle \mu }$ and variance ${\displaystyle \Sigma }$, and ${\displaystyle {\mathcal {N}}(x|\mu ,\Sigma )}$ is the probability density at ${\displaystyle x}$. • A vertical bar denotes conditioning.
A forward diffusion process starts at some starting point ${\displaystyle x{0}\sim q}$, where ${\displaystyle q}$ is the probability distribution to be learned, then repeatedly adds noise to it by${\displaystyle x{t}={\sqrt {1-\beta {t}}}x{t-1}+{\sqrt {\beta {t}}}z{t}}$where ${\displaystyle z{1},...,z{T}}$ are IID (Independent and identically distributed random variables) samples from ${\displaystyle {\mathcal {N}}(0,I)}$. The coefficients ${\displaystyle {\sqrt {1-\beta {t}}}}$ and ${\displaystyle {\sqrt {\beta {t}}}}$ ensure that ${\displaystyle {\mbox{Var}}(X{t})=I}$ assuming that ${\displaystyle {\mbox{Var}}(X{0})=I}$. The values of ${\displaystyle \beta {t}}$ are chosen such that for any starting distribution of ${\displaystyle x{0}}$, if it has finite second moment, then ${\displaystyle \lim {t\to \infty }x{t}|x{0}}$ converges to ${\displaystyle {\mathcal {N}}(0,I)}$.
The entire diffusion process then satisfies${\displaystyle q(x{0:T})=q(x{0})q(x{1}|x{0})\cdots q(x{T}|x{T-1})=q(x{0}){\mathcal {N}}(x{1}|{\sqrt {\alpha {1}}}x{0},\beta {1}I)\cdots {\mathcal {N}}(x{T}|{\sqrt {\alpha {T}}}x{T-1},\beta {T}I)}$or${\displaystyle \ln q(x{0:T})=\ln q(x{0})-\sum {t=1}^{T}{\frac {1}{2\beta {t}}}\|x{t}-{\sqrt {1-\beta {t}}}x{t-1}\|^{2}+C}$where ${\displaystyle C}$ is a normalization constant and often omitted. In particular, we note that ${\displaystyle x{1:T}|x{0}}$ is a Gaussian process, which affords us considerable freedom in reparameterization. For example, by standard manipulation with Gaussian process, ${\displaystyle x{t}|x{0}\sim N\left({\sqrt {{\bar {\alpha }}{t}}}x{0},\sigma {t}^{2}I\right)}$${\displaystyle x{t-1}|x{t},x{0}\sim {\mathcal {N}}({\tilde {\mu }}{t}(x{t},x{0}),{\tilde {\sigma }}{t}^{2}I)}$In particular, notice that for large ${\displaystyle t}$, the variable ${\displaystyle x{t}|x{0}\sim N\left({\sqrt {{\bar {\alpha }}{t}}}x{0},\sigma {t}^{2}I\right)}$ converges to ${\displaystyle {\mathcal {N}}(0,I)}$. That is, after a long enough diffusion process, we end up with some ${\displaystyle x{T}}$ that is very close to ${\displaystyle {\mathcal {N}}(0,I)}$, with all traces of the original ${\displaystyle x{0}\sim q}$ gone.
For example, since${\displaystyle x{t}|x{0}\sim N\left({\sqrt {{\bar {\alpha }}{t}}}x{0},\sigma {t}^{2}I\right)}$we can sample ${\displaystyle x{t}|x{0}}$ directly "in one step", instead of going through all the intermediate steps ${\displaystyle x{1},x{2},...,x{t-1}}$.
Backward diffusion
The key idea of DDPM is to use a neural network parametrized by ${\displaystyle \theta }$. The network takes in two arguments ${\displaystyle x{t},t}$, and outputs a vector ${\displaystyle \mu {\theta }(x{t},t)}$ and a matrix ${\displaystyle \Sigma {\theta }(x{t},t)}$, such that each step in the forward diffusion process can be approximately undone by ${\displaystyle x{t-1}\sim {\mathcal {N}}(\mu {\theta }(x{t},t),\Sigma {\theta }(x{t},t))}$. This then gives us a backward diffusion process ${\displaystyle p{\theta }}$ defined by${\displaystyle p{\theta }(x{T})={\mathcal {N}}(x{T}|0,I)}$${\displaystyle p{\theta }(x{t-1}|x{t})={\mathcal {N}}(x{t-1}|\mu {\theta }(x{t},t),\Sigma {\theta }(x{t},t))}$The goal now is to learn the parameters ${\displaystyle \theta }$ such that ${\displaystyle p{\theta }(x{0})}$ is as close to ${\displaystyle q(x{0})}$ as possible. To do that, we use maximum likelihood estimation with variational inference.
Variational inference
The ELBO inequality states that ${\displaystyle \ln p{\theta }(x{0})\geq E{x{1:T}\sim q(\cdot |x{0})}[\ln p{\theta }(x{0:T})-\ln q(x{1:T}|x{0})]}$, and taking one more expectation, we get${\displaystyle E{x{0}\sim q}[\ln p{\theta }(x{0})]\geq E{x{0:T}\sim q}[\ln p{\theta }(x{0:T})-\ln q(x{1:T}|x{0})]}$We see that maximizing the quantity on the right would give us a lower bound on the likelihood of observed data. This allows us to perform variational inference.
Define the loss function${\displaystyle L(\theta ):=-E{x{0:T}\sim q}[\ln p{\theta }(x{0:T})-\ln q(x{1:T}|x{0})]}$and now the goal is to minimize the loss by stochastic gradient descent. The expression may be simplified to${\displaystyle L(\theta )=\sum {t=1}^{T}E{x{t-1},x{t}\sim q}[-\ln p{\theta }(x{t-1}|x{t})]+E{x{0}\sim q}[D{KL}(q(x{T}|x{0})\|p{\theta }(x{T}))]+C}$where ${\displaystyle C}$ does not depend on the parameter, and thus can be ignored. Since ${\displaystyle p{\theta }(x{T})={\mathcal {N}}(x{T}|0,I)}$ also does not depend on the parameter, the term ${\displaystyle E{x{0}\sim q}[D{KL}(q(x{T}|x{0})\|p{\theta }(x{T}))]}$ can also be ignored. This leaves just ${\displaystyle L(\theta )=\sum {t=1}^{T}L{t}}$ with ${\displaystyle L{t}=E{x{t-1},x{t}\sim q}[-\ln p{\theta }(x{t-1}|x{t})]}$ to be minimized.
Noise prediction network
Since ${\displaystyle x{t-1}|x{t},x{0}\sim {\mathcal {N}}({\tilde {\mu }}{t}(x{t},x{0}),{\tilde {\sigma }}{t}^{2}I)}$, this suggests that we should use ${\displaystyle \mu {\theta }(x{t},t)={\tilde {\mu }}{t}(x{t},x{0})}$; however, the network does not have access to ${\displaystyle x{0}}$, and so it has to estimate it instead. Now, since ${\displaystyle x{t}|x{0}\sim N\left({\sqrt {{\bar {\alpha }}{t}}}x{0},\sigma {t}^{2}I\right)}$, we may write ${\displaystyle x{t}={\sqrt {{\bar {\alpha }}{t}}}x{0}+\sigma {t}z}$, where ${\displaystyle z}$ is some unknown Gaussian noise. Now we see that estimating ${\displaystyle x{0}}$ is equivalent to estimating ${\displaystyle z}$.
Therefore, let the network output a noise vector ${\displaystyle \epsilon {\theta }(x{t},t)}$, and let it predict${\displaystyle \mu {\theta }(x{t},t)={\tilde {\mu }}{t}\left(x{t},{\frac {x{t}-\sigma {t}\epsilon {\theta }(x{t},t)}{\sqrt {{\bar {\alpha }}{t}}}}\right)={\frac {x{t}-\epsilon {\theta }(x{t},t)\beta {t}/\sigma {t}}{\sqrt {\alpha {t}}}}}$It remains to design ${\displaystyle \Sigma {\theta }(x{t},t)}$. The DDPM paper suggested not learning it (since it resulted in "unstable training and poorer sample quality"), but fixing it at some value ${\displaystyle \Sigma {\theta }(x{t},t)=\zeta {t}^{2}I}$, where either ${\displaystyle \zeta {t}^{2}=\beta {t}{\text{ or }}{\tilde {\sigma }}{t}^{2}}$ yielded similar performance.
With this, the loss simplifies to ${\displaystyle L{t}={\frac {\beta {t}^{2}}{2\alpha {t}\sigma {t}^{2}\zeta {t}^{2}}}E{x{0}\sim q;z\sim {\mathcal {N}}(0,I)}\left[\left\|\epsilon {\theta }(x{t},t)-z\right\|^{2}\right]+C}$which may be minimized by stochastic gradient descent. The paper noted empirically that an even simpler loss function${\displaystyle L{simple,t}=E{x{0}\sim q;z\sim {\mathcal {N}}(0,I)}\left[\left\|\epsilon {\theta }(x{t},t)-z\right\|^{2}\right]}$resulted in better models.
Backward diffusion process
After a noise prediction network is trained, it can be used for generating data points in the original distribution in a loop as follows:
• Compute the noise estimate ${\displaystyle \epsilon \leftarrow \epsilon {\theta }(x{t},t)}$ • Compute the original data estimate ${\displaystyle {\tilde {x}}{0}\leftarrow (x{t}-\sigma {t}\epsilon )/{\sqrt {{\bar {\alpha }}{t}}}}$ • Sample the previous data ${\displaystyle x{t-1}\sim {\mathcal {N}}({\tilde {\mu }}{t}(x{t},{\tilde {x}}{0}),{\tilde {\sigma }}{t}^{2}I)}$ • Change time ${\displaystyle t\leftarrow t-1}$
Score-based generative model
Diffusion model
Score-based generative model is another formulation of diffusion modelling. They are also called noise conditional score network (NCSN) or score-matching with Langevin dynamics (SMLD).
The idea of score functions
Consider the problem of image generation. Let ${\displaystyle x}$ represent an image, and let ${\displaystyle q(x)}$ be the probability distribution over all possible images. If we have ${\displaystyle q(x)}$ itself, then we can say for certain how likely a certain image is. However, this is intractable in general.
Most often, we are uninterested in knowing the absolute probability of a certain image. Instead, we are usually only interested in knowing how likely a certain image is compared to its immediate neighbors — e.g. how much more likely is an image of cat compared to some small variants of it? Is it more likely if the image contains two whiskers, or three, or with some Gaussian noise added?
Consequently, we are actually quite uninterested in ${\displaystyle q(x)}$ itself, but rather, ${\displaystyle \nabla {x}\ln q(x)}$. This has two major effects:
• One, we no longer need to normalize ${\displaystyle q(x)}$, but can use any ${\displaystyle {\tilde {q}}(x)=Cq(x)}$, where ${\displaystyle C=\int {\tilde {q}}(x)dx>0}$ is any unknown constant that is of no concern to us. • Two, we are comparing ${\displaystyle q(x)}$ neighbors ${\displaystyle q(x+dx)}$, by ${\displaystyle {\frac {q(x)}{q(x+dx)}}=e^{-\langle \nabla {x}\ln q,dx\rangle }}$
Let the score function be ${\displaystyle s(x):=\nabla {x}\ln q(x)}$; then consider what we can do with ${\displaystyle s(x)}$.
As it turns out, ${\displaystyle s(x)}$ allows us to sample from ${\displaystyle q(x)}$ using thermodynamics. Specifically, if we have a potential energy function ${\displaystyle U(x)=-\ln q(x)}$, and a lot of particles in the potential well, then the distribution at thermodynamic equilibrium is the Boltzmann distribution ${\displaystyle q{U}(x)\propto e^{-U(x)/k{B}T}=q(x)^{1/k{B}T}}$. At temperature ${\displaystyle k{B}T=1}$, the Boltzmann distribution is exactly ${\displaystyle q(x)}$.
Therefore, to model ${\displaystyle q(x)}$, we may start with a particle sampled at any convenient distribution (such as the standard Gaussian distribution), then simulate the motion of the particle forwards according to the Langevin equation ${\displaystyle dx{t}=-\nabla {x{t}}U(x{t})dt+dW{t}}$ and the Boltzmann distribution is, by Fokker-Planck equation, the unique thermodynamic equilibrium. So no matter what distribution ${\displaystyle x{0}}$ has, the distribution of ${\displaystyle x{t}}$ converges in distribution to ${\displaystyle q}$ as ${\displaystyle t\to \infty }$.
Learning the score function
Given a density ${\displaystyle q}$, we wish to learn a score function approximation ${\displaystyle f{\theta }\approx \nabla \ln q}$. This is score matching. Typically, score matching is formalized as minimizing Fisher divergence function ${\displaystyle E{q}[\|f{\theta }(x)-\nabla \ln q(x)\|^{2}]}$. By expanding the integral, and performing an integration by parts, ${\displaystyle E{q}[\|f{\theta }(x)-\nabla \ln q(x)\|^{2}]=E{q}[\|f{\theta }\|^{2}+2\nabla \cdot f{\theta }]+C}$giving us a loss function, also known as the Hyvärinen scoring rule, that can be minimized by stochastic gradient descent.
Annealing the score function
Suppose we need to model the distribution of images, and we want ${\displaystyle x{0}\sim {\mathcal {N}}(0,I)}$, a white-noise image. Now, most white-noise images do not look like real images, so ${\displaystyle q(x{0})\approx 0}$ for large swaths of ${\displaystyle x{0}\sim {\mathcal {N}}(0,I)}$. This presents a problem for learning the score function, because if there are no samples around a certain point, then we can't learn the score function at that point. If we do not know the score function ${\displaystyle \nabla {x{t}}\ln q(x{t})}$ at that point, then we cannot impose the time-evolution equation on a particle:${\displaystyle dx{t}=\nabla {x{t}}\ln q(x{t})dt+dW{t}}$To deal with this problem, we perform annealing. If ${\displaystyle q}$ is too different from a white-noise distribution, then progressively add noise until it is indistinguishable from one. That is, we perform a forward diffusion, then learn the score function, then use the score function to perform a backward diffusion.
Forward diffusion process
Consider again the forward diffusion process, but this time in continuous time:${\displaystyle x{t}={\sqrt {1-\beta {t}}}x{t-1}+{\sqrt {\beta {t}}}z{t}}$By taking the ${\displaystyle \beta {t}\to \beta (t)dt,{\sqrt {dt}}z{t}\to dW{t}}$ limit, we obtain a continuous diffusion process, in the form of a stochastic differential equation:${\displaystyle dx{t}=-{\frac {1}{2}}\beta (t)x{t}dt+{\sqrt {\beta (t)}}dW{t}}$where ${\displaystyle W{t}}$ is a Wiener process (multidimensional Brownian motion).
Now, the equation is exactly a special case of the overdamped Langevin equation${\displaystyle dx{t}=-{\frac {D}{k{B}T}}(\nabla {x}U)dt+{\sqrt {2D}}dW{t}}$where ${\displaystyle D}$ is diffusion tensor, ${\displaystyle T}$ is temperature, and ${\displaystyle U}$ is potential energy field. If we substitute in ${\displaystyle D={\frac {1}{2}}\beta (t)I,k{B}T=1,U={\frac {1}{2}}\|x\|^{2}}$, we recover the above equation. This explains why the phrase "Langevin dynamics" is sometimes used in diffusion models.
Now the above equation is for the stochastic motion of a single particle. Suppose we have a cloud of particles distributed according to ${\displaystyle q}$ at time ${\displaystyle t=0}$, then after a long time, the cloud of particles would settle into the stable distribution of ${\displaystyle {\mathcal {N}}(0,I)}$. Let ${\displaystyle \rho {t}}$ be the density of the cloud of particles at time ${\displaystyle t}$, then we have${\displaystyle \rho {0}=q;\quad \rho {T}\approx {\mathcal {N}}(0,I)}$and the goal is to somehow reverse the process, so that we can start at the end and diffuse back to the beginning.
By Fokker-Planck equation, the density of the cloud evolves according to${\displaystyle \partial {t}\ln \rho {t}={\frac {1}{2}}\beta (t)\left(n+(x+\nabla \ln \rho {t})\cdot \nabla \ln \rho {t}+\Delta \ln \rho {t}\right)}$where ${\displaystyle n}$ is the dimension of space, and ${\displaystyle \Delta }$ is the Laplace operator. Equivalently,${\displaystyle \partial {t}\rho {t}={\frac {1}{2}}\beta (t)(\nabla \cdot (x\rho {t})+\Delta \rho {t})}$
Backward diffusion process
If we have solved ${\displaystyle \rho {t}}$ for time ${\displaystyle t\in [0,T]}$, then we can exactly reverse the evolution of the cloud. Suppose we start with another cloud of particles with density ${\displaystyle \nu {0}=\rho {T}}$, and let the particles in the cloud evolve according to
${\displaystyle dy{t}={\frac {1}{2}}\beta (T-t)y{t}dt+\beta (T-t)\underbrace {\nabla {y{t}}\ln \rho {T-t}\left(y{t}\right)} {\text{score function }}dt+{\sqrt {\beta (T-t)}}dW{t}}$
then by plugging into the Fokker-Planck equation, we find that ${\displaystyle \partial {t}\rho {T-t}=\partial {t}\nu {t}}$. Thus this cloud of points is the original cloud, evolving backwards.
Noise conditional score network (NCSN)
At the continuous limit, ${\displaystyle {\bar {\alpha }}{t}=(1-\beta {1})\cdots (1-\beta {t})=e^{\sum {i}\ln(1-\beta {i})}\to e^{-\int {0}^{t}\beta (t)dt}}$ and so ${\displaystyle x{t}|x{0}\sim N\left(e^{-{\frac {1}{2}}\int {0}^{t}\beta (t)dt}x{0},\left(1-e^{-\int {0}^{t}\beta (t)dt}\right)I\right)}$ In particular, we see that we can directly sample from any point in the continuous diffusion process without going through the intermediate steps, by first sampling ${\displaystyle x{0}\sim q,z\sim {\mathcal {N}}(0,I)}$, then get ${\displaystyle x{t}=e^{-{\frac {1}{2}}\int {0}^{t}\beta (t)dt}x{0}+\left(1-e^{-\int {0}^{t}\beta (t)dt}\right)z}$. That is, we can quickly sample ${\displaystyle x{t}\sim \rho {t}}$ for any ${\displaystyle t\geq 0}$.
Now, define a certain probability distribution ${\displaystyle \gamma }$ over ${\displaystyle [0,\infty )}$, then the score-matching loss function is defined as the expected Fisher divergence: ${\displaystyle L(\theta )=E{t\sim \gamma ,x{t}\sim \rho {t}}[\|f{\theta }(x{t},t)\|^{2}+2\nabla \cdot f{\theta }(x{t},t)]}$ After training, ${\displaystyle f{\theta }(x{t},t)\approx \nabla \ln \rho {t}}$, so we can perform the backwards diffusion process by first sampling ${\displaystyle x{T}\sim {\mathcal {N}}(0,I)}$, then integrating the SDE from ${\displaystyle t=T}$ to ${\displaystyle t=0}$: ${\displaystyle x{t-dt}=x{t}+{\frac {1}{2}}\beta (t)x{t}dt+\beta (t)f{\theta }(x{t},t)dt+{\sqrt {\beta (t)}}dW{t}}$ This may be done by any SDE integration method, such as Euler–Maruyama method.
The name "noise conditional score network" is explained thus:
• "network", because ${\displaystyle f{\theta }}$ is implemented as a neural network. • "score", because the output of the network is interpreted as approximating the score function ${\displaystyle \nabla \ln \rho {t}}$. • "noise conditional", because ${\displaystyle \rho {t}}$ is equal to ${\displaystyle \rho {0}}$ blurred by an added Gaussian noise that increases with time, and so the score function depends on the amount of noise added.
Their equivalence
Diffusion model
DDPM and score-based generative models are equivalent. This means that a network trained using DDPM can be used as a NCSN, and vice versa.
We know that ${\displaystyle x{t}|x{0}\sim N\left({\sqrt {{\bar {\alpha }}{t}}}x{0},\sigma {t}^{2}I\right)}$, so by Tweedie's formula, we have ${\displaystyle \nabla {x{t}}\ln q(x{t})={\frac {1}{\sigma {t}^{2}}}(-x{t}+{\sqrt {{\bar {\alpha }}{t}}}E{q}[x{0}|x{t}])}$ As described previously, the DDPM loss function is ${\displaystyle \sum {t}L{simple,t}}$ with ${\displaystyle L{simple,t}=E{x{0}\sim q;z\sim {\mathcal {N}}(0,I)}\left[\left\|\epsilon {\theta }(x{t},t)-z\right\|^{2}\right]}$ where ${\displaystyle x{t}={\sqrt {{\bar {\alpha }}{t}}}x{0}+\sigma {t}z}$. By a change of variables, ${\displaystyle L{simple,t}=E{x{0},x{t}\sim q}\left[\left\|\epsilon {\theta }(x{t},t)-{\frac {x{t}-{\sqrt {{\bar {\alpha }}{t}}}x{0}}{\sigma {t}}}\right\|^{2}\right]=E{x{t}\sim q,x{0}\sim q(\cdot |x{t})}\left[\left\|\epsilon {\theta }(x{t},t)-{\frac {x{t}-{\sqrt {{\bar {\alpha }}{t}}}x{0}}{\sigma {t}}}\right\|^{2}\right]}$ and the term inside becomes a least squares regression, so if the network actually reaches the global minimum of loss, then we have ${\displaystyle \epsilon {\theta }(x{t},t)={\frac {x{t}-{\sqrt {{\bar {\alpha }}{t}}}E{q}[x{0}|x{t}]}{\sigma {t}}}=-\sigma {t}\nabla {x{t}}\ln q(x{t})}$
Thus, given a good score-based network, its predicted score is a good prediction of the noise (after scaling by ${\displaystyle \sigma {t}}$), and thus can be used for denoising.
Conversely, the continuous limit ${\displaystyle x{t-1}=x{t-dt},\beta {t}=\beta (t)dt,z{t}{\sqrt {dt}}=dW{t}}$ of the backward equation ${\displaystyle x{t-1}={\frac {x{t}}{\sqrt {\alpha {t}}}}-{\frac {\beta {t}}{\sigma {t}{\sqrt {\alpha {t}}}}}\epsilon {\theta }(x{t},t)+{\sqrt {\beta {t}}}z{t};\quad z{t}\sim {\mathcal {N}}(0,I)}$ gives us precisely the same equation as score-based diffusion: ${\displaystyle x{t-dt}=x{t}(1+\beta (t)dt/2)+\beta (t)\nabla {x{t}}\ln q(x{t})dt+{\sqrt {\beta (t)}}dW{t}}$Thus, at infinitesimal steps of DDPM, a denoising network performs score-based diffusion.
Main variants
Noise schedule
Illustration for a linear diffusion noise schedule. With settings ${\displaystyle \beta {1}=10^{-4},\beta {1000}=0.02}$.
In DDPM, the sequence of numbers ${\displaystyle 0=\sigma {0}<\sigma {1}<\cdots <\sigma {T}<1}$ is called a (discrete time) noise schedule. In general, consider a strictly increasing monotonic function ${\displaystyle \sigma }$ of type ${\displaystyle \mathbb {R} \to (0,1)}$, such as the sigmoid function. In that case, a noise schedule is a sequence of real numbers ${\displaystyle \lambda {1}<\lambda {2}<\cdots <\lambda {T}}$. It then defines a sequence of noises ${\displaystyle \sigma {t}:=\sigma (\lambda {t})}$, which then derives the other quantities ${\displaystyle \beta {t}=1-{\frac {1-\sigma {t}^{2}}{1-\sigma {t-1}^{2}}}}$.
In order to use arbitrary noise schedules, instead of training a noise prediction model ${\displaystyle \epsilon {\theta }(x{t},t)}$, one trains ${\displaystyle \epsilon {\theta }(x{t},\sigma {t})}$.
Similarly, for the noise conditional score network, instead of training ${\displaystyle f{\theta }(x{t},t)}$, one trains ${\displaystyle f{\theta }(x{t},\sigma {t})}$.
Denoising Diffusion Implicit Model (DDIM)
The original DDPM method for generating images is slow, since the forward diffusion process usually takes ${\displaystyle T\sim 1000}$ to make the distribution of ${\displaystyle x{T}}$ to appear close to Gaussian. However this means the backward diffusion process also take 1000 steps. Unlike the forward diffusion process, which can skip steps as ${\displaystyle x{t}|x{0}}$ is Gaussian for all ${\displaystyle t\geq 1}$, the backward diffusion process does not allow skipping steps. For example, to sample ${\displaystyle x{t-2}|x{t-1}\sim {\mathcal {N}}(\mu {\theta }(x{t-1},t-1),\Sigma {\theta }(x{t-1},t-1))}$ requires the model to first sample ${\displaystyle x{t-1}}$. Attempting to directly sample ${\displaystyle x{t-2}|x{t}}$ would require us to marginalize out ${\displaystyle x{t-1}}$, which is generally intractable.
DDIM is a method to take any model trained on DDPM loss, and use it to sample with some steps skipped, sacrificing an adjustable amount of quality. If we generate the Markovian chain case in DDPM to non-Markovian case, DDIM corresponds to the case that the reverse process has variance equals to 0. In other words, the reverse process (and also the forward process) is deterministic. When using fewer sampling steps, DDIM outperforms DDPM.
In detail, the DDIM sampling method is as follows. Start with the forward diffusion process ${\displaystyle x{t}={\sqrt {{\bar {\alpha }}{t}}}x{0}+\sigma {t}\epsilon }$. Then, during the backward denoising process, given ${\displaystyle x{t},\epsilon {\theta }(x{t},t)}$, the original data is estimated as ${\displaystyle x{0}'={\frac {x{t}-\sigma {t}\epsilon {\theta }(x{t},t)}{\sqrt {{\bar {\alpha }}{t}}}}}$then the backward diffusion process can jump to any step ${\displaystyle 0\leq s<t}$, and the next denoised sample is ${\displaystyle x{s}={\sqrt {{\bar {\alpha }}{s}}}x{0}'+{\sqrt {\sigma {s}^{2}-(\sigma '{s})^{2}}}\epsilon {\theta }(x{t},t)+\sigma {s}'\epsilon }$where ${\displaystyle \sigma {s}'}$ is an arbitrary real number within the range ${\displaystyle [0,\sigma {s}]}$, and ${\displaystyle \epsilon \sim {\mathcal {N}}(0,I)}$ is a newly sampled Gaussian noise. If all ${\displaystyle \sigma {s}'=0}$, then the backward process becomes deterministic, and this special case of DDIM is also called "DDIM". The original paper noted that when the process is deterministic, samples generated with only 20 steps are already very similar to ones generated with 1000 steps on the high-level.
The original paper recommended defining a single "eta value" ${\displaystyle \eta \in [0,1]}$, such that ${\displaystyle \sigma {s}'=\eta {\tilde {\sigma }}{s}}$. When ${\displaystyle \eta =1}$, this is the original DDPM. When ${\displaystyle \eta =0}$, this is the fully deterministic DDIM. For intermediate values, the process interpolates between them.
By the equivalence, the DDIM algorithm also applies for score-based diffusion models.
Latent diffusion model (LDM)
Since the diffusion model is a general method for modelling probability distributions, if one wants to model a distribution over images, one can first encode the images into a lower-dimensional space by an encoder, then use a diffusion model to model the distribution over encoded images. Then to generate an image, one can sample from the diffusion model, then use a decoder to decode it into an image.
The encoder-decoder pair is most often a variational autoencoder (VAE).
Architectural improvements
proposed various architectural improvements. For example, they proposed log-space interpolation during backward sampling. Instead of sampling from ${\displaystyle x{t-1}\sim {\mathcal {N}}({\tilde {\mu }}{t}(x{t},{\tilde {x}}{0}),{\tilde {\sigma }}{t}^{2}I)}$, they recommended sampling from ${\displaystyle {\mathcal {N}}({\tilde {\mu }}{t}(x{t},{\tilde {x}}{0}),(\sigma {t}^{v}{\tilde {\sigma }}{t}^{1-v})^{2}I)}$ for a learned parameter ${\displaystyle v}$.
In the v-prediction formalism, the noising formula ${\displaystyle x{t}={\sqrt {{\bar {\alpha }}{t}}}x{0}+{\sqrt {1-{\bar {\alpha }}{t}}}\epsilon {t}}$ is reparameterised by an angle ${\displaystyle \phi {t}}$ such that ${\displaystyle \cos \phi {t}={\sqrt {{\bar {\alpha }}{t}}}}$ and a "velocity" defined by ${\displaystyle \cos \phi {t}\epsilon {t}-\sin \phi {t}x{0}}$. The network is trained to predict the velocity ${\displaystyle {\hat {v}}{\theta }}$, and denoising is by ${\displaystyle x{\phi {t}-\delta }=\cos(\delta )\;x{\phi {t}}-\sin(\delta ){\hat {v}}{\theta }\;(x{\phi {t}})}$. This parameterization was found to improve performance, as the model can be trained to reach total noise (i.e. ${\displaystyle \phi {t}=90^{\circ }}$) and then reverse it, whereas the standard parameterization never reaches total noise since ${\displaystyle {\sqrt {{\bar {\alpha }}{t}}}>0}$ is always true.
Classifier guidance
Classifier guidance was proposed in 2021 to improve class-conditional generation by using a classifier. The original publication used CLIP text encoders to improve text-conditional image generation.
Suppose we wish to sample not from the entire distribution of images, but conditional on the image description. We don't want to sample a generic image, but an image that fits the description "black cat with red eyes". Generally, we want to sample from the distribution ${\displaystyle p(x|y)}$, where ${\displaystyle x}$ ranges over images, and ${\displaystyle y}$ ranges over classes of images (a description "black cat with red eyes" is just a very detailed class, and a class "cat" is just a very vague description).
Taking the perspective of the noisy channel model, we can understand the process as follows: To generate an image ${\displaystyle x}$ conditional on description ${\displaystyle y}$, we imagine that the requester really had in mind an image ${\displaystyle x}$, but the image is passed through a noisy channel and came out garbled, as ${\displaystyle y}$. Image generation is then nothing but inferring which ${\displaystyle x}$ the requester had in mind.
In other words, conditional image generation is simply "translating from a textual language into a pictorial language". Then, as in noisy-channel model, we use Bayes theorem to get ${\displaystyle p(x|y)\propto p(y|x)p(x)}$ in other words, if we have a good model of the space of all images, and a good image-to-class translator, we get a class-to-image translator "for free". In the equation for backward diffusion, the score ${\displaystyle \nabla \ln p(x)}$ can be replaced by ${\displaystyle \nabla {x}\ln p(x|y)=\underbrace {\nabla {x}\ln p(x)} {\text{score}}+\underbrace {\nabla {x}\ln p(y|x)} {\text{classifier guidance}}}$ where ${\displaystyle \nabla {x}\ln p(x)}$ is the score function, trained as previously described, and ${\displaystyle \nabla {x}\ln p(y|x)}$ is found by using a differentiable image classifier.
During the diffusion process, we need to condition on the time, giving${\displaystyle \nabla {x{t}}\ln p(x{t}|y,t)=\nabla {x{t}}\ln p(y|x{t},t)+\nabla {x{t}}\ln p(x{t}|t)}$Although, usually the classifier model does not depend on time, in which case ${\displaystyle p(y|x{t},t)=p(y|x{t})}$.
Classifier guidance is defined for the gradient of score function, thus for score-based diffusion network, but as previously noted, score-based diffusion models are equivalent to denoising models by ${\displaystyle \epsilon {\theta }(x{t},t)=-\sigma {t}\nabla {x{t}}\ln p(x{t}|t)}$, and similarly, ${\displaystyle \epsilon {\theta }(x{t},y,t)=-\sigma {t}\nabla {x{t}}\ln p(x{t}|y,t)}$. Therefore, classifier guidance works for denoising diffusion as well, using the modified noise prediction:${\displaystyle \epsilon {\theta }(x{t},y,t)=\epsilon {\theta }(x{t},t)-\underbrace {\sigma {t}\nabla {x{t}}\ln p(y|x{t},t)} {\text{classifier guidance}}}$
With temperature
The classifier-guided diffusion model samples from ${\displaystyle p(x|y)}$, which is concentrated around the maximum a posteriori estimate ${\displaystyle \arg \max {x}p(x|y)}$. If we want to force the model to move towards the maximum likelihood estimate ${\displaystyle \arg \max {x}p(y|x)}$, we can use ${\displaystyle p{\gamma }(x|y)\propto p(y|x)^{\gamma }p(x)}$ where ${\displaystyle \gamma >0}$ is interpretable as inverse temperature. In the context of diffusion models, it is usually called the guidance scale. A high ${\displaystyle \gamma }$ would force the model to sample from a distribution concentrated around ${\displaystyle \arg \max {x}p(y|x)}$. This sometimes improves quality of generated images.
This gives a modification to the previous equation:${\displaystyle \nabla {x}\ln p{\beta }(x|y)=\nabla {x}\ln p(x)+\gamma \nabla {x}\ln p(y|x)}$For denoising models, it corresponds to${\displaystyle \epsilon {\theta }(x{t},y,t)=\epsilon {\theta }(x{t},t)-\gamma \sigma {t}\nabla {x{t}}\ln p(y|x{t},t)}$
Classifier-free guidance (CFG)
If we do not have a classifier ${\displaystyle p(y|x)}$, we could still extract one out of the image model itself: ${\displaystyle \nabla {x}\ln p{\gamma }(x|y)=(1-\gamma )\nabla {x}\ln p(x)+\gamma \nabla {x}\ln p(x|y)}$ Such a model is usually trained by presenting it with both ${\displaystyle (x,y)}$ and ${\displaystyle (x,{\rm {None}})}$, allowing it to model both ${\displaystyle \nabla {x}\ln p(x|y)}$ and ${\displaystyle \nabla {x}\ln p(x)}$.
Note that for CFG, the diffusion model cannot be merely a generative model of the entire data distribution ${\displaystyle \nabla {x}\ln p(x)}$. It must be a conditional generative model ${\displaystyle \nabla {x}\ln p(x|y)}$. For example, in stable diffusion, the diffusion backbone takes as input both a noisy model ${\displaystyle x{t}}$, a time ${\displaystyle t}$, and a conditioning vector ${\displaystyle y}$ (such as a vector encoding a text prompt), and produces a noise prediction ${\displaystyle \epsilon {\theta }(x{t},y,t)}$.
For denoising models, it corresponds to${\displaystyle \epsilon {\theta }(x{t},y,t,\gamma )=\epsilon {\theta }(x{t},t)+\gamma (\epsilon {\theta }(x{t},y,t)-\epsilon {\theta }(x{t},t))}$As sampled by DDIM, the algorithm can be written as${\displaystyle {\begin{aligned}\epsilon {\text{uncond}}&\leftarrow \epsilon {\theta }(x{t},t)\\\epsilon {\text{cond}}&\leftarrow \epsilon {\theta }(x{t},t,c)\\\epsilon {\text{CFG}}&\leftarrow \epsilon {\text{uncond}}+\gamma (\epsilon {\text{cond}}-\epsilon {\text{uncond}})\\x{0}&\leftarrow (x{t}-\sigma {t}\epsilon {\text{CFG}})/{\sqrt {1-\sigma {t}^{2}}}\\x{s}&\leftarrow {\sqrt {1-\sigma {s}^{2}}}x{0}+{\sqrt {\sigma {s}^{2}-(\sigma {s}')^{2}}}\epsilon {\text{uncond}}+\sigma {s}'\epsilon \\\end{aligned}}}$A similar technique applies to language model sampling. Also, if the unconditional generation ${\displaystyle \epsilon {\text{uncond}}\leftarrow \epsilon {\theta }(x{t},t)}$ is replaced by ${\displaystyle \epsilon {\text{neg cond}}\leftarrow \epsilon {\theta }(x{t},t,c')}$, then it results in negative prompting, which pushes the generation away from ${\displaystyle c'}$ condition.
Samplers
Given a diffusion model, one may regard it either as a continuous process, and sample from it by integrating a SDE, or one can regard it as a discrete process, and sample from it by iterating the discrete steps. The choice of the "noise schedule" ${\displaystyle \beta {t}}$ can also affect the quality of samples. A noise schedule is a function that sends a natural number to a noise level: ${\displaystyle t\mapsto \beta {t},\quad t\in \{1,2,\dots \},\beta \in (0,1)}$A noise schedule is more often specified by a map ${\displaystyle t\mapsto \sigma {t}}$. The two definitions are equivalent, since ${\displaystyle \beta {t}=1-{\frac {1-\sigma {t}^{2}}{1-\sigma {t-1}^{2}}}}$.
In the DDPM perspective, one can use the DDPM itself (with noise), or DDIM (with adjustable amount of noise). The case where one adds noise is sometimes called ancestral sampling. One can interpolate between noise and no noise. The amount of noise is denoted ${\displaystyle \eta }$ ("eta value") in the DDIM paper, with ${\displaystyle \eta =0}$ denoting no noise (as in deterministic DDIM), and ${\displaystyle \eta =1}$ denoting full noise (as in DDPM).
In the perspective of SDE, one can use any of the numerical integration methods, such as Euler–Maruyama method, Heun's method, linear multistep methods, etc. Just as in the discrete case, one can add an adjustable amount of noise during the integration.
A survey and comparison of samplers in the context of image generation is in.
Other examples
Notable variants include Poisson flow generative model, consistency model, critically damped Langevin diffusion, GenPhys, cold diffusion, etc.
Flow-based diffusion model
Diffusion model
Abstractly speaking, the idea of diffusion model is to take an unknown probability distribution (the distribution of natural-looking images), then progressively convert it to a known probability distribution (standard Gaussian distribution), by building an absolutely continuous probability path connecting them. The probability path is in fact defined implicitly by the score function ${\displaystyle \nabla \ln p{t}}$.
In denoising diffusion models, the forward process adds noise, and the backward process removes noise. Both the forward and backward processes are SDEs, though the forward process is integrable in closed-form, so it can be done at no computational cost. The backward process is not integrable in closed-form, so it must be integrated step-by-step by standard SDE solvers, which can be very expensive. The probability path in diffusions model is defined through an Itô process and one can retrieve the deterministic process by using the Probability ODE flow formulation.
In flow-based diffusion models, the forward process is a deterministic flow along a time-dependent vector field, and the backward process is also a deterministic flow along the same vector field, but going backwards. Both processes are solutions to ODEs. If the vector field is well-behaved, the ODE will also be well-behaved.
Given two distributions ${\displaystyle \pi {0}}$ and ${\displaystyle \pi {1}}$, a flow-based model is a time-dependent velocity field ${\displaystyle v{t}(x)}$ in ${\displaystyle [0,1]\times \mathbb {R} ^{d}}$, such that if we start by sampling a point ${\displaystyle x\sim \pi {0}}$, and let it move according to the velocity field: ${\displaystyle {\frac {d}{dt}}\phi {t}(x)=v{t}(\phi {t}(x))\quad t\in [0,1],\quad {\text{starting from }}\phi {0}(x)=x}$ we end up with a point ${\displaystyle x{1}\sim \pi {1}}$. The solution ${\displaystyle \phi {t}}$ of the above ODE define a probability path ${\displaystyle p{t}=[\phi {t}]{\#}\pi {0}}$ by the pushforward measure operator. In particular, ${\displaystyle [\phi {1}]{\#}\pi {0}=\pi {1}}$.
The probability path and the velocity field also satisfy the continuity equation, in the sense of probability distribution: ${\displaystyle \partial {t}p{t}+\nabla \cdot (v{t}p{t})=0}$ To construct a probability path, we start by construct a conditional probability path ${\displaystyle p{t}(x\vert z)}$ and the corresponding conditional velocity field ${\displaystyle v{t}(x\vert z)}$ on some conditional distribution ${\displaystyle q(z)}$. A natural choice is the Gaussian conditional probability path: ${\displaystyle p{t}(x\vert z)={\mathcal {N}}\left(m{t}(z),\zeta {t}^{2}I\right)}$ The conditional velocity field which corresponds to the geodesic path between conditional Gaussian path is ${\displaystyle v{t}(x\vert z)={\frac {\zeta {t}'}{\zeta {t}}}(x-m{t}(z))+m{t}'(z)}$ The probability path and velocity field are then computed by marginalizing
${\displaystyle p{t}(x)=\int p{t}(x\vert z)q(z)dz\qquad {\text{ and }}\qquad v{t}(x)=\mathbb {E} {q(z)}\left[{\frac {v{t}(x\vert z)p{t}(x\vert z)}{p{t}(x)}}\right]}$
Optimal transport flow
The idea of optimal transport flow is to construct a probability path minimizing the Wasserstein metric. The distribution on which we condition is an approximation of the optimal transport plan between ${\displaystyle \pi {0}}$ and ${\displaystyle \pi {1}}$: ${\displaystyle z=(x{0},x{1})}$ and ${\displaystyle q(z)=\Gamma (\pi {0},\pi {1})}$, where ${\displaystyle \Gamma }$ is the optimal transport plan, which can be approximated by mini-batch optimal transport. If the batch size is not large, then the transport it computes can be very far from the true optimal transport.
Rectified flow
The idea of rectified flow is to learn a flow model such that the velocity is nearly constant along each flow path. This is beneficial, because we can integrate along such a vector field with very few steps. For example, if an ODE ${\displaystyle {\dot {\phi {t}}}(x)=v{t}(\phi {t}(x))}$ follows perfectly straight paths, it simplifies to ${\displaystyle \phi {t}(x)=x{0}+t\cdot v{0}(x{0})}$, allowing for exact solutions in one step. In practice, we cannot reach such perfection, but when the flow field is nearly so, we can take a few large steps instead of many little steps.
The general idea is to start with two distributions ${\displaystyle \pi {0}}$ and ${\displaystyle \pi {1}}$, then construct a flow field ${\displaystyle \phi ^{0}=\{\phi {t}:t\in [0,1]\}}$ from it, then repeatedly apply a "reflow" operation to obtain successive flow fields ${\displaystyle \phi ^{1},\phi ^{2},\dots }$, each straighter than the previous one. When the flow field is straight enough for the application, we stop.
Generally, for any time-differentiable process ${\displaystyle \phi {t}}$, ${\displaystyle v{t}}$ can be estimated by solving: ${\displaystyle \min {\theta }\int {0}^{1}\mathbb {E} {x\sim p{t}}\left[\lVert {v{t}(x,\theta )-v{t}(x)}\rVert ^{2}\right]\,\mathrm {d} t.}$
In rectified flow, by injecting strong priors that intermediate trajectories are straight, it can achieve both theoretical relevance for optimal transport and computational efficiency, as ODEs with straight paths can be simulated precisely without time discretization.
Transport by rectified flow
Specifically, rectified flow seeks to match an ODE with the marginal distributions of the linear interpolation between points from distributions ${\displaystyle \pi {0}}$ and ${\displaystyle \pi {1}}$. Given observations ${\displaystyle x{0}\sim \pi {0}}$ and ${\displaystyle x{1}\sim \pi {1}}$, the canonical linear interpolation ${\displaystyle x{t}=tx{1}+(1-t)x{0},t\in [0,1]}$ yields a trivial case ${\displaystyle {\dot {x}}{t}=x{1}-x{0}}$, which cannot be causally simulated without ${\displaystyle x{1}}$. To address this, ${\displaystyle x{t}}$ is "projected" into a space of causally simulatable ODEs, by minimizing the least squares loss with respect to the direction ${\displaystyle x{1}-x{0}}$: ${\displaystyle \min {\theta }\int {0}^{1}\mathbb {E} {\pi {0},\pi {1},p{t}}\left[\lVert {(x{1}-x{0})-v{t}(x{t})}\rVert ^{2}\right]\,\mathrm {d} t.}$
The data pair ${\displaystyle (x{0},x{1})}$ can be any coupling of ${\displaystyle \pi {0}}$ and ${\displaystyle \pi {1}}$, typically independent (i.e., ${\displaystyle (x{0},x{1})\sim \pi {0}\times \pi {1}}$) obtained by randomly combining observations from ${\displaystyle \pi {0}}$ and ${\displaystyle \pi {1}}$. This process ensures that the trajectories closely mirror the density map of ${\displaystyle x{t}}$ trajectories but reroute at intersections to ensure causality.
The reflow process
A distinctive aspect of rectified flow is its capability for "reflow", which straightens the trajectory of ODE paths. Denote the rectified flow ${\displaystyle \phi ^{0}=\{\phi {t}:t\in [0,1]\}}$ induced from ${\displaystyle (x{0},x{1})}$ as ${\displaystyle \phi ^{0}={\mathsf {Rectflow}}((x{0},x{1}))}$. Recursively applying this ${\displaystyle {\mathsf {Rectflow}}(\cdot )}$ operator generates a series of rectified flows ${\displaystyle \phi ^{k+1}={\mathsf {Rectflow}}((\phi {0}^{k}(x{0}),\phi {1}^{k}(x{1})))}$. This "reflow" process not only reduces transport costs but also straightens the paths of rectified flows, making ${\displaystyle \phi ^{k}}$ paths straighter with increasing ${\displaystyle k}$.
Rectified flow includes a nonlinear extension where linear interpolation ${\displaystyle x{t}}$ is replaced with any time-differentiable curve that connects ${\displaystyle x{0}}$ and ${\displaystyle x{1}}$, given by ${\displaystyle x{t}=\alpha {t}x{1}+\beta {t}x{0}}$. This framework encompasses DDIM and probability flow ODEs as special cases, with particular choices of ${\displaystyle \alpha {t}}$ and ${\displaystyle \beta {t}}$. However, in the case where the path of ${\displaystyle x{t}}$ is not straight, the reflow process no longer ensures a reduction in convex transport costs, and also no longer straighten the paths of ${\displaystyle \phi {t}}$.
Choice of architecture
Diffusion model
Architecture of Stable Diffusion
The denoising process used by Stable Diffusion
For generating images by DDPM, we need a neural network that takes a time ${\displaystyle t}$ and a noisy image ${\displaystyle x{t}}$, and predicts a noise ${\displaystyle \epsilon {\theta }(x{t},t)}$ from it. Since predicting the noise is the same as predicting the denoised image, then subtracting it from ${\displaystyle x{t}}$, denoising architectures tend to work well. For example, the U-Net, which was found to be good for denoising images, is often used for denoising diffusion models that generate images.
For DDPM, the underlying architecture ("backbone") does not have to be a U-Net. It just has to predict the noise somehow. For example, the diffusion transformer (DiT) uses a Transformer to predict the mean and diagonal covariance of the noise, given the textual conditioning and the partially denoised image. It is the same as standard U-Net-based denoising diffusion model, with a Transformer replacing the U-Net. Mixture of experts-Transformer can also be applied.
DDPM can be used to model general data distributions, not just natural-looking images. For example, Human Motion Diffusion models human motion trajectory by DDPM. Each human motion trajectory is a sequence of poses, represented by either joint rotations or positions. It uses a Transformer network to generate a less noisy trajectory out of a noisy one.
Conditioning
The base diffusion model can only generate unconditionally from the whole distribution. For example, a diffusion model learned on ImageNet would generate images that look like a random image from ImageNet. To generate images from just one category, one would need to impose the condition, and then sample from the conditional distribution. Whatever condition one wants to impose, one needs to first convert the conditioning into a vector of floating point numbers, then feed it into the underlying diffusion model neural network. However, one has freedom in choosing how to convert the conditioning into a vector.
Stable Diffusion, for example, imposes conditioning in the form of cross-attention mechanism, where the query is an intermediate representation of the image in the U-Net, and both key and value are the conditioning vectors. The conditioning can be selectively applied to only parts of an image, and new kinds of conditionings can be finetuned upon the base model, as used in ControlNet.
As a particularly simple example, consider image inpainting. The conditions are ${\displaystyle {\tilde {x}}}$, the reference image, and ${\displaystyle m}$, the inpainting mask. The conditioning is imposed at each step of the backward diffusion process, by first sampling ${\displaystyle {\tilde {x}}{t}\sim N\left({\sqrt {{\bar {\alpha }}{t}}}{\tilde {x}},\sigma {t}^{2}I\right)}$, a noisy version of ${\displaystyle {\tilde {x}}}$, then replacing ${\displaystyle x{t}}$ with ${\displaystyle (1-m)\odot x{t}+m\odot {\tilde {x}}{t}}$, where ${\displaystyle \odot }$ means elementwise multiplication. Another application of cross-attention mechanism is prompt-to-prompt image editing.
Conditioning is not limited to just generating images from a specific category, or according to a specific caption (as in text-to-image). For example, demonstrated generating human motion, conditioned on an audio clip of human walking (allowing syncing motion to a soundtrack), or video of human running, or a text description of human motion, etc. For how conditional diffusion models are mathematically formulated, see a methodological summary in.
Upscaling
As generating an image takes a long time, one can try to generate a small image by a base diffusion model, then upscale it by other models. Upscaling can be done by GAN, Transformer, or signal processing methods like Lanczos resampling.
Diffusion models themselves can be used to perform upscaling. Cascading diffusion model stacks multiple diffusion models one after another, in the style of Progressive GAN. The lowest level is a standard diffusion model that generate 32x32 image, then the image would be upscaled by a diffusion model specifically trained for upscaling, and the process repeats.
In more detail, the diffusion upscaler is trained as follows:
• Sample ${\displaystyle (x{0},z{0},c)}$, where ${\displaystyle x{0}}$ is the high-resolution image, ${\displaystyle z{0}}$ is the same image but scaled down to a low-resolution, and ${\displaystyle c}$ is the conditioning, which can be the caption of the image, the class of the image, etc. • Sample two white noises ${\displaystyle \epsilon {x},\epsilon {z}}$, two time-steps ${\displaystyle t{x},t{z}}$. Compute the noisy versions of the high-resolution and low-resolution images: ${\displaystyle {\begin{cases}x{t{x}}&={\sqrt {{\bar {\alpha }}{t{x}}}}x{0}+\sigma {t{x}}\epsilon {x}\\z{t{z}}&={\sqrt {{\bar {\alpha }}{t{z}}}}z{0}+\sigma {t{z}}\epsilon {z}\end{cases}}}$. • Train the denoising network to predict ${\displaystyle \epsilon {x}}$ given ${\displaystyle x{t{x}},z{t{z}},t{x},t{z},c}$. That is, apply gradient descent on ${\displaystyle \theta }$ on the L2 loss ${\displaystyle \|\epsilon {\theta }(x{t{x}},z{t{z}},t{x},t{z},c)-\epsilon {x}\|{2}^{2}}$.
Examples
Diffusion model
This section collects some notable diffusion models, and briefly describes their architecture.
OpenAI
The DALL-E series by OpenAI are text-conditional diffusion models of images.
The first version of DALL-E (2021) is not actually a diffusion model. Instead, it uses a Transformer architecture that autoregressively generates a sequence of tokens, which is then converted to an image by the decoder of a discrete VAE. Released with DALL-E was the CLIP classifier, which was used by DALL-E to rank generated images according to how close the image fits the text.
GLIDE (2022–03) is a 3.5-billion diffusion model, and a small version was released publicly. Soon after, DALL-E 2 was released (2022–04). DALL-E 2 is a 3.5-billion cascaded diffusion model that generates images from text by "inverting the CLIP image encoder", the technique which they termed "unCLIP".
The unCLIP method contains 4 models: a CLIP image encoder, a CLIP text encoder, an image decoder, and a "prior" model (which can be a diffusion model, or an autoregressive model). During training, the prior model is trained to convert CLIP image encodings to CLIP text encodings. The image decoder is trained to convert CLIP image encodings back to images. During inference, a text is converted by the CLIP text encoder to a vector, then it is converted by the prior model to an image encoding, then it is converted by the image decoder to an image.
Sora (2024–02) is a diffusion Transformer model (DiT).
Stability AI
Stable Diffusion (2022–08), released by Stability AI, consists of a denoising latent diffusion model (860 million parameters), a VAE, and a text encoder. The denoising network is a U-Net, with cross-attention blocks to allow for conditional image generation.
Stable Diffusion 3 (2024–03) changed the latent diffusion model from the UNet to a Transformer model, and so it is a DiT. It uses rectified flow.
Stable Video 4D (2024–07) is a latent diffusion model for videos of 3D objects.
Imagen (2022) uses a T5-XXL language model to encode the input text into an embedding vector. It is a cascaded diffusion model with three sub-models. The first step denoises a white noise to a 64×64 image, conditional on the embedding vector of the text. This model has 2B parameters. The second step upscales the image by 64×64→256×256, conditional on embedding. This model has 650M parameters. The third step is similar, upscaling by 256×256→1024×1024. This model has 400M parameters. The three denoising networks are all U-Nets.
Muse (2023–01) is not a diffusion model, but an encoder-only Transformer that is trained to predict masked image tokens from unmasked image tokens.
Imagen 2 (2023–12) is also diffusion-based. It can generate images based on a prompt that mixes images and text. No further information available. Imagen 3 (2024–05) is too. No further information available.
Veo (2024) generates videos by latent diffusion. The diffusion is conditioned on a vector that encodes both a text prompt and an image prompt.
Meta
Make-A-Video (2022) is a text-to-video diffusion model.
CM3leon (2023) is not a diffusion model, but an autoregressive causally masked Transformer, with mostly the same architecture as LLaMa-2.
Transfusion architectural diagram
Transfusion (2024) is a Transformer that combines autoregressive text generation and denoising diffusion. Specifically, it generates text autoregressively (with causal masking), and generates images by denoising multiple times over image tokens (with all-to-all attention).
Movie Gen (2024) is a series of Diffusion Transformers operating on latent space and by flow matching.