-
Notifications
You must be signed in to change notification settings - Fork 6k
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
patrickvonplaten
merged 79 commits into
huggingface:main
from
dg845:consistency-models-pipeline
Jul 5, 2023
Merged
Changes from 78 commits
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
799ab23
initial commit
dg845 63b7f01
Improve consistency models sampling implementation.
dg845 f2e53da
Add CMStochasticIterativeScheduler, which implements the multi-step s…
dg845 37e1895
Merge branch 'main2' into consistency_unet
7c1e81f
Add Unet blocks for consistency models
3a151bd
Add conversion script for Unet
b6c5e15
Fix bug in new unet blocks
4e93f09
Fix attention weight loading
d137d11
Merge pull request #1 from ayushtues/consistency_unet
dg845 9ae7669
Make design improvements to ConsistencyModelPipeline and CMStochastic…
dg845 54b287e
make style
dg845 5a27c2f
Make small random test UNet class conditional and set resnet_time_sca…
dg845 f2783a8
Add support for converting a test UNet and non-class-conditional UNet…
dg845 ed53b85
make style
dg845 a505c6c
Change num_class_embeds to 1000 to better match the original consiste…
dg845 a927a4a
Add support for distillation in pipeline_consistency_models.py.
dg845 b2e6424
Improve consistency model tests:
dg845 c37e302
make style
dg845 a0a164c
Improve ConsistencyModelPipeline:
dg845 0d1de08
make style
dg845 5f4f406
Improve ConsistencyModelPipeline:
dg845 213b25d
make style
dg845 fbe34c3
Fix bug when randomly generating class labels for class-conditional g…
dg845 0e53d8b
Switch CMStochasticIterativeScheduler to configuring a sigma schedule…
dg845 c3b242e
Remove some unused code and make style.
dg845 43e4379
Fix small bug in CMStochasticIterativeScheduler.
dg845 94c99ca
Add expected slices for multistep sampling tests and make them pass.
dg845 0773b27
Work on consistency model fast tests:
dg845 6867a3a
make style
dg845 f0c85d3
Refactor conversion script to make it easier to add more model archit…
dg845 6adb589
Work on ConsistencyModelPipeline tests:
dg845 c213bf7
make style
dg845 3be6e08
Remove the add_noise and add_noise_to_input methods from CMStochastic…
dg845 660909f
Run python utils/check_copies.py --fix_and_overwrite
dg845 ad5abdc
Make fast tests from PipelineTesterMixin pass.
dg845 a1c11d3
make style
dg845 2dc2fa3
Refactor consistency models pipeline and scheduler:
dg845 7d3dbe3
make style
dg845 55f80ed
Add docstrings and further refactor pipeline and scheduler.
dg845 3234543
make style
dg845 dc4349c
Add initial version of the consistency models documentation.
dg845 85eb796
Refactor custom timesteps logic following DDPMScheduler/IFPipeline an…
dg845 bf3a405
make style
dg845 fd0a253
Convert current slow tests to use fp16 and flash attention.
dg845 7c03ff6
make style
dg845 80d7745
Add slow tests for normal attention on cuda device.
dg845 0662f63
make style
dg845 cce04c0
Fix attention weights loading
ayushtues c1ba8b9
Merge pull request #3 from ayushtues/fix-attention-loading
dg845 62a49a2
Update consistency model fast tests for new test checkpoints with att…
dg845 58f12ca
make style
dg845 071f850
apply suggestions
dg845 6968615
Add add_noise method to CMStochasticIterativeScheduler (copied from E…
dg845 ca15734
Conversion script now outputs pipeline instead of UNet and add suppor…
dg845 a56d3d2
When both timesteps and num_inference_steps are supplied, raise warni…
dg845 a17847e
make style
dg845 8214a33
Add remaining diffusers model checkpoints for models in the original …
dg845 2606d82
apply suggestions from review
dg845 075351a
make style
dg845 8976471
Merge branch 'main' into consistency-models-pipeline
dg845 6486049
fix attention naming
dg845 cf514b3
Add tests for CMStochasticIterativeScheduler.
dg845 b5c2b23
make style
dg845 60b0527
Make CMStochasticIterativeScheduler tests pass.
dg845 7670cd8
make style
dg845 cb48162
Override test_step_shape in CMStochasticIterativeSchedulerTest instea…
dg845 ee9f8bf
make style
dg845 2fc7f2c
rename some models
patrickvonplaten e14189c
Improve API
patrickvonplaten 9f627ed
rename some models
patrickvonplaten 3febb18
Remove duplicated block
patrickvonplaten d430fab
Add docstring and make torch compile work
patrickvonplaten 44d2c22
Merge branch 'main' into consistency-models-pipeline
patrickvonplaten f7675d3
More fixes
patrickvonplaten c66b702
Fixes
patrickvonplaten 66f1603
Apply suggestions from code review
patrickvonplaten 033fbe6
Apply suggestions from code review
patrickvonplaten fd67410
add more docstring
patrickvonplaten 541dd84
update consistency conversion script
patrickvonplaten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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__ |
This file contains hidden or 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,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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.