Skip to content

Commit 551fe8d

Browse files
catboxanonliesened
andauthored
Add node to extend sigmas (Comfy-Org#7901)
* Add ExpandSigmas node * Rename, add interpolation functions Co-authored-by: liesen <liesen.dev@gmail.com> * Move computed interpolation outside loop * Add type hints --------- Co-authored-by: liesen <liesen.dev@gmail.com>
1 parent ff99861 commit 551fe8d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

comfy_extras/nodes_custom_sampler.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import comfy.samplers
23
import comfy.sample
34
from comfy.k_diffusion import sampling as k_diffusion_sampling
@@ -249,6 +250,55 @@ def set_first_sigma(self, sigmas, sigma):
249250
sigmas[0] = sigma
250251
return (sigmas, )
251252

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+
252302
class KSamplerSelect:
253303
@classmethod
254304
def INPUT_TYPES(s):
@@ -735,6 +785,7 @@ def add_noise(self, model, noise, sigmas, latent_image):
735785
"SplitSigmasDenoise": SplitSigmasDenoise,
736786
"FlipSigmas": FlipSigmas,
737787
"SetFirstSigma": SetFirstSigma,
788+
"ExtendIntermediateSigmas": ExtendIntermediateSigmas,
738789

739790
"CFGGuider": CFGGuider,
740791
"DualCFGGuider": DualCFGGuider,

0 commit comments

Comments
 (0)