Skip to content
DeltaFlow

DeltaFlow

Optimal-transport flow matching in PyTorch. Build generative flows from the displacement between two distributions.

Get started GitHub

PyPI License Downloads GitHub Stars Ask DeepWiki Build Status Docs Python Versions

DeltaFlow

DeltaFlow provides composable PyTorch primitives for flow-based generative modelling: train, sample, and repurpose continuous-time flows on any tensor data, from toy distributions to images and latents.

  • Getting Started

    Install DeltaFlow and fit a first flow-matching model in a few lines.

  • Guides

    Task-oriented walkthroughs covering flow matching, delta alignment, training, and inverse problems.

  • Architecture

    The four base abstractions that make every component drop-in.

  • DiT Walkthrough

    Follow an RGB image through every transformer block with interactive tensor views and linked patch highlights.

  • API Reference

    Auto-generated documentation for every public class and function.

What is DeltaFlow?

DeltaFlow is a modular PyTorch library for flow-based generative modelling. It provides composable building blocks to train, sample, and repurpose continuous-time flows on any data that can be represented as a tensor. A probability path, a coupling, a loss, or a solver can be swapped without rewriting the rest of the pipeline.

It covers:

  • Probability paths (interpolants): linear, variance-preserving, optimal-transport, Schrödinger bridge, and equilibrium interpolants.
  • Training objectives (losses): conditional flow matching, time-unconditioned flow matching, equilibrium matching, signed replay, and delta alignment.
  • Couplings and training (trainer): independent and mini-batch OT pairing of noise and data, and a training loop with mixed precision, gradient accumulation, EMA, checkpointing, and streaming data loaders.
  • Samplers (solvers): Euler and Heun ODE integrators, gradient-descent equilibrium sampling, and posterior sampling.
  • Models (models): wrappers that turn any backbone into a velocity field (time-conditioned or time-blind), a DiT with adaLN-Zero conditioning and classifier-free guidance, EMA, and projector heads for representation learning.
  • Inverse problems (inverse): forward operators, likelihoods, and Tweedie estimates for training-free posterior sampling with a pretrained flow.
  • Generative design (expansion): Active Flow Expansion, which grows the set of samples a flow can generate toward what an external verifier accepts.
  • Utilities (utils): numerical-stability helpers, batch OT permutation, and image I/O.

Everything above is domain-agnostic. Domain-specific code is kept at the edges (datasets, examples). As a worked example, the repository includes 2D radiography pipelines (chest X-ray, cephalometric, and hand radiographs) for data-scarce generative pretraining and landmark detection.

The core idea

DeltaFlow is built on a single idea, a delta (the change carried from one distribution to another) and the flow that realises it.

  • Δ, the change between two things. The learning signal is a displacement \(\Delta = x_1 - x_0\), the straight-line velocity carrying a source sample \(x_0\) (noise) to a data sample \(x_1\). Regressing a velocity field onto \(\Delta\) is exactly the flow-matching objective (Lipman et al., 2023).
  • Flow, integrating the change. A learned field \(v_\theta(x, t)\) is integrated as an ODE \(dx/dt = v_\theta(x, t)\), transporting the whole source distribution onto the data distribution.
  • Straighter Δ via optimal transport. Mini-batch optimal-transport coupling solves the discrete Kantorovich assignment on each batch, the source to target pairing that minimises squared transport cost, so the displacements, and the flow that follows them, are as straight as possible and sample in fewer steps. This hard assignment is the zero-entropy limit of the static Schrödinger bridge (Tong et al., 2023).

The same difference principle also drives an optional representation-learning loss, delta alignment, which aligns the guidance-difference feature \(\Delta h = h_\text{cond} - h_\text{uncond}\) for generative pretraining in data-scarce domains.

Visualizing the algorithm

Flow matching learns a time-conditioned velocity field \(v(x, t)\) that transports a simple source density (Gaussian noise, \(t=0\)) onto the data distribution (\(t=1\)). The panels below come from the sampling-flow example.

Sampling snapshots from noise to data

Animated sampling flow

DiT Walkthrough

Follow a three-channel RGB image from patch embedding through every DiT block to the output velocity. Use Next to step through the layers and Next operation to explore attention and MLP transformations inside each block. Switch between RGB, R, G, and B to inspect the image channels.

See the full DiT Walkthrough for the tensor-shape reference, static figures, and instructions to reproduce the trained model.

Probability paths

The interpolant= argument selects the path noise and data connect along. The same training loop, loss, and solver are reused across all of them.

Interpolant Path
LinearInterpolant straight-line rectified-flow path, \(x_t = (1-t)x_0 + t x_1\)
VariancePreservingInterpolant trigonometric diffusion path, \(\alpha_t = \sin(\tfrac{\pi}{2}t)\), \(\sigma_t = \cos(\tfrac{\pi}{2}t)\)
OTInterpolant the linear path applied after a mini-batch OT re-ordering of \(x_0\)
SchrodingerBridgeInterpolant entropic Schrödinger-bridge path, a Brownian bridge \(x_t = (1-t)x_0 + t x_1 + \sigma\sqrt{t(1-t)}\,z\) around the straight line, diffusivity \(\sigma\)

Schrödinger bridge

The dynamic Schrödinger bridge between noise and data, conditioned on an endpoint pair, is a Brownian bridge around the straight-line path. Regressing onto its conditional velocity trains a stochastic interpolant that recovers the entropic optimal-transport bridge as the coupling of \((x_0, x_1)\) approaches the true OT plan. Pairing endpoints with OTCoupling (rather than drawing them independently) is what makes this approximation tight in practice (De Bortoli et al., 2021 and SF2M, Tong et al., 2024).

Schrödinger-bridge conditional paths

Comparing the paths

Every algorithm is the same training loop. Only the interpolant= (and optionally coupling=) argument changes. Training an identical MLP on a two-moons target with each configuration for the same number of steps makes the differences concrete. All four samplers below are integrated from the same noise batch at the same number of steps.

Animated comparison of all four configurations sampling

Sampled trajectories under each configuration

OT coupling produces the straightest source-to-target trajectories of the four, the practical benefit of minimising batch transport cost before regressing. Reproduce these figures from the algorithm-comparison showcase.

What's inside

Module Role
deltaflow.core Abstract base classes every component subclasses
deltaflow.interpolants Probability paths (linear, mini-batch OT, variance-preserving, Schrödinger bridge)
deltaflow.solvers Euler & Heun integration, posterior sampling
deltaflow.losses Flow matching, signed replay, delta alignment
deltaflow.models Backbones, EMA, multi-scale projector heads
deltaflow.inverse Measurement operators, Tweedie decomposition, likelihoods
deltaflow.expansion Active Flow Expansion, verifier-guided growth of the generable set
deltaflow.trainer Streaming data, mixed-precision loop, OT coupling
deltaflow.datasets Radiograph dataset wrappers (chest / cephalo / hand)

Citation

If DeltaFlow is used in published research, please cite it (see CITATION.cff).