Home  >  Article  >  Technology peripherals  >  Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

PHPz
PHPzforward
2023-04-12 08:37:101611browse

To talk about the most influential developments in the AI ​​field this year, the explosive AI mapping is definitely one of them. Designers only need to enter a text description of the image, and AI will generate a high-resolution image of extremely high quality. Currently, the most widely used model is StabilityAI’s open source model Stable Diffusion. Once the model was open sourced, it caused widespread discussion in the community.

However, the biggest problem in using the diffusion model is its extremely slow sampling speed. Model sampling needs to start from pure noise pictures and continuously denoise step by step to finally obtain clear pictures. In this process, the model must calculate at least 50 to 100 steps serially to obtain a higher quality image. This results in the time required to generate an image 50 to 100 times that of other deep generation models, which greatly limits the model. deployment and implementation.

In order to speed up the sampling of diffusion models, many researchers start from the perspective of hardware optimization. For example, Google uses JAX language to compile and run the model on TPU, and the OneFlow team [1] uses self-developed The compiler achieves "image output in one second" with Stable Diffusion. These methods are based on the 50-step sampling algorithm PNDM [2], which has a sharp decline in sampling effect when the number of steps is reduced.

Just a few days ago, this record was refreshed! The official Demo[3] update of Stable Diffusion shows that the time to sample 8 pictures has been directly shortened from the original 8 seconds to 4 seconds! It's twice as fast!

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

The OneFlow team, which is based on self-developed deep learning compiler technology, has successfully transformed the previous "OneFlow" without reducing the sampling effect. "Pictures will appear in seconds" has been shortened to "Pictures will appear in half a second"! Get a high-definition image in less than 0.5 seconds on the GPU! Related work has been published in [1].

In fact, the core driving force of these works comes from the DPM-Solver proposed by the TSAIL team led by Professor Zhu Jun of Tsinghua University, an efficient solver specially designed for diffusion models Filter: This algorithm does not require any additional training, is suitable for both discrete-time and continuous-time diffusion models, can almost converge within 20 to 25 steps, and can obtain very high-quality sampling in only 10 to 15 steps. On Stable Diffusion, a 25-step DPM-Solver can achieve better sampling quality than a 50-step PNDM, so the sampling speed is directly doubled!

Project link:

  • DPM-Solver: A Fast ODE Solver for Diffusion Probabilistic Model Sampling in Around 10 Steps: https: //arxiv.org/abs/2206.00927 (NeurIPS 2022 Oral)
  • DPM-Solver : Fast Solver for Guided Sampling of Diffusion Probabilistic Models: https://arxiv.org/abs/ 2211.01095
  • Project open source code: https://github.com/LuChengTHU/dpm-solver
  • Project online Demo: https:// huggingface.co/spaces/LuChengTHU/dpmsolver_sdm

The definition and sampling method of the diffusion model

The diffusion model defines a forward process that continuously adds noise. The picture is gradually turned into Gaussian noise, and then a reverse process is defined to gradually denoise the Gaussian noise into a clear picture to obtain samples:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

## During the sampling process, diffusion models can be divided into two categories according to whether additional noise is added: one is the diffusion stochastic differential equation model (Diffusion SDE), and the other is the diffusion ordinary differential equation (Diffusion ODE). The training objective functions of both models are the same. A "noise prediction network" is trained by minimizing the mean square error with noise:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

##Based on The sampling process of Diffusion SDE can be regarded as discretizing the following stochastic differential equation:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

And it is proved in [4] that DDPM[5] is the above First-order discretization of SDE.

The sampling process based on Diffusion ODE can be regarded as discretizing the following ordinary differential equation:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

And it is proved in [6] that DDIM[7] is a first-order discretization of the above ODE.

However, these first-order discretization methods converge extremely slowly, and the sampling of the diffusion model usually requires 100 to 1000 serial calculations to obtain a high-quality picture. Usually, in order to speed up the sampling of diffusion models, researchers often use high-order solvers for Diffusion ODE to speed up the process, such as the classic Runge-Kutta method (RK45). This is because ODE does not bring additional randomness. , the discretization step size can be relatively larger. After given the solution at time s, the Runge-Kutta method is based on the discretization of the following integral:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

Such discretization treats the Diffusion ODE as a whole A black box loses the known information of the ODE and is difficult to converge in less than 50 steps.

