Runtime error when using mps device

SaraMoosbauer: <p>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 (<br>
device = device = torch.device(“mps”)<br>
my_net = nn.Sequential(<br>
nn.Conv2d(1, 8, 4, stride=2),<br>
nn.ELU(),<br>
nn.Conv2d(8, 32, 3, stride=2),<br>
nn.ELU(),<br>
# (old-school) global average pooling<br>
nn.Conv2d(32, 64, 6),<br>
nn.ELU(),<br>
nn.Flatten(),<br>
nn.Linear(64, 10)<br>
).to(device))<br>
RuntimeError: PyTorch is not linked with support for mps devices<br>
Any Ideas how to solve this?</p>

albanD: <p>Hi,</p>
<p>Hi,<br>
Which mac hardware are you using?<br>
Also which version of python (in particular, is it x86 or arm version)?</p>

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

albanD: <p>You can check the platform for your python build with <code>import platform; print(platform.platform())</code>.<br>
If that says <code>x86</code>, then you have the wrong version of python installed.<br>
Can you double check that?</p>

SaraMoosbauer: <p>I have the wrong python version, thanks for your help</p>

ludwigwinkler: <p>Is this the direction we have to go install arm64 Python versions in the conda environment?</p>
<aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/65415996/how-to-specify-the-architecture-or-platform-for-a-new-conda-environment-apple">
  <header class="source">

      <a href="https://stackoverflow.com/questions/65415996/how-to-specify-the-architecture-or-platform-for-a-new-conda-environment-apple" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/4068755/bob" target="_blank" rel="noopener nofollow ugc">
    <img alt="Bob" src="https://www.gravatar.com/avatar/9bd8b7712f68e74658c350f54b76ffed?s=256&amp;d=identicon&amp;r=PG&amp;f=1" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/65415996/how-to-specify-the-architecture-or-platform-for-a-new-conda-environment-apple" target="_blank" rel="noopener nofollow ugc">How to specify the architecture or platform for a new conda environment? (Apple Silicon)</a>
</h4>

<div class="tags">
  <strong>anaconda, conda, miniconda, anaconda3</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/4068755/bob" target="_blank" rel="noopener nofollow ugc">
    Bob
  </a>
  on <a href="https://stackoverflow.com/questions/65415996/how-to-specify-the-architecture-or-platform-for-a-new-conda-environment-apple" target="_blank" rel="noopener nofollow ugc">09:26PM - 22 Dec 20 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


SaraMoosbauer: <p>I personally completely deinstalled anaconda and reinstalled it using the new m1 version</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://www.anaconda.com/blog/new-release-anaconda-distribution-now-supporting-m1">
  <header class="source">
      <img src="https://www.anaconda.com/assets/build/favicons/favicon-32x32-20c6665c85.png" class="site-icon" width="32" height="32">

      <a href="https://www.anaconda.com/blog/new-release-anaconda-distribution-now-supporting-m1" target="_blank" rel="noopener nofollow ugc">Anaconda</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/369;"><img src="https://assets.anaconda.com/production/Blog/distro-m1-release-hero.png?w=1200&amp;h=630&amp;q=82&amp;auto=format&amp;fit=clip&amp;dm=1652199667&amp;s=a93a7e075d98803a19b1e0dda0355c13" class="thumbnail" width="690" height="369"></div>

<h3><a href="https://www.anaconda.com/blog/new-release-anaconda-distribution-now-supporting-m1" target="_blank" rel="noopener nofollow ugc">Anaconda | New Release: Anaconda Distribution Now Supporting M1</a></h3>

  <p>The 2022.05 release of Anaconda Distribution features native compiling for Apple M1’s ARM64 architecture (boasting 20% faster compute), Anaconda Navigator 2.1.4, conda 4.12.0, as well as several new and updated packages. 2022.05 is also the last...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


pannous: <p>Maybe still of use to others:</p>
<p>The installation command generated on <a href="https://pytorch.org/get-started/locally/" class="inline-onebox" rel="noopener nofollow ugc">Start Locally | PyTorch</a> didn’t install the latest version for me. What did it was:</p>
<pre><code class="lang-auto">pip3 install --pre torch==1.12.0.dev20220518 --extra-index-url https://download.pytorch.org/whl/nightly/cpu 
</code></pre>

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

