Cannot initialize CUDA without ATen_cuda library

username: hopewise
cooked: <p>I tried to run my python code, however, I got this error:</p>
<pre><code class="lang-auto">/Users/Apple/anaconda3/bin/python /Users/Apple/Downloads/linear-regression.py
Traceback (most recent call last):
  File "/Users/Apple/Downloads/linear-regression.py", line 39, in &lt;module&gt;
    model.cuda()
  File "/Users/Apple/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 258, in cuda
    return self._apply(lambda t: t.cuda(device))
  File "/Users/Apple/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 185, in _apply
    module._apply(fn)
  File "/Users/Apple/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 191, in _apply
    param.data = fn(param.data)
  File "/Users/Apple/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 258, in &lt;lambda&gt;
    return self._apply(lambda t: t.cuda(device))
RuntimeError: Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library; this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason.  The CUDA library MUST be loaded, EVEN IF you don't directly use any symbols from the CUDA library! One common culprit is a lack of -Wl,--no-as-needed in your link arguments; many dynamic linkers will delete dynamic library dependencies if you don't depend on any of their symbols.  You can check if this has occurred by using ldd on your binary to see if there is a dependency on *_cuda.so library.
</code></pre>
<p>Any idea?</p>

username: ptrblck
cooked: <p>How did you install PyTorch, from source or using conda?<br>
What does <code>torch.cuda.is_available()</code> return?</p>

username: hopewise
cooked: <p>Thanks for your reply, <code>torch.cuda.is_available()</code> returns <code>False</code><br>
I’ve installed PyTorch using <code>conda install pytorch torchvision -c pytorch</code></p>

username: ptrblck
cooked: <p>But I assume you do have a GPU installed?<br>
Could you check the NVIDIA kernel drivers?<br>
It looks like your GPU isn’t properly installed.</p>

username: hopewise
cooked: <p>I am an old iMac (2011) I am not use that I have GPU on board… how can I check?<br>
also, incase I don’t have GPUs, is there a way to emulate them? (for development and learning purpose)</p>

username: ptrblck
cooked: <p>I’ve never used a Mac, but apparently you can check it in “About this mac &gt; More info &gt; Graphics/Displays”.<br>
If you have a suitable GPU, note that you would have to build PyTorch from source to get GPU support on a Mac.<br>
You can find the build instructions <a href="https://github.com/pytorch/pytorch#from-source">here</a>.</p>
<p>Also, I’m not familiar with emulating a GPU.<br>
You could have a look at <a href="https://colab.research.google.com/">Google Colab</a>, which provides a free GPU for your notebook for a certain amount of time.</p>

username: SimonW
cooked: <p>Mac binaries do not ship with CUDA support. Moreover, from <a href="https://support.apple.com/kb/sp623?locale=en_US">this page</a>, it seems your machine don’t have a CUDA GPU anyways. However, your code calls <code>model.cuda()</code>. Hence the error.</p>

username: SimonW
cooked: <p>The error message is misleading indeed. I have opened an issue at <a href="https://github.com/pytorch/pytorch/issues/11430">https://github.com/pytorch/pytorch/issues/11430</a></p>

username: hopewise
cooked: <p>Thanks for info, now my code looks like this for every <code>cuda</code>:</p>
<pre><code class="lang-auto">if torch.cuda.is_available():
    model.cuda()
if torch.cuda.is_available():
        labels = Variable(torch.from_numpy(y_train).cuda())
    else:
        labels = Variable(torch.from_numpy(y_train))
</code></pre>

username: SimonW
cooked: <p>You shout just define one device object and use that through out your script though… This is probably helpful <a href="https://pytorch.org/2018/04/22/0_4_0-migration-guide.html">https://pytorch.org/2018/04/22/0_4_0-migration-guide.html</a></p>

username: Moussa_Traore
cooked: <p>Hey <a class="mention" href="/u/ptrblck">@ptrblck</a>, so I am having the same exact error after building pytorch from source. Do you have any idea why this might be happening ?</p>

username: ptrblck
cooked: <p>How did you build PyTorch and which OS are you using?<br>
Also, which PyTorch version are you building (master or an older tag)?</p>

username: Moussa_Traore
cooked: <p>I built pytorch from master on Ubuntu 20.04 using this gist I made: <a href="https://gist.github.com/traoremp/bc2f9c1118aafd0029534c016d763ccd" rel="nofollow noopener">https://gist.github.com/traoremp/bc2f9c1118aafd0029534c016d763ccd</a></p>

username: ptrblck
cooked: <p>Not sure, if Ubuntu 20.04 is causing the issue, but the provided script works fine in a docker using Ubuntu 16.04.</p>

username: Moussa_Traore
cooked: <p>Hello,</p>
<p>Thanks for your message.<br>
So I started the whole process over and was able to successfully build from source!!<br>
I don’t know why it was not working before.<br>
Maybe I missed a LD_LIBRARY_PATH :</p>
<blockquote>
<p>LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64:/usr/local/cuda-10.2/lib64</p>
</blockquote>
<p>Thanks !</p>