Skip to content
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

Add support for APG (adaptive projected guidance) #593

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

stduhpf
Copy link
Contributor

@stduhpf stduhpf commented Feb 12, 2025

Implements this paper: https://arxiv.org/abs/2410.02416

TLDR:

APG is a set of 3 modilfications for CFG:

  • reverse momentum: The CFG update is getting steered away from (or closer to) the previous step's CFG update ( --apg-momentum)
  • normalization: the L2 norm of the CFG update is clamped to some value "norm threshold" (--apg-nt)
  • projection: the CFG update (out_uncond-out_cond) is orthogonally projected on the same "direction" as out_cond. The final update is linearly interpolated between the original update and the projected update with the parameter "eta" (--apg-eta)

No extra forward pass is required, so the performance cost is negligible.

Thanks mostly to the normalization, but also the projection, this allows to take adventage of very large CFG scales without getting deep-fried output images. I'm not sure how usefull the reverse momentum really is, but it was in the paper so I added it too.

Usage

[your usual command with cfg here] --apg-eta 0 --apg-nt 5 --apg-momentum -0.5

Recommanded values:

  • eta between 0 and 1, closer to 0 seems better. In the paper, they recommend setting it to 0 altogether
  • norm threshold between 1 and 15
  • momentum negative and close to 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant