Conditional Flow Matching
Alexander Tong, Nikolay Malkin, Guillaume Huguet, Yanlei Zhang, Jarrid Rector-Brooks, Kilian Fatras, Guy Wolf, Yoshua Bengio
Description
Conditional Flow Matching is a fast way to train Continuous Normalizing Flow (CNF) models. CFM is a simulation-free training objective for continuous normalizing flows that allows conditional generative modeling and speeds up training and inference. See this http link for the preprint.
Under this general framework we introduce Optimal Transport Conditional Flow Matching (OT-CFM). OT-CFM creates dynamical optimal transport flows between marginal distributions by introducing an optimal transport condition.
Examples
The density, vector field, and trajectories of simulation-free CNF training schemes. The first two methods variance preserving SDE (VP-SDE) and Flow Matching (FM) require a gaussian source distribution so do not appear in the above example mapping 8 Gaussians to the two moons dataset. Training action matching with the same architecture (3x64 MLP with SeLU activations) underfits however, with a Relu SiLU and SiLU activations as suggested in the example code it seems to fit better under our training setup (Action-Matching (Swish). The models to produce the GIF are stored in notebooks/models
and can be visualized with this .
Relevant Related Works
There are many interesting related works to check out on simulation free training of flow models including:
- Flow Matching for Generative Modeling (Lipman et al. 2023) Paper
- Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow (Liu et al. 2023) Paper
- Building Normalizing Flows with Stochastic Interpolants (Albergo et al. 2023) Paper
- Action Matching: Learning Stochastic Dynamics From Samples (Neklyudov et al. 2022) Paper Code
- Riemannian Flow Matching on General Geometries (Chen et al. 2023) Paper
Code Contributions
This repo is extracted from a larger private codebase which loses the original commit history which contains work from other authors on the paper.
How to run
Run a simple minimal example here . Or install the more efficient code locally with these steps.
Install dependencies
# clone project
git clone https://github.com/atong01/conditional-flow-matching.git
cd conditional-flow-matching
# [OPTIONAL] create conda environment
conda create -n myenv python=3.10
conda activate myenv
# install pytorch according to instructions
# https://pytorch.org/get-started/
# install requirements
pip install -r requirements.txt
Train model with default configuration
# train on CPU
python src/train.py trainer=cpu
# train on GPU
python src/train.py trainer=gpu
Train model with chosen experiment configuration from configs/experiment/
python src/train.py experiment=experiment_name
You can override any parameter from command line like this
python src/train.py trainer.max_epochs=20 datamodule.batch_size=64
You can also train a large set of models in parallel with SLURM as shown in scripts/two-dim-cfm.sh
which trains the models used in the first 3 lines of Table 2.
Project Structure
The directory structure of new project looks like this:
βββ configs <- Hydra configuration files
β βββ callbacks <- Callbacks configs
β βββ datamodule <- Datamodule configs
β βββ debug <- Debugging configs
β βββ experiment <- Experiment configs
β βββ extras <- Extra utilities configs
β βββ hparams_search <- Hyperparameter search configs
β βββ hydra <- Hydra configs
β βββ launcher <- Hydra launcher configs
β βββ local <- Local configs
β βββ logger <- Logger configs
β βββ model <- Model configs
β βββ paths <- Project paths configs
β βββ trainer <- Trainer configs
β β
β βββ eval.yaml <- Main config for evaluation
β βββ train.yaml <- Main config for training
β
βββ data <- Project data
β
βββ logs <- Logs generated by hydra and lightning loggers
β
βββ notebooks <- Jupyter notebooks. Naming convention is a number (for ordering),
β the creator's initials, and a short `-` delimited description,
β e.g. `1.0-jqp-initial-data-exploration.ipynb`.
β
βββ scripts <- Shell scripts
β
βββ src <- Source code
β βββ datamodules <- Lightning datamodules
β βββ models <- Lightning models
β βββ utils <- Utility scripts
β β
β βββ eval.py <- Run evaluation
β βββ train.py <- Run training
β
βββ tests <- Tests of any kind
β
βββ .gitignore <- List of files ignored by git
βββ .pre-commit-config.yaml <- Configuration of pre-commit hooks for code formatting
βββ pyproject.toml <- Configuration options for testing and linting
βββ requirements.txt <- File for installing python dependencies
βββ setup.py <- File for installing project as a package
βββ README.md
β‘ Β Β Your Superpowers
Override any config parameter from command line
python train.py trainer.max_epochs=20 model.optimizer.lr=1e-4
Note: You can also add new parameters with
+
sign.
python train.py +model.new_param="owo"
Train on CPU, GPU, multi-GPU and TPU
# train on CPU
python train.py trainer=cpu
# train on 1 GPU
python train.py trainer=gpu
# train on TPU
python train.py +trainer.tpu_cores=8
# train with DDP (Distributed Data Parallel) (4 GPUs)
python train.py trainer=ddp trainer.devices=4
# train with DDP (Distributed Data Parallel) (8 GPUs, 2 nodes)
python train.py trainer=ddp trainer.devices=4 trainer.num_nodes=2
# simulate DDP on CPU processes
python train.py trainer=ddp_sim trainer.devices=2
# accelerate training on mac
python train.py trainer=mps
Warning: Currently there are problems with DDP mode, read this issue to learn more.
Train with mixed precision
# train with pytorch native automatic mixed precision (AMP)
python train.py trainer=gpu +trainer.precision=16
Train model with any logger available in PyTorch Lightning, like W&B or Tensorboard
# set project and entity names in `configs/logger/wandb`
wandb:
project: "your_project_name"
entity: "your_wandb_team_name"
# train model with Weights&Biases (link to wandb dashboard should appear in the terminal)
python train.py logger=wandb
Note: Lightning provides convenient integrations with most popular logging frameworks. Learn more here.
Note: Using wandb requires you to setup account first. After that just complete the config as below.
Note: Click here to see example wandb dashboard generated with this template.
Train model with chosen experiment config
python train.py experiment=example
Note: Experiment configs are placed in configs/experiment/.
Attach some callbacks to run
python train.py callbacks=default
Note: Callbacks can be used for things such as as model checkpointing, early stopping and many more.
Note: Callbacks configs are placed in configs/callbacks/.
Use different tricks available in Pytorch Lightning
# gradient clipping may be enabled to avoid exploding gradients
python train.py +trainer.gradient_clip_val=0.5
# run validation loop 4 times during a training epoch
python train.py +trainer.val_check_interval=0.25
# accumulate gradients
python train.py +trainer.accumulate_grad_batches=10
# terminate training after 12 hours
python train.py +trainer.max_time="00:12:00:00"
Note: PyTorch Lightning provides about 40+ useful trainer flags.
Easily debug
# runs 1 epoch in default debugging mode
# changes logging directory to `logs/debugs/...`
# sets level of all command line loggers to 'DEBUG'
# enforces debug-friendly configuration
python train.py debug=default
# run 1 train, val and test loop, using only 1 batch
python train.py debug=fdr
# print execution time profiling
python train.py debug=profiler
# try overfitting to 1 batch
python train.py debug=overfit
# raise exception if there are any numerical anomalies in tensors, like NaN or +/-inf
python train.py +trainer.detect_anomaly=true
# log second gradient norm of the model
python train.py +trainer.track_grad_norm=2
# use only 20% of the data
python train.py +trainer.limit_train_batches=0.2 \
+trainer.limit_val_batches=0.2 +trainer.limit_test_batches=0.2
Note: Visit configs/debug/ for different debugging configs.
Resume training from checkpoint
python train.py ckpt_path="/path/to/ckpt/name.ckpt"
Note: Checkpoint can be either path or URL.
Note: Currently loading ckpt doesn't resume logger experiment, but it will be supported in future Lightning release.
Evaluate checkpoint on test dataset
python eval.py ckpt_path="/path/to/ckpt/name.ckpt"
Note: Checkpoint can be either path or URL.
Create a sweep over hyperparameters
# this will run 6 experiments one after the other,
# each with different combination of batch_size and learning rate
python train.py -m datamodule.batch_size=32,64,128 model.lr=0.001,0.0005
Note: Hydra composes configs lazily at job launch time. If you change code or configs after launching a job/sweep, the final composed configs might be impacted.
Create a sweep over hyperparameters with Optuna
# this will run hyperparameter search defined in `configs/hparams_search/mnist_optuna.yaml`
# over chosen experiment config
python train.py -m hparams_search=mnist_optuna experiment=example
Note: Using Optuna Sweeper doesn't require you to add any boilerplate to your code, everything is defined in a single config file.
Warning: Optuna sweeps are not failure-resistant (if one job crashes then the whole sweep crashes).
Execute all experiments from folder
python train.py -m 'experiment=glob(*)'
Note: Hydra provides special syntax for controlling behavior of multiruns. Learn more here. The command above executes all experiments from configs/experiment/.
Execute run for multiple different seeds
python train.py -m seed=1,2,3,4,5 trainer.deterministic=True logger=csv tags=["benchmark"]
Note:
trainer.deterministic=True
makes pytorch more deterministic but impacts the performance.
Execute sweep on a remote AWS cluster
Note: This should be achievable with simple config using Ray AWS launcher for Hydra. Example is not implemented in this template.
Use Hydra tab completion
Note: Hydra allows you to autocomplete config argument overrides in shell as you write them, by pressing
tab
key. Read the docs.
Apply pre-commit hooks
pre-commit run -a
Note: Apply pre-commit hooks to do things like auto-formatting code and configs, performing code analysis or removing output from jupyter notebooks. See # Best Practices for more.
Run tests
# run all tests
pytest
# run tests from specific file
pytest tests/test_train.py
# run all tests except the ones marked as slow
pytest -k "not slow"
Use tags
Each experiment should be tagged in order to easily filter them across files or in logger UI:
python train.py tags=["mnist","experiment_X"]
If no tags are provided, you will be asked to input them from command line:
>>> python train.py tags=[]
[2022-07-11 15:40:09,358][src.utils.utils][INFO] - Enforcing tags! <cfg.extras.enforce_tags=True>
[2022-07-11 15:40:09,359][src.utils.rich_utils][WARNING] - No tags provided in config. Prompting user to input tags...
Enter a list of comma separated tags (dev):
If no tags are provided for multirun, an error will be raised:
>>> python train.py -m +x=1,2,3 tags=[]
ValueError: Specify tags before launching a multirun!
Note: Appending lists from command line is currently not supported in hydra :(
β€οΈ Β Β Contributions
Have a question? Found a bug? Missing a specific feature? Feel free to file a new issue, discussion or PR with respective title and description.
Before making an issue, please verify that:
- The problem still exists on the current
main
branch. - Your python dependencies are updated to recent versions.
Suggestions for improvements are always welcome!
License
Conditional-Flow-Matching is licensed under the MIT License.
MIT License
Copyright (c) 2023 Alexander Tong
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.