Skip to content

Add Consistency Models Pipeline #3492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 79 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
799ab23
initial commit
dg845 May 15, 2023
63b7f01
Improve consistency models sampling implementation.
dg845 May 17, 2023
f2e53da
Add CMStochasticIterativeScheduler, which implements the multi-step s…
dg845 May 18, 2023
37e1895
Merge branch 'main2' into consistency_unet
May 22, 2023
7c1e81f
Add Unet blocks for consistency models
May 22, 2023
3a151bd
Add conversion script for Unet
May 22, 2023
b6c5e15
Fix bug in new unet blocks
May 22, 2023
4e93f09
Fix attention weight loading
May 23, 2023
d137d11
Merge pull request #1 from ayushtues/consistency_unet
dg845 May 25, 2023
9ae7669
Make design improvements to ConsistencyModelPipeline and CMStochastic…
dg845 May 26, 2023
54b287e
make style
dg845 May 26, 2023
5a27c2f
Make small random test UNet class conditional and set resnet_time_sca…
dg845 May 30, 2023
f2783a8
Add support for converting a test UNet and non-class-conditional UNet…
dg845 May 30, 2023
ed53b85
make style
dg845 May 30, 2023
a505c6c
Change num_class_embeds to 1000 to better match the original consiste…
dg845 May 30, 2023
a927a4a
Add support for distillation in pipeline_consistency_models.py.
dg845 May 30, 2023
b2e6424
Improve consistency model tests:
dg845 May 30, 2023
c37e302
make style
dg845 May 30, 2023
a0a164c
Improve ConsistencyModelPipeline:
dg845 May 30, 2023
0d1de08
make style
dg845 May 30, 2023
5f4f406
Improve ConsistencyModelPipeline:
dg845 May 30, 2023
213b25d
make style
dg845 May 30, 2023
fbe34c3
Fix bug when randomly generating class labels for class-conditional g…
dg845 May 30, 2023
0e53d8b
Switch CMStochasticIterativeScheduler to configuring a sigma schedule…
dg845 Jun 1, 2023
c3b242e
Remove some unused code and make style.
dg845 Jun 1, 2023
43e4379
Fix small bug in CMStochasticIterativeScheduler.
dg845 Jun 1, 2023
94c99ca
Add expected slices for multistep sampling tests and make them pass.
dg845 Jun 2, 2023
0773b27
Work on consistency model fast tests:
dg845 Jun 2, 2023
6867a3a
make style
dg845 Jun 2, 2023
f0c85d3
Refactor conversion script to make it easier to add more model archit…
dg845 Jun 4, 2023
6adb589
Work on ConsistencyModelPipeline tests:
dg845 Jun 4, 2023
c213bf7
make style
dg845 Jun 4, 2023
3be6e08
Remove the add_noise and add_noise_to_input methods from CMStochastic…
dg845 Jun 4, 2023
660909f
Run python utils/check_copies.py --fix_and_overwrite
dg845 Jun 4, 2023
ad5abdc
Make fast tests from PipelineTesterMixin pass.
dg845 Jun 4, 2023
a1c11d3
make style
dg845 Jun 4, 2023
2dc2fa3
Refactor consistency models pipeline and scheduler:
dg845 Jun 7, 2023
7d3dbe3
make style
dg845 Jun 7, 2023
55f80ed
Add docstrings and further refactor pipeline and scheduler.
dg845 Jun 7, 2023
3234543
make style
dg845 Jun 7, 2023
dc4349c
Add initial version of the consistency models documentation.
dg845 Jun 9, 2023
85eb796
Refactor custom timesteps logic following DDPMScheduler/IFPipeline an…
dg845 Jun 10, 2023
bf3a405
make style
dg845 Jun 10, 2023
fd0a253
Convert current slow tests to use fp16 and flash attention.
dg845 Jun 13, 2023
7c03ff6
make style
dg845 Jun 13, 2023
80d7745
Add slow tests for normal attention on cuda device.
dg845 Jun 13, 2023
0662f63
make style
dg845 Jun 13, 2023
cce04c0
Fix attention weights loading
ayushtues Jun 13, 2023
c1ba8b9
Merge pull request #3 from ayushtues/fix-attention-loading
dg845 Jun 13, 2023
62a49a2
Update consistency model fast tests for new test checkpoints with att…
dg845 Jun 13, 2023
58f12ca
make style
dg845 Jun 13, 2023
071f850
apply suggestions
dg845 Jun 16, 2023
6968615
Add add_noise method to CMStochasticIterativeScheduler (copied from E…
dg845 Jun 18, 2023
ca15734
Conversion script now outputs pipeline instead of UNet and add suppor…
dg845 Jun 21, 2023
a56d3d2
When both timesteps and num_inference_steps are supplied, raise warni…
dg845 Jun 21, 2023
a17847e
make style
dg845 Jun 21, 2023
8214a33
Add remaining diffusers model checkpoints for models in the original …
dg845 Jun 21, 2023
2606d82
apply suggestions from review
dg845 Jun 29, 2023
075351a
make style
dg845 Jun 29, 2023
8976471
Merge branch 'main' into consistency-models-pipeline
dg845 Jun 30, 2023
6486049
fix attention naming
dg845 Jun 30, 2023
cf514b3
Add tests for CMStochasticIterativeScheduler.
dg845 Jul 3, 2023
b5c2b23
make style
dg845 Jul 3, 2023
60b0527
Make CMStochasticIterativeScheduler tests pass.
dg845 Jul 4, 2023
7670cd8
make style
dg845 Jul 4, 2023
cb48162
Override test_step_shape in CMStochasticIterativeSchedulerTest instea…
dg845 Jul 4, 2023
ee9f8bf
make style
dg845 Jul 4, 2023
2fc7f2c
rename some models
patrickvonplaten Jul 5, 2023
e14189c
Improve API
patrickvonplaten Jul 5, 2023
9f627ed
rename some models
patrickvonplaten Jul 5, 2023
3febb18
Remove duplicated block
patrickvonplaten Jul 5, 2023
d430fab
Add docstring and make torch compile work
patrickvonplaten Jul 5, 2023
44d2c22
Merge branch 'main' into consistency-models-pipeline
patrickvonplaten Jul 5, 2023
f7675d3
More fixes
patrickvonplaten Jul 5, 2023
c66b702
Fixes
patrickvonplaten Jul 5, 2023
66f1603
Apply suggestions from code review
patrickvonplaten Jul 5, 2023
033fbe6
Apply suggestions from code review
patrickvonplaten Jul 5, 2023
fd67410
add more docstring
patrickvonplaten Jul 5, 2023
541dd84
update consistency conversion script
patrickvonplaten Jul 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
title: Audio Diffusion
- local: api/pipelines/audioldm
title: AudioLDM
- local: api/pipelines/consistency_models
title: Consistency Models
- local: api/pipelines/controlnet
title: ControlNet
- local: api/pipelines/cycle_diffusion
Expand Down Expand Up @@ -274,6 +276,8 @@
- sections:
- local: api/schedulers/overview
title: Overview
- local: api/schedulers/cm_stochastic_iterative
title: Consistency Model Multistep Scheduler
- local: api/schedulers/ddim
title: DDIM
- local: api/schedulers/ddim_inverse
Expand Down
87 changes: 87 additions & 0 deletions docs/source/en/api/pipelines/consistency_models.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Consistency Models

Consistency Models were proposed in [Consistency Models](https://arxiv.org/abs/2303.01469) by Yang Song, Prafulla Dhariwal, Mark Chen, and Ilya Sutskever.

The abstract of the [paper](https://arxiv.org/pdf/2303.01469.pdf) is as follows:

*Diffusion models have significantly advanced the fields of image, audio, and video generation, but they depend on an iterative sampling process that causes slow generation. To overcome this limitation, we propose consistency models, a new family of models that generate high quality samples by directly mapping noise to data. They support fast one-step generation by design, while still allowing multistep sampling to trade compute for sample quality. They also support zero-shot data editing, such as image inpainting, colorization, and super-resolution, without requiring explicit training on these tasks. Consistency models can be trained either by distilling pre-trained diffusion models, or as standalone generative models altogether. Through extensive experiments, we demonstrate that they outperform existing distillation techniques for diffusion models in one- and few-step sampling, achieving the new state-of-the-art FID of 3.55 on CIFAR-10 and 6.20 on ImageNet 64x64 for one-step generation. When trained in isolation, consistency models become a new family of generative models that can outperform existing one-step, non-adversarial generative models on standard benchmarks such as CIFAR-10, ImageNet 64x64 and LSUN 256x256. *

Resources:

* [Paper](https://arxiv.org/abs/2303.01469)
* [Original Code](https://github.com/openai/consistency_models)

Available Checkpoints are:
- *cd_imagenet64_l2 (64x64 resolution)* [openai/consistency-model-pipelines](https://huggingface.co/openai/consistency-model-pipelines)
- *cd_imagenet64_lpips (64x64 resolution)* [openai/diffusers-cd_imagenet64_lpips](https://huggingface.co/openai/diffusers-cd_imagenet64_lpips)
- *ct_imagenet64 (64x64 resolution)* [openai/diffusers-ct_imagenet64](https://huggingface.co/openai/diffusers-ct_imagenet64)
- *cd_bedroom256_l2 (256x256 resolution)* [openai/diffusers-cd_bedroom256_l2](https://huggingface.co/openai/diffusers-cd_bedroom256_l2)
- *cd_bedroom256_lpips (256x256 resolution)* [openai/diffusers-cd_bedroom256_lpips](https://huggingface.co/openai/diffusers-cd_bedroom256_lpips)
- *ct_bedroom256 (256x256 resolution)* [openai/diffusers-ct_bedroom256](https://huggingface.co/openai/diffusers-ct_bedroom256)
- *cd_cat256_l2 (256x256 resolution)* [openai/diffusers-cd_cat256_l2](https://huggingface.co/openai/diffusers-cd_cat256_l2)
- *cd_cat256_lpips (256x256 resolution)* [openai/diffusers-cd_cat256_lpips](https://huggingface.co/openai/diffusers-cd_cat256_lpips)
- *ct_cat256 (256x256 resolution)* [openai/diffusers-ct_cat256](https://huggingface.co/openai/diffusers-ct_cat256)

## Available Pipelines

| Pipeline | Tasks | Demo | Colab |
|:---:|:---:|:---:|:---:|
| [ConsistencyModelPipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/pipeline_consistency_models.py) | *Unconditional Image Generation* | | |

This pipeline was contributed by our community members [dg845](https://github.com/dg845) and [ayushtues](https://huggingface.co/ayushtues) :heart:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure contributors are mentioned


## Usage Example

```python
import torch

from diffusers import ConsistencyModelPipeline

device = "cuda"
# Load the cd_imagenet64_l2 checkpoint.
model_id_or_path = "openai/diffusers-cd_imagenet64_l2"
pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
pipe.to(device)

# Onestep Sampling
image = pipe(num_inference_steps=1).images[0]
image.save("consistency_model_onestep_sample.png")

# Onestep sampling, class-conditional image generation
# ImageNet-64 class label 145 corresponds to king penguins
image = pipe(num_inference_steps=1, class_labels=145).images[0]
image.save("consistency_model_onestep_sample_penguin.png")

# Multistep sampling, class-conditional image generation
# Timesteps can be explicitly specified; the particular timesteps below are from the original Github repo.
# https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L77
image = pipe(timesteps=[22, 0], class_labels=145).images[0]
image.save("consistency_model_multistep_sample_penguin.png")
```

For an additional speed-up, one can also make use of `torch.compile`. Multiple images can be generated in <1 second as follows:

```py
import torch
from diffusers import ConsistencyModelPipeline

device = "cuda"
# Load the cd_bedroom256_lpips checkpoint.
model_id_or_path = "openai/diffusers-cd_bedroom256_lpips"
pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
pipe.to(device)

pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)

# Multistep sampling
# Timesteps can be explicitly specified; the particular timesteps below are from the original Github repo:
# https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L83
for _ in range(10):
image = pipe(timesteps=[17, 0]).images[0]
image.show()
```

## ConsistencyModelPipeline
[[autodoc]] ConsistencyModelPipeline
- all
- __call__
11 changes: 11 additions & 0 deletions docs/source/en/api/schedulers/cm_stochastic_iterative.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Consistency Model Multistep Scheduler

## Overview

Multistep and onestep scheduler (Algorithm 1) introduced alongside consistency models in the paper [Consistency Models](https://arxiv.org/abs/2303.01469) by Yang Song, Prafulla Dhariwal, Mark Chen, and Ilya Sutskever.
Based on the [original consistency models implementation](https://github.com/openai/consistency_models).
Should generate good samples from [`ConsistencyModelPipeline`] in one or a small number of steps.

## CMStochasticIterativeScheduler
[[autodoc]] CMStochasticIterativeScheduler

Loading