Skip to content
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

Add Latent Consistency Models Pipeline #5448

Merged
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
904b7f5
initial commit for LatentConsistencyModelPipeline and LCMScheduler ba…
dg845 Oct 19, 2023
4e25452
Add callback and freeu support.
dg845 Oct 19, 2023
3eec980
apply suggestions from review
dg845 Oct 19, 2023
cadb86b
Clean up LCMScheduler
dg845 Oct 19, 2023
0740f36
Remove timeindex argument to LCMScheduler.step.
dg845 Oct 19, 2023
b960037
Add support for clipping or thresholding the predicted original sample.
dg845 Oct 19, 2023
468511a
Remove unused methods and arguments in LCMScheduler.
dg845 Oct 19, 2023
6b00756
Improve comment about (lack of) negative prompt support.
dg845 Oct 20, 2023
cbc4d55
Change input guidance_scale to match the StableDiffusionPipeline (Ima…
dg845 Oct 20, 2023
e8f65bf
Move lcm_origin_steps from pipeline __call__ to LCMScheduler.__init__…
dg845 Oct 20, 2023
f800547
Fix typo when clipping/thresholding in LCMScheduler.
dg845 Oct 20, 2023
21cb605
Add some initial LCMScheduler tests.
dg845 Oct 20, 2023
ae358ac
add type annotations from review
dg845 Oct 20, 2023
00f4b48
Fix type annotation bug.
dg845 Oct 20, 2023
959e7f8
Override test_add_noise_device in LCMSchedulerTest since hardcoded ti…
dg845 Oct 20, 2023
696d987
Add generator argument pipeline prepare_latents call.
dg845 Oct 20, 2023
43361b7
Cast LCMScheduler.timesteps to long in set_timesteps.
dg845 Oct 20, 2023
13a54b8
Add onestep and multistep full loop scheduler tests.
dg845 Oct 20, 2023
a61b4ae
Set default height/width to None and don't hardcode guidance scale em…
dg845 Oct 20, 2023
3de1465
Add initial LatentConsistencyPipeline fast and slow tests.
dg845 Oct 20, 2023
cd4b0ab
Add initial documentation for LatentConsistencyModelPipeline and LCMS…
dg845 Oct 20, 2023
a321639
Merge branch 'main' into latent-consistency-models-pipeline
sayakpaul Oct 20, 2023
9df89ea
Make remaining failing fast tests pass.
dg845 Oct 23, 2023
0dd2a82
make style
dg845 Oct 23, 2023
cbbd187
Make original_inference_steps configurable from pipeline __call__ again.
dg845 Oct 23, 2023
8868d34
make style
dg845 Oct 23, 2023
92b68b2
Remove guidance_rescale arg from pipeline __call__ since LCM currentl…
dg845 Oct 23, 2023
24c8cad
Make LCMScheduler defaults match config of LCM_Dreamshaper_v7 checkpo…
dg845 Oct 23, 2023
e191555
Fix LatentConsistencyPipeline slow tests and add dummy expected slices.
dg845 Oct 23, 2023
e439118
Add checks for original_steps in LCMScheduler.set_timesteps.
dg845 Oct 24, 2023
516d8fd
make fix-copies
dg845 Oct 24, 2023
e57913d
Improve LatentConsistencyModelPipeline docs.
dg845 Oct 24, 2023
6faa29f
Apply suggestions from code review
patrickvonplaten Oct 24, 2023
b08df59
Apply suggestions from code review
patrickvonplaten Oct 24, 2023
264bd77
Apply suggestions from code review
patrickvonplaten Oct 24, 2023
888b96d
Update src/diffusers/schedulers/scheduling_lcm.py
patrickvonplaten Oct 24, 2023
686c0c0
Apply suggestions from code review
patrickvonplaten Oct 24, 2023
2cd1019
Merge branch 'main' into latent-consistency-models-pipeline
patrickvonplaten Oct 24, 2023
2f2d14c
finish
patrickvonplaten Oct 24, 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 @@ -250,6 +250,8 @@
title: Kandinsky
- local: api/pipelines/kandinsky_v22
title: Kandinsky 2.2
- local: api/pipelines/latent_consistency_models
title: Latent Consistency Models
- local: api/pipelines/latent_diffusion
title: Latent Diffusion
- local: api/pipelines/panorama
Expand Down Expand Up @@ -366,6 +368,8 @@
title: KDPM2AncestralDiscreteScheduler
- local: api/schedulers/dpm_discrete
title: KDPM2DiscreteScheduler
- local: api/schedulers/lcm
title: LCMScheduler
- local: api/schedulers/lms_discrete
title: LMSDiscreteScheduler
- local: api/schedulers/pndm
Expand Down
44 changes: 44 additions & 0 deletions docs/source/en/api/pipelines/latent_consistency_models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Latent Consistency Models

Latent Consistency Models (LCMs) were proposed in [Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inference](https://arxiv.org/abs/2310.04378) by Simian Luo, Yiqin Tan, Longbo Huang, Jian Li, and Hang Zhao.

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

*Latent Diffusion models (LDMs) have achieved remarkable results in synthesizing high-resolution images. However, the iterative sampling process is computationally intensive and leads to slow generation. Inspired by Consistency Models (song et al.), we propose Latent Consistency Models (LCMs), enabling swift inference with minimal steps on any pre-trained LDMs, including Stable Diffusion (rombach et al). Viewing the guided reverse diffusion process as solving an augmented probability flow ODE (PF-ODE), LCMs are designed to directly predict the solution of such ODE in latent space, mitigating the need for numerous iterations and allowing rapid, high-fidelity sampling. Efficiently distilled from pre-trained classifier-free guided diffusion models, a high-quality 768 x 768 2~4-step LCM takes only 32 A100 GPU hours for training. Furthermore, we introduce Latent Consistency Fine-tuning (LCF), a novel method that is tailored for fine-tuning LCMs on customized image datasets. Evaluation on the LAION-5B-Aesthetics dataset demonstrates that LCMs achieve state-of-the-art text-to-image generation performance with few-step inference.*

A demo for the [SimianLuo/LCM_Dreamshaper_v7](https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7) checkpoint can be found [here](https://huggingface.co/spaces/SimianLuo/Latent_Consistency_Model).

This pipeline was contributed by [luosiallen](https://luosiallen.github.io/) and [dg845](https://github.com/dg845).

```python
import torch
from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("SimianLuo/LCM_Dreamshaper_v7", torch_dtype=torch.float32)

# To save GPU memory, torch.float16 can be used, but it may compromise image quality.
pipe.to(torch_device="cuda", torch_dtype=torch.float32)

prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"

# Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps.
num_inference_steps = 4

images = pipe(prompt=prompt, num_inference_steps=num_inference_steps, guidance_scale=8.0).images
```

## LatentConsistencyModelPipeline

[[autodoc]] LatentConsistencyModelPipeline
- all
- __call__
- enable_freeu
- disable_freeu
- enable_vae_slicing
- disable_vae_slicing
- enable_vae_tiling
- disable_vae_tiling

## StableDiffusionPipelineOutput

[[autodoc]] pipelines.stable_diffusion.StableDiffusionPipelineOutput
9 changes: 9 additions & 0 deletions docs/source/en/api/schedulers/lcm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Latent Consistency Model Multistep Scheduler

## Overview

Multistep and onestep scheduler (Algorithm 3) introduced alongside latent consistency models in the paper [Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inference](https://arxiv.org/abs/2310.04378) by Simian Luo, Yiqin Tan, Longbo Huang, Jian Li, and Hang Zhao.
This scheduler should be able to generate good samples from [`LatentConsistencyModelPipeline`] in 1-8 steps.

## LCMScheduler
[[autodoc]] LCMScheduler
4 changes: 4 additions & 0 deletions src/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"KarrasVeScheduler",
"KDPM2AncestralDiscreteScheduler",
"KDPM2DiscreteScheduler",
"LCMScheduler",
"PNDMScheduler",
"RePaintScheduler",
"SchedulerMixin",
Expand Down Expand Up @@ -226,6 +227,7 @@
"KandinskyV22Pipeline",
"KandinskyV22PriorEmb2EmbPipeline",
"KandinskyV22PriorPipeline",
"LatentConsistencyModelPipeline",
"LDMTextToImagePipeline",
"MusicLDMPipeline",
"PaintByExamplePipeline",
Expand Down Expand Up @@ -499,6 +501,7 @@
KarrasVeScheduler,
KDPM2AncestralDiscreteScheduler,
KDPM2DiscreteScheduler,
LCMScheduler,
PNDMScheduler,
RePaintScheduler,
SchedulerMixin,
Expand Down Expand Up @@ -564,6 +567,7 @@
KandinskyV22Pipeline,
KandinskyV22PriorEmb2EmbPipeline,
KandinskyV22PriorPipeline,
LatentConsistencyModelPipeline,
LDMTextToImagePipeline,
MusicLDMPipeline,
PaintByExamplePipeline,
Expand Down
2 changes: 2 additions & 0 deletions src/diffusers/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"KandinskyV22PriorEmb2EmbPipeline",
"KandinskyV22PriorPipeline",
]
_import_structure["latent_consistency_models"] = ["LatentConsistencyModelPipeline"]
_import_structure["latent_diffusion"].extend(["LDMTextToImagePipeline"])
_import_structure["musicldm"] = ["MusicLDMPipeline"]
_import_structure["paint_by_example"] = ["PaintByExamplePipeline"]
Expand Down Expand Up @@ -331,6 +332,7 @@
KandinskyV22PriorEmb2EmbPipeline,
KandinskyV22PriorPipeline,
)
from .latent_consistency_models import LatentConsistencyModelPipeline
from .latent_diffusion import LDMTextToImagePipeline
from .musicldm import MusicLDMPipeline
from .paint_by_example import PaintByExamplePipeline
Expand Down
22 changes: 22 additions & 0 deletions src/diffusers/pipelines/latent_consistency_models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import TYPE_CHECKING

from ...utils import (
_LazyModule,
)


_import_structure = {"pipeline_latent_consistency_models": ["LatentConsistencyModelPipeline"]}


if TYPE_CHECKING:
from .pipeline_latent_consistency_models import LatentConsistencyModelPipeline

else:
import sys

sys.modules[__name__] = _LazyModule(
__name__,
globals()["__file__"],
_import_structure,
module_spec=__spec__,
)
Loading
Loading