errors.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import sys
  2. import textwrap
  3. import traceback
  4. def report(message: str, *, exc_info: bool = False) -> None:
  5. """
  6. Print an error message to stderr, with optional traceback.
  7. """
  8. for line in message.splitlines():
  9. print("***", line, file=sys.stderr)
  10. if exc_info:
  11. print(textwrap.indent(traceback.format_exc(), " "), file=sys.stderr)
  12. print("---", file=sys.stderr)
  13. def print_error_explanation(message):
  14. lines = message.strip().split("\n")
  15. max_len = max([len(x) for x in lines])
  16. print('=' * max_len, file=sys.stderr)
  17. for line in lines:
  18. print(line, file=sys.stderr)
  19. print('=' * max_len, file=sys.stderr)
  20. def display(e: Exception, task, *, full_traceback=False):
  21. print(f"{task or 'error'}: {type(e).__name__}", file=sys.stderr)
  22. te = traceback.TracebackException.from_exception(e)
  23. if full_traceback:
  24. # include frames leading up to the try-catch block
  25. te.stack = traceback.StackSummary(traceback.extract_stack()[:-2] + te.stack)
  26. print(*te.format(), sep="", file=sys.stderr)
  27. message = str(e)
  28. if "copying a param with shape torch.Size([640, 1024]) from checkpoint, the shape in current model is torch.Size([640, 768])" in message:
  29. print_error_explanation("""
  30. The most likely cause of this is you are trying to load Stable Diffusion 2.0 model without specifying its config file.
  31. See https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#stable-diffusion-20 for how to solve this.
  32. """)
  33. already_displayed = {}
  34. def display_once(e: Exception, task):
  35. if task in already_displayed:
  36. return
  37. display(e, task)
  38. already_displayed[task] = 1
  39. def run(code, task):
  40. try:
  41. code()
  42. except Exception as e:
  43. display(task, e)