Problem with mps in Jupyter notebook

When I verify mps support using a recommended Python script:

import torch
if torch.backends.mps.is_available():
mps_device = torch.device(“mps”)
x = torch.ones(1, device=mps_device)
print (x)
else:
print (“MPS device not found.”)

I get output the following output:
tensor([1.], device=‘mps:0’)

But when I use the same script in Jupyter notebook, I get:
AttributeError Traceback (most recent call last)
Input In [1], in <cell line: 2>()
1 import torch
----> 2 if torch.backends.mps.is_available():
3 mps_device = torch.device(“mps”)
4 x = torch.ones(1, device=mps_device)

AttributeError: module ‘torch.backends’ has no attribute ‘mps’

You are most likely using a different virtual environment in your Jupyter notebook, so make sure the same env is used with the same PyTorch binary.
You can check the installed PyTorch release e.g. via print(torch.__version__).

1 Like

Yes, you are absolutely right! Thanks a lot!