Skip to content

Commit e11052a

Browse files
Add ipndm sampler.
1 parent 97ae6ef commit e11052a

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

comfy/k_diffusion/sampling.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,3 +841,44 @@ def sample_heunpp2(model, x, sigmas, extra_args=None, callback=None, disable=Non
841841
d_prime = w1 * d + w2 * d_2 + w3 * d_3
842842
x = x + d_prime * dt
843843
return x
844+
845+
846+
#From https://github.com/zju-pi/diff-sampler/blob/main/diff-solvers-main/solvers.py
847+
#under Apache 2 license
848+
def sample_ipndm(model, x, sigmas, extra_args=None, callback=None, disable=None, max_order=4):
849+
extra_args = {} if extra_args is None else extra_args
850+
s_in = x.new_ones([x.shape[0]])
851+
852+
x_next = x
853+
854+
buffer_model = []
855+
for i in trange(len(sigmas) - 1, disable=disable):
856+
t_cur = sigmas[i]
857+
t_next = sigmas[i + 1]
858+
859+
x_cur = x_next
860+
861+
denoised = model(x_cur, t_cur * s_in, **extra_args)
862+
if callback is not None:
863+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
864+
865+
d_cur = (x_cur - denoised) / t_cur
866+
867+
order = min(max_order, i+1)
868+
if order == 1: # First Euler step.
869+
x_next = x_cur + (t_next - t_cur) * d_cur
870+
elif order == 2: # Use one history point.
871+
x_next = x_cur + (t_next - t_cur) * (3 * d_cur - buffer_model[-1]) / 2
872+
elif order == 3: # Use two history points.
873+
x_next = x_cur + (t_next - t_cur) * (23 * d_cur - 16 * buffer_model[-1] + 5 * buffer_model[-2]) / 12
874+
elif order == 4: # Use three history points.
875+
x_next = x_cur + (t_next - t_cur) * (55 * d_cur - 59 * buffer_model[-1] + 37 * buffer_model[-2] - 9 * buffer_model[-3]) / 24
876+
877+
if len(buffer_model) == max_order - 1:
878+
for k in range(max_order - 2):
879+
buffer_model[k] = buffer_model[k+1]
880+
buffer_model[-1] = d_cur
881+
else:
882+
buffer_model.append(d_cur)
883+
884+
return x_next

comfy/samplers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ def max_denoise(self, model_wrap, sigmas):
539539

540540
KSAMPLER_NAMES = ["euler", "euler_ancestral", "heun", "heunpp2","dpm_2", "dpm_2_ancestral",
541541
"lms", "dpm_fast", "dpm_adaptive", "dpmpp_2s_ancestral", "dpmpp_sde", "dpmpp_sde_gpu",
542-
"dpmpp_2m", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "dpmpp_3m_sde", "dpmpp_3m_sde_gpu", "ddpm", "lcm"]
542+
"dpmpp_2m", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "dpmpp_3m_sde", "dpmpp_3m_sde_gpu", "ddpm", "lcm",
543+
"ipndm"]
543544

544545
class KSAMPLER(Sampler):
545546
def __init__(self, sampler_function, extra_options={}, inpaint_options={}):

0 commit comments

Comments
 (0)