Flow Matching from the Ground Up

Simulation-based generative models are all the rage. In particular, flow matching now dominates the image and video generation landscape. Because it tolerates few simulation steps well, it has also been widely adopted in applications sensitive to inference time (mostly, robotics). Like we did for denoising diffusion models, this post studies flow matching from first principles.

$\quad$ This post overlaps with another, focused on ODE solvers. The latter is a good complementary read.
ⓘ
$\quad$ Also relevant is the post on denoising diffusion probabilistic models.
ⓘ

Preliminaries

This section introduces the necessary tools to understand and describe flow matching. In particular, we will focus on ordinary differential equations (ODEs). In the next section, we will see that colloquially, flow matching is about learning an ODE’s vector field, and letting a numerical solver generate samples by simulating some particle’s dynamics. But first, we will need some foundations.

Cauchy problems

Let $f:\mathbb{R}^d\times[0,1]\mapsto\mathbb{R}^d$ describe a family of vector fields indexed by the time variable $t\in[0,1]$ (these bounds are for convenience; any bounded interval of $\mathbb{R}$ would do). Let $y:[0,1]\mapsto\mathbb{R}^d$ describe the motion of a ‘particle’ which follows the instantaneous vector field $f(\cdot, t)$. This motion is described by a non-autonomous ODE; when paired with an initial position $y_0$ in $\mathbb{R}^d$, it forms a Cauchy problem: $$ \tag{1} \frac{dy}{dt}(t) = f\big(y(t), t\big)\; \text{ for any } t\in[0,1] \text{ and } y(0)=y_0\;. $$

The existence and uniqueness of a solution $y$ to a given Cauchy problem is not guaranteed, and essentially depends on the smoothness of the vector fields. This is captured by the Cauchy-Lipschitz theorem, which states some sufficient conditions for a Cauchy problem to be well-posed. We state a (weak) version of said theorem:

$\qquad\qquad\qquad\qquad \text{Let } f \text{ be continuous over } \mathbb{R}^d\times[0,1] \text{ and } f(\cdot, t) \text{ } L\text{-Lipschitz for all } t\in[0,1]. \\ \qquad\qquad\qquad\qquad \text{Then (1) has a unique solution.} $
Cauchy-Lipschitz
▶ Proof

Flow

From now on, the vector fields $f$ respect the conditions of the above theorem. In such conditions, each Cauchy problem admits a unique solution. It is quite convenient to have a shorthand for the state of the solution of any given Cauchy problem at some given time $t$. That’s the point of the flow $\psi: \mathbb{R}^d \times [0,1]\mapsto\mathbb{R}^d$ where, concretely, $\psi(y_0, t)=y(t)$ with $y$ the solution of the ODE with initial condition $y(0)=y_0$. In particular, we have that for any $y_0\in\mathbb{R}^d$ and $t\in[0,1]$: $$ \frac{\partial \psi}{\partial t}(y_0, t) = f(\psi(y_0, t), t) \quad\text{ and }\quad \psi(y_0, 0) = y_0\;. $$

A consequence of the Cauchy-Lipschitz theorem is that $\psi(\cdot, t)$ is an invertible map from $\mathbb{R}^d$ to itself. In practical terms, this means that trajectories starting from different initial positions do not collide; two particles that started from different positions cannot sit at the same place at the same time. When $f$ is continuously differentiable, one can further prove that the flow is a diffeomorphism: a continuously differentiable, invertible map from $\mathbb{R}^d$ to itself, whose inverse is also continuously differentiable (we will not prove this result here).

▶ Proof
t = 0.00
Fig1. The flow of a two-dimensional (autonomous) ODE.

Continuity equation

The flow $\psi$ describes the evolution of each particle under the vector fields $f(\cdot, t)$. Below, we will be interested in how densities change under said fields. More precisely, given some probability measure $p$, we will want to know the push-forward $p_t := [\psi(\cdot, t)]_\sharp p$. For convenience, we will overload probability measures with their densities and denote $p(\cdot, t)$ the density of $p_t$. (Observe that $p_t$ does have a density, since $\psi(\cdot, t)$ is a diffeomorphism.) It is characterised by a differential equation known as the continuity equation.

$\qquad\qquad\qquad\qquad\quad \; \text{ For any }t\in[0,1] , \, p(\cdot, t) \text{ is the unique solution to the partial differential equation}:$ $$ \tag{2} \frac{\partial p}{\partial t}(y, t) + \sum_{i=1}^d \frac{\partial (p f_i)}{\partial y_i}(y, t) = 0,\quad \forall y\in\mathbb{R}^d\;. $$
Continuity equation

