|
| 1 | +import math |
1 | 2 | import comfy.samplers |
2 | 3 | import comfy.sample |
3 | 4 | from comfy.k_diffusion import sampling as k_diffusion_sampling |
@@ -249,6 +250,55 @@ def set_first_sigma(self, sigmas, sigma): |
249 | 250 | sigmas[0] = sigma |
250 | 251 | return (sigmas, ) |
251 | 252 |
|
| 253 | +class ExtendIntermediateSigmas: |
| 254 | + @classmethod |
| 255 | + def INPUT_TYPES(s): |
| 256 | + return {"required": |
| 257 | + {"sigmas": ("SIGMAS", ), |
| 258 | + "steps": ("INT", {"default": 2, "min": 1, "max": 100}), |
| 259 | + "start_at_sigma": ("FLOAT", {"default": -1.0, "min": -1.0, "max": 20000.0, "step": 0.01, "round": False}), |
| 260 | + "end_at_sigma": ("FLOAT", {"default": 12.0, "min": 0.0, "max": 20000.0, "step": 0.01, "round": False}), |
| 261 | + "spacing": (['linear', 'cosine', 'sine'],), |
| 262 | + } |
| 263 | + } |
| 264 | + RETURN_TYPES = ("SIGMAS",) |
| 265 | + CATEGORY = "sampling/custom_sampling/sigmas" |
| 266 | + |
| 267 | + FUNCTION = "extend" |
| 268 | + |
| 269 | + def extend(self, sigmas: torch.Tensor, steps: int, start_at_sigma: float, end_at_sigma: float, spacing: str): |
| 270 | + if start_at_sigma < 0: |
| 271 | + start_at_sigma = float("inf") |
| 272 | + |
| 273 | + interpolator = { |
| 274 | + 'linear': lambda x: x, |
| 275 | + 'cosine': lambda x: torch.sin(x*math.pi/2), |
| 276 | + 'sine': lambda x: 1 - torch.cos(x*math.pi/2) |
| 277 | + }[spacing] |
| 278 | + |
| 279 | + # linear space for our interpolation function |
| 280 | + x = torch.linspace(0, 1, steps + 1, device=sigmas.device)[1:-1] |
| 281 | + computed_spacing = interpolator(x) |
| 282 | + |
| 283 | + extended_sigmas = [] |
| 284 | + for i in range(len(sigmas) - 1): |
| 285 | + sigma_current = sigmas[i] |
| 286 | + sigma_next = sigmas[i+1] |
| 287 | + |
| 288 | + extended_sigmas.append(sigma_current) |
| 289 | + |
| 290 | + if end_at_sigma <= sigma_current <= start_at_sigma: |
| 291 | + interpolated_steps = computed_spacing * (sigma_next - sigma_current) + sigma_current |
| 292 | + extended_sigmas.extend(interpolated_steps.tolist()) |
| 293 | + |
| 294 | + # Add the last sigma value |
| 295 | + if len(sigmas) > 0: |
| 296 | + extended_sigmas.append(sigmas[-1]) |
| 297 | + |
| 298 | + extended_sigmas = torch.FloatTensor(extended_sigmas) |
| 299 | + |
| 300 | + return (extended_sigmas,) |
| 301 | + |
252 | 302 | class KSamplerSelect: |
253 | 303 | @classmethod |
254 | 304 | def INPUT_TYPES(s): |
@@ -735,6 +785,7 @@ def add_noise(self, model, noise, sigmas, latent_image): |
735 | 785 | "SplitSigmasDenoise": SplitSigmasDenoise, |
736 | 786 | "FlipSigmas": FlipSigmas, |
737 | 787 | "SetFirstSigma": SetFirstSigma, |
| 788 | + "ExtendIntermediateSigmas": ExtendIntermediateSigmas, |
738 | 789 |
|
739 | 790 | "CFGGuider": CFGGuider, |
740 | 791 | "DualCFGGuider": DualCFGGuider, |
|
0 commit comments