-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Adaptive Projected Guidance #9626
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
Conversation
Thanks for your interest in our work! Please note that we always convert the output of the model to the denoised predictions (pred_x0) and compute the guidance there. We found that APG performs better when applied to the denoised predictions. We also have a discussion on this step in Section 5.2 (Figure 12). Ideally, APG should be implemented like this: x0_pred_text = get_x0_from_noise(noise_pred_text, latents, t)
x0_pred_uncond = get_x0_from_noise(noise_pred_uncond, latents, t)
x0_guided = normalized_guidance(...)
noise_pred = get_noise_from_x0(x0_guided, latents, t) (A better solution would be having a flag that allows the users to choose whether APG should be applied to the model output or the denoised prediction) |
hi @hlky Thanks for the PR! I'm a bit reluctant to add this to SD and SDXL, as these pipelines are already getting bloated and can become overwhelming for newcomers, especially given that this is not the only CFG alternative and won't be the last one. @apolinario has suggested ideas to make guidance a separate "component" that you can swap out just like schedulers - I'm happy to explore that now! I will draft a PR soon, and we can work together and experiment with different ideas from there! This may also fit better in an experimental project that we are working on to make a composable pipeline that targets the community and company users and allows them to mix and match different features without writing much code. So, we will see! |
@Msadat97 Thanks! I missed that section. Applying the guidance to the denoised predictions does indeed produce much better results: @yiyixuxu After fixing this locally for denoised predictions I'd have to agree, it needs a little more than present here, specifically we'd actually need some changes to schedulers to allow easy conversion between noised and denoised predictions. Making guidance a separate component sounds like a great idea, hope to see that in soon, I'd be happy to work with you on that and any changes to schedulers. |
@hlky Can you share/commit the change that result to this enhancement? |
a528e6c
to
c7e62c4
Compare
Certainly, I've pushed those changes, however please note this will only work with some schedulers like Euler, and while it does work for 2nd order schedulers like Heun and DPM2 it's not 100% as the |
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. |
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. Please note that issues that do not follow the contributing guidelines are likely to be ignored. |
What does this PR do?
This PR implements APG (Adaptive Projected Guidance) from Algorithm 1 in
Eliminating Oversaturation and Artifacts of High Guidance Scales in Diffusion Models
.Algorithm 1 is slightly modified to combine
project
intonormalized_guidance
, this simply reduces the number of methods to be copied between pipelines.APG is added to
StableDiffusionPipeline
andStableDiffusionXLPipeline
. The following parameters are introduced:Default values are taken from Stable Diffusion XL in Table 10. The existing
eta
parameter is used, rather than adding a new parameter, as per the docstringeta
is only used inDDIMScheduler
so this should be ok.Fixes #9585
Example usage:
CFG:

APG:

There's certainly some improvement, however further testing would be beneficial to confirm the findings in the paper.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
cc @yiyixuxu @asomoza