DPM-Solver: A solver specially designed for diffusion models

DPM-Solver is based on the semi-linear structure of Diffusion ODE, through accurate and analytical Calculating the linear terms in the ODE, we can get:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

The remaining integral term is a complex integral with respect to time. However, the proposer of DPM-Solver discovered that this integral can be obtained in a very simple form by substituting log-SNR (log signal-to-noise ratio):

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

The remaining integral is an exponentially weighted integral about the noise prediction model. By performing Taylor expansion on the noise prediction model, we can get an estimate of the integral:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

There are two items in this estimate: one is the full The derivative part (vector) and the other term is the coefficient part (scalar). Another core contribution of DPM-Solver is that the coefficient can be calculated analytically by integrating by parts:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

while the remaining total derivative part is It can be approximated by the numerical method of the traditional ODE solver (without any derivative operation):

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

Based on the above 4 points, DPM-Solver does To calculate all known terms as accurately as possible, only the neural network part is approximated, thus minimizing the discretization error:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

In addition, based on this derivation, we can get that DDIM is essentially the first-order form of DPM-Solver, which can also explain why DDIM can still achieve good acceleration effects when the number of steps is small:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

In the experiment, DPM-Solver achieved an acceleration effect far exceeding that of other sampling algorithms, and it almost converged in only 15-20 steps:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

And the quantitative results in the paper show that the additional calculation amount introduced by DPM-Solver is completely negligible, that is, the acceleration effect on the number of steps is directly proportional to the acceleration effect on time - therefore, based on 25 steps The sampling speed of DPM-Solver and Stable-Diffusion models is directly doubled! For example, the figure below shows the effect of different sampling algorithms on Stable-Diffusion as the number of steps changes. It can be seen that DPM-Solver can obtain very high-quality sampling in 10 to 15 steps:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

Using DPM-Solver

The use of DPM-Solver is very simple. It can be based on the official code provided by the author or the mainstream Diffusers library. For example, based on the official code provided by the author (https://github.com/LuChengTHU/dpm-solver), only 3 lines are needed:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

The official code supports four diffusion models:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

and also supports unconditional sampling, classifier guidance and classifier-free guidance:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

The DPM-Solver based on the Diffusers library is also very simple, you only need to define the scheduler:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

In addition, the author team also provides an online Demo: https://huggingface.co/spaces/LuChengTHU/dpmsolver_sdm

The picture below is 15 steps For example, you can see that the image quality is very high:

Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps

I believe that based on DPM-Solver, the sampling speed of the diffusion model will no longer be a bottleneck.

About the author

The first author of the DPM-Solver paper is Dr. Lu Cheng from the TSAIL team of Tsinghua University. He also wrote in the discussion about the diffusion model on Zhihu An introductory introduction to the principle of diffusion model, currently has 2000 likes: https://www.zhihu.com/question/536012286/answer/2533146567

Tsinghua University TSAIL team long-term Committed to the theory and algorithm research of Bayesian machine learning, it is one of the earliest teams in the world to study deep probabilistic generation models. It has achieved systematic and in-depth research results in Bayesian models, efficient algorithms and probabilistic programming libraries. Bao Fan, another doctoral student in the team, proposed Analytic-DPM [8][9], which provided a simple and surprising analytical form for the optimal mean and variance of the diffusion model, and won the ICLR 2022 Outstanding Paper Award. In terms of probabilistic programming, Machine Heart reported on the "ZhuSuan" deep probabilistic programming library released by the team as early as 2017 (https://zhusuan.readthedocs.io/en/latest/) [10], which is the earliest in the world. One of the programming libraries for deep probabilistic models. In addition, it is worth mentioning that the two core authors of the diffusion probability model, Song Yang and Song Jiaming, both received scientific research training under the guidance of Professor Zhu Jun as undergraduates, and later went to Stanford University to study for doctoral degrees. The co-authors of the paper, Zhou Yuhao, Chen Jianfei, and Li Chongxuan, are also outstanding doctoral students trained by the TSAIL group. Zhou Yuhao is a student, and Chen Jianfei and Li Chongxuan teach in the Computer Department of Tsinghua University and the Hillhouse School of Artificial Intelligence of Renmin University respectively.


The above is the detailed content of Stable Diffusion sampling speed doubled! Diffusion model sampling algorithm in only 10 to 25 steps. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete