errors.py 1.4 KB

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