Skip to content

Add Power Shift Scheduler and associated node #8867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions comfy/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,29 @@ def kl_optimal_scheduler(n: int, sigma_min: float, sigma_max: float) -> torch.Te
sigmas[:-1] = (adj_idxs * math.atan(sigma_min) + (1 - adj_idxs) * math.atan(sigma_max)).tan_()
return sigmas

def power_shift_scheduler(model_sampling, steps, power=2.0, midpoint_shift=1.0, discard_penultimate=False):
total_timesteps = (len(model_sampling.sigmas) - 1)
x = numpy.linspace(0, 1, steps, endpoint=False)
x = x**midpoint_shift

ts_normalized = (1 - x**power)**power
ts = numpy.rint(ts_normalized * total_timesteps)

sigs = []
last_t = -1
for t in ts:
t_int = min(int(t), total_timesteps)
if t_int != last_t:
sigs.append(float(model_sampling.sigmas[t_int]))
last_t = t_int

sigs.append(0.0)
if discard_penultimate is True:
sigmas = torch.FloatTensor(sigs)
return torch.cat((sigmas[:-2], sigmas[-1:]))
else:
return torch.FloatTensor(sigs)

def get_mask_aabb(masks):
if masks.numel() == 0:
return torch.zeros((0, 4), device=masks.device, dtype=torch.int)
Expand Down Expand Up @@ -1052,6 +1075,7 @@ class SchedulerHandler(NamedTuple):
"normal": SchedulerHandler(normal_scheduler),
"linear_quadratic": SchedulerHandler(linear_quadratic_schedule),
"kl_optimal": SchedulerHandler(kl_optimal_scheduler, use_ms=False),
"power_shift": SchedulerHandler(power_shift_scheduler),
}
SCHEDULER_NAMES = list(SCHEDULER_HANDLERS)

Expand Down
27 changes: 27 additions & 0 deletions comfy_extras/nodes_custom_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,33 @@ def get_sigmas(self, steps, beta_d, beta_min, eps_s):
sigmas = k_diffusion_sampling.get_sigmas_vp(n=steps, beta_d=beta_d, beta_min=beta_min, eps_s=eps_s)
return (sigmas, )

class PowerShiftScheduler:
@classmethod
def INPUT_TYPES(s):
return {"required":
{"model": ("MODEL",),
"steps": ("INT", {"default": 20, "min": 3, "max": 1000}),
"power": ("FLOAT", {"default": 2.0, "min": 0.0, "max": 5.0, "step": 0.001}),
"midpoint_shift": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 5.0, "step": 0.001}),
"discard_penultimate": ("BOOLEAN", {"default": False}),
"denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
}
}
RETURN_TYPES = ("SIGMAS",)
CATEGORY = "sampling/custom_sampling/schedulers"

FUNCTION = "get_sigmas"

def get_sigmas(self, model, steps, power, midpoint_shift, discard_penultimate, denoise):
total_steps = steps
if denoise < 1.0:
total_steps = int(steps/denoise)

sigmas = comfy.samplers.power_shift_scheduler(model.get_model_object("model_sampling"), total_steps, power, midpoint_shift, discard_penultimate=discard_penultimate).cpu()
sigmas = sigmas[-(steps + 1):]

return (sigmas, )

class SplitSigmas:
@classmethod
def INPUT_TYPES(s):
Expand Down
Loading