Path: blob/master/modules/call_queue.py
3055 views
import os.path1from functools import wraps2import html3import time45from modules import shared, progress, errors, devices, fifo_lock, profiling67queue_lock = fifo_lock.FIFOLock()8910def wrap_queued_call(func):11def f(*args, **kwargs):12with queue_lock:13res = func(*args, **kwargs)1415return res1617return f181920def wrap_gradio_gpu_call(func, extra_outputs=None):21@wraps(func)22def f(*args, **kwargs):2324# if the first argument is a string that says "task(...)", it is treated as a job id25if args and type(args[0]) == str and args[0].startswith("task(") and args[0].endswith(")"):26id_task = args[0]27progress.add_task_to_queue(id_task)28else:29id_task = None3031with queue_lock:32shared.state.begin(job=id_task)33progress.start_task(id_task)3435try:36res = func(*args, **kwargs)37progress.record_results(id_task, res)38finally:39progress.finish_task(id_task)4041shared.state.end()4243return res4445return wrap_gradio_call(f, extra_outputs=extra_outputs, add_stats=True)464748def wrap_gradio_call(func, extra_outputs=None, add_stats=False):49@wraps(func)50def f(*args, **kwargs):51try:52res = func(*args, **kwargs)53finally:54shared.state.skipped = False55shared.state.interrupted = False56shared.state.stopping_generation = False57shared.state.job_count = 058shared.state.job = ""59return res6061return wrap_gradio_call_no_job(f, extra_outputs, add_stats)626364def wrap_gradio_call_no_job(func, extra_outputs=None, add_stats=False):65@wraps(func)66def f(*args, extra_outputs_array=extra_outputs, **kwargs):67run_memmon = shared.opts.memmon_poll_rate > 0 and not shared.mem_mon.disabled and add_stats68if run_memmon:69shared.mem_mon.monitor()70t = time.perf_counter()7172try:73res = list(func(*args, **kwargs))74except Exception as e:75# When printing out our debug argument list,76# do not print out more than a 100 KB of text77max_debug_str_len = 13107278message = "Error completing request"79arg_str = f"Arguments: {args} {kwargs}"[:max_debug_str_len]80if len(arg_str) > max_debug_str_len:81arg_str += f" (Argument list truncated at {max_debug_str_len}/{len(arg_str)} characters)"82errors.report(f"{message}\n{arg_str}", exc_info=True)8384if extra_outputs_array is None:85extra_outputs_array = [None, '']8687error_message = f'{type(e).__name__}: {e}'88res = extra_outputs_array + [f"<div class='error'>{html.escape(error_message)}</div>"]8990devices.torch_gc()9192if not add_stats:93return tuple(res)9495elapsed = time.perf_counter() - t96elapsed_m = int(elapsed // 60)97elapsed_s = elapsed % 6098elapsed_text = f"{elapsed_s:.1f} sec."99if elapsed_m > 0:100elapsed_text = f"{elapsed_m} min. "+elapsed_text101102if run_memmon:103mem_stats = {k: -(v//-(1024*1024)) for k, v in shared.mem_mon.stop().items()}104active_peak = mem_stats['active_peak']105reserved_peak = mem_stats['reserved_peak']106sys_peak = mem_stats['system_peak']107sys_total = mem_stats['total']108sys_pct = sys_peak/max(sys_total, 1) * 100109110toltip_a = "Active: peak amount of video memory used during generation (excluding cached data)"111toltip_r = "Reserved: total amount of video memory allocated by the Torch library "112toltip_sys = "System: peak amount of video memory allocated by all running programs, out of total capacity"113114text_a = f"<abbr title='{toltip_a}'>A</abbr>: <span class='measurement'>{active_peak/1024:.2f} GB</span>"115text_r = f"<abbr title='{toltip_r}'>R</abbr>: <span class='measurement'>{reserved_peak/1024:.2f} GB</span>"116text_sys = f"<abbr title='{toltip_sys}'>Sys</abbr>: <span class='measurement'>{sys_peak/1024:.1f}/{sys_total/1024:g} GB</span> ({sys_pct:.1f}%)"117118vram_html = f"<p class='vram'>{text_a}, <wbr>{text_r}, <wbr>{text_sys}</p>"119else:120vram_html = ''121122if shared.opts.profiling_enable and os.path.exists(shared.opts.profiling_filename):123profiling_html = f"<p class='profile'> [ <a href='{profiling.webpath()}' download>Profile</a> ] </p>"124else:125profiling_html = ''126127# last item is always HTML128res[-1] += f"<div class='performance'><p class='time'>Time taken: <wbr><span class='measurement'>{elapsed_text}</span></p>{vram_html}{profiling_html}</div>"129130return tuple(res)131132return f133134135136