Optimal Transport for Generative Models

The theory of optimal transport applied to flow models: Wasserstein distances, Monge and Kantorovich formulations, Brenier's theorem, Sinkhorn algorithm, and how OT coupling improves flow matching.

Advanced

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Monge Problem
  5. Kantorovich Relaxation
  6. Wasserstein Distance
  7. Brenier's Theorem
  8. Entropic Regularization & Sinkhorn
  9. Mini-Batch OT for Flow Matching
  10. Dynamic Optimal Transport
  11. Wasserstein Gradient Flows
  12. Common Pitfalls
  13. Summary
  14. Exercises

Learning Objectives

  1. State and compare the Monge and Kantorovich formulations of OT.
  2. Define the Wasserstein-pp distance and its metric properties.
  3. State Brenier's theorem and its connection to convex functions.
  4. Derive the Sinkhorn algorithm for entropic OT.
  5. Apply mini-batch OT to improve flow matching training.

Notation

  • μ,ν\mu, \nu — source and target distributions
  • T:RdRdT: \mathbb{R}^d \to \mathbb{R}^d — transport map
  • γΠ(μ,ν)\gamma \in \Pi(\mu, \nu) — coupling (joint distribution)
  • WpW_p — Wasserstein-pp distance
  • ε\varepsilon — entropic regularization strength

Core Intuition

Optimal transport asks: what's the cheapest way to move mass from one distribution to another? For generative models, this means finding the most efficient mapping from noise to data. OT maps follow straight, non-crossing paths — exactly what flow matching needs for fast generation. The Wasserstein distance gives a geometrically meaningful metric between distributions, unlike KL divergence.

Optimal Transport

W₂ ≈ 28.11
OT plan
1
Source μTarget ν
Explore: Optimal transport finds the minimum-cost coupling between distributions. W₂ distance measures the cost; OT maps are used in flow matching and diffusion bridge methods.

The Monge Problem

Monge (1781): Find a transport map TT pushing μ\mu to ν\nu that minimizes total cost:

infT:T#μ=νxT(x)pdμ(x).(1)\inf_{T: T_\#\mu = \nu} \int \|x - T(x)\|^p d\mu(x). \tag{1}

Constraint: T#μ=νT_\#\mu = \nu means for any set AA: μ(T1(A))=ν(A)\mu(T^{-1}(A)) = \nu(A).

Problem: The Monge map may not exist (e.g., if μ\mu is a Dirac mass and ν\nu is not).


Kantorovich Relaxation

Kantorovich (1942): Relax to probabilistic couplings:

infγΠ(μ,ν)xypdγ(x,y),(2)\inf_{\gamma \in \Pi(\mu, \nu)} \int \|x - y\|^p d\gamma(x, y), \tag{2}