The continuity equation is essentially a statement of mass preservation; the variation of mass in an (infinitesimal) volume is explained by how much density comes in and out of said volume.

▶ Proof

Flow matching

Let $p_\star$ some distribution over $\mathbb{R}^d$ that we which to model—at least, create some $\hat{p}$ that resembles $p_\star$ and that we can freely sample from. Similarly to denoising diffusion models, flow matching [1,2] attempts this by “transforming noise to data”. Given some initial distribution $p_0$, it transforms its samples by simulating their motion under a non-autonomous ordinary differential equation (ODE). It is the vector fields of said ODE that are learned from data, so that the resulting $\hat{p}$ provides a good approximation of $p_\star$.

t = 0.35
Fig2. Transporting $p_0$ to $p_\star$.
▶ Solving the ODE

From now on, we write the time index after a bar: $p(\cdot\vert t)$ denotes the density at time $t$ (it plays the role of $p(\cdot, t)$ in the previous section) and $f(\cdot\vert t)$ the vector field at time $t$. This leaves room for extra conditioning variables, as in $p(\cdot\vert z, t)$.

Conditional paths

We first look at the simpler problem of turning $p_0$ into $\delta_z$, the point mass located at $z\in\mathbb{R}^d$. How we interpolate between the two distributions is called the conditional probability path $p(\cdot\vert z, t)$; it is a sequence of distributions that only has to satisfy the boundary conditions that $p(\cdot\vert z, 0)=p_0(\cdot)$ and $p(\cdot\vert z, 1)=\delta_z(\cdot)$. The conditional probability path is declarative—in the sense that it is a design choice left to the experimenter.

▶ About point masses

To be relevant, one still needs to find a family of conditional vector fields $\{f(\cdot\vert z, t)\}_t$ that generates it— meaning that the flow of the related ODE checks $ [\psi(\cdot, t)]_\sharp p_0 = p(\cdot\vert z, t)\;. $ Moving forward, we will make the assumption that the conditional vector fields supporting the chosen interpolating path are known. By the continuity equation, this means that for any $y\in\mathbb{R}^d$: $$ \tag{3} \frac{\partial p}{\partial t}(y\vert z, t) + \sum_{i=1}^d \frac{\partial (pf_i)}{\partial y_i}(y\vert z, t) = 0\;. $$

Marginal paths

Now that we can turn $p_0$ into a point mass, we can go back to transforming it to $p_\star$. Choosing a conditional interpolating path $p(\cdot\vert z, t)$ is enough, in the sense that the marginal path: $$ p(\cdot\vert t) := \int_z p_\star(z)p(\cdot\vert z, t)dz $$ turns $p_0$ into $p_\star$. Indeed, it checks $p(y\vert 0)=p_0(y)$ and $p(y\vert 1)=p_\star(y)$. The vector fields that support this transformation, called the marginal vector fields, can be derived from the conditional ones. Indeed, we have $[\psi(\cdot, t)]_\sharp p_0 = p(\cdot\vert t)$ for the ODE whose vector fields write, for any $y\in\mathbb{R}^d$: $$ \tag{4} f_\star(y\vert t) = \int_{z} f(y \vert z, t) \frac{p_\star(z)p(y\vert z, t)}{p(y\vert t)}dz \;. $$

▶ Proof

Learning objective

There is one issue with (4): it requires computing an integral which involves the unknown $p_\star$. Instead, we will approximate the vector fields $f_\star$ as the solution of a regression problem. Define, for $t\in[0,1]$: $$ \tag{5} \hat{f}(\cdot\vert t) := \argmin_{\tilde f\in\mathcal{F}} \mathbb{E}_{z\sim p_\star}\mathbb{E}_{y\sim p(\cdot\vert z, t)}\Big[ \big\| \tilde f(y\vert t) - f(y\vert z, t)\big\|^2\Big] \;, $$ where $\mathcal{F}$ is some hypothesis class. Observe that we are regressing directly against the (assumed known) conditional vector fields. This is not an issue; one can show that if $f_\star\in\mathcal{F}$, then $\hat{f}=f_\star$. As (5) can easily be approximated by sampling, finding $\hat{f}$ essentially boils down to some form of stochastic gradient descent. We illustrate this plainly in the following section.

▶ Proof
▶ About conditional distributions

Gaussian paths

Let’s take a step back to describe the workflow we have proposed so far to transform $p_0$ to $p_\star$: $$ \text{choose }p(\cdot\vert z, t) \underset{(3)}{\longrightarrow} \text{ find } f(\cdot \vert z, t)\underset{(4)}{\longrightarrow} \text{ compute }f_\star(\cdot\vert t) $$ We have so far assumed that the second step was done for us—this is far from trivial as we do not have a constructive tool for it, only a way to verify a candidate (the continuity equation). In this section, we choose a particular conditional interpolating path which satisfies this assumption. It also births the algorithm that most actually refer to when talking about flow matching.

