2023-07-10 18:18:34 +00:00
|
|
|
import logging
|
|
|
|
|
2023-02-01 14:28:16 +00:00
|
|
|
import torch
|
2023-11-28 19:45:04 +00:00
|
|
|
from torch import Tensor
|
2023-03-24 06:58:18 +00:00
|
|
|
import platform
|
2023-02-01 14:28:16 +00:00
|
|
|
from modules.sd_hijack_utils import CondFunc
|
|
|
|
from packaging import version
|
2023-08-09 07:25:35 +00:00
|
|
|
from modules import shared
|
2023-02-01 14:28:16 +00:00
|
|
|
|
2023-07-12 12:16:42 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2023-07-10 18:18:34 +00:00
|
|
|
|
2023-02-01 14:28:16 +00:00
|
|
|
|
2023-07-06 07:37:10 +00:00
|
|
|
# before torch version 1.13, has_mps is only available in nightly pytorch and macOS 12.3+,
|
|
|
|
# use check `getattr` and try it for compatibility.
|
2024-03-04 06:37:23 +00:00
|
|
|
# in torch version 1.13, backends.mps.is_available() and backends.mps.is_built() are introduced in to check mps availability,
|
2023-07-06 07:37:10 +00:00
|
|
|
# since torch 2.0.1+ nightly build, getattr(torch, 'has_mps', False) was deprecated, see https://github.com/pytorch/pytorch/pull/103279
|
2023-02-01 14:28:16 +00:00
|
|
|
def check_for_mps() -> bool:
|
2023-07-06 07:37:10 +00:00
|
|
|
if version.parse(torch.__version__) <= version.parse("2.0.1"):
|
|
|
|
if not getattr(torch, 'has_mps', False):
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
torch.zeros(1).to(torch.device("mps"))
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return torch.backends.mps.is_available() and torch.backends.mps.is_built()
|
2023-07-10 18:18:34 +00:00
|
|
|
|
|
|
|
|
2023-02-01 14:28:16 +00:00
|
|
|
has_mps = check_for_mps()
|
|
|
|
|
|
|
|
|
2023-07-10 18:18:34 +00:00
|
|
|
def torch_mps_gc() -> None:
|
|
|
|
try:
|
2023-08-09 07:25:35 +00:00
|
|
|
if shared.state.current_latent is not None:
|
2023-07-12 12:17:13 +00:00
|
|
|
log.debug("`current_latent` is set, skipping MPS garbage collection")
|
|
|
|
return
|
2023-07-10 18:18:34 +00:00
|
|
|
from torch.mps import empty_cache
|
|
|
|
empty_cache()
|
|
|
|
except Exception:
|
|
|
|
log.warning("MPS garbage collection failed", exc_info=True)
|
|
|
|
|
|
|
|
|
2023-02-01 14:28:16 +00:00
|
|
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/89784
|
|
|
|
def cumsum_fix(input, cumsum_func, *args, **kwargs):
|
|
|
|
if input.device.type == 'mps':
|
|
|
|
output_dtype = kwargs.get('dtype', input.dtype)
|
|
|
|
if output_dtype == torch.int64:
|
|
|
|
return cumsum_func(input.cpu(), *args, **kwargs).to(input.device)
|
2023-03-11 22:35:17 +00:00
|
|
|
elif output_dtype == torch.bool or cumsum_needs_int_fix and (output_dtype == torch.int8 or output_dtype == torch.int16):
|
2023-02-01 14:28:16 +00:00
|
|
|
return cumsum_func(input.to(torch.int32), *args, **kwargs).to(torch.int64)
|
|
|
|
return cumsum_func(input, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2023-11-28 19:45:04 +00:00
|
|
|
# MPS workaround for https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14046
|
|
|
|
def interpolate_with_fp32_fallback(orig_func, *args, **kwargs) -> Tensor:
|
|
|
|
try:
|
|
|
|
return orig_func(*args, **kwargs)
|
|
|
|
except RuntimeError as e:
|
|
|
|
if "not implemented for" in str(e) and "Half" in str(e):
|
|
|
|
input_tensor = args[0]
|
|
|
|
return orig_func(input_tensor.to(torch.float32), *args[1:], **kwargs).to(input_tensor.dtype)
|
|
|
|
else:
|
|
|
|
print(f"An unexpected RuntimeError occurred: {str(e)}")
|
|
|
|
|
2023-02-01 14:28:16 +00:00
|
|
|
if has_mps:
|
2023-03-24 06:58:18 +00:00
|
|
|
if platform.mac_ver()[0].startswith("13.2."):
|
|
|
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/95188, thanks to danieldk (https://github.com/explosion/curated-transformers/pull/124)
|
|
|
|
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)
|
|
|
|
|
2023-02-01 14:28:16 +00:00
|
|
|
if version.parse(torch.__version__) < version.parse("1.13"):
|
|
|
|
# PyTorch 1.13 doesn't need these fixes but unfortunately is slower and has regressions that prevent training from working
|
|
|
|
|
|
|
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/79383
|
|
|
|
CondFunc('torch.Tensor.to', lambda orig_func, self, *args, **kwargs: orig_func(self.contiguous(), *args, **kwargs),
|
|
|
|
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'))
|
2023-05-11 15:28:15 +00:00
|
|
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/80800
|
2023-02-01 14:28:16 +00:00
|
|
|
CondFunc('torch.nn.functional.layer_norm', lambda orig_func, *args, **kwargs: orig_func(*([args[0].contiguous()] + list(args[1:])), **kwargs),
|
|
|
|
lambda _, *args, **kwargs: args and isinstance(args[0], torch.Tensor) and args[0].device.type == 'mps')
|
|
|
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/90532
|
|
|
|
CondFunc('torch.Tensor.numpy', lambda orig_func, self, *args, **kwargs: orig_func(self.detach(), *args, **kwargs), lambda _, self, *args, **kwargs: self.requires_grad)
|
|
|
|
elif version.parse(torch.__version__) > version.parse("1.13.1"):
|
|
|
|
cumsum_needs_int_fix = not torch.Tensor([1,2]).to(torch.device("mps")).equal(torch.ShortTensor([1,1]).to(torch.device("mps")).cumsum(0))
|
|
|
|
cumsum_fix_func = lambda orig_func, input, *args, **kwargs: cumsum_fix(input, orig_func, *args, **kwargs)
|
|
|
|
CondFunc('torch.cumsum', cumsum_fix_func, None)
|
|
|
|
CondFunc('torch.Tensor.cumsum', cumsum_fix_func, None)
|
|
|
|
CondFunc('torch.narrow', lambda orig_func, *args, **kwargs: orig_func(*args, **kwargs).clone(), None)
|
2023-05-08 19:49:43 +00:00
|
|
|
|
2023-03-24 07:04:47 +00:00
|
|
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/96113
|
2023-05-08 19:49:43 +00:00
|
|
|
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')
|
2023-05-08 20:32:40 +00:00
|
|
|
|
2023-11-28 19:45:04 +00:00
|
|
|
# MPS workaround for https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14046
|
|
|
|
CondFunc('torch.nn.functional.interpolate', interpolate_with_fp32_fallback, None)
|
|
|
|
|
2023-05-08 20:32:40 +00:00
|
|
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/92311
|
|
|
|
if platform.processor() == 'i386':
|
|
|
|
for funcName in ['torch.argmax', 'torch.Tensor.argmax']:
|
2023-05-11 15:28:15 +00:00
|
|
|
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')
|