nodes_custom_sampler.py 10.7 KB
Newer Older
comfyanonymous's avatar
comfyanonymous committed
1
2
3
4
import comfy.samplers
import comfy.sample
from comfy.k_diffusion import sampling as k_diffusion_sampling
import latent_preview
5
import torch
6
import comfy.utils
comfyanonymous's avatar
comfyanonymous committed
7

8
9
10
11
12
13
14
15
16
17
18

class BasicScheduler:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"model": ("MODEL",),
                     "scheduler": (comfy.samplers.SCHEDULER_NAMES, ),
                     "steps": ("INT", {"default": 20, "min": 1, "max": 10000}),
                      }
               }
    RETURN_TYPES = ("SIGMAS",)
19
    CATEGORY = "sampling/custom_sampling/schedulers"
20
21
22
23
24
25
26
27

    FUNCTION = "get_sigmas"

    def get_sigmas(self, model, scheduler, steps):
        sigmas = comfy.samplers.calculate_sigmas_scheduler(model.model, scheduler, steps).cpu()
        return (sigmas, )


comfyanonymous's avatar
comfyanonymous committed
28
29
30
31
32
33
34
35
36
37
38
class KarrasScheduler:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"steps": ("INT", {"default": 20, "min": 1, "max": 10000}),
                     "sigma_max": ("FLOAT", {"default": 14.614642, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}),
                     "sigma_min": ("FLOAT", {"default": 0.0291675, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}),
                     "rho": ("FLOAT", {"default": 7.0, "min": 0.0, "max": 100.0, "step":0.01, "round": False}),
                    }
               }
    RETURN_TYPES = ("SIGMAS",)
39
    CATEGORY = "sampling/custom_sampling/schedulers"
comfyanonymous's avatar
comfyanonymous committed
40
41
42
43
44
45
46

    FUNCTION = "get_sigmas"

    def get_sigmas(self, steps, sigma_max, sigma_min, rho):
        sigmas = k_diffusion_sampling.get_sigmas_karras(n=steps, sigma_min=sigma_min, sigma_max=sigma_max, rho=rho)
        return (sigmas, )

47
48
49
50
51
52
53
54
55
56
class ExponentialScheduler:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"steps": ("INT", {"default": 20, "min": 1, "max": 10000}),
                     "sigma_max": ("FLOAT", {"default": 14.614642, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}),
                     "sigma_min": ("FLOAT", {"default": 0.0291675, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}),
                    }
               }
    RETURN_TYPES = ("SIGMAS",)
57
    CATEGORY = "sampling/custom_sampling/schedulers"
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

    FUNCTION = "get_sigmas"

    def get_sigmas(self, steps, sigma_max, sigma_min):
        sigmas = k_diffusion_sampling.get_sigmas_exponential(n=steps, sigma_min=sigma_min, sigma_max=sigma_max)
        return (sigmas, )

class PolyexponentialScheduler:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"steps": ("INT", {"default": 20, "min": 1, "max": 10000}),
                     "sigma_max": ("FLOAT", {"default": 14.614642, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}),
                     "sigma_min": ("FLOAT", {"default": 0.0291675, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}),
                     "rho": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step":0.01, "round": False}),
                    }
               }
    RETURN_TYPES = ("SIGMAS",)
76
    CATEGORY = "sampling/custom_sampling/schedulers"
77
78
79
80
81
82
83

    FUNCTION = "get_sigmas"

    def get_sigmas(self, steps, sigma_max, sigma_min, rho):
        sigmas = k_diffusion_sampling.get_sigmas_polyexponential(n=steps, sigma_min=sigma_min, sigma_max=sigma_max, rho=rho)
        return (sigmas, )

comfyanonymous's avatar
comfyanonymous committed
84
85
86
87
88
89
class SDTurboScheduler:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"model": ("MODEL",),
                     "steps": ("INT", {"default": 1, "min": 1, "max": 10}),
90
                     "denoise": ("FLOAT", {"default": 1.0, "min": 0, "max": 1.0, "step": 0.01}),
comfyanonymous's avatar
comfyanonymous committed
91
92
93
94
95
96
97
                      }
               }
    RETURN_TYPES = ("SIGMAS",)
    CATEGORY = "sampling/custom_sampling/schedulers"

    FUNCTION = "get_sigmas"

98
99
100
    def get_sigmas(self, model, steps, denoise):
        start_step = 10 - int(10 * denoise)
        timesteps = torch.flip(torch.arange(1, 11) * 100 - 1, (0,))[start_step:start_step + steps]
