lllyasviel commited on
Commit
5afc367
·
1 Parent(s): 3850303
Files changed (2) hide show
  1. modules/core.py +2 -2
  2. modules/samplers_advanced.py +200 -0
modules/core.py CHANGED
@@ -11,7 +11,7 @@ import comfy.utils
11
  from comfy.sd import load_checkpoint_guess_config
12
  from nodes import VAEDecode, EmptyLatentImage, CLIPTextEncode
13
  from comfy.sample import prepare_mask, broadcast_cond, load_additional_models, cleanup_additional_models
14
- from comfy.samplers import KSampler
15
 
16
 
17
  opCLIPTextEncode = CLIPTextEncode()
@@ -123,7 +123,7 @@ def ksampler(model, positive, negative, latent, seed=None, steps=30, cfg=9.0, sa
123
 
124
  models = load_additional_models(positive, negative, model.model_dtype())
125
 
126
- sampler = KSampler(real_model, steps=steps, device=device, sampler=sampler_name, scheduler=scheduler,
127
  denoise=denoise, model_options=model.model_options)
128
 
129
  samples = sampler.sample(noise, positive_copy, negative_copy, cfg=cfg, latent_image=latent_image,
 
11
  from comfy.sd import load_checkpoint_guess_config
12
  from nodes import VAEDecode, EmptyLatentImage, CLIPTextEncode
13
  from comfy.sample import prepare_mask, broadcast_cond, load_additional_models, cleanup_additional_models
14
+ from modules.samplers_advanced import KSamplerAdvanced
15
 
16
 
17
  opCLIPTextEncode = CLIPTextEncode()
 
123
 
124
  models = load_additional_models(positive, negative, model.model_dtype())
125
 
126
+ sampler = KSamplerAdvanced(real_model, steps=steps, device=device, sampler=sampler_name, scheduler=scheduler,
127
  denoise=denoise, model_options=model.model_options)
128
 
129
  samples = sampler.sample(noise, positive_copy, negative_copy, cfg=cfg, latent_image=latent_image,
modules/samplers_advanced.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from comfy.samplers import *
2
+
3
+
4
+ class KSamplerAdvanced:
5
+ SCHEDULERS = ["normal", "karras", "exponential", "simple", "ddim_uniform"]
6
+ SAMPLERS = ["euler", "euler_ancestral", "heun", "dpm_2", "dpm_2_ancestral",
7
+ "lms", "dpm_fast", "dpm_adaptive", "dpmpp_2s_ancestral", "dpmpp_sde", "dpmpp_sde_gpu",
8
+ "dpmpp_2m", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "ddim", "uni_pc", "uni_pc_bh2"]
9
+
10
+ def __init__(self, model, steps, device, sampler=None, scheduler=None, denoise=None, model_options={}):
11
+ self.model = model
12
+ self.model_denoise = CFGNoisePredictor(self.model)
13
+ if self.model.model_type == model_base.ModelType.V_PREDICTION:
14
+ self.model_wrap = CompVisVDenoiser(self.model_denoise, quantize=True)
15
+ else:
16
+ self.model_wrap = k_diffusion_external.CompVisDenoiser(self.model_denoise, quantize=True)
17
+
18
+ self.model_k = KSamplerX0Inpaint(self.model_wrap)
19
+ self.device = device
20
+ if scheduler not in self.SCHEDULERS:
21
+ scheduler = self.SCHEDULERS[0]
22
+ if sampler not in self.SAMPLERS:
23
+ sampler = self.SAMPLERS[0]
24
+ self.scheduler = scheduler
25
+ self.sampler = sampler
26
+ self.sigma_min=float(self.model_wrap.sigma_min)
27
+ self.sigma_max=float(self.model_wrap.sigma_max)
28
+ self.set_steps(steps, denoise)
29
+ self.denoise = denoise
30
+ self.model_options = model_options
31
+
32
+ def calculate_sigmas(self, steps):
33
+ sigmas = None
34
+
35
+ discard_penultimate_sigma = False
36
+ if self.sampler in ['dpm_2', 'dpm_2_ancestral']:
37
+ steps += 1
38
+ discard_penultimate_sigma = True
39
+
40
+ if self.scheduler == "karras":
41
+ sigmas = k_diffusion_sampling.get_sigmas_karras(n=steps, sigma_min=self.sigma_min, sigma_max=self.sigma_max)
42
+ elif self.scheduler == "exponential":
43
+ sigmas = k_diffusion_sampling.get_sigmas_exponential(n=steps, sigma_min=self.sigma_min, sigma_max=self.sigma_max)
44
+ elif self.scheduler == "normal":
45
+ sigmas = self.model_wrap.get_sigmas(steps)
46
+ elif self.scheduler == "simple":
47
+ sigmas = simple_scheduler(self.model_wrap, steps)
48
+ elif self.scheduler == "ddim_uniform":
49
+ sigmas = ddim_scheduler(self.model_wrap, steps)
50
+ else:
51
+ print("error invalid scheduler", self.scheduler)
52
+
53
+ if discard_penultimate_sigma:
54
+ sigmas = torch.cat([sigmas[:-2], sigmas[-1:]])
55
+ return sigmas
56
+
57
+ def set_steps(self, steps, denoise=None):
58
+ self.steps = steps
59
+ if denoise is None or denoise > 0.9999:
60
+ self.sigmas = self.calculate_sigmas(steps).to(self.device)
61
+ else:
62
+ new_steps = int(steps/denoise)
63
+ sigmas = self.calculate_sigmas(new_steps).to(self.device)
64
+ self.sigmas = sigmas[-(steps + 1):]
65
+
66
+ def sample(self, noise, positive, negative, cfg, latent_image=None, start_step=None, last_step=None, force_full_denoise=False, denoise_mask=None, sigmas=None, callback=None, disable_pbar=False, seed=None):
67
+ if sigmas is None:
68
+ sigmas = self.sigmas
69
+ sigma_min = self.sigma_min
70
+
71
+ if last_step is not None and last_step < (len(sigmas) - 1):
72
+ sigma_min = sigmas[last_step]
73
+ sigmas = sigmas[:last_step + 1]
74
+ if force_full_denoise:
75
+ sigmas[-1] = 0
76
+
77
+ if start_step is not None:
78
+ if start_step < (len(sigmas) - 1):
79
+ sigmas = sigmas[start_step:]
80
+ else:
81
+ if latent_image is not None:
82
+ return latent_image
83
+ else:
84
+ return torch.zeros_like(noise)
85
+
86
+ positive = positive[:]
87
+ negative = negative[:]
88
+
89
+ resolve_cond_masks(positive, noise.shape[2], noise.shape[3], self.device)
90
+ resolve_cond_masks(negative, noise.shape[2], noise.shape[3], self.device)
91
+
92
+ calculate_start_end_timesteps(self.model_wrap, negative)
93
+ calculate_start_end_timesteps(self.model_wrap, positive)
94
+
95
+ #make sure each cond area has an opposite one with the same area
96
+ for c in positive:
97
+ create_cond_with_same_area_if_none(negative, c)
98
+ for c in negative:
99
+ create_cond_with_same_area_if_none(positive, c)
100
+
101
+ pre_run_control(self.model_wrap, negative + positive)
102
+
103
+ apply_empty_x_to_equal_area(list(filter(lambda c: c[1].get('control_apply_to_uncond', False) == True, positive)), negative, 'control', lambda cond_cnets, x: cond_cnets[x])
104
+ apply_empty_x_to_equal_area(positive, negative, 'gligen', lambda cond_cnets, x: cond_cnets[x])
105
+
106
+ if self.model.is_adm():
107
+ positive = encode_adm(self.model, positive, noise.shape[0], noise.shape[3], noise.shape[2], self.device, "positive")
108
+ negative = encode_adm(self.model, negative, noise.shape[0], noise.shape[3], noise.shape[2], self.device, "negative")
109
+
110
+ if latent_image is not None:
111
+ latent_image = self.model.process_latent_in(latent_image)
112
+
113
+ extra_args = {"cond":positive, "uncond":negative, "cond_scale": cfg, "model_options": self.model_options, "seed":seed}
114
+
115
+ cond_concat = None
116
+ if hasattr(self.model, 'concat_keys'): #inpaint
117
+ cond_concat = []
118
+ for ck in self.model.concat_keys:
119
+ if denoise_mask is not None:
120
+ if ck == "mask":
121
+ cond_concat.append(denoise_mask[:,:1])
122
+ elif ck == "masked_image":
123
+ cond_concat.append(latent_image) #NOTE: the latent_image should be masked by the mask in pixel space
124
+ else:
125
+ if ck == "mask":
126
+ cond_concat.append(torch.ones_like(noise)[:,:1])
127
+ elif ck == "masked_image":
128
+ cond_concat.append(blank_inpaint_image_like(noise))
129
+ extra_args["cond_concat"] = cond_concat
130
+
131
+ if sigmas[0] != self.sigmas[0] or (self.denoise is not None and self.denoise < 1.0):
132
+ max_denoise = False
133
+ else:
134
+ max_denoise = True
135
+
136
+
137
+ if self.sampler == "uni_pc":
138
+ samples = uni_pc.sample_unipc(self.model_wrap, noise, latent_image, sigmas, sampling_function=sampling_function, max_denoise=max_denoise, extra_args=extra_args, noise_mask=denoise_mask, callback=callback, disable=disable_pbar)
139
+ elif self.sampler == "uni_pc_bh2":
140
+ samples = uni_pc.sample_unipc(self.model_wrap, noise, latent_image, sigmas, sampling_function=sampling_function, max_denoise=max_denoise, extra_args=extra_args, noise_mask=denoise_mask, callback=callback, variant='bh2', disable=disable_pbar)
141
+ elif self.sampler == "ddim":
142
+ timesteps = []
143
+ for s in range(sigmas.shape[0]):
144
+ timesteps.insert(0, self.model_wrap.sigma_to_discrete_timestep(sigmas[s]))
145
+ noise_mask = None
146
+ if denoise_mask is not None:
147
+ noise_mask = 1.0 - denoise_mask
148
+
149
+ ddim_callback = None
150
+ if callback is not None:
151
+ total_steps = len(timesteps) - 1
152
+ ddim_callback = lambda pred_x0, i: callback(i, pred_x0, None, total_steps)
153
+
154
+ sampler = DDIMSampler(self.model, device=self.device)
155
+ sampler.make_schedule_timesteps(ddim_timesteps=timesteps, verbose=False)
156
+ z_enc = sampler.stochastic_encode(latent_image, torch.tensor([len(timesteps) - 1] * noise.shape[0]).to(self.device), noise=noise, max_denoise=max_denoise)
157
+ samples, _ = sampler.sample_custom(ddim_timesteps=timesteps,
158
+ conditioning=positive,
159
+ batch_size=noise.shape[0],
160
+ shape=noise.shape[1:],
161
+ verbose=False,
162
+ unconditional_guidance_scale=cfg,
163
+ unconditional_conditioning=negative,
164
+ eta=0.0,
165
+ x_T=z_enc,
166
+ x0=latent_image,
167
+ img_callback=ddim_callback,
168
+ denoise_function=self.model_wrap.predict_eps_discrete_timestep,
169
+ extra_args=extra_args,
170
+ mask=noise_mask,
171
+ to_zero=sigmas[-1]==0,
172
+ end_step=sigmas.shape[0] - 1,
173
+ disable_pbar=disable_pbar)
174
+
175
+ else:
176
+ extra_args["denoise_mask"] = denoise_mask
177
+ self.model_k.latent_image = latent_image
178
+ self.model_k.noise = noise
179
+
180
+ if max_denoise:
181
+ noise = noise * torch.sqrt(1.0 + sigmas[0] ** 2.0)
182
+ else:
183
+ noise = noise * sigmas[0]
184
+
185
+ k_callback = None
186
+ total_steps = len(sigmas) - 1
187
+ if callback is not None:
188
+ k_callback = lambda x: callback(x["i"], x["denoised"], x["x"], total_steps)
189
+
190
+ if latent_image is not None:
191
+ noise += latent_image
192
+ if self.sampler == "dpm_fast":
193
+ samples = k_diffusion_sampling.sample_dpm_fast(self.model_k, noise, sigma_min, sigmas[0], total_steps, extra_args=extra_args, callback=k_callback, disable=disable_pbar)
194
+ elif self.sampler == "dpm_adaptive":
195
+ samples = k_diffusion_sampling.sample_dpm_adaptive(self.model_k, noise, sigma_min, sigmas[0], extra_args=extra_args, callback=k_callback, disable=disable_pbar)
196
+ else:
197
+ samples = getattr(k_diffusion_sampling, "sample_{}".format(self.sampler))(self.model_k, noise, sigmas, extra_args=extra_args, callback=k_callback, disable=disable_pbar)
198
+
199
+ return self.model.process_latent_out(samples.to(torch.float32))
200
+