forked from openai/consistency_models
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3d86141
Showing
30 changed files
with
6,519 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Compiled python modules. | ||
*.pyc | ||
|
||
# Byte-compiled | ||
_pycache__/ | ||
.cache/ | ||
.idea/ | ||
|
||
# Python egg metadata, regenerated from source files by setuptools. | ||
/*.egg-info | ||
.eggs/ | ||
|
||
# PyPI distribution artifacts. | ||
build/ | ||
dist/ | ||
|
||
# Tests | ||
.pytest_cache/ | ||
|
||
# Other | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 OpenAI | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Consistency Models | ||
|
||
This repository contains the codebase for [Consistency Models](https://arxiv.org/abs/2303.01469), implemented using PyTorch for conducting large-scale experiments on ImageNet-64, LSUN Bedroom-256, and LSUN Cat-256. We have based our repository on [openai/guided-diffusion](https://github.com/openai/guided-diffusion), which was initially released under the MIT license. Our modifications have enabled support for consistency distillation, consistency training, as well as several sampling and editing algorithms discussed in the paper. | ||
|
||
The repository for CIFAR-10 experiments is in JAX and will be released separately. | ||
|
||
# Pre-trained models | ||
|
||
We have released checkpoints for the main models in the paper. Before using these models, please review the corresponding [model card](model-card.md) to understand the intended use and limitations of these models. | ||
|
||
Here are the download links for each model checkpoint: | ||
|
||
* EDM on ImageNet-64: [edm_imagenet64_ema.pt](https://openaipublic.blob.core.windows.net/consistency/edm_imagenet64_ema.pt) | ||
* CD on ImageNet-64 with l2 metric: [cd_imagenet64_l2.pt](https://openaipublic.blob.core.windows.net/consistency/cd_imagenet64_l2.pt) | ||
* CD on ImageNet-64 with LPIPS metric: [cd_imagenet64_lpips.pt](https://openaipublic.blob.core.windows.net/consistency/cd_imagenet64_lpips.pt) | ||
* CT on ImageNet-64: [ct_imagenet64.pt](https://openaipublic.blob.core.windows.net/consistency/ct_imagenet64.pt) | ||
* EDM on LSUN Bedroom-256: [edm_bedroom256_ema.pt](https://openaipublic.blob.core.windows.net/consistency/edm_bedroom256_ema.pt) | ||
* CD on LSUN Bedroom-256 with l2 metric: [cd_bedroom256_l2.pt](https://openaipublic.blob.core.windows.net/consistency/cd_bedroom256_l2.pt) | ||
* CD on LSUN Bedroom-256 with LPIPS metric: [cd_bedroom256_lpips.pt](https://openaipublic.blob.core.windows.net/consistency/cd_bedroom256_lpips.pt) | ||
* CT on LSUN Bedroom-256: [ct_bedroom256.pt](https://openaipublic.blob.core.windows.net/consistency/ct_bedroom256.pt) | ||
* EDM on LSUN Cat-256: [edm_cat256_ema.pt](https://openaipublic.blob.core.windows.net/consistency/edm_cat256_ema.pt) | ||
* CD on LSUN Cat-256 with l2 metric: [cd_cat256_l2.pt](https://openaipublic.blob.core.windows.net/consistency/cd_cat256_l2.pt) | ||
* CD on LSUN Cat-256 with LPIPS metric: [cd_cat256_lpips.pt](https://openaipublic.blob.core.windows.net/consistency/cd_cat256_lpips.pt) | ||
* CT on LSUN Cat-256: [ct_cat256.pt](https://openaipublic.blob.core.windows.net/consistency/ct_cat256.pt) | ||
|
||
# Dependencies | ||
|
||
To install all packages in this codebase along with their dependencies, run | ||
```sh | ||
pip install -e . | ||
``` | ||
|
||
# Model training and sampling | ||
|
||
We provide examples of EDM training, consistency distillation, consistency training, single-step generation, and multistep generation in [cm/scripts/launch.sh](scripts/launch.sh). | ||
|
||
# Evaluations | ||
|
||
To compare different generative models, we use FID, Precision, Recall, and Inception Score. These metrics can all be calculated using batches of samples stored in `.npz` (numpy) files. One can evaluate samples with [cm/evaluations/evaluator.py](cm/evaluations/evaluator.py) in the same way as described in [openai/guided-diffusion](https://github.com/openai/guided-diffusion), with reference dataset batches provided therein. | ||
|
||
# Citation | ||
|
||
If you find this method and/or code useful, please consider citing | ||
|
||
```bibtex | ||
@article{song2023consistency, | ||
title={Consistency Models}, | ||
author={Song, Yang and Dhariwal, Prafulla and Chen, Mark and Sutskever, Ilya}, | ||
journal={arXiv preprint arXiv:2303.01469}, | ||
year={2023}, | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Codebase for "Improved Denoising Diffusion Probabilistic Models". | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
""" | ||
Helpers for distributed training. | ||
""" | ||
|
||
import io | ||
import os | ||
import socket | ||
|
||
import blobfile as bf | ||
from mpi4py import MPI | ||
import torch as th | ||
import torch.distributed as dist | ||
|
||
# Change this to reflect your cluster layout. | ||
# The GPU for a given rank is (rank % GPUS_PER_NODE). | ||
GPUS_PER_NODE = 8 | ||
|
||
SETUP_RETRY_COUNT = 3 | ||
|
||
|
||
def setup_dist(): | ||
""" | ||
Setup a distributed process group. | ||
""" | ||
if dist.is_initialized(): | ||
return | ||
os.environ["CUDA_VISIBLE_DEVICES"] = f"{MPI.COMM_WORLD.Get_rank() % GPUS_PER_NODE}" | ||
|
||
comm = MPI.COMM_WORLD | ||
backend = "gloo" if not th.cuda.is_available() else "nccl" | ||
|
||
if backend == "gloo": | ||
hostname = "localhost" | ||
else: | ||
hostname = socket.gethostbyname(socket.getfqdn()) | ||
os.environ["MASTER_ADDR"] = comm.bcast(hostname, root=0) | ||
os.environ["RANK"] = str(comm.rank) | ||
os.environ["WORLD_SIZE"] = str(comm.size) | ||
|
||
port = comm.bcast(_find_free_port(), root=0) | ||
os.environ["MASTER_PORT"] = str(port) | ||
dist.init_process_group(backend=backend, init_method="env://") | ||
|
||
|
||
def dev(): | ||
""" | ||
Get the device to use for torch.distributed. | ||
""" | ||
if th.cuda.is_available(): | ||
return th.device("cuda") | ||
return th.device("cpu") | ||
|
||
|
||
def load_state_dict(path, **kwargs): | ||
""" | ||
Load a PyTorch file without redundant fetches across MPI ranks. | ||
""" | ||
chunk_size = 2**30 # MPI has a relatively small size limit | ||
if MPI.COMM_WORLD.Get_rank() == 0: | ||
with bf.BlobFile(path, "rb") as f: | ||
data = f.read() | ||
num_chunks = len(data) // chunk_size | ||
if len(data) % chunk_size: | ||
num_chunks += 1 | ||
MPI.COMM_WORLD.bcast(num_chunks) | ||
for i in range(0, len(data), chunk_size): | ||
MPI.COMM_WORLD.bcast(data[i : i + chunk_size]) | ||
else: | ||
num_chunks = MPI.COMM_WORLD.bcast(None) | ||
data = bytes() | ||
for _ in range(num_chunks): | ||
data += MPI.COMM_WORLD.bcast(None) | ||
|
||
return th.load(io.BytesIO(data), **kwargs) | ||
|
||
|
||
def sync_params(params): | ||
""" | ||
Synchronize a sequence of Tensors across ranks from rank 0. | ||
""" | ||
for p in params: | ||
with th.no_grad(): | ||
dist.broadcast(p, 0) | ||
|
||
|
||
def _find_free_port(): | ||
try: | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
s.bind(("", 0)) | ||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
return s.getsockname()[1] | ||
finally: | ||
s.close() |
Oops, something went wrong.