forked from asagi4/ComfyUI-Adaptive-Guidance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
199 lines (167 loc) · 7.26 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import comfy.samplers
import comfy_extras.nodes_perpneg
import torch
cos = torch.nn.CosineSimilarity(dim=1)
class Guider_AdaptiveGuidance(comfy.samplers.CFGGuider):
threshold_timestep = 0
uz_scale = 0.0
initial_disabled_steps = 0
current_step = 0
def set_cfg(self, cfg):
self.cfg = cfg
def set_threshold(self, threshold):
self.threshold = threshold
def set_uncond_zero_scale(self, scale):
self.uz_scale = scale
def set_initial_disabled_steps(self, steps):
self.initial_disabled_steps = steps
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()
self.current_step += 1
if self.current_step <= self.initial_disabled_steps or 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
uncond_pred, cond_pred = comfy.samplers.calc_cond_batch(
self.inner_model, [uncond, cond], x, timestep, model_options
)
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"\nAdaptiveGuidance: Cosine similarity {sim:.4f} exceeds threshold, setting CFG to 1.0")
self.threshold_timestep = ts
return comfy.samplers.cfg_function(
self.inner_model,
cond_pred,
uncond_pred,
self.cfg,
x,
timestep,
model_options=model_options,
cond=cond,
uncond=uncond,
)
class AdaptiveGuidanceGuiderV2:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
"positive": ("CONDITIONING",),
"negative": ("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}),
"initial_disabled_steps": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}),
},
"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, threshold, cfg, initial_disabled_steps, 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)
g.set_cfg(cfg)
g.set_initial_disabled_steps(initial_disabled_steps)
return (g,)
class Guider_PerpNegAG(comfy_extras.nodes_perpneg.Guider_PerpNeg):
threshold_timestep = 0
uz_scale = 0.0
initial_disabled_steps = 0
current_step = 0
def set_threshold(self, threshold):
self.threshold = threshold
def set_uncond_zero_scale(self, scale):
self.uz_scale = scale
def set_initial_disabled_steps(self, steps):
self.initial_disabled_steps = steps
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()
self.current_step += 1
if self.current_step <= self.initial_disabled_steps or 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
uncond_pred, cond_pred = comfy.samplers.calc_cond_batch(
self.inner_model, [uncond, cond], x, timestep, model_options
)
if not self.threshold >= 1.0:
# 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(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,
cond_pred,
uncond_pred,
self.cfg,
x,
timestep,
model_options=model_options,
cond=cond,
uncond=uncond,
)
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}),
"initial_disabled_steps": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}),
},
"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, initial_disabled_steps, 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)
g.set_initial_disabled_steps(initial_disabled_steps)
return (g,)
NODE_CLASS_MAPPINGS = {
"AdaptiveGuidanceV2": AdaptiveGuidanceGuiderV2,
#"PerpNegAdaptiveGuidanceGuider": PerpNegAGGuider,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"AdaptiveGuidanceV2": "AdaptiveGuiderWithInit",
#"PerpNegAdaptiveGuidanceGuider": "PerpNegAdaptiveGuider",
}