where Π(μ,ν)={γ:π1#γ=μ,π2#γ=ν}\Pi(\mu, \nu) = \{\gamma : \pi_{1\#}\gamma = \mu, \pi_{2\#}\gamma = \nu\}.

Advantages over Monge:

  • Always has a solution (linear program over convex set).
  • Allows mass splitting (one source point can go to multiple targets).
  • Duality theory available.

Kantorovich dual:

Wpp(μ,ν)=sup(ϕ,ψ)Φcϕdμ+ψdν,(3)W_p^p(\mu, \nu) = \sup_{(\phi, \psi) \in \Phi_c} \int\phi\,d\mu + \int\psi\,d\nu, \tag{3}

where Φc={(ϕ,ψ):ϕ(x)+ψ(y)c(x,y)}\Phi_c = \{(\phi, \psi): \phi(x) + \psi(y) \leq c(x,y)\}.


Wasserstein Distance

Definition:

Wp(μ,ν)=(infγΠ(μ,ν)xypdγ(x,y))1/p.(4)\boxed{W_p(\mu, \nu) = \left(\inf_{\gamma \in \Pi(\mu,\nu)} \int \|x-y\|^p d\gamma(x,y)\right)^{1/p}.} \tag{4}

Properties:

  • WpW_p is a metric on probability distributions.
  • Metrizes weak convergence (unlike KL which can be infinite).
  • For p=2p=2: connected to displacement convexity and gradient flows.

W2W_2 for Gaussians (closed form):

W22(N(μ1,Σ1),N(μ2,Σ2))=μ1μ22+tr(Σ1+Σ22(Σ11/2Σ2Σ11/2)1/2).(5)W_2^2(\mathcal{N}(\mu_1, \Sigma_1), \mathcal{N}(\mu_2, \Sigma_2)) = \|\mu_1 - \mu_2\|^2 + \text{tr}\left(\Sigma_1 + \Sigma_2 - 2(\Sigma_1^{1/2}\Sigma_2\Sigma_1^{1/2})^{1/2}\right). \tag{5}

Brenier's Theorem

Theorem (Brenier, 1991): If μ\mu is absolutely continuous and c(x,y)=xy2c(x,y) = \|x-y\|^2, then the optimal transport map exists, is unique, and is the gradient of a convex function:

T(x)=ϕ(x),(6)T^*(x) = \nabla\phi(x), \tag{6}

for some convex ϕ:RdR\phi: \mathbb{R}^d \to \mathbb{R}.

Implication: The OT map is curl-free (irrotational). Paths don't "twist" — they follow the gradient of a potential. This is why OT maps give straight, non-crossing paths.

For generative models: If we could find the Brenier map from N(0,I)\mathcal{N}(0, I) to pdatap_{\text{data}}, we'd have the optimal one-step generator. But computing it exactly is intractable for complex distributions.


Entropic Regularization & Sinkhorn

Entropic OT: Add entropy to make the problem smooth:

minγΠ(μ,ν)cdγ+εH(γ),(7)\min_{\gamma \in \Pi(\mu, \nu)} \int c\,d\gamma + \varepsilon H(\gamma), \tag{7}

where H(γ)=γlogγH(\gamma) = -\int \gamma\log\gamma.

Solution form: γ(x,y)=a(x)ec(x,y)/εb(y)\gamma^*(x, y) = a(x) \cdot e^{-c(x,y)/\varepsilon} \cdot b(y).

Sinkhorn algorithm (discrete case): Given cost matrix CRn×n\mathbf{C} \in \mathbb{R}^{n \times n}, kernel K=eC/ε\mathbf{K} = e^{-\mathbf{C}/\varepsilon}:

ua/(Kv),vb/(KTu).(8)\mathbf{u} \leftarrow \mathbf{a} / (\mathbf{K}\mathbf{v}), \quad \mathbf{v} \leftarrow \mathbf{b} / (\mathbf{K}^T\mathbf{u}). \tag{8}

Iterate until convergence. Transport plan: γij=uiKijvj\gamma_{ij} = u_i K_{ij} v_j.

Cost: O(n2)O(n^2) per iteration (matrix-vector products). Converges in O(1/ε)O(1/\varepsilon) iterations.


Mini-Batch OT for Flow Matching

Problem: Full OT between p0p_0 and p1p_1 is intractable. But we can approximate within each training batch.

Mini-batch OT (Tong et al., 2023):

  1. Sample batch: {x0i}i=1Bp0\{x_0^i\}_{i=1}^B \sim p_0, {x1j}j=1Bp1\{x_1^j\}_{j=1}^B \sim p_1.
  2. Compute cost matrix: Cij=x0ix1j2C_{ij} = \|x_0^i - x_1^j\|^2.
  3. Solve assignment: π=argminπiCi,π(i)\pi^* = \arg\min_\pi \sum_i C_{i,\pi(i)} (Hungarian, O(B3)O(B^3)).
  4. Train on paired batch: (x0i,x1π(i))(x_0^i, x_1^{\pi^*(i)}).

Effect on training:

  • Without OT: paths cross, velocity is multi-valued, need many sampling steps.
  • With mini-batch OT: paths approximately non-crossing within each batch, straighter learned flow.

Quality improvement: 2-5 FID points better, especially at few sampling steps.


Dynamic Optimal Transport

Benamou-Brenier formulation: OT as a fluid dynamics problem:

W22(μ,ν)=inf(ρt,vt)01vt(x)2ρt(x)dxdt,(9)W_2^2(\mu, \nu) = \inf_{(\rho_t, v_t)} \int_0^1 \int \|v_t(x)\|^2 \rho_t(x)\,dx\,dt, \tag{9}

subject to the continuity equation tρ+(ρv)=0\partial_t\rho + \nabla\cdot(\rho v) = 0 with ρ0=μ,ρ1=ν\rho_0 = \mu, \rho_1 = \nu.

Connection to flow matching: The OT velocity field vtv_t^* gives the kinetic-energy-minimizing flow. This is exactly what we want: minimum-energy paths = straight paths = few-step generation.


Wasserstein Gradient Flows

Many processes in ML can be viewed as gradient flows in Wasserstein space:

tρ=(ρδFδρ),(10)\partial_t\rho = \nabla\cdot\left(\rho\nabla\frac{\delta\mathcal{F}}{\delta\rho}\right), \tag{10}

where F\mathcal{F} is a functional (energy).

Examples:

  • F=KL(ρp)\mathcal{F} = \text{KL}(\rho\|p^*): converges to target pp^* (Langevin dynamics).
  • F=H(ρ)\mathcal{F} = H(\rho) (entropy): heat equation.
  • F=W22(ρ,ν)\mathcal{F} = W_2^2(\rho, \nu): displacement interpolation.

Common Pitfalls

Pitfall 1. Applying full OT (Hungarian algorithm) to large batches. O(B3)O(B^3) is expensive; use Sinkhorn with ε>0\varepsilon > 0 for B>256B > 256.

Pitfall 2. Setting entropic regularization ε\varepsilon too large. The coupling becomes too blurry (close to independent); OT benefit vanishes. Use ε0.01\varepsilon \approx 0.010.10.1 relative to cost scale.

Pitfall 3. Assuming mini-batch OT equals global OT. It's an approximation; pairs within a batch don't guarantee global optimality. Use larger batches for better approximation.


Summary

  • Monge problem: Find cheapest map TT from μ\mu to ν\nu.
  • Kantorovich relaxation: Probabilistic couplings; always solvable.
  • Wasserstein distance: Geometry-aware metric on distributions.
  • Brenier's theorem: OT map = gradient of convex function (no crossing).
  • Sinkhorn: Efficient entropic OT via alternating scaling.
  • Mini-batch OT improves flow matching by reducing path crossing.
  • Dynamic OT connects to minimum-energy flows.

Exercises

Exercise 1. Compute W2W_2 between N(0,1)\mathcal{N}(0, 1) and N(3,4)\mathcal{N}(3, 4) using the closed-form Gaussian formula.

Exercise 2. Implement Sinkhorn for two empirical distributions of 100 points each in 2D. Visualize the transport plan.

Exercise 3. Prove that the Brenier map between two 1D distributions is the quantile function composition: T=Fν1FμT = F_\nu^{-1} \circ F_\mu.

Exercise 4. For mini-batch OT with B=64B=64: compare the computational cost of Hungarian vs Sinkhorn (50 iterations) algorithms.

Exercise 5. Show that the displacement interpolation ρt=[(1t)Id+tT]#μ\rho_t = [(1-t)\text{Id} + tT]_\# \mu satisfies the continuity equation with constant velocity field v(x)=T(x)xv(x) = T(x) - x.