errors.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import sys
  2. import textwrap
  3. import traceback
  4. exception_records = []
  5. def format_traceback(tb):
  6. return [[f"{x.filename}, line {x.lineno}, {x.name}", x.line] for x in traceback.extract_tb(tb)]
  7. def format_exception(e, tb):
  8. return {"exception": str(e), "traceback": format_traceback(tb)}
  9. def get_exceptions():
  10. try:
  11. return list(reversed(exception_records))
  12. except Exception as e:
  13. return str(e)
  14. def record_exception():
  15. _, e, tb = sys.exc_info()
  16. if e is None:
  17. return
  18. if exception_records and exception_records[-1] == e:
  19. return
  20. exception_records.append(format_exception(e, tb))
  21. if len(exception_records) > 5:
  22. exception_records.pop(0)
  23. def report(message: str, *, exc_info: bool = False) -> None:
  24. """
  25. Print an error message to stderr, with optional traceback.
  26. """
  27. record_exception()
  28. for line in message.splitlines():
  29. print("***", line, file=sys.stderr)
  30. if exc_info:
  31. print(textwrap.indent(traceback.format_exc(), " "), file=sys.stderr)
  32. print("---", file=sys.stderr)
  33. def print_error_explanation(message):
  34. record_exception()
  35. lines = message.strip().split("\n")
  36. max_len = max([len(x) for x in lines])
  37. print('=' * max_len, file=sys.stderr)
  38. for line in lines:
  39. print(line, file=sys.stderr)
  40. print('=' * max_len, file=sys.stderr)
  41. def display(e: Exception, task, *, full_traceback=False):
  42. record_exception()
  43. print(f"{task or 'error'}: {type(e).__name__}", file=sys.stderr)
  44. te = traceback.TracebackException.from_exception(e)
  45. if full_traceback:
  46. # include frames leading up to the try-catch block
  47. te.stack = traceback.StackSummary(traceback.extract_stack()[:-2] + te.stack)
  48. print(*te.format(), sep="", file=sys.stderr)
  49. message = str(e)
  50. if "copying a param with shape torch.Size([640, 1024]) from checkpoint, the shape in current model is torch.Size([640, 768])" in message:
  51. print_error_explanation("""
  52. The most likely cause of this is you are trying to load Stable Diffusion 2.0 model without specifying its config file.
  53. See https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#stable-diffusion-20 for how to solve this.
  54. """)
  55. already_displayed = {}
  56. def display_once(e: Exception, task):
  57. record_exception()
  58. if task in already_displayed:
  59. return
  60. display(e, task)
  61. already_displayed[task] = 1
  62. def run(code, task):
  63. try:
  64. code()
  65. except Exception as e:
  66. display(task, e)
  67. def check_versions():
  68. from packaging import version
  69. from modules import shared
  70. import torch
  71. import gradio
  72. expected_torch_version = "2.0.0"
  73. expected_xformers_version = "0.0.20"
  74. expected_gradio_version = "3.41.2"
  75. if version.parse(torch.__version__) < version.parse(expected_torch_version):
  76. print_error_explanation(f"""
  77. You are running torch {torch.__version__}.
  78. The program is tested to work with torch {expected_torch_version}.
  79. To reinstall the desired version, run with commandline flag --reinstall-torch.
  80. Beware that this will cause a lot of large files to be downloaded, as well as
  81. there are reports of issues with training tab on the latest version.
  82. Use --skip-version-check commandline argument to disable this check.
  83. """.strip())
  84. if shared.xformers_available:
  85. import xformers
  86. if version.parse(xformers.__version__) < version.parse(expected_xformers_version):
  87. print_error_explanation(f"""
  88. You are running xformers {xformers.__version__}.
  89. The program is tested to work with xformers {expected_xformers_version}.
  90. To reinstall the desired version, run with commandline flag --reinstall-xformers.
  91. Use --skip-version-check commandline argument to disable this check.
  92. """.strip())
  93. if gradio.__version__ != expected_gradio_version:
  94. print_error_explanation(f"""
  95. You are running gradio {gradio.__version__}.
  96. The program is designed to work with gradio {expected_gradio_version}.
  97. Using a different version of gradio is extremely likely to break the program.
  98. Reasons why you have the mismatched gradio version can be:
  99. - you use --skip-install flag.
  100. - you use webui.py to start the program instead of launch.py.
  101. - an extension installs the incompatible gradio version.
  102. Use --skip-version-check commandline argument to disable this check.
  103. """.strip())