Skip to content

Commit 10f6254

Browse files
Merge pull request #4021 from AUTOMATIC1111/add-kdiff-cfgdenoiser-callback
Add mid-kdiffusion cfgdenoiser script callback - access latents, conditionings and sigmas mid-sampling
2 parents 5510c28 + 5b6bedf commit 10f6254

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

modules/script_callbacks.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,32 @@ def __init__(self, image, p, filename, pnginfo):
2626
"""dictionary with parameters for image's PNG info data; infotext will have the key 'parameters'"""
2727

2828

29+
class CFGDenoiserParams:
30+
def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps):
31+
self.x = x
32+
"""Latent image representation in the process of being denoised"""
33+
34+
self.image_cond = image_cond
35+
"""Conditioning image"""
36+
37+
self.sigma = sigma
38+
"""Current sigma noise step value"""
39+
40+
self.sampling_step = sampling_step
41+
"""Current Sampling step number"""
42+
43+
self.total_sampling_steps = total_sampling_steps
44+
"""Total number of sampling steps planned"""
45+
46+
2947
ScriptCallback = namedtuple("ScriptCallback", ["script", "callback"])
3048
callbacks_app_started = []
3149
callbacks_model_loaded = []
3250
callbacks_ui_tabs = []
3351
callbacks_ui_settings = []
3452
callbacks_before_image_saved = []
3553
callbacks_image_saved = []
54+
callbacks_cfg_denoiser = []
3655

3756

3857
def clear_callbacks():
@@ -41,7 +60,7 @@ def clear_callbacks():
4160
callbacks_ui_settings.clear()
4261
callbacks_before_image_saved.clear()
4362
callbacks_image_saved.clear()
44-
63+
callbacks_cfg_denoiser.clear()
4564

4665
def app_started_callback(demo: Blocks, app: FastAPI):
4766
for c in callbacks_app_started:
@@ -95,6 +114,14 @@ def image_saved_callback(params: ImageSaveParams):
95114
report_exception(c, 'image_saved_callback')
96115

97116

117+
def cfg_denoiser_callback(params: CFGDenoiserParams):
118+
for c in callbacks_cfg_denoiser:
119+
try:
120+
c.callback(params)
121+
except Exception:
122+
report_exception(c, 'cfg_denoiser_callback')
123+
124+
98125
def add_callback(callbacks, fun):
99126
stack = [x for x in inspect.stack() if x.filename != __file__]
100127
filename = stack[0].filename if len(stack) > 0 else 'unknown file'
@@ -147,3 +174,12 @@ def on_image_saved(callback):
147174
- params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
148175
"""
149176
add_callback(callbacks_image_saved, callback)
177+
178+
179+
def on_cfg_denoiser(callback):
180+
"""register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
181+
The callback is called with one argument:
182+
- params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
183+
"""
184+
add_callback(callbacks_cfg_denoiser, callback)
185+

modules/sd_samplers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from modules.shared import opts, cmd_opts, state
1414
import modules.shared as shared
15+
from modules.script_callbacks import CFGDenoiserParams, cfg_denoiser_callback
1516

1617

1718
SamplerData = namedtuple('SamplerData', ['name', 'constructor', 'aliases', 'options'])
@@ -280,6 +281,12 @@ def forward(self, x, sigma, uncond, cond, cond_scale, image_cond):
280281
image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])
281282
sigma_in = torch.cat([torch.stack([sigma[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [sigma])
282283

284+
denoiser_params = CFGDenoiserParams(x_in, image_cond_in, sigma_in, state.sampling_step, state.sampling_steps)
285+
cfg_denoiser_callback(denoiser_params)
286+
x_in = denoiser_params.x
287+
image_cond_in = denoiser_params.image_cond
288+
sigma_in = denoiser_params.sigma
289+
283290
if tensor.shape[1] == uncond.shape[1]:
284291
cond_in = torch.cat([tensor, uncond])
285292

0 commit comments

Comments
 (0)