Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Bug]: alphas_cumprod are downcasted to half precision during model load despite existing at full precision during sampling #14071

Closed
1 task done
drhead opened this issue Nov 23, 2023 · 1 comment · Fixed by #14145
Labels
bug Report of a confirmed bug

Comments

@drhead
Copy link
Contributor

drhead commented Nov 23, 2023

Is there an existing issue for this?

  • I have searched the existing issues and checked the recent builds/commits

What happened?

During model loading, alphas_cumprod is downcasted to half precision along with the rest of the model. However, any sampler will always use those values as full precision floats. Not only is this not saving memory, it also noticeably changes results due to the loss of precision, and it may in fact be one of the most major factors changing results between mixed and full precision inference. I am considering this to be a bug since it serves no clear optimization purpose and alters results in a way that is less aligned with model training. I have also tested a crude fix for the issue and have found that it does not impact generation speed in a significant manner.

Perhaps more importantly, this problem is an obstacle to implementing zero terminal SNR noise schedules like in #13052, since downcasting the values that we derive SNR from will result in sampler sigmas being rounded to infinity shortly after they pass 65000. It is possible to use a zero terminal SNR noise schedule that even plays nicely with k-diffusion samplers by setting the last alpha_bar value to a very small number like 4.8973451890853435e-08, but in fp16 format this number will be rounded down to zero which will break things.

Steps to reproduce the problem

  1. Load a model
  2. model.half() gets called in modules/sd_models.py, causing model.alphas_cumprod to be downcast
  3. Observe the loss of precision
  4. Inspect the dtype of alphas_cumprod values (or values directly derived from them, like the sigma schedule) as used within a K-diffusion sampler
  5. Observe that they are being used in full-precision float format

What should have happened?

Downcasting the model to half precision should be implemented in a way that leaves alphas_cumprod alone. Since this change will change seeds, a compatibility option should be added that will downcast and upcast a copy of alphas_cumprod right before sampling in order to simulate old behavior. Implementing that compatibility option would also provide all of the necessary infrastructure to cleanly implement an option to use a zero terminal SNR noise schedule so #13052 may as well be implemented at the same time.

Sysinfo

sysinfo-2023-11-23-17-00.txt

What browsers do you use to access the UI ?

Mozilla Firefox

Console logs

This is an internal issue, logs won't help here.

Additional information

No response

@drhead drhead added the bug-report Report of a bug, yet to be confirmed label Nov 23, 2023
@catboxanon catboxanon added bug Report of a confirmed bug and removed bug-report Report of a bug, yet to be confirmed labels Nov 29, 2023
@catboxanon catboxanon linked a pull request Dec 4, 2023 that will close this issue
4 tasks
@ibrainventures
Copy link
Contributor

@drhead

after some tests with the 1.7.dev Version i found a issue with some models according to the downcast. Details what and how i wrote in this issue:

#14610

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Report of a confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants