The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Implement my alternative take on CFG++ as the euler_pp sampler.
comfyanonymous69d710e40f43
diff --git a/comfy/k_diffusion/sampling.py b/comfy/k_diffusion/sampling.pyindex 78896b8b5..9b58cb1dc 100644--- a/comfy/k_diffusion/sampling.py+++ b/comfy/k_diffusion/sampling.py@@ -7,7 +7,7 @@ import torchsdefrom tqdm.auto import trange, tqdmfrom . import utils-+import comfy.model_patcherdef append_zero(x):return torch.cat([x, x.new_zeros([1])])@@ -945,3 +945,56 @@ def sample_ipndm_v(model, x, sigmas, extra_args=None, callback=None, disable=Nonbuffer_model.append(d_cur.detach())return x_next++[email protected]_grad()+def sample_euler_pp(model, x, sigmas, extra_args=None, callback=None, disable=None):+ extra_args = {} if extra_args is None else extra_args++ temp = [0]+ def post_cfg_function(args):+ temp[0] = args["uncond_denoised"]+ return args["denoised"]++ model_options = extra_args.get("model_options", {}).copy()+ extra_args["model_options"] = comfy.model_patcher.set_model_options_post_cfg_function(model_options, post_cfg_function, disable_cfg1_optimization=True)++ s_in = x.new_ones([x.shape[0]])+ for i in trange(len(sigmas) - 1, disable=disable):+ sigma_hat = sigmas[i]+ denoised = model(x, sigma_hat * s_in, **extra_args)+ d = to_d(x - denoised + temp[0], sigma_hat, denoised)+ if callback is not None:+ callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})+ dt = sigmas[i + 1] - sigma_hat+ # Euler method+ x = x + d * dt+ return x+[email protected]_grad()+def sample_euler_ancestral_pp(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):+ """Ancestral sampling with Euler method steps."""+ extra_args = {} if extra_args is None else extra_args+ noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler++ temp = [0]+ def post_cfg_function(args):+ temp[0] = args["uncond_denoised"]+ return args["denoised"]++ model_options = extra_args.get("model_options", {}).copy()+ extra_args["model_options"] = comfy.model_patcher.set_model_options_post_cfg_function(model_options, post_cfg_function, disable_cfg1_optimization=True)++ s_in = x.new_ones([x.shape[0]])+ for i in trange(len(sigmas) - 1, disable=disable):+ denoised = model(x, sigmas[i] * s_in, **extra_args)+ sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)+ if callback is not None:+ callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})+ d = to_d(x - denoised + temp[0], sigmas[i], denoised)+ # Euler method+ dt = sigma_down - sigmas[i]+ x = x + d * dt+ if sigmas[i + 1] > 0:+ x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up+ return xdiff --git a/comfy/samplers.py b/comfy/samplers.pyindex 55b1486b4..c3a80dde0 100644--- a/comfy/samplers.py+++ b/comfy/samplers.py@@ -537,7 +537,7 @@ class Sampler:sigma = float(sigmas[0])return math.isclose(max_sigma, sigma, rel_tol=1e-05) or sigma > max_sigma-KSAMPLER_NAMES = ["euler", "euler_ancestral", "heun", "heunpp2","dpm_2", "dpm_2_ancestral",+KSAMPLER_NAMES = ["euler", "euler_pp", "euler_ancestral", "euler_ancestral_pp", "heun", "heunpp2","dpm_2", "dpm_2_ancestral","lms", "dpm_fast", "dpm_adaptive", "dpmpp_2s_ancestral", "dpmpp_sde", "dpmpp_sde_gpu","dpmpp_2m", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "dpmpp_3m_sde", "dpmpp_3m_sde_gpu", "ddpm", "lcm","ipndm", "ipndm_v"]diff --git a/comfy_extras/nodes_advanced_samplers.py b/comfy_extras/nodes_advanced_samplers.pyindex 7aa89c644..cee3a10c4 100644--- a/comfy_extras/nodes_advanced_samplers.py+++ b/comfy_extras/nodes_advanced_samplers.py@@ -82,29 +82,6 @@ def sample_euler_cfgpp(model, x, sigmas, extra_args=None, callback=None, disablex = denoised + sigmas[i + 1] * dreturn x[email protected]_grad()-def sample_euler_cfgpp_alt(model, x, sigmas, extra_args=None, callback=None, disable=None):- extra_args = {} if extra_args is None else extra_args-- temp = [0]- def post_cfg_function(args):- temp[0] = args["uncond_denoised"]- return args["denoised"]-- model_options = extra_args.get("model_options", {}).copy()- extra_args["model_options"] = comfy.model_patcher.set_model_options_post_cfg_function(model_options, post_cfg_function, disable_cfg1_optimization=True)-- s_in = x.new_ones([x.shape[0]])- for i in trange(len(sigmas) - 1, disable=disable):- sigma_hat = sigmas[i]- denoised = model(x, sigma_hat * s_in, **extra_args)- d = to_d(x - denoised + temp[0], sigma_hat, denoised)- if callback is not None:- callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})- dt = sigmas[i + 1] - sigma_hat- # Euler method- x = x + d * dt- return xclass SamplerEulerCFGpp:@classmethod@@ -122,7 +99,7 @@ class SamplerEulerCFGpp:if version == "regular":sampler = comfy.samplers.KSAMPLER(sample_euler_cfgpp)else:- sampler = comfy.samplers.KSAMPLER(sample_euler_cfgpp_alt)+ sampler = comfy.samplers.ksampler("euler_pp")return (sampler, )NODE_CLASS_MAPPINGS = {