Metadata-Version: 2.4
Name: rectified-flow-pytorch
Version: 0.9.14
Summary: Rectified Flow in Pytorch
Project-URL: Homepage, https://pypi.org/project/rectified-flow-pytorch/
Project-URL: Repository, https://codeberg.org/lucidrains/rectified-flow-pytorch
Author-email: Phil Wang <lucidrains@gmail.com>
License: MIT License
        
        Copyright (c) 2024 Phil Wang
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: artificial intelligence,deep learning,rectified flow
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: accelerate
Requires-Dist: einops>=0.8.0
Requires-Dist: einx>=0.3.0
Requires-Dist: ema-pytorch>=0.5.2
Requires-Dist: hyper-connections>=0.1.8
Requires-Dist: pillow>=9.4.0
Requires-Dist: rotary-embedding-torch>=0.8.9
Requires-Dist: scipy
Requires-Dist: torch-einops-utils>=0.0.30
Requires-Dist: torch>=2.0
Requires-Dist: torchdiffeq
Requires-Dist: torchvision
Requires-Dist: x-transformers>=2.3.21
Provides-Extra: examples
Requires-Dist: datasets; extra == 'examples'
Provides-Extra: examples-fpo
Requires-Dist: adam-atan2-pytorch; extra == 'examples-fpo'
Requires-Dist: assoc-scan>=0.0.2; extra == 'examples-fpo'
Requires-Dist: box2d-py; extra == 'examples-fpo'
Requires-Dist: einops>=0.8.0; extra == 'examples-fpo'
Requires-Dist: einx>=0.3.0; extra == 'examples-fpo'
Requires-Dist: ema-pytorch>=0.7.3; extra == 'examples-fpo'
Requires-Dist: fire; extra == 'examples-fpo'
Requires-Dist: gymnasium[box2d]>=1.0.0; extra == 'examples-fpo'
Requires-Dist: hl-gauss-pytorch>=0.1.15; extra == 'examples-fpo'
Requires-Dist: hyper-connections>=0.2.0; extra == 'examples-fpo'
Requires-Dist: lion-pytorch; extra == 'examples-fpo'
Requires-Dist: moviepy>=1.0.3; extra == 'examples-fpo'
Requires-Dist: numpy>=2.2.5; extra == 'examples-fpo'
Requires-Dist: torch>=2.4; extra == 'examples-fpo'
Requires-Dist: tqdm; extra == 'examples-fpo'
Requires-Dist: x-transformers>=2.3.21; extra == 'examples-fpo'
Provides-Extra: examples-ql
Requires-Dist: assoc-scan; extra == 'examples-ql'
Requires-Dist: box2d-py; extra == 'examples-ql'
Requires-Dist: fire; extra == 'examples-ql'
Requires-Dist: gymnasium[box2d]>=1.0.0; extra == 'examples-ql'
Requires-Dist: moviepy>=1.0.3; extra == 'examples-ql'
Requires-Dist: numpy>=2.2.5; extra == 'examples-ql'
Requires-Dist: tqdm; extra == 'examples-ql'
Requires-Dist: x-mlps-pytorch>=0.0.20; extra == 'examples-ql'
Provides-Extra: test
Requires-Dist: discrete-continuous-embed-readout; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Description-Content-Type: text/markdown

<img src="./rf.png" width="400px"></img>

## Rectified Flow - Pytorch

Implementation of <a href="https://www.cs.utexas.edu/~lqiang/rectflow/html/intro.html">rectified flow</a> and some of its followup research / improvements in Pytorch

<a href="https://drscotthawley.github.io/blog/posts/FlowModels.html">Tutorial</a> from <a href="https://github.com/drscotthawley">Dr. Scott Hawley</a>

Youtube AI Educators - <a href="https://www.youtube.com/watch?v=7NNxK3CqaDk">Yannic</a> | <a href="https://www.youtube.com/watch?v=7cMzfkWFWhI">Outlier</a>

<img src="./images/oxford-flowers.sample.png" width="350px"></img>

*32 batch size, 11k steps oxford flowers*

## Appreciation

