module 'torch' has no attribute 'has_mps'

Viewed 1275

I just followed a youtube video that teaches how to install PyTorch nightly for MacBook to accelerate by m1 chip. However, I came across a problem really wierd. I can see in the jupyter notebook that torch.has_mps = True. But in jupyter notebook in vscode, it shows that module 'torch' has no attribute 'has_mps'.

Can anyone kindly tell me why? really confusing.

1 Answers

Just make sure you installed the nightly build of PyTorch. Apple Silicon support in PyTorch is currently available only in nightly builds.

e.g., if you're using conda, try this:

conda install pytorch torchvision -c pytorch-nightly

or with pip

pip3 install --pre torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cpu

See more here:

https://pytorch.org/get-started/locally/

The script to verify that you're using the correct version is like you write, you can simply open python REPL in your env where you installed the above:

import torch
torch.has_mps

And you should get

True

To select the device, use "mps" instead of "cuda" (what you see in tutorials):

device = "mps" if torch.has_mps else "cpu"
print(f'Using device: {device}')

P.S. Although the guide suggest to install torchaudio, it will not work, at least with conda environments.

P.P.S. Also, try with the environment in this github repo:

https://github.com/causevic/mlboxm1/blob/main/pytorch_mac_m1.yml

Related