ホーム 地域 Investigating the KL-optimal scheduler + alpha (January 2025)

Investigating the KL-optimal scheduler + alpha (January 2025)

雨宮 千夏 雨宮 千夏 • 更新: • 公開: • 読了約 27 分
目次

It might be relatively recent, but a scheduler called KL-optimal was quietly added to ComfyUI. Upon investigating, it seems KL-optimal itself has been around for a while.
It is also available in reForge and Forge, but is anyone using it?
So, when I searched for it, I found an article on Civitai that summarizes samplers and schedulers, and it was covered there.

The site above contains a huge amount of information about samplers and schedulers, including their combinations.
If you are interested, please take a look.

In this article, KL-optimal is recommended as a scheduler for SDXL.
When I actually used it, I had the impression that it tended to generate high-quality images (personal feeling).

Now, this article will focus on what KL-optimal is. Finally, I also tried to see if it could be used with HunyanVideo.

"What is KL?"

nc85d71e6168b


A. It is Kullback-Leibler.

On the site mentioned above, there is an article called "Kullback-Leibler Divergence" related to KL-optimal. It is from 2019, so it is about 5 years old.

It seems there is a mathematical definition.

<GPT Explanation>

KL divergence is an index that measures the difference between two probability distributions and is widely used in information theory and machine learning. In diffusion models, it is used to evaluate the difference between the data distribution output by the generative model and the actual data distribution. By minimizing KL divergence, it is expected that the generated images will become closer to the actual images.

"What is the KL-optimal scheduler doing?"


Mechanism of the KL-Optimal Scheduler

The KL-Optimal scheduler is a method that optimizes the amount of noise removal at each step in the reverse diffusion process. Specifically, it is designed to minimize "Kullback-Leibler Divergence," an index that measures the difference between the generated data distribution and the actual data distribution. By doing this, it aims to make the generated images closer to the actual images.

"How is it implemented in ComfyUI?"


I had the ComfyUI script analyzed.

Mechanism of the KL-Optimal Scheduler (within ComfyUI)
The kl_optimal_scheduler function in the script is responsible for the implementation of the KL-Optimal scheduler.
The features and process of this function are as follows.

  1. Core of Scheduling
    kl_optimal_scheduler performs scheduling in the following way: Parameter settings:
    n: Number of steps (determines the resolution of generation).
    sigma_min and sigma_max: Minimum and maximum values of the noise schedule.
    Scheduling logic:
    The method of gradually reducing the noise level uses math.atan and math.tan, aiming to minimize KL divergence.

  2. Implementation Points
    Generation of scheduling curve:
    adj_idxs generates an even division of [0, 1] (to divide the noise progression evenly).
    sigmas[:-1] calculates the noise level for each step.
    The atan function is used to introduce non-linearity, which improves the accuracy of noise removal.
    Final adjustment:
    Sets the noise level to zero in the final step (sigmas[:-1]).

So, what does that mean?


What happened to KL divergence minimization?In the ComfyUI script, rather than directly minimizing KL divergence, it indirectly adjusts the noise level so that the generated image distribution approaches the original image distribution.
Where it is optimized
It adopts a non-linear noise reduction schedule, allowing for the generation of high-quality images with fewer steps than a standard linear schedule.

Summary of ComfyUI script analysis

Regarding the scheduler, it can be summarized as shown in the table below.

I will briefly list the mechanisms and features of each.

1. KL-Optimal Scheduler
  • Mechanism:

    • KL Divergence Minimization: Adjusts the noise level to minimize the difference (KL divergence) between the true data distribution and the generated data distribution. This results in the generation of more natural and high-quality images.

    • Non-linearity of noise reduction: Uses an atan function, allowing for more precise control as the noise removal process proceeds non-linearly.

  • Features:

    • High-quality image generation can be expected.

    • High-quality results can be obtained with fewer steps.

    • Based on a theoretical background, it is effective in situations where quality optimization is critical.

2. Simple Scheduler
  • Mechanism:

    • This is a simple linear scheduler. Noise is reduced at a constant rate.

  • Features:

    • Linear noise reduction: Noise levels decrease uniformly. It is simple to calculate and easy to configure, but as a result, the quality of the generated images may be inferior to KL-Optimal or other complex schedulers.

    • Efficiency: Processing speed is fast and complex calculations are unnecessary, allowing for relatively high-speed image generation.

  • Pros and Cons:

    • Simple and easy to use.

    • Image quality can be sacrificed.

    • May lack detail reproduction compared to more complex scheduling.

3. Normal Scheduler
  • Mechanism:

    • Noise levels usually decrease linearly within a certain range, but in some cases, they may decrease exponentially.

  • Features:

    • Linear decrease: Standard noise schedules progress linearly based on the specified number of steps. Since the normal_scheduler reduces noise based on this, noise is removed relatively evenly.

    • Different noise reduction method: By adjusting noise levels according to time, more flexible control is possible compared to other schedulers.

  • Pros and Cons:

    • Easier to obtain relatively stable results.

    • Because there is no non-linear change, it may not be suitable for precise control.

    • For some complex models or images, results may potentially be slightly blurry.

