stable-diffusion-webui
149 строк · 4.5 Кб
1import sys2import textwrap3import traceback4
5
6exception_records = []7
8
9def format_traceback(tb):10return [[f"{x.filename}, line {x.lineno}, {x.name}", x.line] for x in traceback.extract_tb(tb)]11
12
13def format_exception(e, tb):14return {"exception": str(e), "traceback": format_traceback(tb)}15
16
17def get_exceptions():18try:19return list(reversed(exception_records))20except Exception as e:21return str(e)22
23
24def record_exception():25_, e, tb = sys.exc_info()26if e is None:27return28
29if exception_records and exception_records[-1] == e:30return31
32exception_records.append(format_exception(e, tb))33
34if len(exception_records) > 5:35exception_records.pop(0)36
37
38def report(message: str, *, exc_info: bool = False) -> None:39"""40Print an error message to stderr, with optional traceback.
41"""
42
43record_exception()44
45for line in message.splitlines():46print("***", line, file=sys.stderr)47if exc_info:48print(textwrap.indent(traceback.format_exc(), " "), file=sys.stderr)49print("---", file=sys.stderr)50
51
52def print_error_explanation(message):53record_exception()54
55lines = message.strip().split("\n")56max_len = max([len(x) for x in lines])57
58print('=' * max_len, file=sys.stderr)59for line in lines:60print(line, file=sys.stderr)61print('=' * max_len, file=sys.stderr)62
63
64def display(e: Exception, task, *, full_traceback=False):65record_exception()66
67print(f"{task or 'error'}: {type(e).__name__}", file=sys.stderr)68te = traceback.TracebackException.from_exception(e)69if full_traceback:70# include frames leading up to the try-catch block71te.stack = traceback.StackSummary(traceback.extract_stack()[:-2] + te.stack)72print(*te.format(), sep="", file=sys.stderr)73
74message = str(e)75if "copying a param with shape torch.Size([640, 1024]) from checkpoint, the shape in current model is torch.Size([640, 768])" in message:76print_error_explanation("""77The most likely cause of this is you are trying to load Stable Diffusion 2.0 model without specifying its config file.
78See https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#stable-diffusion-20 for how to solve this.
79""")80
81
82already_displayed = {}83
84
85def display_once(e: Exception, task):86record_exception()87
88if task in already_displayed:89return90
91display(e, task)92
93already_displayed[task] = 194
95
96def run(code, task):97try:98code()99except Exception as e:100display(task, e)101
102
103def check_versions():104from packaging import version105from modules import shared106
107import torch108import gradio109
110expected_torch_version = "2.1.2"111expected_xformers_version = "0.0.23.post1"112expected_gradio_version = "3.41.2"113
114if version.parse(torch.__version__) < version.parse(expected_torch_version):115print_error_explanation(f"""116You are running torch {torch.__version__}.117The program is tested to work with torch {expected_torch_version}.118To reinstall the desired version, run with commandline flag --reinstall-torch.
119Beware that this will cause a lot of large files to be downloaded, as well as
120there are reports of issues with training tab on the latest version.
121
122Use --skip-version-check commandline argument to disable this check.
123""".strip())124
125if shared.xformers_available:126import xformers127
128if version.parse(xformers.__version__) < version.parse(expected_xformers_version):129print_error_explanation(f"""130You are running xformers {xformers.__version__}.131The program is tested to work with xformers {expected_xformers_version}.132To reinstall the desired version, run with commandline flag --reinstall-xformers.
133
134Use --skip-version-check commandline argument to disable this check.
135""".strip())136
137if gradio.__version__ != expected_gradio_version:138print_error_explanation(f"""139You are running gradio {gradio.__version__}.140The program is designed to work with gradio {expected_gradio_version}.141Using a different version of gradio is extremely likely to break the program.
142
143Reasons why you have the mismatched gradio version can be:
144- you use --skip-install flag.
145- you use webui.py to start the program instead of launch.py.
146- an extension installs the incompatible gradio version.
147
148Use --skip-version-check commandline argument to disable this check.
149""".strip())150
151