- [Lucas](https://github.com/lucasnewman) for the LSD flow, SoFlow, and Split Mean Flow implementations!

- [Pranoy](https://github.com/pranoyr) for the Unconstrained Alignment (UA) Flow implementation, as well as various bug fixes!

## Install

```bash
$ pip install rectified-flow-pytorch
```

## Usage

```python
import torch
from rectified_flow_pytorch import RectifiedFlow, Unet

model = Unet(dim = 64)

rectified_flow = RectifiedFlow(model)

images = torch.randn(1, 3, 256, 256)

loss = rectified_flow(images)
loss.backward()

sampled = rectified_flow.sample()
assert sampled.shape[1:] == images.shape[1:]
```

For reflow as described in the paper

```python
import torch
from rectified_flow_pytorch import RectifiedFlow, Reflow, Unet

model = Unet(dim = 64)

rectified_flow = RectifiedFlow(model)

images = torch.randn(1, 3, 256, 256)

loss = rectified_flow(images)
loss.backward()

# do the above for many real images

reflow = Reflow(rectified_flow)

reflow_loss = reflow()
reflow_loss.backward()

# then do the above in a loop many times for reflow - you can reflow multiple times by redefining Reflow(reflow.model) and looping again

sampled = reflow.sample()
assert sampled.shape[1:] == images.shape[1:]
```

With a `Trainer` based on `accelerate`

```python
import torch
from rectified_flow_pytorch import RectifiedFlow, ImageDataset, Unet, Trainer

model = Unet(dim = 64)

rectified_flow = RectifiedFlow(model)

img_dataset = ImageDataset(
    folder = './path/to/your/images',
    image_size = 256
)

trainer = Trainer(
    rectified_flow,
    dataset = img_dataset,
    num_train_steps = 70_000,
    results_folder = './results'   # samples will be saved periodically to this folder
)

trainer()
```

## Examples

Quick test on oxford flowers

```bash
$ pip install .[examples]
```

Then

```bash
$ python train_oxford.py
```

## Citations

```bibtex
@article{Liu2022FlowSA,
    title   = {Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow},
    author  = {Xingchao Liu and Chengyue Gong and Qiang Liu},
    journal = {ArXiv},
    year    = {2022},
    volume  = {abs/2209.03003},
    url     = {https://api.semanticscholar.org/CorpusID:252111177}
}
```

```bibtex
@article{Lee2024ImprovingTT,
    title   = {Improving the Training of Rectified Flows},
    author  = {Sangyun Lee and Zinan Lin and Giulia Fanti},
    journal = {ArXiv},
    year    = {2024},
    volume  = {abs/2405.20320},
    url     = {https://api.semanticscholar.org/CorpusID:270123378}
}
```

```bibtex
@article{Esser2024ScalingRF,
    title   = {Scaling Rectified Flow Transformers for High-Resolution Image Synthesis},
    author  = {Patrick Esser and Sumith Kulal and A. Blattmann and Rahim Entezari and Jonas Muller and Harry Saini and Yam Levi and Dominik Lorenz and Axel Sauer and Frederic Boesel and Dustin Podell and Tim Dockhorn and Zion English and Kyle Lacey and Alex Goodwin and Yannik Marek and Robin Rombach},
    journal = {ArXiv},
    year    = {2024},
    volume  = {abs/2403.03206},
    url     = {https://api.semanticscholar.org/CorpusID:268247980}
}
```

```bibtex
@article{Li2024ImmiscibleDA,
    title   = {Immiscible Diffusion: Accelerating Diffusion Training with Noise Assignment},
    author  = {Yiheng Li and Heyang Jiang and Akio Kodaira and Masayoshi Tomizuka and Kurt Keutzer and Chenfeng Xu},
    journal = {ArXiv},
    year    = {2024},
    volume  = {abs/2406.12303},
    url     = {https://api.semanticscholar.org/CorpusID:270562607}
}
```

```bibtex
@article{Yang2024ConsistencyFM,
    title   = {Consistency Flow Matching: Defining Straight Flows with Velocity Consistency},
    author  = {Ling Yang and Zixiang Zhang and Zhilong Zhang and Xingchao Liu and Minkai Xu and Wentao Zhang and Chenlin Meng and Stefano Ermon and Bin Cui},
    journal = {ArXiv},
    year    = {2024},
    volume  = {abs/2407.02398},
    url     = {https://api.semanticscholar.org/CorpusID:270878436}
}
```

```bibtex
@article{Zhu2024HyperConnections,
    title   = {Hyper-Connections},
    author  = {Defa Zhu and Hongzhi Huang and Zihao Huang and Yutao Zeng and Yunyao Mao and Banggu Wu and Qiyang Min and Xun Zhou},
    journal = {ArXiv},
    year    = {2024},
    volume  = {abs/2409.19606},
    url     = {https://api.semanticscholar.org/CorpusID:272987528}
}
```

```bibtex
@inproceedings{Sun2025F5RTTSIF,
    title   = {F5R-TTS: Improving Flow-Matching based Text-to-Speech with Group Relative Policy Optimization},
    author  = {Xiaohui Sun and Ruitong Xiao and Jianye Mo and Bowen Wu and Qun Yu and Baoxun Wang},
    year    = {2025},
    url     = {https://api.semanticscholar.org/CorpusID:277510064}
}
```

```bibtex
@inproceedings{Geng2025MeanFF,
    title   = {Mean Flows for One-step Generative Modeling},
    author  = {Zhengyang Geng and Mingyang Deng and Xingjian Bai and J. Zico Kolter and Kaiming He},
    year    = {2025},
    url     = {https://api.semanticscholar.org/CorpusID:278769814}
}
```

```bibtex
@article{Sun2025IsNC,
    title   = {Is Noise Conditioning Necessary for Denoising Generative Models?},
    author  = {Qiao Sun and Zhicheng Jiang and Hanhong Zhao and Kaiming He},
    journal = {ArXiv},
    year    = {2025},
    volume  = {abs/2502.13129},
    url     = {https://api.semanticscholar.org/CorpusID:276421559}
}
```

```bibtex
@article{Park2025FlowQ,
    title   = {Flow Q-Learning},
    author  = {Seohong Park and Qiyang Li and Sergey Levine},
    journal = {ArXiv},
    year    = {2025},
    volume  = {abs/2502.02538},
    url     = {https://api.semanticscholar.org/CorpusID:276107180}
}
```

```bibtex
@misc{mcallister2025flowmatchingpolicygradients,
    title   = {Flow Matching Policy Gradients},
    author  = {David McAllister and Songwei Ge and Brent Yi and Chung Min Kim and Ethan Weber and Hongsuk Choi and Haiwen Feng and Angjoo Kanazawa},
    year    = {2025},
    eprint  = {2507.21053},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2507.21053},
}
```

```bibtex
@misc{li2025basicsletdenoisinggenerative,
    title   = {Back to Basics: Let Denoising Generative Models Denoise},
    author  = {Tianhong Li and Kaiming He},
    year    = {2025},
    eprint  = {2511.13720},
    archivePrefix = {arXiv},
    primaryClass = {cs.CV},
    url     = {https://arxiv.org/abs/2511.13720},
}
```

```bibtex
@misc{clavier2024bootstrappingexpectilesreinforcementlearning,
    title   = {Bootstrapping Expectiles in Reinforcement Learning},
    author  = {Pierre Clavier and Emmanuel Rachelson and Erwan Le Pennec and Matthieu Geist},
    year    = {2024},
    eprint  = {2406.04081},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2406.04081},
}
```

```bibtex
@inproceedings{anonymous2025flow,
    title   = {Flow Policy Gradients for Legged Robots},
    author  = {Anonymous},
    booktitle = {Submitted to The Fourteenth International Conference on Learning Representations},
    year    = {2025},
    url     = {https://openreview.net/forum?id=BA6n0nmagi},
    note    = {under review}
}
```

```bibtex
@misc{luo2025soflowsolutionflowmodels,
    title   = {SoFlow: Solution Flow Models for One-Step Generative Modeling},
    author  = {Tianze Luo and Haotian Yuan and Zhuang Liu},
    year    = {2025},
    eprint  = {2512.15657},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2512.15657},
}
```

```bibtex
@misc{wang2025equilibriummatchinggenerativemodeling,
    title   = {Equilibrium Matching: Generative Modeling with Implicit Energy-Based Models},
    author  = {Runqian Wang and Yilun Du},
    year    = {2025},
    eprint  = {2510.02300},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2510.02300},
}
```

```bibtex
@misc{boffi2025buildconsistencymodellearning,
    title   = {How to build a consistency model: Learning flow maps via self-distillation},
    author  = {Nicholas M. Boffi and Michael S. Albergo and Eric Vanden-Eijnden},
    year    = {2025},
    eprint  = {2505.18825},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2505.18825},
}
```

```bibtex
@misc{chefer2026self,
    title   = {Self-Supervised Flow Matching for Scalable Multi-Modal Synthesis},
    author  = {Hila Chefer and Patrick Esser and Dominik Lorenz and Dustin Podell and Vikash Raja and Vinh Tong and Antonio Torralba and Robin Rombach},
    year    = {2026},
    url     = {https://bfl.ai/research/self-flow},
    note    = {Preprint}
}
```

```bibtex
@misc{farebrother2025temporaldifferenceflows,
    title   = {Temporal Difference Flows},
    author  = {Jesse Farebrother and Matteo Pirotta and Andrea Tirinzoni and Rémi Munos and Alessandro Lazaric and Ahmed Touati},
    year    = {2025},
    eprint  = {2503.09817},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2503.09817},
}
```

```bibtex
@misc{farebrother2026compositionalplanningjumpyworld,
    title   = {Compositional Planning with Jumpy World Models},
    author  = {Jesse Farebrother and Matteo Pirotta and Andrea Tirinzoni and Marc G. Bellemare and Alessandro Lazaric and Ahmed Touati},
    year    = {2026},
    eprint  = {2602.19634},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2602.19634},
}
```

```bibtex
@inproceedings{dong2026valueflows,
    title   = {Value Flows},
    author  = {Perry Dong and Chongyi Zheng and Chelsea Finn and Dorsa Sadigh and Benjamin Eysenbach},
    booktitle = {International Conference on Learning Representations},
    year    = {2026},
    url     = {https://pd-perry.github.io/value-flows}
}
```
