Load and compare different schedulers
Last updated
Last updated
Diffusion pipelines are inherently a collection of diffusion models and schedulers that are partly independent from each other. This means that one is able to switch out parts of the pipeline to better customize a pipeline to oneβs use case. The best example of this is the .
Whereas diffusion models usually simply define the forward pass from noise to a less noisy sample, schedulers define the whole denoising process, i.e.:
How many denoising steps?
Stochastic or deterministic?
What algorithm to use to find the denoised sample
They can be quite complex and often define a trade-off between denoising speed and denoising quality. It is extremely difficult to measure quantitatively which scheduler works best for a given diffusion pipeline, so it is often recommended to simply try out which works best.
The following paragraphs show how to do so with the 𧨠Diffusers library.
Letβs start by loading the model in the :
Copied
Next, we move it to GPU:
Copied
The scheduler is always one of the components of the pipeline and is usually called "scheduler"
. So it can be accessed via the "scheduler"
property.
Copied
Output:
Copied
Copied
Next, we create a generator from a random seed that will ensure that we can generate similar images as well as run the pipeline:
Copied
Now we show how easy it is to change the scheduler of a pipeline. Every scheduler has a property SchedulerMixin.compatibles
which defines all compatible schedulers. You can take a look at all available, compatible schedulers for the Stable Diffusion pipeline as follows.
Copied
Output:
Copied
Cool, lots of schedulers to look at. Feel free to have a look at their respective class definitions:
Copied
returns a dictionary of the configuration of the scheduler:
Output:
Copied
Copied
Cool, now we can run the pipeline again to compare the generation quality.
Copied
Copied
Copied
and:
Copied
Copied
As you can see most images look very similar and are arguably of very similar quality. It often really depends on the specific use case which scheduler to choose. A good approach is always to run multiple different schedulers to compare results.
Copied
The following Flax schedulers are not yet compatible with the Flax Stable Diffusion Pipeline:
FlaxLMSDiscreteScheduler
FlaxDDPMScheduler
We can see that the scheduler is of type . Cool, now letβs compare the scheduler in its performance to other schedulers. First we define a prompt on which we will test all the different schedulers:
,
,
,
,
,
,
.
We will now compare the input prompt with all other schedulers. To change the scheduler of the pipeline you can make use of the convenient ConfigMixin.config
property in combination with the function.
This configuration can then be used to instantiate a scheduler of a different class that is compatible with the pipeline. Here, we change the scheduler to the .
If you are a JAX/Flax user, please check instead.
So far we have tried running the stable diffusion pipeline with two schedulers: and . A number of better schedulers have been released that can be run with much fewer steps, letβs compare them here:
usually leads to better results:
and can generate high quality results with as little as 30 steps.
At the time of writing this doc gives arguably the best speed/quality trade-off and can be run with as little as 20 steps.
If you are a JAX/Flax user, you can also change the default pipeline scheduler. This is a complete example of how to run inference using the Flax Stable Diffusion pipeline and the super-fast :