Skip to content

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

deepxde/backend/pytorch/tensor.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,23 @@
2121
else:
2222
torch.set_default_tensor_type(torch.cuda.FloatTensor)
2323
elif torch.backends.mps.is_available():
24+
fallback_device = torch.get_default_device()
2425
torch.set_default_device("mps")
25-
torch._dynamo.disable() # A temporary trick to evade the Pytorch MPS bug (https://github.com/pytorch/pytorch/issues/149184)
26+
27+
# As of March 2025, the MacOS X-based GitHub Actions building enviroment sees
28+
# the MPS GPU, but can't access it. Thus as try-except workaround is applied.
29+
try:
30+
# A temporary trick to evade the Pytorch optimizer bug on MPS
31+
# See https://github.com/pytorch/pytorch/issues/149184
32+
torch._dynamo.disable()
33+
34+
except Exception as e:
35+
import warnings
36+
warnings.warn(
37+
f'An MPS GPU has been detected, but cannot be used.'
38+
f'Falling back to CPU.\nThe exception message is:\n {e}'
39+
)
40+
torch.set_default_device(fallback_device)
2641

2742

2843
lib = torch

0 commit comments

Comments
 (0)