Skip to content

Commit d159142

Browse files
christian-byrneampagentKosinkadink
authored
refactor: rename Mahiro CFG to Similarity-Adaptive Guidance (#12172)
* refactor: rename Mahiro CFG to Similarity-Adaptive Guidance Rename the display name to better describe what the node does: adaptively blends guidance based on cosine similarity between positive and negative conditions. Amp-Thread-ID: https://ampcode.com/threads/T-019c0d36-8b43-745f-b7b2-e35b53f17fa1 Co-authored-by: Amp <amp@ampcode.com> * feat: add search aliases for old mahiro name Amp-Thread-ID: https://ampcode.com/threads/T-019c0d36-8b43-745f-b7b2-e35b53f17fa1 * rename: Similarity-Adaptive Guidance → Positive-Biased Guidance (per reviewer) - display_name changed to 'Positive-Biased Guidance' to avoid SAG acronym collision - search_aliases expanded: mahiro, mahiro cfg, similarity-adaptive guidance, positive-biased cfg - ruff format applied --------- Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: Jedrzej Kosinski <kosinkadink1@gmail.com>
1 parent 1080bd4 commit d159142

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

comfy_extras/nodes_mahiro.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Mahiro(io.ComfyNode):
1010
def define_schema(cls):
1111
return io.Schema(
1212
node_id="Mahiro",
13-
display_name="Mahiro CFG",
13+
display_name="Positive-Biased Guidance",
1414
category="_for_testing",
1515
description="Modify the guidance to scale more on the 'direction' of the positive prompt rather than the difference between the negative prompt.",
1616
inputs=[
@@ -20,27 +20,35 @@ def define_schema(cls):
2020
io.Model.Output(display_name="patched_model"),
2121
],
2222
is_experimental=True,
23+
search_aliases=[
24+
"mahiro",
25+
"mahiro cfg",
26+
"similarity-adaptive guidance",
27+
"positive-biased cfg",
28+
],
2329
)
2430

2531
@classmethod
2632
def execute(cls, model) -> io.NodeOutput:
2733
m = model.clone()
34+
2835
def mahiro_normd(args):
29-
scale: float = args['cond_scale']
30-
cond_p: torch.Tensor = args['cond_denoised']
31-
uncond_p: torch.Tensor = args['uncond_denoised']
32-
#naive leap
36+
scale: float = args["cond_scale"]
37+
cond_p: torch.Tensor = args["cond_denoised"]
38+
uncond_p: torch.Tensor = args["uncond_denoised"]
39+
# naive leap
3340
leap = cond_p * scale
34-
#sim with uncond leap
41+
# sim with uncond leap
3542
u_leap = uncond_p * scale
3643
cfg = args["denoised"]
3744
merge = (leap + cfg) / 2
3845
normu = torch.sqrt(u_leap.abs()) * u_leap.sign()
3946
normm = torch.sqrt(merge.abs()) * merge.sign()
4047
sim = F.cosine_similarity(normu, normm).mean()
41-
simsc = 2 * (sim+1)
42-
wm = (simsc*cfg + (4-simsc)*leap) / 4
48+
simsc = 2 * (sim + 1)
49+
wm = (simsc * cfg + (4 - simsc) * leap) / 4
4350
return wm
51+
4452
m.set_model_sampler_post_cfg_function(mahiro_normd)
4553
return io.NodeOutput(m)
4654

0 commit comments

Comments
 (0)