Let $p_0 = \mathcal{N}(0,\mathbf{I}_d)$ and $\mu, \sigma$ be two real-valued functions, such that $\mu(0)=\sigma(1)=0$ and $\mu(1)=\sigma(0)=1$. Define: $$ \tag{6} p(y\vert z, t) := \mathcal{N}\big(y\vert \mu(t)z, \sigma(t)^2\mathbf{I}_d\big)\;. $$

▶ About denoising diffusion models

The conditional path of (6) is generated by the conditional vector fields:

$$ \tag{7} f(y\vert z, t) := \dot\mu(t) z + \big(y-\mu(t)z\big)\frac{\dot\sigma(t)}{\sigma(t)} \;. $$

▶ Proof
▶ About straight paths

That is enough to give ’the’ flow matching algorithm. Below, we provide some pseudocode that illustrates this; the regression searches over a parametric hypothesis class $\mathcal{F}_\Theta=\{f_\theta : \mathbb{R}^d\times[0,1]\mapsto\mathbb{R}^d, \theta\in\Theta\}$. Compared to (5), the regression loss also includes sampling over the time interval. Observe that the training algorithm is simulation-free; we do not have to simulate the ODE.

$\textbf{init } \text{data } \{z^i\}_{i=1}^n, \text{ functions } \mu, \sigma, \text{ parameter } \theta, \text{ learning rate } \gamma\;.\\$ $\textbf{while } \text{not converged}\\$ $\quad\text{sample } i\in\{1\ldots n\},\\$ $\quad\text{sample } t\sim\mathcal{U}([0, 1]),\\$ $\quad\text{sample } \varepsilon\sim\mathcal{N}(0, \mathbf{I}_d)\\$ $\quad\text{interpolate } y = \mu(t)z^i + \sigma(t)\varepsilon,\\$ $\quad\text{compute target } u = \dot\mu(t)z^i + \dot\sigma(t)\varepsilon,\\$ $\quad\text{update } \theta\leftarrow \theta - \gamma \nabla_\theta\|f_\theta(y\vert t) - u\|_2^2\\$ $\textbf{end while}\\$
$\texttt{Training}$

As described earlier, drawing samples from our learned model $\hat{p}$ boils down to ‘solving’ the ODE induced by the vector fields $f_{\hat\theta}(\cdot\vert t)$. We illustrate below how this can be approximated, with a simple Euler scheme.

$\textbf{init } \text{number of steps } N, \text{ parameter } \hat\theta\;.\\$ $\text{sample } y_0\sim\mathcal{N}(0, \mathbf{I}_d)\\$ $\textbf{for } k=0, \ldots, N-1\\$ $$ y_{k+1} = y_k + \frac{1}{N}f_{\hat\theta}\left(y_k\vert k/N\right)\;. $$ $\textbf{end for}\\$ $\textbf{return } y_N$
$\texttt{Sampling}$

Computing likelihood

We finish this post by showing how one can derive the likelihood of any sample $y_1$ under the flow matching model. This makes it an ’explicit’ generative model (opposed to implicit, which can only produce samples). Denote $g(t) = \log p(y(t)\vert t)$ the log-probability density following a particle—that is, with $y(t)$ satisfying (1). Thanks to the continuity equation, one can show that $g(t)$ also checks a differential equation; for $t\in[0,1]$: $$ \tag{8} \frac{dg}{dt}(t) = - \sum_{i=1}^d \frac{\partial f_i}{\partial y_i}(y(t)\vert t)\;. $$

▶ Proof

This gives us a direct route to computing $p(y_1\vert 1)$ for any $y_1\in\mathbb{R}^d$. We start by solving the original ODE from (1) backward in time, starting from $y(1)=y_1$. This gives us the trajectory $y(\cdot)$ that checks the final condition, and we denote $y_0 = y(0)$ the associated initial condition. We can then solve (8) forward, since $g(0)=\log p(y_0\vert 0)$ is known. This yields $g(1)=\log p(y(1)\vert 1)$ and the desired result.

$\quad$ Above is a $\textit{very}$ inefficient (but somewhat intuitive) protocol. See Section C of [1] for the real deal.
⚠

References

[1] Flow matching for generative modelling. Lipman et al. 2023.
[2] Flow straight and fast. Liu et al. ICLR 2023
[3] Diffusion meets flow matching: two sides of the same coin. Gao et al. 2024
[4] Classifier-free diffusion guidance. Ho & Salimans, 2022.

The structure (and content) for this post was very largely influenced by this course.