The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Add cfgpp_ud10_ab sampler. (#15951)
comfyanonymous3ac5d7941dfa
diff --git a/comfy/k_diffusion/sampling.py b/comfy/k_diffusion/sampling.pyindex 11b2479e0..4a638008a 100644--- a/comfy/k_diffusion/sampling.py+++ b/comfy/k_diffusion/sampling.py@@ -416,6 +416,74 @@ def linear_multistep_coeff(order, t, i, j):return integrate.quad(fn, t[i], t[i + 1], epsrel=1e-4)[0]+def _sample_cfgpp_history(model, x, sigmas, extra_args=None, callback=None, disable=None, history_weight=0.5, zero_weight=None, zero_order=1, uncond_history_weight=0.0):+ """CFG++ Euler with variable-step AB2 history and optional sigma-zero extrapolation."""+ extra_args = {} if extra_args is None else extra_args+ model_sampling = model.inner_model.model_patcher.get_model_object("model_sampling")+ lambda_fn = partial(sigma_to_half_log_snr, model_sampling=model_sampling)+ s_in = x.new_ones([x.shape[0]])+ sigmas_cpu = sigmas.detach().cpu().numpy()+ derivatives = []+ denoised_history = []+ old_uncond_d = None+ uncond_denoised = None++ def post_cfg_function(args):+ nonlocal uncond_denoised+ uncond_denoised = args["uncond_denoised"] if args["uncond"] is not None else args["cond_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)++ 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})++ alpha_s = sigmas[i] * lambda_fn(sigmas[i]).exp()+ alpha_t = sigmas[i + 1] * lambda_fn(sigmas[i + 1]).exp() if sigmas[i + 1] != 0 else sigmas[i + 1].new_ones([])+ current_uncond_d = to_d(x, sigmas[i], alpha_s * uncond_denoised)+ uncond_d = current_uncond_d+ dt = sigmas[i + 1] - sigmas[i]+ if i > 0 and uncond_history_weight:+ step_ratio = dt / (sigmas[i] - sigmas[i - 1])+ uncond_d = uncond_d + uncond_history_weight * step_ratio * (current_uncond_d - old_uncond_d)+ euler_step = alpha_t * denoised + sigmas[i + 1] * uncond_d - x+ d = euler_step / dt+ derivatives.append(d)+ if len(derivatives) > 2:+ derivatives.pop(0)++ if len(derivatives) == 1:+ step = euler_step+ else:+ coeffs = [linear_multistep_coeff(2, sigmas_cpu, i, j) for j in range(2)]+ history_step = sum(coeff * derivative for coeff, derivative in zip(coeffs, reversed(derivatives)))+ step = torch.lerp(euler_step, history_step, history_weight)+ x = x + step+ if sigmas[i + 1] == 0 and zero_weight is not None and denoised_history:+ if zero_order == 2 and len(denoised_history) > 1:+ sigma_0, sigma_1, sigma_2 = sigmas[i - 2], sigmas[i - 1], sigmas[i]+ weight_0 = sigma_1 * sigma_2 / ((sigma_0 - sigma_1) * (sigma_0 - sigma_2))+ weight_1 = sigma_0 * sigma_2 / ((sigma_1 - sigma_0) * (sigma_1 - sigma_2))+ weight_2 = sigma_0 * sigma_1 / ((sigma_2 - sigma_0) * (sigma_2 - sigma_1))+ zero_prediction = weight_0 * denoised_history[-2] + weight_1 * denoised_history[-1] + weight_2 * denoised+ else:+ denoised_slope = (denoised - denoised_history[-1]) / (sigmas[i] - sigmas[i - 1])+ zero_prediction = denoised - sigmas[i] * denoised_slope+ x = torch.lerp(x, zero_prediction, zero_weight)+ denoised_history.append(denoised)+ if len(denoised_history) > 2:+ denoised_history.pop(0)+ old_uncond_d = current_uncond_d+ return x+++def sample_cfgpp_ud10_ab(model, x, sigmas, extra_args=None, callback=None, disable=None):+ return _sample_cfgpp_history(model, x, sigmas, extra_args=extra_args, callback=callback, disable=disable, history_weight=0.25, zero_weight=1.0, uncond_history_weight=0.1)++@torch.no_grad()def sample_lms(model, x, sigmas, extra_args=None, callback=None, disable=None, order=4):extra_args = {} if extra_args is None else extra_argsdiff --git a/comfy/samplers.py b/comfy/samplers.pyindex 94307c1a7..17f7bb8ba 100755--- a/comfy/samplers.py+++ b/comfy/samplers.py@@ -971,7 +971,7 @@ class Sampler:KSAMPLER_NAMES = ["euler", "euler_cfg_pp", "euler_ancestral", "euler_ancestral_cfg_pp", "heun", "heunpp2", "exp_heun_2_x0", "exp_heun_2_x0_sde", "dpm_2", "dpm_2_ancestral","lms", "dpm_fast", "dpm_adaptive", "dpmpp_2s_ancestral", "dpmpp_2s_ancestral_cfg_pp", "dpmpp_sde", "dpmpp_sde_gpu","dpmpp_2m", "dpmpp_2m_cfg_pp", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "dpmpp_2m_sde_heun", "dpmpp_2m_sde_heun_gpu", "dpmpp_3m_sde", "dpmpp_3m_sde_gpu", "ddpm", "lcm",- "ipndm", "ipndm_v", "deis", "res_multistep", "res_multistep_cfg_pp", "res_multistep_ancestral", "res_multistep_ancestral_cfg_pp",+ "ipndm", "ipndm_v", "deis", "cfgpp_ud10_ab", "res_multistep", "res_multistep_cfg_pp", "res_multistep_ancestral", "res_multistep_ancestral_cfg_pp","gradient_estimation", "gradient_estimation_cfg_pp", "er_sde", "seeds_2", "seeds_3", "sa_solver", "sa_solver_pece"]class KSAMPLER(Sampler):