lllyasviel commited on
Commit
9654763
·
1 Parent(s): c1f84af
Files changed (2) hide show
  1. modules/core.py +2 -2
  2. modules/samplers_advanced.py +44 -3
modules/core.py CHANGED
@@ -190,7 +190,7 @@ def ksampler_with_refiner(model, positive, negative, refiner, refiner_positive,
190
 
191
  models = load_additional_models(positive, negative, model.model_dtype())
192
 
193
- sampler = KSamplerWithRefiner(model=model.model, refiner_model=refiner.model, steps=steps, device=device,
194
  sampler=sampler_name, scheduler=scheduler,
195
  denoise=denoise, model_options=model.model_options)
196
 
@@ -198,7 +198,7 @@ def ksampler_with_refiner(model, positive, negative, refiner, refiner_positive,
198
  refiner_negative=refiner_negative_copy, refiner_switch_step=refiner_switch_step,
199
  cfg=cfg, latent_image=latent_image,
200
  start_step=start_step, last_step=last_step, force_full_denoise=force_full_denoise,
201
- denoise_mask=noise_mask, sigmas=sigmas, callback=callback, disable_pbar=disable_pbar,
202
  seed=seed)
203
 
204
  samples = samples.cpu()
 
190
 
191
  models = load_additional_models(positive, negative, model.model_dtype())
192
 
193
+ sampler = KSamplerWithRefiner(model=model, refiner_model=refiner, steps=steps, device=device,
194
  sampler=sampler_name, scheduler=scheduler,
195
  denoise=denoise, model_options=model.model_options)
196
 
 
198
  refiner_negative=refiner_negative_copy, refiner_switch_step=refiner_switch_step,
199
  cfg=cfg, latent_image=latent_image,
200
  start_step=start_step, last_step=last_step, force_full_denoise=force_full_denoise,
201
+ denoise_mask=noise_mask, sigmas=sigmas, callback_function=callback, disable_pbar=disable_pbar,
202
  seed=seed)
203
 
204
  samples = samples.cpu()
modules/samplers_advanced.py CHANGED
@@ -1,5 +1,7 @@
1
  from comfy.samplers import *
2
 
 
 
3
 
4
  class KSamplerWithRefiner:
5
  SCHEDULERS = ["normal", "karras", "exponential", "simple", "ddim_uniform"]
@@ -8,8 +10,11 @@ class KSamplerWithRefiner:
8
  "dpmpp_2m", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "ddim", "uni_pc", "uni_pc_bh2"]
9
 
10
  def __init__(self, model, refiner_model, steps, device, sampler=None, scheduler=None, denoise=None, model_options={}):
11
- self.model = model
12
- self.refiner_model = refiner_model
 
 
 
13
 
14
  self.model_denoise = CFGNoisePredictor(self.model)
15
  self.refiner_model_denoise = CFGNoisePredictor(self.refiner_model)
@@ -77,7 +82,7 @@ class KSamplerWithRefiner:
77
 
78
  def sample(self, noise, positive, negative, refiner_positive, refiner_negative, cfg, latent_image=None,
79
  start_step=None, last_step=None, refiner_switch_step=None,
80
- force_full_denoise=False, denoise_mask=None, sigmas=None, callback=None, disable_pbar=False, seed=None):
81
  if sigmas is None:
82
  sigmas = self.sigmas
83
  sigma_min = self.sigma_min
@@ -125,6 +130,42 @@ class KSamplerWithRefiner:
125
  negative = encode_adm(self.model, negative, noise.shape[0], noise.shape[3], noise.shape[2], self.device,
126
  "negative")
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  if latent_image is not None:
129
  latent_image = self.model.process_latent_in(latent_image)
130
 
 
1
  from comfy.samplers import *
2
 
3
+ import comfy.model_management
4
+
5
 
6
  class KSamplerWithRefiner:
7
  SCHEDULERS = ["normal", "karras", "exponential", "simple", "ddim_uniform"]
 
10
  "dpmpp_2m", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "ddim", "uni_pc", "uni_pc_bh2"]
11
 
12
  def __init__(self, model, refiner_model, steps, device, sampler=None, scheduler=None, denoise=None, model_options={}):
13
+ self.model_patcher = model
14
+ self.refiner_model_patcher = refiner_model
15
+
16
+ self.model = model.model
17
+ self.refiner_model = refiner_model.model
18
 
19
  self.model_denoise = CFGNoisePredictor(self.model)
20
  self.refiner_model_denoise = CFGNoisePredictor(self.refiner_model)
 
82
 
83
  def sample(self, noise, positive, negative, refiner_positive, refiner_negative, cfg, latent_image=None,
84
  start_step=None, last_step=None, refiner_switch_step=None,
85
+ force_full_denoise=False, denoise_mask=None, sigmas=None, callback_function=None, disable_pbar=False, seed=None):
86
  if sigmas is None:
87
  sigmas = self.sigmas
88
  sigma_min = self.sigma_min
 
130
  negative = encode_adm(self.model, negative, noise.shape[0], noise.shape[3], noise.shape[2], self.device,
131
  "negative")
132
 
133
+ refiner_positive = refiner_positive[:]
134
+ refiner_negative = refiner_negative[:]
135
+
136
+ resolve_cond_masks(refiner_positive, noise.shape[2], noise.shape[3], self.device)
137
+ resolve_cond_masks(refiner_negative, noise.shape[2], noise.shape[3], self.device)
138
+
139
+ calculate_start_end_timesteps(self.refiner_model_wrap, refiner_positive)
140
+ calculate_start_end_timesteps(self.refiner_model_wrap, refiner_negative)
141
+
142
+ # make sure each cond area has an opposite one with the same area
143
+ for c in refiner_positive:
144
+ create_cond_with_same_area_if_none(refiner_negative, c)
145
+ for c in refiner_negative:
146
+ create_cond_with_same_area_if_none(refiner_positive, c)
147
+
148
+ if self.model.is_adm():
149
+ refiner_positive = encode_adm(self.refiner_model, refiner_positive, noise.shape[0],
150
+ noise.shape[3], noise.shape[2], self.device, "positive")
151
+ refiner_negative = encode_adm(self.refiner_model, refiner_negative, noise.shape[0],
152
+ noise.shape[3], noise.shape[2], self.device, "negative")
153
+
154
+ def refiner_switch():
155
+ comfy.model_management.load_model_gpu(self.refiner_model_patcher)
156
+ self.model_denoise.inner_model = self.refiner_model_denoise.inner_model
157
+ for i in range(len(positive)):
158
+ positive[i] = refiner_positive[i]
159
+ for i in range(len(negative)):
160
+ negative[i] = refiner_negative[i]
161
+ return
162
+
163
+ def callback(step, x0, x, total_steps):
164
+ if step == refiner_switch_step:
165
+ refiner_switch()
166
+ if callback_function is not None:
167
+ callback_function(step, x0, x, total_steps)
168
+
169
  if latent_image is not None:
170
  latent_image = self.model.process_latent_in(latent_image)
171