title: Making classifier free guidance help in naive conditioning jumps
author: yoinked
tags: cfg, diffusion, guidance, code
abstract: >-
CFG is a bad idea, what if we naively jump the conditioning and correct it
afterwards?
date_published: 2024-12-08T00:00:00.000Z
paperid: 2024-1208.1
Making classifier free guidance help in naive raw conditioning jumps
1. Abstract
Classifier free guidance (CFG) has always been a nuisance in diffusion models, due to how it works. We present MaHiRo (Making classifier free guidance help in naive raw conditioning leaps), a new novel way to guide diffusion models.
2. Methodology
2.1. Basis
The idea of guidance in diffusion models refers to a way to make the model produce a certain conditioning, usually avoiding a different one. A "naive" way to do this is with CFG, in which the desired cond p
and undesired cond u
are used to shift the p
conditioning away from the u
cond at a certain scale. This method, while simple, works at a basic level, but, this usually harms the model as you destroy the conditioning and pay more attention to the opposite of the u
conditioning. This leads us to our new method.
2.2 Assistance
Given two guidances, delta_a
and delta_b
, which are the changes in the positive conditioning, we can calculate their similarity with several methods, the most useful one being the cosine similarity, which describes just how rotated the two conditionings are, with 0 being perpendicular, 1 being positively parallel and -1 being negatively parallel. We use this in MaHiRo to find how "bad" a leap is.
2.3 Leap and correction
MaHiRo uses a concept known as a "conditioning leaps", in which we use the scale is used to boost a conditioning. In MaHiRo, we take the positive conditioning and leap it to make the leaped positive, we then use this leap and merge it with the true CFG guidance, then use cosine similarity to get the similarity between that and the leaped uncond. If we get a high similarity value, we know that the leap is bad, and should trust the plain CFG output more, if we get a low similarity value, we know that the leap is good, and should follow the leap more instead. We take the similarity value and use that as a weighting factor between the positive leap and the CFG to produce the final conditioning.
3. Results / code
A desmos graph showing the comparison between CFG and MaHiRo is located here, code for usage in ComfyUI is here. Images below are generated with a scale of 2.6:
4. Conclusion
We believe that MaHiRo can improve guidance in diffusion models by avoiding the inverse of the negative conditioning when possible and using the plain positive conditioning.