This commit is contained in:
Haoming 2025-10-08 15:29:24 +08:00
parent 3c32560717
commit d0cef92786
2 changed files with 86 additions and 85 deletions

View File

@ -56,16 +56,16 @@ class MemUsageMonitor(threading.Thread):
time.sleep(1 / self.opts.memmon_poll_rate)
def dump_debug(self):
print(self, 'recorded data:')
print(self, "recorded data:")
for k, v in self.read().items():
print(k, -(v // -(1024 ** 2)))
print(k, -(v // -(1024**2)))
print(self, 'raw torch memory stats:')
print(self, "raw torch memory stats:")
tm = torch.cuda.memory_stats(self.device)
for k, v in tm.items():
if 'bytes' not in k:
if "bytes" not in k:
continue
print('\t' if 'peak' in k else '', k, -(v // -(1024 ** 2)))
print("\t" if "peak" in k else "", k, -(v // -(1024**2)))
print(torch.cuda.memory_summary())

View File

@ -1,18 +1,19 @@
import inspect
import os
import re
import sys
import inspect
from collections import namedtuple
from dataclasses import dataclass
import gradio as gr
from modules import shared, paths, script_callbacks, extensions, script_loading, scripts_postprocessing, errors, timer, util
from modules import errors, extensions, paths, script_callbacks, script_loading, scripts_postprocessing, shared, timer, util
topological_sort = util.topological_sort
AlwaysVisible = object()
class MaskBlendArgs:
def __init__(self, current_latent, nmask, init_latent, mask, blended_latent, denoiser=None, sigma=None):
self.current_latent = current_latent
@ -25,21 +26,25 @@ class MaskBlendArgs:
self.is_final_blend = denoiser is None
self.sigma = sigma
class PostSampleArgs:
def __init__(self, samples):
self.samples = samples
class PostprocessImageArgs:
def __init__(self, image, index):
self.image = image
self.index = index
class PostProcessMaskOverlayArgs:
def __init__(self, index, mask_for_overlay, overlay_image):
self.index = index
self.mask_for_overlay = mask_for_overlay
self.overlay_image = overlay_image
class PostprocessBatchListArgs:
def __init__(self, images):
self.images = images
@ -121,7 +126,7 @@ class Script:
- False if the script should not be shown in UI at all
- True if the script should be shown in UI if it's selected in the scripts dropdown
- script.AlwaysVisible if the script should be shown in UI at all times
"""
"""
return True
@ -339,11 +344,11 @@ class Script:
"""helper function to generate id for a HTML element, constructs final id out of script name, tab and user-supplied item_id"""
need_tabname = self.show(True) == self.show(False)
tabkind = 'img2img' if self.is_img2img else 'txt2img'
tabkind = "img2img" if self.is_img2img else "txt2img"
tabname = f"{tabkind}_" if need_tabname else ""
title = re.sub(r'[^a-z_0-9]', '', re.sub(r'\s', '_', self.title().lower()))
title = re.sub(r"[^a-z_0-9]", "", re.sub(r"\s", "_", self.title().lower()))
return f'script_{tabname}{title}_{item_id}'
return f"script_{tabname}{title}_{item_id}"
def before_hr(self, p, *args):
"""
@ -359,9 +364,9 @@ class ScriptBuiltinUI(Script):
"""helper function to generate id for a HTML element, constructs final id out of tab and user-supplied item_id"""
need_tabname = self.show(True) == self.show(False)
tabname = ('img2img' if self.is_img2img else 'txt2img') + "_" if need_tabname else ""
tabname = ("img2img" if self.is_img2img else "txt2img") + "_" if need_tabname else ""
return f'{tabname}{item_id}'
return f"{tabname}{item_id}"
def show(self, is_img2img):
return AlwaysVisible
@ -562,21 +567,21 @@ class ScriptRunner:
self.callback_map = {}
self.callback_names = [
'before_process',
'process',
'before_process_batch',
'after_extra_networks_activate',
'process_batch',
'postprocess',
'postprocess_batch',
'postprocess_batch_list',
'post_sample',
'on_mask_blend',
'postprocess_image',
'postprocess_maskoverlay',
'postprocess_image_after_composite',
'before_component',
'after_component',
"before_process",
"process",
"before_process_batch",
"after_extra_networks_activate",
"process_batch",
"postprocess",
"postprocess_batch",
"postprocess_batch_list",
"post_sample",
"on_mask_blend",
"postprocess_image",
"postprocess_maskoverlay",
"postprocess_image_after_composite",
"before_component",
"after_component",
]
self.on_before_component_elem_id = {}
@ -674,7 +679,7 @@ class ScriptRunner:
if v is not None:
setattr(arg_info, field, v)
choices = getattr(control, 'choices', None) # as of gradio 3.41, some items in choices are strings, and some are tuples where the first elem is the string
choices = getattr(control, "choices", None) # as of gradio 3.41, some items in choices are strings, and some are tuples where the first elem is the string
if choices is not None:
arg_info.choices = [x[0] if isinstance(x, tuple) else x for x in choices]
@ -732,14 +737,14 @@ class ScriptRunner:
def select_script(script_index):
if script_index is None:
script_index = 0
selected_script = self.selectable_scripts[script_index - 1] if script_index>0 else None
selected_script = self.selectable_scripts[script_index - 1] if script_index > 0 else None
return [gr.update(visible=selected_script == s) for s in self.selectable_scripts]
def init_field(title):
"""called when an initial value is set from ui-config.json to show script's UI components"""
if title == 'None':
if title == "None":
return
script_index = self.titles.index(title)
@ -747,16 +752,12 @@ class ScriptRunner:
dropdown.init_field = init_field
dropdown.change(
fn=select_script,
inputs=[dropdown],
outputs=[script.group for script in self.selectable_scripts]
)
dropdown.change(fn=select_script, inputs=[dropdown], outputs=[script.group for script in self.selectable_scripts])
self.script_load_ctr = 0
def onload_script_visibility(params):
title = params.get('Script', None)
title = params.get("Script", None)
if title:
try:
title_index = self.titles.index(title)
@ -764,13 +765,13 @@ class ScriptRunner:
self.script_load_ctr = (self.script_load_ctr + 1) % len(self.titles)
return gr.update(visible=visibility)
except ValueError:
params['Script'] = None
params["Script"] = None
massage = f'Cannot find Script: "{title}"'
print(massage)
gr.Warning(massage)
return gr.update(visible=False)
self.infotext_fields.append((dropdown, lambda x: gr.update(value=x.get('Script', 'None'))))
self.infotext_fields.append((dropdown, lambda x: gr.update(value=x.get("Script", "None"))))
self.infotext_fields.extend([(script.group, onload_script_visibility) for script in self.selectable_scripts])
self.apply_on_before_component_callbacks()
@ -783,12 +784,12 @@ class ScriptRunner:
if script_index == 0 or script_index is None:
return None
script = self.selectable_scripts[script_index-1]
script = self.selectable_scripts[script_index - 1]
if script is None:
return None
script_args = args[script.args_from:script.args_to]
script_args = args[script.args_from : script.args_to]
processed = script.run(p, *script_args)
shared.total_tqdm.clear()
@ -796,14 +797,14 @@ class ScriptRunner:
return processed
def list_scripts_for_method(self, method_name):
if method_name in ('before_component', 'after_component'):
if method_name in ("before_component", "after_component"):
return self.scripts
else:
return self.alwayson_scripts
def create_ordered_callbacks_list(self, method_name, *, enable_user_sort=True):
def create_ordered_callbacks_list(self, method_name, *, enable_user_sort=True):
script_list = self.list_scripts_for_method(method_name)
category = f'script_{method_name}'
category = f"script_{method_name}"
callbacks = []
for script in script_list:
@ -816,11 +817,11 @@ class ScriptRunner:
def ordered_callbacks(self, method_name, *, enable_user_sort=True):
script_list = self.list_scripts_for_method(method_name)
category = f'script_{method_name}'
category = f"script_{method_name}"
scrpts_len, callbacks = self.callback_map.get(category, (-1, None))
scripts_len, callbacks = self.callback_map.get(category, (-1, None))
if callbacks is None or scrpts_len != len(script_list):
if callbacks is None or scripts_len != len(script_list):
callbacks = self.create_ordered_callbacks_list(method_name, enable_user_sort=enable_user_sort)
self.callback_map[category] = len(script_list), callbacks
@ -830,57 +831,57 @@ class ScriptRunner:
return [x.callback for x in self.ordered_callbacks(method_name)]
def before_process(self, p):
for script in self.ordered_scripts('before_process'):
for script in self.ordered_scripts("before_process"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.before_process(p, *script_args)
except Exception:
errors.report(f"Error running before_process: {script.filename}", exc_info=True)
def process(self, p):
for script in self.ordered_scripts('process'):
for script in self.ordered_scripts("process"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.process(p, *script_args)
except Exception:
errors.report(f"Error running process: {script.filename}", exc_info=True)
def process_before_every_sampling(self, p, **kwargs):
for script in self.ordered_scripts('process_before_every_sampling'):
for script in self.ordered_scripts("process_before_every_sampling"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.process_before_every_sampling(p, *script_args, **kwargs)
except Exception:
errors.report(f"Error running process_before_every_sampling: {script.filename}", exc_info=True)
def before_process_batch(self, p, **kwargs):
for script in self.ordered_scripts('before_process_batch'):
for script in self.ordered_scripts("before_process_batch"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.before_process_batch(p, *script_args, **kwargs)
except Exception:
errors.report(f"Error running before_process_batch: {script.filename}", exc_info=True)
def before_process_init_images(self, p, pp, **kwargs):
for script in self.ordered_scripts('before_process_init_images'):
for script in self.ordered_scripts("before_process_init_images"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.before_process_init_images(p, pp, *script_args, **kwargs)
except Exception:
errors.report(f"Error running before_process_init_images: {script.filename}", exc_info=True)
def after_extra_networks_activate(self, p, **kwargs):
for script in self.ordered_scripts('after_extra_networks_activate'):
for script in self.ordered_scripts("after_extra_networks_activate"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.after_extra_networks_activate(p, *script_args, **kwargs)
except Exception:
errors.report(f"Error running after_extra_networks_activate: {script.filename}", exc_info=True)
def process_batch(self, p, **kwargs):
for script in self.ordered_scripts('process_batch'):
for script in self.ordered_scripts("process_batch"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.process_batch(p, *script_args, **kwargs)
except Exception:
errors.report(f"Error running process_batch: {script.filename}", exc_info=True)
@ -888,71 +889,71 @@ class ScriptRunner:
def process_before_every_sampling(self, p, **kwargs):
for script in self.alwayson_scripts:
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.process_before_every_sampling(p, *script_args, **kwargs)
except Exception:
errors.report(f"Error running process_before_every_sampling: {script.filename}", exc_info=True)
def postprocess(self, p, processed):
for script in self.ordered_scripts('postprocess'):
for script in self.ordered_scripts("postprocess"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.postprocess(p, processed, *script_args)
except Exception:
errors.report(f"Error running postprocess: {script.filename}", exc_info=True)
def postprocess_batch(self, p, images, **kwargs):
for script in self.ordered_scripts('postprocess_batch'):
for script in self.ordered_scripts("postprocess_batch"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.postprocess_batch(p, *script_args, images=images, **kwargs)
except Exception:
errors.report(f"Error running postprocess_batch: {script.filename}", exc_info=True)
def postprocess_batch_list(self, p, pp: PostprocessBatchListArgs, **kwargs):
for script in self.ordered_scripts('postprocess_batch_list'):
for script in self.ordered_scripts("postprocess_batch_list"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.postprocess_batch_list(p, pp, *script_args, **kwargs)
except Exception:
errors.report(f"Error running postprocess_batch_list: {script.filename}", exc_info=True)
def post_sample(self, p, ps: PostSampleArgs):
for script in self.ordered_scripts('post_sample'):
for script in self.ordered_scripts("post_sample"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.post_sample(p, ps, *script_args)
except Exception:
errors.report(f"Error running post_sample: {script.filename}", exc_info=True)
def on_mask_blend(self, p, mba: MaskBlendArgs):
for script in self.ordered_scripts('on_mask_blend'):
for script in self.ordered_scripts("on_mask_blend"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.on_mask_blend(p, mba, *script_args)
except Exception:
errors.report(f"Error running post_sample: {script.filename}", exc_info=True)
def postprocess_image(self, p, pp: PostprocessImageArgs):
for script in self.ordered_scripts('postprocess_image'):
for script in self.ordered_scripts("postprocess_image"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.postprocess_image(p, pp, *script_args)
except Exception:
errors.report(f"Error running postprocess_image: {script.filename}", exc_info=True)
def postprocess_maskoverlay(self, p, ppmo: PostProcessMaskOverlayArgs):
for script in self.ordered_scripts('postprocess_maskoverlay'):
for script in self.ordered_scripts("postprocess_maskoverlay"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.postprocess_maskoverlay(p, ppmo, *script_args)
except Exception:
errors.report(f"Error running postprocess_image: {script.filename}", exc_info=True)
def postprocess_image_after_composite(self, p, pp: PostprocessImageArgs):
for script in self.ordered_scripts('postprocess_image_after_composite'):
for script in self.ordered_scripts("postprocess_image_after_composite"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.postprocess_image_after_composite(p, pp, *script_args)
except Exception:
errors.report(f"Error running postprocess_image_after_composite: {script.filename}", exc_info=True)
@ -964,7 +965,7 @@ class ScriptRunner:
except Exception:
errors.report(f"Error running on_before_component: {script.filename}", exc_info=True)
for script in self.ordered_scripts('before_component'):
for script in self.ordered_scripts("before_component"):
try:
script.before_component(component, **kwargs)
except Exception:
@ -977,7 +978,7 @@ class ScriptRunner:
except Exception:
errors.report(f"Error running on_after_component: {script.filename}", exc_info=True)
for script in self.ordered_scripts('after_component'):
for script in self.ordered_scripts("after_component"):
try:
script.after_component(component, **kwargs)
except Exception:
@ -1005,20 +1006,20 @@ class ScriptRunner:
self.scripts[si].args_to = args_to
def before_hr(self, p):
for script in self.ordered_scripts('before_hr'):
for script in self.ordered_scripts("before_hr"):
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.before_hr(p, *script_args)
except Exception:
errors.report(f"Error running before_hr: {script.filename}", exc_info=True)
def setup_scrips(self, p, *, is_ui=True):
for script in self.ordered_scripts('setup'):
for script in self.ordered_scripts("setup"):
if not is_ui and script.setup_for_ui_only:
continue
try:
script_args = p.script_args[script.args_from:script.args_to]
script_args = p.script_args[script.args_from : script.args_to]
script.setup(p, *script_args)
except Exception:
errors.report(f"Error running setup: {script.filename}", exc_info=True)
@ -1044,7 +1045,7 @@ class ScriptRunner:
index = script.args_from + i
if isinstance(args, tuple):
return args[:index] + (value,) + args[index + 1:]
return args[:index] + (value,) + args[index + 1 :]
elif isinstance(args, list):
args[index] = value
return args