lucidrains/rectified-flow-pytorch

Implementation of rectified flow and some of its followup research / improvements in Pytorch

rectified‑flow‑pytorch – PyTorch implementation of Rectified Flow

What it is – A small, pip‑installable library that reproduces the Rectified Flow generative‑model technique (Liu et al., 2022) and several follow‑up improvements. It provides a ready‑to‑use RectifiedFlow class, a simple UNet backbone, utilities for loading image datasets, and a trainer built on 🤗 Accelerate.

Why it matters – Rectified Flow is a flow‑matching / diffusion‑like method that learns a straight mapping from noise to data in a single step, offering fast sampling and competitive image quality. This repo lets researchers and hobbyists experiment with the method without writing the math from scratch.


Quick start

pip install rectified-flow-pytorch          # core library
pip install .[examples]                     # optional example dependencies
import torch
from rectified_flow_pytorch import RectifiedFlow, Unet

# a tiny UNet backbone (you can swap in any torch.nn.Module)
model = Unet(dim=64)

# wrap it in the RectifiedFlow wrapper
flow = RectifiedFlow(model)

# dummy data – shape (batch, 3, 256, 256)
imgs = torch.randn(1, 3, 256, 256)

# forward pass returns a loss that can be back‑propagated
loss = flow(imgs)
loss.backward()

# generate a sample (same shape as the input images)
sample = flow.sample()
assert sample.shape[1:] == imgs.shape[1:]

Re‑flow (iterative refinement)

The library also ships a Reflow wrapper that lets you apply the learned flow repeatedly, as described in the original paper.

from rectified_flow_pytorch import Reflow

reflow = Reflow(flow)
reflow_loss = reflow()
reflow_loss.backward()

sample = reflow.sample()

Training on your own images

from rectified_flow_pytorch import ImageDataset, Trainer

train_set = ImageDataset(folder='./my_images', image_size=256)
trainer = Trainer(
    flow,
    dataset=train_set,
    num_train_steps=70_000,
    results_folder='./results'   # samples saved periodically
)
trainer()   # starts the training loop (uses accelerate under the hood)

What’s inside?

Component Role
RectifiedFlow Core loss and sampler implementing the straight‑flow objective.
Reflow Convenience wrapper for applying the flow multiple times (the “re‑flow” procedure).
Unet Minimal UNet architecture used as the default backbone; fully configurable via the dim argument.
ImageDataset Simple torch.utils.data.Dataset that loads all images from a folder and resizes them to a square size.
Trainer Training loop built on 🤗 Accelerate; handles optimizer, learning‑rate schedule, checkpointing, and periodic image saving.

Example script

The repo ships train_oxford.py, which trains a model on the Oxford‑Flowers dataset (32‑batch, 11 k steps). After installing the extra examples dependencies you can run:

python train_oxford.py

The script demonstrates dataset loading, trainer configuration, and how to visualise generated samples.


References & related work

The README lists the original Rectified Flow paper and a handful of newer arXiv works that extend or analyze flow‑matching methods (e.g., Improving the Training of Rectified Flows, Scaling Rectified Flow Transformers, Consistency Flow Matching). These citations help users locate the academic context and explore advanced variants.


Who might use this?

  • Researchers experimenting with flow‑matching, diffusion alternatives, or one‑step generative models.
  • Practitioners who need a fast‑sampling image generator and want a lightweight PyTorch implementation.
  • Students looking for a concise codebase to study the mathematics of Rectified Flow.

Bottom line

rectified-flow-pytorch is a focused, well‑documented implementation of a cutting‑edge generative‑model technique. With a single pip install you get a usable model, training utilities, and example scripts, making it easy to prototype and benchmark Rectified Flow on your own image data.

Related

  • Project
  • Project
  • Project
  • Project
  • Project