comfyanonymous's avatar
comfyanonymous committed
101
102
103
104
        sigmas = model.model.model_sampling.sigma(timesteps)
        sigmas = torch.cat([sigmas, sigmas.new_zeros([1])])
        return (sigmas, )

comfyanonymous's avatar
comfyanonymous committed
105
106
107
108
109
110
111
112
113
114
115
class VPScheduler:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"steps": ("INT", {"default": 20, "min": 1, "max": 10000}),
                     "beta_d": ("FLOAT", {"default": 19.9, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}), #TODO: fix default values
                     "beta_min": ("FLOAT", {"default": 0.1, "min": 0.0, "max": 1000.0, "step":0.01, "round": False}),
                     "eps_s": ("FLOAT", {"default": 0.001, "min": 0.0, "max": 1.0, "step":0.0001, "round": False}),
                    }
               }
    RETURN_TYPES = ("SIGMAS",)
116
    CATEGORY = "sampling/custom_sampling/schedulers"
comfyanonymous's avatar
comfyanonymous committed
117
118
119
120
121
122
123

    FUNCTION = "get_sigmas"

    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, )

comfyanonymous's avatar
comfyanonymous committed
124
125
126
127
128
129
130
131
132
class SplitSigmas:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"sigmas": ("SIGMAS", ),
                    "step": ("INT", {"default": 0, "min": 0, "max": 10000}),
                     }
                }
    RETURN_TYPES = ("SIGMAS","SIGMAS")
133
    CATEGORY = "sampling/custom_sampling/sigmas"
comfyanonymous's avatar
comfyanonymous committed
134
135
136
137
138

    FUNCTION = "get_sigmas"

    def get_sigmas(self, sigmas, step):
        sigmas1 = sigmas[:step + 1]
comfyanonymous's avatar
comfyanonymous committed
139
        sigmas2 = sigmas[step:]
comfyanonymous's avatar
comfyanonymous committed
140
        return (sigmas1, sigmas2)
comfyanonymous's avatar
comfyanonymous committed
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class FlipSigmas:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"sigmas": ("SIGMAS", ),
                     }
                }
    RETURN_TYPES = ("SIGMAS",)
    CATEGORY = "sampling/custom_sampling/sigmas"

    FUNCTION = "get_sigmas"

    def get_sigmas(self, sigmas):
        sigmas = sigmas.flip(0)
        if sigmas[0] == 0:
            sigmas[0] = 0.0001
        return (sigmas,)

comfyanonymous's avatar
comfyanonymous committed
160
161
162
163
class KSamplerSelect:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
164
                    {"sampler_name": (comfy.samplers.SAMPLER_NAMES, ),
comfyanonymous's avatar
comfyanonymous committed
165
166
167
                      }
               }
    RETURN_TYPES = ("SAMPLER",)
168
    CATEGORY = "sampling/custom_sampling/samplers"
comfyanonymous's avatar
comfyanonymous committed
169
170
171
172

    FUNCTION = "get_sampler"

    def get_sampler(self, sampler_name):
173
        sampler = comfy.samplers.sampler_object(sampler_name)
comfyanonymous's avatar
comfyanonymous committed
174
175
        return (sampler, )

comfyanonymous's avatar
comfyanonymous committed
176
177
178
179
180
181
182
183
184
185
186
class SamplerDPMPP_2M_SDE:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"solver_type": (['midpoint', 'heun'], ),
                     "eta": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step":0.01, "round": False}),
                     "s_noise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step":0.01, "round": False}),
                     "noise_device": (['gpu', 'cpu'], ),
                      }
               }
    RETURN_TYPES = ("SAMPLER",)
187
    CATEGORY = "sampling/custom_sampling/samplers"
comfyanonymous's avatar
comfyanonymous committed
188
189
190
191
192
193
194
195

    FUNCTION = "get_sampler"

    def get_sampler(self, solver_type, eta, s_noise, noise_device):
        if noise_device == 'cpu':
            sampler_name = "dpmpp_2m_sde"
        else:
            sampler_name = "dpmpp_2m_sde_gpu"
196
        sampler = comfy.samplers.ksampler(sampler_name, {"eta": eta, "s_noise": s_noise, "solver_type": solver_type})
comfyanonymous's avatar
comfyanonymous committed
197
198
199
        return (sampler, )


comfyanonymous's avatar
comfyanonymous committed
200
201
202
203
204
205
206
207
208
209
210
class SamplerDPMPP_SDE:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"eta": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step":0.01, "round": False}),
                     "s_noise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step":0.01, "round": False}),
                     "r": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 100.0, "step":0.01, "round": False}),
                     "noise_device": (['gpu', 'cpu'], ),
                      }
               }
    RETURN_TYPES = ("SAMPLER",)
