diff --git a/CITATION.cff b/CITATION.cff new file mode 100644 index 0000000..d03931e --- /dev/null +++ b/CITATION.cff @@ -0,0 +1,67 @@ +cff-version: 1.2.0 +message: "If you use this software, please cite both of the following papers." +title: "TorchCFM: Conditional Flow Matching" +abstract: "TorchCFM is a PyTorch library for training continuous normalizing flow models with Conditional Flow Matching (CFM). It implements flow matching variants including optimal transport, Schrodinger bridge, and variance-preserving couplings, with examples for image generation, single-cell dynamics, and tabular data." +authors: + - family-names: Tong + given-names: Alexander + - family-names: Fatras + given-names: Kilian +license: MIT +repository-code: "https://github.com/atong01/conditional-flow-matching" +version: "1.0.7" +date-released: "2024-01-01" +keywords: + - flow-matching + - generative-models + - optimal-transport + - pytorch + - schrodinger-bridges +references: + - type: article + title: "Improving and generalizing flow-based generative models with minibatch optimal transport" + authors: + - family-names: Tong + given-names: Alexander + - family-names: Fatras + given-names: Kilian + - family-names: Malkin + given-names: Nikolay + - family-names: Huguet + given-names: Guillaume + - family-names: Zhang + given-names: Yanlei + - family-names: Rector-Brooks + given-names: Jarrid + - family-names: Wolf + given-names: Guy + - family-names: Bengio + given-names: Yoshua + journal: "Transactions on Machine Learning Research" + year: 2024 + issn: "2835-8856" + url: "https://openreview.net/forum?id=CD9Snc73AW" + doi: "10.48550/arXiv.2302.00482" + - type: article + title: "Simulation-Free Schrödinger Bridges via Score and Flow Matching" + authors: + - family-names: Tong + given-names: Alexander + - family-names: Malkin + given-names: Nikolay + - family-names: Fatras + given-names: Kilian + - family-names: Atanackovic + given-names: Lazar + - family-names: Zhang + given-names: Yanlei + - family-names: Huguet + given-names: Guillaume + - family-names: Wolf + given-names: Guy + - family-names: Bengio + given-names: Yoshua + journal: "arXiv preprint" + year: 2023 + url: "https://arxiv.org/abs/2307.03672" + doi: "10.48550/arXiv.2307.03672" diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..d3609ac --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,83 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned with this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at alex.tong@mail.mcgill.ca. All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of actions. + +**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at [https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..76e030f --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,149 @@ +# Contributing to TorchCFM + +Thanks for your interest in contributing to TorchCFM! This document covers the +basics of setting up a development environment and the process for submitting +changes. By participating, you are expected to uphold the +[Code of Conduct](CODE_OF_CONDUCT.md). + +## Development setup + +TorchCFM targets Python 3.10. The recommended workflow uses conda: + +```bash +# clone your fork +git clone https://github.com//conditional-flow-matching.git +cd conditional-flow-matching + +# create a conda environment with python 3.10 +conda create -n torchcfm python=3.10 +conda activate torchcfm + +# install pytorch following https://pytorch.org/get-started/locally/ + +# install runtime dependencies and the package in editable mode +pip install -r requirements.txt +pip install -e . +``` + +The editable install (`pip install -e .`) lets you iterate on `torchcfm/` +without reinstalling. Most examples live in `examples/` as Jupyter notebooks; to +run them, register the conda env as a kernel: + +```bash +conda install -c anaconda ipykernel +python -m ipykernel install --user --name=torchcfm +``` + +## Code style and pre-commit + +We use [ruff](https://docs.astral.sh/ruff/) for linting and formatting, with a +line length of 99, and [pyupgrade](https://github.com/asottile/pyupgrade). Both +configurations are in [`pyproject.toml`](pyproject.toml) and wired up in +[`.pre-commit-config.yaml`](.pre-commit-config.yaml). After cloning, install the +hooks once: + +```bash +pip install pre-commit +pre-commit install +``` + +Pre-commit will then run automatically on every `git commit`. You can also run +it manually on all files: + +```bash +pre-commit run --all-files +``` + +The ruff configuration selects the `C`, `E`, `F`, `I`, and `W` rule families, +ignores `E501` (line length is enforced by the formatter), and sorts imports +with `isort`. Run the formatter and linter directly with: + +```bash +ruff format . +ruff check --fix . +``` + +## Testing + +Tests live in the `tests/` directory and are run with +[pytest](https://docs.pytest.org/). After installing the package, run the full +suite with: + +```bash +pytest tests/ -v +``` + +Some tests are marked `slow` and can be skipped during quick iteration: + +```bash +pytest tests/ -v -m "not slow" +``` + +Please add or update tests alongside any change to the `torchcfm` package. +Doctests are enabled in [`pyproject.toml`](pyproject.toml) +(`--doctest-modules`), so docstrings in `torchcfm/` are executed as part of the +suite. + +## Pull request process + +1. **Fork** the repository and clone your fork locally (see the dev setup). +2. **Create a branch** from `main` for your change: + ```bash + git checkout -b fix/my-improvement + ``` +3. **Make your changes**, keeping commits focused. Add or update tests as + needed. +4. **Run pre-commit and tests locally** before pushing: + ```bash + pre-commit run --files + pytest tests/ -v + ``` +5. **Push** to your fork and **open a pull request** against `main`. In the PR + description, describe what changed and why, and link any related issues. +6. A maintainer will review your PR. Please address review feedback by pushing + additional commits (avoid force-pushing unless requested). + +## Reporting bugs + +Before opening an issue, please verify that: + +- The problem reproduces on the current `main` branch. +- Your Python and PyTorch versions are recent. +- You are not seeing an already-reported issue (search existing issues first). + +When filing a bug report, include: + +- A minimal reproducible example (code + the exact command you ran). +- Your environment: OS, Python version, PyTorch version, and `torchcfm` version + (see `torchcfm/version.py`). +- The full traceback, if an error is raised. + +## Adding a new flow matching method + +Flow matching losses are implemented as classes in +[`torchcfm/conditional_flow_matching.py`](torchcfm/conditional_flow_matching.py). +All matchers inherit from the base `ConditionalFlowMatcher` and override the +relevant methods (e.g. `compute_mu_t`, `compute_conditional_flow`, +`sample_xt`, `compute_score`). The existing concrete matchers are: + +- `ExactOptimalTransportConditionalFlowMatcher` — minibatch OT coupling (OT-CFM). +- `TargetConditionalFlowMatcher` — Gaussian-to-data flow (Lipman et al. 2023). +- `SchrodingerBridgeConditionalFlowMatcher` — entropic OT / Schrödinger bridge. +- `VariancePreservingConditionalFlowMatcher` — variance-preserving (trigonometric) + interpolation. + +To add a new method: + +1. Subclass `ConditionalFlowMatcher` (or one of its subclasses) in + `conditional_flow_matching.py` and implement the conditional probability + path and vector field. +2. Document the class and its methods with NumPy-style docstrings — these become + doctests. +3. Add a test under `tests/` that checks your matcher produces tensors of the + expected shape and that the loss is finite. +4. Update the README's matcher list and, if applicable, add an example notebook + under `examples/`. + +## Questions + +For questions about usage or suggestions for improvements, please [open an issue](https://github.com/atong01/conditional-flow-matching/issues). diff --git a/README.md b/README.md index abae7fa..572fb62 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ This repository contains the code to reproduce the main experiments and illustra If you find this code useful in your research, please cite the following papers (expand for BibTeX): +This repository is also citable via [`CITATION.cff`](CITATION.cff). See the file for details. +
A. Tong, N. Malkin, G. Huguet, Y. Zhang, J. Rector-Brooks, K. Fatras, G. Wolf, Y. Bengio. Improving and Generalizing Flow-Based Generative Models with Minibatch Optimal Transport, 2023. @@ -187,6 +189,8 @@ The directory structure looks like this: ## ❤️  Code Contributions +Contributions are welcome! Please read our [contributing guidelines](CONTRIBUTING.md) and review the [Code of Conduct](CODE_OF_CONDUCT.md) before getting started. + This toolbox has been created and is maintained by - [Alexander Tong](http://alextong.net)