forked from ClownsharkBatwing/RES4LYF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_samplers.py
303 lines (267 loc) · 12.6 KB
/
extra_samplers.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import torch
from torch import FloatTensor
from tqdm.auto import trange
from math import pi
from comfy.k_diffusion.sampling import get_ancestral_step, to_d
import functools
from .noise_classes import *
@cast_fp64
@torch.no_grad()
def sample_dpmpp_sde_advanced(
model, x, sigmas, extra_args=None, callback=None, disable=None,
eta=1., s_noise=1., noise_sampler=None, r=1/2, k=1.0, scale=0.1, noise_sampler_type="brownian", alpha: FloatTensor = torch.zeros((1,))
):
#DPM-Solver++ (stochastic with ita parameter).
if len(sigmas) <= 1:
return x
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
seed = extra_args.get("seed", None)
noise_sampler = NOISE_GENERATOR_CLASSES.get(noise_sampler_type)(x=x, seed=seed, sigma_min=sigma_min, sigma_max=sigma_max)
extra_args = {} if extra_args is None else extra_args
s_in = x.new_ones([x.shape[0]])
sigma_fn = lambda t: t.neg().exp()
t_fn = lambda sigma: sigma.log().neg()
for i in trange(len(sigmas) - 1, disable=disable):
denoised = model(x, sigmas[i] * s_in, **extra_args)
if callback is not None:
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
if noise_sampler_type == "fractal":
noise_sampler.alpha = alpha[i]
noise_sampler.k = k
noise_sampler.scale = scale
if sigmas[i + 1] == 0:
# Euler method
d = to_d(x, sigmas[i], denoised)
dt = sigmas[i + 1] - sigmas[i]
x = x + d * dt
else:
# DPM-Solver++
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
h = t_next - t
s = t + h * r
fac = 1 / (2 * r)
# Step 1
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(s), eta)
s_ = t_fn(sd)
x_2 = (sigma_fn(s_) / sigma_fn(t)) * x - (t - s_).expm1() * denoised
x_2 = x_2 + noise_sampler(sigma=sigma_fn(t), sigma_next=sigma_fn(s)) * s_noise * su
denoised_2 = model(x_2, sigma_fn(s) * s_in, **extra_args)
# Step 2
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(t_next), eta)
t_next_ = t_fn(sd)
denoised_d = (1 - fac) * denoised + fac * denoised_2
x = (sigma_fn(t_next_) / sigma_fn(t)) * x - (t - t_next_).expm1() * denoised_d
x = x + noise_sampler(sigma=sigma_fn(t), sigma_next=sigma_fn(t_next)) * s_noise * su
return x
@cast_fp64
def sample_dpmpp_dualsdemomentum_advanced(model, x, sigmas, seed=42, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler_type="gaussian", noise_sampler=None, r=1/2, momentum=0.0, momentums=None, etas=None, s_noises=None,rs=None,scheduled_r=False):
return sample_dpmpp_dualsde_momentum_advanced(model, x, sigmas, seed=seed, extra_args=extra_args, callback=callback, disable=disable, eta=etas, s_noise=s_noises, noise_sampler_type=noise_sampler_type, noise_sampler=noise_sampler, r=rs, momentum=momentums, scheduled_r=False)
@cast_fp64
@torch.no_grad()
def sample_dpmpp_dualsde_momentum_advanced (
model,
x,
sigmas,
seed=42,
extra_args=None,
callback=None,
disable=None,
noise_sampler=None,
noise_sampler_type=None,
momentum: FloatTensor = torch.zeros((1,)),
eta: FloatTensor = torch.zeros((1,)),
s_noise: FloatTensor = torch.zeros((1,)),
r: FloatTensor = torch.zeros((1,)),
scheduled_r=False
):
"""DPM-Solver++ (Stochastic with Momentum). Personal modified sampler by Clybius"""
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
noise_sampler = NOISE_GENERATOR_CLASSES.get(noise_sampler_type)(x=x, seed=seed, sigma_min=sigma_min, sigma_max=sigma_max)
extra_args = {} if extra_args is None else extra_args
s_in = x.new_ones([x.shape[0]])
sigma_fn = lambda t: t.neg().exp()
t_fn = lambda sigma: sigma.log().neg()
denoisedsde_1, denoisedsde_2, denoisedsde_3 = None, None, None
h_1, h_2, h_3 = None, None, None
def momentum_func(diff, velocity, timescale=1.0, current_momentum=0): # Diff is current diff, vel is previous diff
offset=-current_momentum / 2.0
if velocity is None:
momentum_vel = diff
else:
momentum_vel = current_momentum * (timescale + offset) * velocity + (1 - current_momentum * (timescale + offset)) * diff
return momentum_vel
vel = None
vel_2 = None
vel_sde = None
current_r = r[0]
for i in trange(len(sigmas) - 1, disable=disable):
time = sigmas[i] / sigma_max
current_momentum = momentum[i]
current_eta = eta[i]
current_s_noise = s_noise[i]
if scheduled_r == True:
current_r = r[i]
denoised = model(x, sigmas[i] * s_in, **extra_args)
if callback is not None:
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
if sigmas[i + 1] == 0:
# Euler method
d = to_d(x, sigmas[i], denoised)
dt = sigmas[i + 1] - sigmas[i]
x = x + d * dt
else:
# DPM-Solver++
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
h = t_next - t
h_eta = h * (current_eta + 1)
s = t + h * current_r
fac = 1 / (2 * current_r)
# Step 1
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(s), current_eta)
s_ = t_fn(sd)
diff_2 = momentum_func((t - s_).expm1() * denoised, vel_2, time, current_momentum)
vel_2 = diff_2
x_2 = (sigma_fn(s_) / sigma_fn(t)) * x - diff_2
x_2 = x_2 + noise_sampler(sigma=sigma_fn(t), sigma_next=sigma_fn(s)) * current_s_noise * su
denoised_2 = model(x_2, sigma_fn(s) * s_in, **extra_args)
# Step 2
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(t_next), current_eta)
t_next_ = t_fn(sd)
denoised_d = (1 - fac) * denoised + fac * denoised_2
diff = momentum_func((t - t_next_).expm1() * denoised_d, vel, time, current_momentum)
vel = diff
x = (sigma_fn(t_next_) / sigma_fn(t)) * x - diff
if h_3 is not None:
r0 = h_3 / h_2
r1 = h_2 / h
r2 = h / h_1
d1_0 = (denoised_d - denoisedsde_1) / r2
d1_1 = (denoisedsde_1 - denoisedsde_2) / r1
d1_2 = (denoisedsde_2 - denoisedsde_3) / r0
d1 = d1_0 + (d1_0 - d1_1) * r2 / (r2 + r1) + ((d1_0 - d1_1) * r2 / (r2 + r1) - (d1_1 - d1_2) * r1 / (r0 + r1)) * r2 / ((r2 + r1) * (r0 + r1))
d2 = (d1_0 - d1_1) / (r2 + r1) + ((d1_0 - d1_1) * r2 / (r2 + r1) - (d1_1 - d1_2) * r1 / (r0 + r1)) / ((r2 + r1) * (r0 + r1))
phi_3 = h_eta.neg().expm1() / h_eta + 1
phi_4 = phi_3 / h_eta - 0.5
diff = momentum_func(phi_3 * d1 - phi_4 * d2, vel_sde, time, current_momentum)
vel_sde = diff
x = x + diff
elif h_2 is not None:
r0 = h_1 / h
r1 = h_2 / h
d1_0 = (denoised_d - denoisedsde_1) / r0
d1_1 = (denoisedsde_1 - denoisedsde_2) / r1
d1 = d1_0 + (d1_0 - d1_1) * r0 / (r0 + r1)
d2 = (d1_0 - d1_1) / (r0 + r1)
phi_2 = h_eta.neg().expm1() / h_eta + 1
phi_3 = phi_2 / h_eta - 0.5
diff = momentum_func(phi_2 * d1 - phi_3 * d2, vel_sde, time, current_momentum)
vel_sde = diff
x = x + diff
elif h_1 is not None:
current_r = h_1 / h
d = (denoised_d - denoisedsde_1) / current_r
phi_2 = h_eta.neg().expm1() / h_eta + 1
diff = momentum_func(phi_2 * d, vel_sde, time)
vel_sde = diff
x = x + diff
if current_eta:
x = x + noise_sampler(sigma=sigma_fn(t), sigma_next=sigma_fn(t_next)) * current_s_noise * su
#if 'denoised_d' in locals():
denoisedsde_1, denoisedsde_2, denoisedsde_3 = denoised_d, denoisedsde_1, denoisedsde_2
#if 'h' in locals():
h_1, h_2, h_3 = h, h_1, h_2
return x
# Many thanks to Kat + Birch-San for this wonderful sampler implementation! https://github.com/Birch-san/sdxl-play/commits/res/
from .refined_exp_solver import sample_refined_exp_s_advanced
@cast_fp64
def sample_res_solver_advanced(model,
x,
sigmas, itas, c2s, momentums, offsets,
guides_1, guides_2, latent_guide_1, latent_guide_2, guide_mode_1, guide_mode_2, guide_1_channels,
alpha, k, clownseed=0, alphas=None, latent_noise=None,
extra_args=None, callback=None, disable=None, noise_sampler_type="gaussian", noise_sampler=None, denoise_to_zero=True, simple_phi_calc=False, c2=0.5, momentum=0.0, offset=0.0):
return sample_refined_exp_s_advanced(
model=model,
x=x,
clownseed=clownseed,
sigmas=sigmas,
latent_guide_1=latent_guide_1,
latent_guide_2=latent_guide_2,
guide_1=guides_1,
guide_2=guides_2,
guide_mode_1=guide_mode_1,
guide_mode_2=guide_mode_2,
guide_1_channels=guide_1_channels,
extra_args=extra_args,
callback=callback,
disable=disable,
noise_sampler=noise_sampler,
denoise_to_zero=denoise_to_zero,
simple_phi_calc=simple_phi_calc,
c2=c2s,
ita=itas,
momentum=momentums,
offset=offsets,
alpha=alphas,
noise_sampler_type=noise_sampler_type,
k=k,
latent_noise=latent_noise
)
# The following function adds the samplers during initialization, in __init__.py
def add_samplers():
from comfy.samplers import KSampler, k_diffusion_sampling
if hasattr(KSampler, "DISCARD_PENULTIMATE_SIGMA_SAMPLERS"):
KSampler.DISCARD_PENULTIMATE_SIGMA_SAMPLERS |= discard_penultimate_sigma_samplers
added = 0
for sampler in extra_samplers: #getattr(self, "sample_{}".format(extra_samplers))
if sampler not in KSampler.SAMPLERS:
try:
idx = KSampler.SAMPLERS.index("uni_pc_bh2") # Last item in the samplers list
KSampler.SAMPLERS.insert(idx+1, sampler) # Add our custom samplers
setattr(k_diffusion_sampling, "sample_{}".format(sampler), extra_samplers[sampler])
added += 1
except ValueError as _err:
pass
if added > 0:
import importlib
importlib.reload(k_diffusion_sampling)
# The following function adds the samplers during initialization, in __init__.py
def add_schedulers():
from comfy.samplers import KSampler, k_diffusion_sampling
added = 0
for scheduler in extra_schedulers: #getattr(self, "sample_{}".format(extra_samplers))
if scheduler not in KSampler.SCHEDULERS:
try:
idx = KSampler.SCHEDULERS.index("ddim_uniform") # Last item in the samplers list
KSampler.SCHEDULERS.insert(idx+1, scheduler) # Add our custom samplers
setattr(k_diffusion_sampling, "get_sigmas_{}".format(scheduler), extra_schedulers[scheduler])
added += 1
except ValueError as err:
pass
if added > 0:
import importlib
importlib.reload(k_diffusion_sampling)
# Add any extra samplers to the following dictionary
#from .refined_exp_solver import sample_refined_exp_s, sample_refined_exp_s_advanced
extra_samplers = {
"res_momentumized_advanced": sample_res_solver_advanced,
"dpmpp_dualsde_momentumized_advanced": sample_dpmpp_dualsdemomentum_advanced,
"dpmpp_sde_advanced": sample_dpmpp_sde_advanced,
}
discard_penultimate_sigma_samplers = set((
"dpmpp_dualsde_momentumized",
"clyb_4m_sde_momentumized"
))
def get_sigmas_simple_exponential(model, steps):
s = model.model_sampling
sigs = []
ss = len(s.sigmas) / steps
for x in range(steps):
sigs += [float(s.sigmas[-(1 + int(x * ss))])]
sigs += [0.0]
sigs = torch.FloatTensor(sigs)
exp = torch.exp(torch.log(torch.linspace(1, 0, steps + 1)))
return sigs * exp
extra_schedulers = {
"simple_exponential": get_sigmas_simple_exponential
}