ComfyUI/comfy_extras/nodes_perpneg.py

57 lines
1.9 KiB
Python
Raw Normal View History

2023-12-15 18:58:16 +00:00
import torch
import comfy.model_management
2024-04-05 15:33:36 +00:00
import comfy.sampler_helpers
2023-12-15 18:58:16 +00:00
import comfy.samplers
import comfy.utils
2024-04-05 15:33:36 +00:00
#TODO: This node should be removed and replaced with one that uses the new Guider/SamplerCustomAdvanced.
2023-12-15 18:58:16 +00:00
class PerpNeg:
@classmethod
def INPUT_TYPES(s):
return {"required": {"model": ("MODEL", ),
"empty_conditioning": ("CONDITIONING", ),
2024-03-18 19:51:23 +00:00
"neg_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.01}),
2023-12-15 18:58:16 +00:00
}}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "_for_testing"
def patch(self, model, empty_conditioning, neg_scale):
2023-12-15 18:58:16 +00:00
m = model.clone()
2024-04-05 15:33:36 +00:00
nocond = comfy.sampler_helpers.convert_cond(empty_conditioning)
2023-12-15 18:58:16 +00:00
def cfg_function(args):
model = args["model"]
noise_pred_pos = args["cond_denoised"]
noise_pred_neg = args["uncond_denoised"]
cond_scale = args["cond_scale"]
x = args["input"]
sigma = args["sigma"]
model_options = args["model_options"]
2023-12-16 07:30:16 +00:00
nocond_processed = comfy.samplers.encode_model_conds(model.extra_conds, nocond, x, x.device, "negative")
(noise_pred_nocond,) = comfy.samplers.calc_cond_batch(model, [nocond_processed], x, sigma, model_options)
2023-12-15 18:58:16 +00:00
pos = noise_pred_pos - noise_pred_nocond
neg = noise_pred_neg - noise_pred_nocond
perp = neg - ((torch.mul(neg, pos).sum())/(torch.norm(pos)**2)) * pos
2023-12-15 18:58:16 +00:00
perp_neg = perp * neg_scale
cfg_result = noise_pred_nocond + cond_scale*(pos - perp_neg)
cfg_result = x - cfg_result
return cfg_result
m.set_model_sampler_cfg_function(cfg_function)
return (m, )
NODE_CLASS_MAPPINGS = {
"PerpNeg": PerpNeg,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"PerpNeg": "Perp-Neg",
}