4. Beta Scheduler
  • Mechanism:

    • The beta_scheduler uses a beta distribution to remove noise. Specifically, it determines noise levels based on the cumulative distribution function (CDF) of the beta distribution.

  • Features:

    • By using the Beta distribution, flexible and step-wise varying noise reduction is performed during the denoising process.

    • It introduces non-linear changes, which can enhance smoothness and diversity in image generation.

  • Pros and Cons:

    • Allows for detailed denoising of complex images.

    • It may have higher computational costs and might not be suitable if speed is a priority.

5. Karras Scheduler
  • Mechanism:

    • Proposed by Karras et al., it uses exponential reciprocals to adjust noise.

    • In the get_sigmas_karras function, the non-linearity of noise reduction can be adjusted using the rho parameter.

  • Features:

    • Customizability: By adjusting rho, it is possible to control the decay from nearly linear to rapid.

    • Smoothness: Since the noise schedule progresses smoothly, the results have high stability.

    • Versatility: Compatible with a wide range of models.

  • Usage:

    • Effective when stable generation is required or when considering adaptation to different image styles.

6. Exponential Scheduler
  • Mechanism:

    • Implemented by the get_sigmas_exponential function, where the noise schedule decreases exponentially.

    • The noise level is adjusted using the exp() function.

  • Features:

    • Speed: Faster convergence compared to linear scheduling.

    • Controllability: The rate of noise reduction can be adjusted using exponential function bases.

    • Computational Efficiency: Relatively lightweight with low computational load.

  • Applications:

    • Suitable for use when high-speed processing is required or in environments with limited computational resources.

7. SGM Uniform Scheduler
  • Mechanism:

    • A method based on SGM (Score-based Generative Models).

    • Noise reduction proceeds at equal intervals.

  • Features:

    • Uniform noise reduction: The progression of each step is uniform.

    • Linear: Since there is little non-linearity, simple control is possible.

    • Stability: Depending on the model, stable noise removal can be expected.

  • Usage:

    • Suitable for basic verification of simple models and noise removal algorithms.

8. DDIM Uniform Scheduler
  • Mechanism:

    • Based on DDIM (Denoising Diffusion Implicit Models), it proceeds with noise removal in uniform steps.

    • Explicit noise control is possible, and there is flexibility in scheduling.

  • Features:

    • Efficiency: Scheduling is efficient, and high-quality results can be obtained even with a small number of steps.

    • Model Adaptability: Shows good results with many models.

    • Flexibility: Easy to adjust the noise schedule.

  • Usage:

    • Effective for situations where you want to test model performance in a short time or when you want to reduce the number of steps.

<Aside>
I think I've managed to get a general idea of the differences between schedulers.
The above content was generated by having GPT analyze ComfyUI scripts and explain their behavior in Diffusion models.
KL-Optimal makes use of statistics and difficult concepts, so it's cool, which is also a plus.

Now, I have had it suggest schedulers that might be suitable for HunyuanVideo by having it reference additional scripts related to HunyuanVideo.

Perhaps because I had been researching it until then, KL-Optimal is included.
The beta scheduler, which was good in previous articles, is included.

The following are the results of testing the above schedulers with the Euler sampler, excluding Beta.
20 steps

In the case of KL-Optimal

In the case of Karras

In the case of Exponential

Total failure…………………

Is this a hallucination!!!


While calming my agitated feelings, I decided to trust GPT and conduct additional research on KL-Optimal.

To conclude, I was able to generate a video that successfully formed an image even with KL-Optimal.

Barely, not a hallucination... I want to believe.

Things I felt when I managed to form an image
1. Step count: It didn't work at 20, but it appeared at around 50.
2. Sampler: A sampler that shows a person a little at 20 steps might produce a formed image if you increase the number of steps.

However, since 50 steps take quite a long time, one might wonder, wouldn't others be better?

By the way, the sampler was created using one called heun.
It was also a discovery that it could be done with this.

Below is one created with the same sampler and the beta scheduler. 50 steps.
I have the impression that the quality of the video improves as the number of steps increases.

I think this is an area where research will continue to progress in the future.


Regarding videos, the amount of movement is a good point, so I felt that if there is a compatible sampler, KL-Optimal could potentially be one of the options for a scheduler.




同じカテゴリの記事

雨宮 千夏
デジタルマーケター 雨宮 千夏
プロフィールを見る →

アイドルと音楽シーンの取材を担当。公式サイトの公演情報、リリース告知、公開チャートを照合してから構成しています。

おすすめの関連記事