D_H: <p>Hi albanD,</p>
<p>I am running x86 in Spyder and do not know how to change the version.</p>
<p>I have redownloaded anaconda from this link: <a href="https://www.anaconda.com/blog/new-release-anaconda-distribution-now-supporting-m1" class="inline-onebox" rel="noopener nofollow ugc">Anaconda | New Release: Anaconda Distribution Now Supporting M1</a></p>
<p>But it actually downloads: Anaconda3-2022.05-MacOSX-x86_64</p>
<p>As you can, this gets x86 again and my problem persists.</p>
<p>How can I change the python version so I can get mps to work?<br>
Thanks!</p>

psobolewskiPhD: <p>Not sure Spyder has an arm64 release, but if you want an arm64 python env to test PyTorch <code>mps</code> I’d recommend conda-forge:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/conda-forge/miniforge">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">

      <a href="https://github.com/conda-forge/miniforge" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://opengraph.githubassets.com/0c0156d72647c680c55b9fcf54769c80b808e970bb9321fd5efdb11baeefe5b9/conda-forge/miniforge" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/conda-forge/miniforge" target="_blank" rel="noopener nofollow ugc">GitHub - conda-forge/miniforge: A conda-forge distribution.</a></h3>

  <p>A conda-forge distribution. Contribute to conda-forge/miniforge development by creating an account on GitHub.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
mamba is <img src="https://discuss.pytorch.org/images/emoji/apple/fire.png?v=12" title=":fire:" class="emoji" alt=":fire:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/rocket.png?v=12" title=":rocket:" class="emoji" alt=":rocket:" loading="lazy" width="20" height="20"></p>

D_H: <p>Thanks for your response! I’m fairly new here.</p>
<p>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.</p>
<p>Which sort of platform should I be running when trying to use mps?</p>
<p>Thanks!!!</p>

psobolewskiPhD: <p>Should work fine in jupyter on M1, as long as it’s arm64 native and not emulated x86.</p>

D_H: <p>Sadly I get the same issue in Jupyter NB within the torch-nightly environment:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/d/3de5ad5accae885d025c7dd38b5a954d79dd025a.png" data-download-href="https://discuss.pytorch.org/uploads/default/3de5ad5accae885d025c7dd38b5a954d79dd025a" title="Screen Shot 2022-05-26 at 1.09.38 pm"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/d/3de5ad5accae885d025c7dd38b5a954d79dd025a_2_690x370.png" alt="Screen Shot 2022-05-26 at 1.09.38 pm" data-base62-sha1="8PzcOM8Y7RHCPyTxyXUrMfbQvd8" width="690" height="370" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/d/3de5ad5accae885d025c7dd38b5a954d79dd025a_2_690x370.png, https://discuss.pytorch.org/uploads/default/original/3X/3/d/3de5ad5accae885d025c7dd38b5a954d79dd025a.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/d/3de5ad5accae885d025c7dd38b5a954d79dd025a.png 2x" data-dominant-color="F3F3F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2022-05-26 at 1.09.38 pm</span><span class="informations">990×532 127 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Any advice on this would be greatly appreciated!</p>

psobolewskiPhD: <p>Looks like you have 2 issues there, based on <code>platform.platform()</code>:</p>
<ol>
<li>you’re not on macOS 12.3 which is required for mps</li>
<li>if you’re on M1, you’re using an emulated x86 env and not arm64<br>
See the instructions from the PyTorch MPS announcement:<br>
<a href="https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/#getting-started" class="inline-onebox" rel="noopener nofollow ugc">Introducing Accelerated PyTorch Training on Mac | PyTorch</a>
</li>
</ol>

D_H: <p>Hi Peter,</p>
<ol>
<li>
<p>This is a problem because I am presently running macOS 12.4 on an M1 but it still picks up 10.16 somehow</p>
</li>
<li>
<p>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</p>
</li>
</ol>

psobolewskiPhD: <p>Well, then the only thing I can conclude is that your issue must be related to your anaconda env.<br>
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.<br>
For comparison my rosetta emulated conda-forge env:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; platform.platform()
'macOS-12.3.1-x86_64-i386-64bit'
</code></pre>
<p>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.<br>
This yields, on my 12.3.1 M1:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; platform.platform()
'macOS-12.3.1-arm64-arm-64bit'
</code></pre>

Ling: <p>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!</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self, activation=nn.Sigmoid(),<br>
input_size=1<em>28</em>28, hidden_size=100, classes=10):<br>
super(Net, self).<strong>init</strong>()<br>
self.input_size = input_size</p>
<pre><code>    # 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
</code></pre>
<p>net = Net()<br>
net.to(device)</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)</p>

m_ax: <p>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?</p>

Ling: <p>I reinstalled anaconda and python for Mac and the problem is solved.</p>