211
    CATEGORY = "sampling/custom_sampling/samplers"
comfyanonymous's avatar
comfyanonymous committed
212
213
214
215
216
217
218
219

    FUNCTION = "get_sampler"

    def get_sampler(self, eta, s_noise, r, noise_device):
        if noise_device == 'cpu':
            sampler_name = "dpmpp_sde"
        else:
            sampler_name = "dpmpp_sde_gpu"
220
        sampler = comfy.samplers.ksampler(sampler_name, {"eta": eta, "s_noise": s_noise, "r": r})
comfyanonymous's avatar
comfyanonymous committed
221
222
        return (sampler, )

comfyanonymous's avatar
comfyanonymous committed
223
224
225
226
227
class SamplerCustom:
    @classmethod
    def INPUT_TYPES(s):
        return {"required":
                    {"model": ("MODEL",),
228
                    "add_noise": ("BOOLEAN", {"default": True}),
comfyanonymous's avatar
comfyanonymous committed
229
                    "noise_seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
230
                    "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step":0.1, "round": 0.01}),
comfyanonymous's avatar
comfyanonymous committed
231
232
233
234
235
236
237
238
239
240
241
242
243
                    "positive": ("CONDITIONING", ),
                    "negative": ("CONDITIONING", ),
                    "sampler": ("SAMPLER", ),
                    "sigmas": ("SIGMAS", ),
                    "latent_image": ("LATENT", ),
                     }
                }

    RETURN_TYPES = ("LATENT","LATENT")
    RETURN_NAMES = ("output", "denoised_output")

    FUNCTION = "sample"

244
    CATEGORY = "sampling/custom_sampling"
comfyanonymous's avatar
comfyanonymous committed
245
246
247
248

    def sample(self, model, add_noise, noise_seed, cfg, positive, negative, sampler, sigmas, latent_image):
        latent = latent_image
        latent_image = latent["samples"]
249
        if not add_noise:
comfyanonymous's avatar
comfyanonymous committed
250
251
252
253
254
255
256
257
258
259
260
261
            noise = torch.zeros(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, device="cpu")
        else:
            batch_inds = latent["batch_index"] if "batch_index" in latent else None
            noise = comfy.sample.prepare_noise(latent_image, noise_seed, batch_inds)

        noise_mask = None
        if "noise_mask" in latent:
            noise_mask = latent["noise_mask"]

        x0_output = {}
        callback = latent_preview.prepare_callback(model, sigmas.shape[-1] - 1, x0_output)

262
        disable_pbar = not comfy.utils.PROGRESS_BAR_ENABLED
comfyanonymous's avatar
comfyanonymous committed
263
264
265
266
267
268
269
270
271
272
273
274
275
        samples = comfy.sample.sample_custom(model, noise, cfg, sampler, sigmas, positive, negative, latent_image, noise_mask=noise_mask, callback=callback, disable_pbar=disable_pbar, seed=noise_seed)

        out = latent.copy()
        out["samples"] = samples
        if "x0" in x0_output:
            out_denoised = latent.copy()
            out_denoised["samples"] = model.model.process_latent_out(x0_output["x0"].cpu())
        else:
            out_denoised = out
        return (out, out_denoised)

NODE_CLASS_MAPPINGS = {
    "SamplerCustom": SamplerCustom,
276
    "BasicScheduler": BasicScheduler,
comfyanonymous's avatar
comfyanonymous committed
277
    "KarrasScheduler": KarrasScheduler,
278
279
    "ExponentialScheduler": ExponentialScheduler,
    "PolyexponentialScheduler": PolyexponentialScheduler,
comfyanonymous's avatar
comfyanonymous committed
280
    "VPScheduler": VPScheduler,
comfyanonymous's avatar
comfyanonymous committed
281
    "SDTurboScheduler": SDTurboScheduler,
comfyanonymous's avatar
comfyanonymous committed
282
    "KSamplerSelect": KSamplerSelect,
comfyanonymous's avatar
comfyanonymous committed
283
    "SamplerDPMPP_2M_SDE": SamplerDPMPP_2M_SDE,
comfyanonymous's avatar
comfyanonymous committed
284
    "SamplerDPMPP_SDE": SamplerDPMPP_SDE,
comfyanonymous's avatar
comfyanonymous committed
285
    "SplitSigmas": SplitSigmas,
286
    "FlipSigmas": FlipSigmas,
comfyanonymous's avatar
comfyanonymous committed
287
}