Skip to content

flow matching lcm scheduler #11170

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 5 commits into from
Apr 12, 2025
Merged

flow matching lcm scheduler #11170

merged 5 commits into from
Apr 12, 2025

Conversation

quickjkee
Copy link
Contributor

@quickjkee quickjkee commented Mar 29, 2025

Why?

Most cutting-edge image generative models (SD3.5,
FLUX) are formulated using flow matching. Moreover, some state-of-the-art distillation methods (DMD2, CD) use stochastic multistep sampling (LCM scheduler). However, there is no realization of the LCM scheduler for the flow matching formulation.

What does this PR do?

Thus, this PR provides a scheduling_flow_match_lcm.py that is in the same spirit as the scheduling_lcm.py. The goal is for distilled models (using, e.g SD3.5 or FLUX) to use this sampler.

Moreover, as an additional tool, this PR provides the ability to generate on different resolution scales.
Which could significantly accelerate the generation process. This concept was introduced in SwD.

How it works?

For example, one of the most prominent distillation approaches, DMD2, uses LCM. It is distilled from SDXL. But there is also an implementation for SD3.5 Large and with PR it works as follows:

import torch
from diffusers.schedulers.scheduling_flow_match_lcm import FlowMatchLCMScheduler
from diffusers import StableDiffusion3Pipeline
from peft import PeftModel

# Loading models
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-large", 
                                                torch_dtype=torch.float16)
pipe = pipe.to("cuda")
lora_path = 'yresearch/stable-diffusion-3.5-large-dmd2'
pipe.transformer = PeftModel.from_pretrained(
    pipe.transformer,
    lora_path,
)

# LCM scheduler 
pipe.scheduler = FlowMatchLCMScheduler.from_config(pipe.scheduler.config, 
                                                   shift=1.0)

# Generation
image = pipe(
    "a cat reading a newspaper",
    sigmas=[1.0000, 0.9454, 0.8959, 0.7904, 0.7371, 0.6022],
    guidance_scale=0.0,
    generator=torch.Generator().manual_seed(0),
).images[0]

To generate in a scale-wise regime, it is necessary to provide scale factors through the pipe.scheduler.set_scale_factors.
This modification is optional.

import torch
from diffusers.schedulers.scheduling_flow_match_lcm import FlowMatchLCMScheduler
from diffusers import StableDiffusion3Pipeline
from peft import PeftModel

# Loading models
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-large", 
                                                torch_dtype=torch.float16)
pipe = pipe.to("cuda")
lora_path = 'yresearch/swd-large-6-steps'
pipe.transformer = PeftModel.from_pretrained(
    pipe.transformer,
    lora_path,
)

# LCM scheduler
pipe.scheduler = FlowMatchLCMScheduler.from_config(pipe.scheduler.config, 
                                                   shift=1.0)

# Setting up the scale factors
# We define it wrt input size 
# In this case: 32->48->64->80->96->128
pipe.scheduler.set_scale_factors(
    scale_factors=[1.5, 2., 2.5, 3., 4.], 
    upscale_type='bicubic'
)

# Generation
image = pipe(
    "a cat reading a newspaper",
    sigmas=[1.0000, 0.9454, 0.8959, 0.7904, 0.7371, 0.6022],
    guidance_scale=0.0,
    generator=torch.Generator().manual_seed(0),
    width=256,
    height=256
).images[0]

* stochastic sampling
* upscaling for scale-wise generation
@quickjkee quickjkee changed the title add flow matching lcm scheduler flow matching lcm scheduler Mar 29, 2025
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@hlky
Copy link
Contributor

hlky commented Mar 29, 2025

@bot /style

Copy link
Contributor

Style fixes have been applied. View the workflow run here.

@DN6 DN6 requested review from yiyixuxu and hlky March 31, 2025 08:04
@yiyixuxu
Copy link
Collaborator

thanks @quickjkee
do we have a training script using this scheduler?

@quickjkee
Copy link
Contributor Author

thanks @quickjkee do we have a training script using this scheduler?

thanks for the question and reviewing @yiyixuxu!
I don't think so. This scheduler is only needed for inference, as the basic LCM scheduler.

@yiyixuxu
Copy link
Collaborator

yiyixuxu commented Apr 7, 2025

@quickjkee
do you have a training script for these checkpoints that can be used with this scheduler? (even though the training itself does not require it)

@ShuyUSTC
Copy link

Thank you for your great contribution! Is there any plan to merge this pr? Our HiDream-I1 fast and dev model use this flow_match_lcm scheduler by default. We are waiting for this pr to be merged. Thanks!

Copy link
Contributor

@hlky hlky left a comment

Choose a reason for hiding this comment

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

Hi @quickjkee. Left a few suggestions on unused parameters but LGTM, thanks!

Co-authored-by: hlky <hlky@hlky.ac>
@yiyixuxu yiyixuxu merged commit ec0b2b3 into huggingface:main Apr 12, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants