11 Commits
Author SHA1 Message Date
asagi4 babad4adf8 Bump version 2024-08-01 13:39:07 +03:00
asagi4 b2a168a704 reformat code 2024-08-01 13:38:25 +03:00
asagi4 f939f09181 Merge pull request #4 from chaObserv/perp-neg-ag
Add perpneg-adaptive-guidance guider
2024-08-01 13:37:33 +03:00
asagi4 631141c9e3 Merge branch 'master' into perp-neg-ag 2024-08-01 13:35:41 +03:00
asagi4 a3e939cbfb Merge pull request #3 from chaObserv/refactor-alignment
Refactor names to be more comfy-like
2024-08-01 13:30:13 +03:00
asagi4 c1390008cc Merge pull request #5 from ComfyNodePRs/licence-update
Update PyProject Toml - License
2024-07-31 19:03:33 +03:00
snomiao 73de969fa3 chore(licence-update): Update PyProject Toml - License 2024-07-31 13:52:00 +00:00
chaObserv 39b0a1515a Delete space 2024-07-03 21:53:31 +08:00
chaObserv 48eb69fffc Add perpneg-ag guider 2024-07-03 21:05:51 +08:00
chaObserv 8f86e66d0f fix mapping issue 2024-07-03 17:51:53 +08:00
chaObserv 0980cc7ff1 Refactor guider's name to be more comfy-like 2024-07-03 16:15:34 +08:00
2 changed files with 118 additions and 9 deletions
+116 -7
View File
@@ -1,10 +1,11 @@
import comfy.samplers
import comfy_extras.nodes_perpneg
import torch
cos = torch.nn.CosineSimilarity(dim=1)
class AdaptiveGuider(comfy.samplers.CFGGuider):
class Guider_AdaptiveGuidance(comfy.samplers.CFGGuider):
threshold_timestep = 0
uz_scale = 0.0
@@ -43,7 +44,7 @@ class AdaptiveGuider(comfy.samplers.CFGGuider):
# Is this reshape correct? It at least gives a scalar value...
sim = cos(cond_pred.reshape(1, -1), uncond_pred.reshape(1, -1)).item()
if sim >= self.threshold:
print("AdaptiveGuidance: Cosine similarity", sim, "exceeds threshold, setting CFG to 1.0")
print(f"\nAdaptiveGuidance: Cosine similarity {sim:.4f} exceeds threshold, setting CFG to 1.0")
self.threshold_timestep = ts
return comfy.samplers.cfg_function(
self.inner_model,
@@ -58,7 +59,7 @@ class AdaptiveGuider(comfy.samplers.CFGGuider):
)
class AdaptiveGuidance:
class AdaptiveGuidanceGuider:
@classmethod
def INPUT_TYPES(s):
return {
@@ -73,12 +74,12 @@ class AdaptiveGuidance:
}
RETURN_TYPES = ("GUIDER",)
FUNCTION = "patch"
FUNCTION = "get_guider"
CATEGORY = "sampling/custom_sampling/guiders"
def patch(self, model, positive, negative, threshold, cfg, uncond_zero_scale=0.0):
g = AdaptiveGuider(model)
def get_guider(self, model, positive, negative, threshold, cfg, uncond_zero_scale=0.0):
g = Guider_AdaptiveGuidance(model)
g.set_conds(positive, negative)
g.set_threshold(threshold)
g.set_uncond_zero_scale(uncond_zero_scale)
@@ -87,4 +88,112 @@ class AdaptiveGuidance:
return (g,)
NODE_CLASS_MAPPINGS = {"AdaptiveGuidance": AdaptiveGuidance}
class Guider_PerpNegAG(comfy_extras.nodes_perpneg.Guider_PerpNeg):
threshold_timestep = 0
uz_scale = 0.0
def set_threshold(self, threshold):
self.threshold = threshold
def set_uncond_zero_scale(self, scale):
self.uz_scale = scale
def zero_cond(self, args):
cond = args["cond_denoised"]
x = args["input"]
x -= x.mean()
cond -= cond.mean()
return x - (cond / cond.std() ** 0.5) * self.uz_scale
def predict_noise(self, x, timestep, model_options={}, seed=None):
cond = self.conds.get("positive")
uncond = self.conds.get("negative")
ts = timestep[0].item()
if self.threshold_timestep > ts:
if self.uz_scale > 0.0:
model_options = model_options.copy()
model_options["sampler_cfg_function"] = self.zero_cond
return comfy.samplers.sampling_function(
self.inner_model, x, timestep, uncond, cond, 1.0, model_options=model_options, seed=seed
)
self.threshold_timestep = 0
# From comfy_extras.nodes_perpneg - Guider_PerpNeg
# No need for calculating perp-neg when skipping negative
empty_cond = self.conds.get("empty_negative_prompt")
(cond_pred, uncond_pred, empty_cond_pred) = comfy.samplers.calc_cond_batch(
self.inner_model, [cond, uncond, empty_cond], x, timestep, model_options
)
cfg_result = comfy_extras.nodes_perpneg.perp_neg(
x, cond_pred, uncond_pred, empty_cond_pred, self.neg_scale, self.cfg
)
if not self.threshold >= 1.0:
sim = cos(cond_pred.reshape(1, -1), uncond_pred.reshape(1, -1)).item()
if sim >= self.threshold:
print(f"\nPerpNegAG: Cosine similarity {sim:.4f} exceeds threshold, setting CFG to 1.0")
self.threshold_timestep = ts
# From comfy_extras.nodes_perpneg - Guider_PerpNeg
for fn in model_options.get("sampler_post_cfg_function", []):
args = {
"denoised": cfg_result,
"cond": cond,
"uncond": uncond,
"model": self.inner_model,
"uncond_denoised": uncond_pred,
"cond_denoised": cond_pred,
"sigma": timestep,
"model_options": model_options,
"input": x,
# not in the original call in samplers.py:cfg_function, but made available for future hooks
"empty_cond": empty_cond,
"empty_cond_denoised": empty_cond_pred,
}
cfg_result = fn(args)
return cfg_result
class PerpNegAGGuider:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
"positive": ("CONDITIONING",),
"negative": ("CONDITIONING",),
"empty_conditioning": ("CONDITIONING",),
"threshold": ("FLOAT", {"default": 0.990, "min": 0.90, "max": 1.0, "step": 0.001, "round": 0.001}),
"cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step": 0.1, "round": 0.01}),
"neg_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.01}),
},
"optional": {"uncond_zero_scale": ("FLOAT", {"default": 0.0, "max": 2.0, "step": 0.01})},
}
RETURN_TYPES = ("GUIDER",)
FUNCTION = "get_guider"
CATEGORY = "sampling/custom_sampling/guiders"
def get_guider(
self, model, positive, negative, empty_conditioning, threshold, cfg, neg_scale, uncond_zero_scale=0.0
):
g = Guider_PerpNegAG(model)
g.set_conds(positive, negative, empty_conditioning)
g.set_threshold(threshold)
g.set_uncond_zero_scale(uncond_zero_scale)
g.set_cfg(cfg, neg_scale)
return (g,)
NODE_CLASS_MAPPINGS = {
"AdaptiveGuidance": AdaptiveGuidanceGuider,
"PerpNegAdaptiveGuidanceGuider": PerpNegAGGuider,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"AdaptiveGuidance": "AdaptiveGuider",
"PerpNegAdaptiveGuidanceGuider": "PerpNegAdaptiveGuider",
}
+2 -2
View File
@@ -1,8 +1,8 @@
[project]
name = "comfyui-adaptive-guidance"
description = "An implementation of adaptive guidance for ComfyUI\nSee https://bcv-uniandes.github.io/adaptiveguidance-wp/"
version = "0.1.0"
license = "GPL-3.0"
version = "0.2.0"
license = { text = "GNU General Public License v3.0" }
[project.urls]
Repository = "https://github.com/asagi4/ComfyUI-Adaptive-Guidance"