2023-06-14 12:53:13 -06:00
|
|
|
from functools import wraps
|
2022-11-27 23:00:10 -07:00
|
|
|
import html
|
|
|
|
import time
|
|
|
|
|
2023-08-21 03:48:56 -06:00
|
|
|
from modules import shared, progress, errors, devices, fifo_lock
|
2022-11-27 23:00:10 -07:00
|
|
|
|
2023-08-21 03:48:56 -06:00
|
|
|
queue_lock = fifo_lock.FIFOLock()
|
2022-11-27 23:00:10 -07:00
|
|
|
|
2023-04-16 21:50:08 -06:00
|
|
|
|
2022-11-27 23:00:10 -07:00
|
|
|
def wrap_queued_call(func):
|
|
|
|
def f(*args, **kwargs):
|
|
|
|
with queue_lock:
|
|
|
|
res = func(*args, **kwargs)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
def wrap_gradio_gpu_call(func, extra_outputs=None):
|
2023-06-14 12:53:13 -06:00
|
|
|
@wraps(func)
|
2023-04-29 13:15:20 -06:00
|
|
|
def f(*args, **kwargs):
|
2022-11-27 23:00:10 -07:00
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
# if the first argument is a string that says "task(...)", it is treated as a job id
|
2023-06-02 05:58:10 -06:00
|
|
|
if args and type(args[0]) == str and args[0].startswith("task(") and args[0].endswith(")"):
|
2023-01-15 08:50:56 -07:00
|
|
|
id_task = args[0]
|
2023-04-29 13:15:20 -06:00
|
|
|
progress.add_task_to_queue(id_task)
|
2023-01-15 08:50:56 -07:00
|
|
|
else:
|
|
|
|
id_task = None
|
2022-11-27 23:00:10 -07:00
|
|
|
|
|
|
|
with queue_lock:
|
2023-06-30 04:11:31 -06:00
|
|
|
shared.state.begin(job=id_task)
|
2023-04-29 13:15:20 -06:00
|
|
|
progress.start_task(id_task)
|
2023-01-15 08:50:56 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
res = func(*args, **kwargs)
|
2023-04-29 13:16:54 -06:00
|
|
|
progress.record_results(id_task, res)
|
2023-01-15 08:50:56 -07:00
|
|
|
finally:
|
2023-04-29 13:15:20 -06:00
|
|
|
progress.finish_task(id_task)
|
2022-11-27 23:00:10 -07:00
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
shared.state.end()
|
2022-11-27 23:00:10 -07:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2023-04-29 13:15:20 -06:00
|
|
|
return wrap_gradio_call(f, extra_outputs=extra_outputs, add_stats=True)
|
2022-11-27 23:00:10 -07:00
|
|
|
|
|
|
|
|
2023-04-16 21:50:08 -06:00
|
|
|
def wrap_gradio_call(func, extra_outputs=None, add_stats=False):
|
2023-06-14 12:53:13 -06:00
|
|
|
@wraps(func)
|
2023-04-16 21:50:08 -06:00
|
|
|
def f(*args, extra_outputs_array=extra_outputs, **kwargs):
|
2022-11-27 23:00:10 -07:00
|
|
|
run_memmon = shared.opts.memmon_poll_rate > 0 and not shared.mem_mon.disabled and add_stats
|
|
|
|
if run_memmon:
|
|
|
|
shared.mem_mon.monitor()
|
|
|
|
t = time.perf_counter()
|
|
|
|
|
|
|
|
try:
|
2023-04-16 21:50:08 -06:00
|
|
|
res = list(func(*args, **kwargs))
|
2022-11-27 23:00:10 -07:00
|
|
|
except Exception as e:
|
2023-05-28 23:54:13 -06:00
|
|
|
# When printing out our debug argument list,
|
|
|
|
# do not print out more than a 100 KB of text
|
|
|
|
max_debug_str_len = 131072
|
|
|
|
message = "Error completing request"
|
|
|
|
arg_str = f"Arguments: {args} {kwargs}"[:max_debug_str_len]
|
|
|
|
if len(arg_str) > max_debug_str_len:
|
|
|
|
arg_str += f" (Argument list truncated at {max_debug_str_len}/{len(arg_str)} characters)"
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"{message}\n{arg_str}", exc_info=True)
|
2022-11-27 23:00:10 -07:00
|
|
|
|
|
|
|
shared.state.job = ""
|
|
|
|
shared.state.job_count = 0
|
|
|
|
|
|
|
|
if extra_outputs_array is None:
|
|
|
|
extra_outputs_array = [None, '']
|
|
|
|
|
2023-05-09 13:17:58 -06:00
|
|
|
error_message = f'{type(e).__name__}: {e}'
|
|
|
|
res = extra_outputs_array + [f"<div class='error'>{html.escape(error_message)}</div>"]
|
2022-11-27 23:00:10 -07:00
|
|
|
|
2023-07-31 13:01:53 -06:00
|
|
|
devices.torch_gc()
|
|
|
|
|
2022-11-27 23:00:10 -07:00
|
|
|
shared.state.skipped = False
|
|
|
|
shared.state.interrupted = False
|
2024-01-01 06:50:59 -07:00
|
|
|
shared.state.stopping_generation = False
|
2022-11-27 23:00:10 -07:00
|
|
|
shared.state.job_count = 0
|
|
|
|
|
|
|
|
if not add_stats:
|
|
|
|
return tuple(res)
|
|
|
|
|
|
|
|
elapsed = time.perf_counter() - t
|
|
|
|
elapsed_m = int(elapsed // 60)
|
|
|
|
elapsed_s = elapsed % 60
|
2023-07-14 13:51:58 -06:00
|
|
|
elapsed_text = f"{elapsed_s:.1f} sec."
|
2022-11-27 23:00:10 -07:00
|
|
|
if elapsed_m > 0:
|
2023-07-14 13:51:58 -06:00
|
|
|
elapsed_text = f"{elapsed_m} min. "+elapsed_text
|
2022-11-27 23:00:10 -07:00
|
|
|
|
|
|
|
if run_memmon:
|
|
|
|
mem_stats = {k: -(v//-(1024*1024)) for k, v in shared.mem_mon.stop().items()}
|
|
|
|
active_peak = mem_stats['active_peak']
|
|
|
|
reserved_peak = mem_stats['reserved_peak']
|
|
|
|
sys_peak = mem_stats['system_peak']
|
|
|
|
sys_total = mem_stats['total']
|
2023-07-14 13:51:58 -06:00
|
|
|
sys_pct = sys_peak/max(sys_total, 1) * 100
|
2022-11-27 23:00:10 -07:00
|
|
|
|
2023-07-14 13:51:58 -06:00
|
|
|
toltip_a = "Active: peak amount of video memory used during generation (excluding cached data)"
|
|
|
|
toltip_r = "Reserved: total amout of video memory allocated by the Torch library "
|
|
|
|
toltip_sys = "System: peak amout of video memory allocated by all running programs, out of total capacity"
|
|
|
|
|
|
|
|
text_a = f"<abbr title='{toltip_a}'>A</abbr>: <span class='measurement'>{active_peak/1024:.2f} GB</span>"
|
|
|
|
text_r = f"<abbr title='{toltip_r}'>R</abbr>: <span class='measurement'>{reserved_peak/1024:.2f} GB</span>"
|
|
|
|
text_sys = f"<abbr title='{toltip_sys}'>Sys</abbr>: <span class='measurement'>{sys_peak/1024:.1f}/{sys_total/1024:g} GB</span> ({sys_pct:.1f}%)"
|
|
|
|
|
|
|
|
vram_html = f"<p class='vram'>{text_a}, <wbr>{text_r}, <wbr>{text_sys}</p>"
|
2022-11-27 23:00:10 -07:00
|
|
|
else:
|
|
|
|
vram_html = ''
|
|
|
|
|
|
|
|
# last item is always HTML
|
2023-07-14 13:51:58 -06:00
|
|
|
res[-1] += f"<div class='performance'><p class='time'>Time taken: <wbr><span class='measurement'>{elapsed_text}</span></p>{vram_html}</div>"
|
2022-11-27 23:00:10 -07:00
|
|
|
|
|
|
|
return tuple(res)
|
|
|
|
|
|
|
|
return f
|