diff options
author | brkirch <brkirch@users.noreply.github.com> | 2022-12-17 08:21:19 +0000 |
---|---|---|
committer | brkirch <brkirch@users.noreply.github.com> | 2022-12-17 09:22:58 +0000 |
commit | 16b4509fa60ec03102b2452b41799dafccd35970 (patch) | |
tree | 37efe0fbd67c70902a5d39f8d5249b9ef3e89ed6 /modules/devices.py | |
parent | 685f9631b56ff8bd43bce24ff5ce0f9a0e9af490 (diff) | |
download | stable-diffusion-webui-gfx803-16b4509fa60ec03102b2452b41799dafccd35970.tar.gz stable-diffusion-webui-gfx803-16b4509fa60ec03102b2452b41799dafccd35970.tar.bz2 stable-diffusion-webui-gfx803-16b4509fa60ec03102b2452b41799dafccd35970.zip |
Add numpy fix for MPS on PyTorch 1.12.1
When saving training results with torch.save(), an exception is thrown:
"RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead."
So for MPS, check if Tensor.requires_grad and detach() if necessary.
Diffstat (limited to 'modules/devices.py')
-rw-r--r-- | modules/devices.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/modules/devices.py b/modules/devices.py index f8cffae1..800510b7 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -125,7 +125,16 @@ def layer_norm_fix(*args, **kwargs): return orig_layer_norm(*args, **kwargs) +# MPS workaround for https://github.com/pytorch/pytorch/issues/90532 +orig_tensor_numpy = torch.Tensor.numpy +def numpy_fix(self, *args, **kwargs): + if self.requires_grad: + self = self.detach() + return orig_tensor_numpy(self, *args, **kwargs) + + # PyTorch 1.13 doesn't need these fixes but unfortunately is slower and has regressions that prevent training from working if has_mps() and version.parse(torch.__version__) < version.parse("1.13"): torch.Tensor.to = tensor_to_fix torch.nn.functional.layer_norm = layer_norm_fix + torch.Tensor.numpy = numpy_fix |