errors.py 4.1 KB

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