call_queue.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import html
  2. import threading
  3. import time
  4. from modules import shared, progress, errors
  5. queue_lock = threading.Lock()
  6. def wrap_queued_call(func):
  7. def f(*args, **kwargs):
  8. with queue_lock:
  9. res = func(*args, **kwargs)
  10. return res
  11. return f
  12. def wrap_gradio_gpu_call(func, extra_outputs=None):
  13. def f(*args, **kwargs):
  14. # if the first argument is a string that says "task(...)", it is treated as a job id
  15. if args and type(args[0]) == str and args[0].startswith("task(") and args[0].endswith(")"):
  16. id_task = args[0]
  17. progress.add_task_to_queue(id_task)
  18. else:
  19. id_task = None
  20. with queue_lock:
  21. shared.state.begin()
  22. progress.start_task(id_task)
  23. try:
  24. res = func(*args, **kwargs)
  25. progress.record_results(id_task, res)
  26. finally:
  27. progress.finish_task(id_task)
  28. shared.state.end()
  29. return res
  30. return wrap_gradio_call(f, extra_outputs=extra_outputs, add_stats=True)
  31. def wrap_gradio_call(func, extra_outputs=None, add_stats=False):
  32. def f(*args, extra_outputs_array=extra_outputs, **kwargs):
  33. run_memmon = shared.opts.memmon_poll_rate > 0 and not shared.mem_mon.disabled and add_stats
  34. if run_memmon:
  35. shared.mem_mon.monitor()
  36. t = time.perf_counter()
  37. try:
  38. res = list(func(*args, **kwargs))
  39. except Exception as e:
  40. # When printing out our debug argument list,
  41. # do not print out more than a 100 KB of text
  42. max_debug_str_len = 131072
  43. message = "Error completing request"
  44. arg_str = f"Arguments: {args} {kwargs}"[:max_debug_str_len]
  45. if len(arg_str) > max_debug_str_len:
  46. arg_str += f" (Argument list truncated at {max_debug_str_len}/{len(arg_str)} characters)"
  47. errors.report(f"{message}\n{arg_str}", exc_info=True)
  48. shared.state.job = ""
  49. shared.state.job_count = 0
  50. if extra_outputs_array is None:
  51. extra_outputs_array = [None, '']
  52. error_message = f'{type(e).__name__}: {e}'
  53. res = extra_outputs_array + [f"<div class='error'>{html.escape(error_message)}</div>"]
  54. shared.state.skipped = False
  55. shared.state.interrupted = False
  56. shared.state.job_count = 0
  57. if not add_stats:
  58. return tuple(res)
  59. elapsed = time.perf_counter() - t
  60. elapsed_m = int(elapsed // 60)
  61. elapsed_s = elapsed % 60
  62. elapsed_text = f"{elapsed_s:.2f}s"
  63. if elapsed_m > 0:
  64. elapsed_text = f"{elapsed_m}m "+elapsed_text
  65. if run_memmon:
  66. mem_stats = {k: -(v//-(1024*1024)) for k, v in shared.mem_mon.stop().items()}
  67. active_peak = mem_stats['active_peak']
  68. reserved_peak = mem_stats['reserved_peak']
  69. sys_peak = mem_stats['system_peak']
  70. sys_total = mem_stats['total']
  71. sys_pct = round(sys_peak/max(sys_total, 1) * 100, 2)
  72. vram_html = f"<p class='vram'>Torch active/reserved: {active_peak}/{reserved_peak} MiB, <wbr>Sys VRAM: {sys_peak}/{sys_total} MiB ({sys_pct}%)</p>"
  73. else:
  74. vram_html = ''
  75. # last item is always HTML
  76. res[-1] += f"<div class='performance'><p class='time'>Time taken: <wbr>{elapsed_text}</p>{vram_html}</div>"
  77. return tuple(res)
  78. return f