Runtime error when using mps device

Hi I’ve tried running some code on the “maps” device, however I get the following error, when trying to load a neural net on the mps device (
device = device = torch.device(“mps”)
my_net = nn.Sequential(
nn.Conv2d(1, 8, 4, stride=2),
nn.ELU(),
nn.Conv2d(8, 32, 3, stride=2),
nn.ELU(),
# (old-school) global average pooling
nn.Conv2d(32, 64, 6),
nn.ELU(),
nn.Flatten(),
nn.Linear(64, 10)
).to(device))
RuntimeError: PyTorch is not linked with support for mps devices
Any Ideas how to solve this?

Hi,

Hi,
Which mac hardware are you using?
Also which version of python (in particular, is it x86 or arm version)?

1 Like

It’s a M1 Macbook air with python 3.9.12, I am not exactly sure on the version, but most likely arm architecture

You can check the platform for your python build with import platform; print(platform.platform()).
If that says x86, then you have the wrong version of python installed.
Can you double check that?

1 Like

I have the wrong python version, thanks for your help

1 Like

Is this the direction we have to go install arm64 Python versions in the conda environment?

I personally completely deinstalled anaconda and reinstalled it using the new m1 version

1 Like

Maybe still of use to others:

The installation command generated on Start Locally | PyTorch didn’t install the latest version for me. What did it was:

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

Hi,

You should remove the pin to the torch version: pip3 install --pre torch--extra-index-url https://download.pytorch.org/whl/nightly/cpu
Also, if you install domain libraries (vision/text/etc) they might downgrade your PyTorch right now (we are looking into a fix for that).

Hi albanD,

I am running x86 in Spyder and do not know how to change the version.

I have redownloaded anaconda from this link: Anaconda | New Release: Anaconda Distribution Now Supporting M1

But it actually downloads: Anaconda3-2022.05-MacOSX-x86_64

As you can, this gets x86 again and my problem persists.

How can I change the python version so I can get mps to work?
Thanks!

Not sure Spyder has an arm64 release, but if you want an arm64 python env to test PyTorch mps I’d recommend conda-forge:

mamba is :fire: :rocket:

1 Like

Thanks for your response! I’m fairly new here.

So I understand that I seemingly cannot use Spyder if I wish to use the mps device. I also don’t seem to be able to access it thought Jupyter NB. I’m not too sure about Miniforge.

Which sort of platform should I be running when trying to use mps?

Thanks!!!

Should work fine in jupyter on M1, as long as it’s arm64 native and not emulated x86.

1 Like

Sadly I get the same issue in Jupyter NB within the torch-nightly environment:

Any advice on this would be greatly appreciated!

Looks like you have 2 issues there, based on platform.platform():

  1. you’re not on macOS 12.3 which is required for mps
  2. if you’re on M1, you’re using an emulated x86 env and not arm64
    See the instructions from the PyTorch MPS announcement:
    Introducing Accelerated PyTorch Training on Mac | PyTorch
2 Likes

Hi Peter,

  1. This is a problem because I am presently running macOS 12.4 on an M1 but it still picks up 10.16 somehow

  2. In an attempt to get access to an arm64 env I reinstalled anaconda as Sara seemed to have success with this… but the problem persists

Well, then the only thing I can conclude is that your issue must be related to your anaconda env.
You are getting a x86 conda env that is also, oddly, reporting the wrong platform: 10.16 isn’t a real macOS release, it was a code for Big Sur which became 11.0.
For comparison my rosetta emulated conda-forge env:

>>> platform.platform()
'macOS-12.3.1-x86_64-i386-64bit'

So, I suggest using conda-forge miniforge or mambaforge, as I posted above. It’s also recommended by Apple for tensorflow-metal. Just make sure you use one of the -arm64 options, not x86.
This yields, on my 12.3.1 M1:

>>> platform.platform()
'macOS-12.3.1-arm64-arm-64bit'
1 Like

Hi, I am using macOS-13.3.1-arm64-arm-64bit, but got the same error message: “RuntimeError: PyTorch is not linked with support for mps devices” on “net.to(device)”, do you have any idea how to fix it? Thanks!

class Net(nn.Module):
def init(self, activation=nn.Sigmoid(),
input_size=12828, hidden_size=100, classes=10):
super(Net, self).init()
self.input_size = input_size

    # Here we initialize our activation and set up our two linear layers
    self.activation = activation
    self.fc1 = nn.Linear(input_size, hidden_size)
    self.fc2 = nn.Linear(hidden_size, classes)

def forward(self, x):
    x = x.view(-1, self.input_size) # flatten
    x = self.fc1(x)
    x = self.activation(x)
    x = self.fc2(x)

    return x

net = Net()
net.to(device)

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

Hi, I am getting the same error “RuntimeError: PyTorch is not linked with support for mps devices” when trying to run pytorch from a google colab notebook. Any idea what I can do?

I reinstalled anaconda and python for Mac and the problem is solved.