Path: blob/master/modules/errors.py
3055 views
import sys1import textwrap2import traceback345exception_records = []678def format_traceback(tb):9return [[f"{x.filename}, line {x.lineno}, {x.name}", x.line] for x in traceback.extract_tb(tb)]101112def format_exception(e, tb):13return {"exception": str(e), "traceback": format_traceback(tb)}141516def get_exceptions():17try:18return list(reversed(exception_records))19except Exception as e:20return str(e)212223def record_exception():24_, e, tb = sys.exc_info()25if e is None:26return2728if exception_records and exception_records[-1] == e:29return3031exception_records.append(format_exception(e, tb))3233if len(exception_records) > 5:34exception_records.pop(0)353637def report(message: str, *, exc_info: bool = False) -> None:38"""39Print an error message to stderr, with optional traceback.40"""4142record_exception()4344for line in message.splitlines():45print("***", line, file=sys.stderr)46if exc_info:47print(textwrap.indent(traceback.format_exc(), " "), file=sys.stderr)48print("---", file=sys.stderr)495051def print_error_explanation(message):52record_exception()5354lines = message.strip().split("\n")55max_len = max([len(x) for x in lines])5657print('=' * max_len, file=sys.stderr)58for line in lines:59print(line, file=sys.stderr)60print('=' * max_len, file=sys.stderr)616263def display(e: Exception, task, *, full_traceback=False):64record_exception()6566print(f"{task or 'error'}: {type(e).__name__}", file=sys.stderr)67te = traceback.TracebackException.from_exception(e)68if full_traceback:69# include frames leading up to the try-catch block70te.stack = traceback.StackSummary(traceback.extract_stack()[:-2] + te.stack)71print(*te.format(), sep="", file=sys.stderr)7273message = str(e)74if "copying a param with shape torch.Size([640, 1024]) from checkpoint, the shape in current model is torch.Size([640, 768])" in message:75print_error_explanation("""76The most likely cause of this is you are trying to load Stable Diffusion 2.0 model without specifying its config file.77See https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#stable-diffusion-20 for how to solve this.78""")798081already_displayed = {}828384def display_once(e: Exception, task):85record_exception()8687if task in already_displayed:88return8990display(e, task)9192already_displayed[task] = 1939495def run(code, task):96try:97code()98except Exception as e:99display(task, e)100101102def check_versions():103from packaging import version104from modules import shared105106import torch107import gradio108109expected_torch_version = "2.1.2"110expected_xformers_version = "0.0.23.post1"111expected_gradio_version = "3.41.2"112113if version.parse(torch.__version__) < version.parse(expected_torch_version):114print_error_explanation(f"""115You are running torch {torch.__version__}.116The program is tested to work with torch {expected_torch_version}.117To reinstall the desired version, run with commandline flag --reinstall-torch.118Beware that this will cause a lot of large files to be downloaded, as well as119there are reports of issues with training tab on the latest version.120121Use --skip-version-check commandline argument to disable this check.122""".strip())123124if shared.xformers_available:125import xformers126127if version.parse(xformers.__version__) < version.parse(expected_xformers_version):128print_error_explanation(f"""129You are running xformers {xformers.__version__}.130The program is tested to work with xformers {expected_xformers_version}.131To reinstall the desired version, run with commandline flag --reinstall-xformers.132133Use --skip-version-check commandline argument to disable this check.134""".strip())135136if gradio.__version__ != expected_gradio_version:137print_error_explanation(f"""138You are running gradio {gradio.__version__}.139The program is designed to work with gradio {expected_gradio_version}.140Using a different version of gradio is extremely likely to break the program.141142Reasons why you have the mismatched gradio version can be:143- you use --skip-install flag.144- you use webui.py to start the program instead of launch.py.145- an extension installs the incompatible gradio version.146147Use --skip-version-check commandline argument to disable this check.148""".strip())149150151152