stable-diffusion-webui

Форк
0
98 строк · 5.8 Кб
1
import logging
2

3
import torch
4
from torch import Tensor
5
import platform
6
from modules.sd_hijack_utils import CondFunc
7
from packaging import version
8
from modules import shared
9

10
log = logging.getLogger(__name__)
11

12

13
# before torch version 1.13, has_mps is only available in nightly pytorch and macOS 12.3+,
14
# use check `getattr` and try it for compatibility.
15
# in torch version 1.13, backends.mps.is_available() and backends.mps.is_built() are introduced in to check mps availabilty,
16
# since torch 2.0.1+ nightly build, getattr(torch, 'has_mps', False) was deprecated, see https://github.com/pytorch/pytorch/pull/103279
17
def check_for_mps() -> bool:
18
    if version.parse(torch.__version__) <= version.parse("2.0.1"):
19
        if not getattr(torch, 'has_mps', False):
20
            return False
21
        try:
22
            torch.zeros(1).to(torch.device("mps"))
23
            return True
24
        except Exception:
25
            return False
26
    else:
27
        return torch.backends.mps.is_available() and torch.backends.mps.is_built()
28

29

30
has_mps = check_for_mps()
31

32

33
def torch_mps_gc() -> None:
34
    try:
35
        if shared.state.current_latent is not None:
36
            log.debug("`current_latent` is set, skipping MPS garbage collection")
37
            return
38
        from torch.mps import empty_cache
39
        empty_cache()
40
    except Exception:
41
        log.warning("MPS garbage collection failed", exc_info=True)
42

43

44
# MPS workaround for https://github.com/pytorch/pytorch/issues/89784
45
def cumsum_fix(input, cumsum_func, *args, **kwargs):
46
    if input.device.type == 'mps':
47
        output_dtype = kwargs.get('dtype', input.dtype)
48
        if output_dtype == torch.int64:
49
            return cumsum_func(input.cpu(), *args, **kwargs).to(input.device)
50
        elif output_dtype == torch.bool or cumsum_needs_int_fix and (output_dtype == torch.int8 or output_dtype == torch.int16):
51
            return cumsum_func(input.to(torch.int32), *args, **kwargs).to(torch.int64)
52
    return cumsum_func(input, *args, **kwargs)
53

54

55
# MPS workaround for https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14046
56
def interpolate_with_fp32_fallback(orig_func, *args, **kwargs) -> Tensor:
57
    try:
58
        return orig_func(*args, **kwargs)
59
    except RuntimeError as e:
60
        if "not implemented for" in str(e) and "Half" in str(e):
61
            input_tensor = args[0]
62
            return orig_func(input_tensor.to(torch.float32), *args[1:], **kwargs).to(input_tensor.dtype)
63
        else:
64
            print(f"An unexpected RuntimeError occurred: {str(e)}")
65

66
if has_mps:
67
    if platform.mac_ver()[0].startswith("13.2."):
68
        # MPS workaround for https://github.com/pytorch/pytorch/issues/95188, thanks to danieldk (https://github.com/explosion/curated-transformers/pull/124)
69
        CondFunc('torch.nn.functional.linear', lambda _, input, weight, bias: (torch.matmul(input, weight.t()) + bias) if bias is not None else torch.matmul(input, weight.t()), lambda _, input, weight, bias: input.numel() > 10485760)
70

71
    if version.parse(torch.__version__) < version.parse("1.13"):
72
        # PyTorch 1.13 doesn't need these fixes but unfortunately is slower and has regressions that prevent training from working
73

74
        # MPS workaround for https://github.com/pytorch/pytorch/issues/79383
75
        CondFunc('torch.Tensor.to', lambda orig_func, self, *args, **kwargs: orig_func(self.contiguous(), *args, **kwargs),
76
                                                          lambda _, self, *args, **kwargs: self.device.type != 'mps' and (args and isinstance(args[0], torch.device) and args[0].type == 'mps' or isinstance(kwargs.get('device'), torch.device) and kwargs['device'].type == 'mps'))
77
        # MPS workaround for https://github.com/pytorch/pytorch/issues/80800
78
        CondFunc('torch.nn.functional.layer_norm', lambda orig_func, *args, **kwargs: orig_func(*([args[0].contiguous()] + list(args[1:])), **kwargs),
79
                                                                                        lambda _, *args, **kwargs: args and isinstance(args[0], torch.Tensor) and args[0].device.type == 'mps')
80
        # MPS workaround for https://github.com/pytorch/pytorch/issues/90532
81
        CondFunc('torch.Tensor.numpy', lambda orig_func, self, *args, **kwargs: orig_func(self.detach(), *args, **kwargs), lambda _, self, *args, **kwargs: self.requires_grad)
82
    elif version.parse(torch.__version__) > version.parse("1.13.1"):
83
        cumsum_needs_int_fix = not torch.Tensor([1,2]).to(torch.device("mps")).equal(torch.ShortTensor([1,1]).to(torch.device("mps")).cumsum(0))
84
        cumsum_fix_func = lambda orig_func, input, *args, **kwargs: cumsum_fix(input, orig_func, *args, **kwargs)
85
        CondFunc('torch.cumsum', cumsum_fix_func, None)
86
        CondFunc('torch.Tensor.cumsum', cumsum_fix_func, None)
87
        CondFunc('torch.narrow', lambda orig_func, *args, **kwargs: orig_func(*args, **kwargs).clone(), None)
88

89
        # MPS workaround for https://github.com/pytorch/pytorch/issues/96113
90
        CondFunc('torch.nn.functional.layer_norm', lambda orig_func, x, normalized_shape, weight, bias, eps, **kwargs: orig_func(x.float(), normalized_shape, weight.float() if weight is not None else None, bias.float() if bias is not None else bias, eps).to(x.dtype), lambda _, input, *args, **kwargs: len(args) == 4 and input.device.type == 'mps')
91

92
        # MPS workaround for https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14046
93
        CondFunc('torch.nn.functional.interpolate', interpolate_with_fp32_fallback, None)
94

95
        # MPS workaround for https://github.com/pytorch/pytorch/issues/92311
96
        if platform.processor() == 'i386':
97
            for funcName in ['torch.argmax', 'torch.Tensor.argmax']:
98
                CondFunc(funcName, lambda _, input, *args, **kwargs: torch.max(input.float() if input.dtype == torch.int64 else input, *args, **kwargs)[1], lambda _, input, *args, **kwargs: input.device.type == 'mps')
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.