CUDA not available after following DCGAN tutorial?

Spas: <p>Hey, I am really impressed with the intuitiveness of Pytorch’s both Python and C++ apis and want to use it at work where we mainly do C++ development, but I am struggling to get the GAN demo going because of a weird issue.</p>
<ol start="0">
<li>.Setup : FastAI Paperspace Ubuntu instance with all the latest version of pytorch.</li>
<li>When I open up the Python interperter and run torch.cuda.is_available() -&gt; TRUE</li>
<li>When I do the same thing from a C++ (exactly following this tutorial <a href="https://github.com/pytorch/examples/tree/master/cpp/dcgan" rel="nofollow noopener">https://github.com/pytorch/examples/tree/master/cpp/dcgan</a>) I get FALSE for toch::cuda::is_available() :?</li>
</ol>
<p>my CMakeLists.txt looks like this :</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(train-gan)

find_package(Torch REQUIRED)
find_package(CUDA 10.1 REQUIRED)

add_executable(train-gan train_gan.cpp dis.cpp gen.cpp)

target_link_libraries(train-gan "${TORCH_LIBRARIES}")
target_link_libraries(train-gan "${CUDA_LIBRARIES}")

set_property(TARGET train-gan PROPERTY CXX_STANDARD 11)
</code></pre>
<p>the full project you can find on my github -&gt; <a href="https://github.com/skalaydzhiyski/cpp-gan" rel="nofollow noopener">https://github.com/skalaydzhiyski/cpp-gan</a></p>
<p>I am new to cmake (as probably visible from the repo) and just want to get torch to use GPU in C++.</p>
<p>Let me know if you need any more information and thanks in advance.</p>

ptrblck: <p>Are you able to run the DCGAN example on the GPU or is it using your CPU?</p>
<p>I just cloned your repo and tried to run it.<br>
It looks like my GPU was detected:</p>
<pre><code class="lang-auto">Number of colour channels: 4
Running on device: cuda
</code></pre>
<p>However, I get an error after these lines:</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'c10::Error'
  what():  Error opening images file at ./mnist/train-images-idx3-ubyte (read_images at /pytorch/torch/csrc/api/src/data/datasets/mnist.cpp:66)
frame #0: std::function&lt;std::string ()&gt;::operator()() const + 0x11 (0x7fa06a35bb91 in /home/pbialecki/libs/libtorch_nightly/libtorch/lib/libc10.so)
</code></pre>

Spas: <p>Hey, thank you so much for the quick reply I have literally been biting my nails <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>I always get “Running on device: cpu” and that is on a machine that is giving me TRUE when I run cuda.is_available() from python interpreter AND also is recognized by running nvidia-smi. It seems I have everything setup but the application just doesn’t pick up that I have cuda installed :?</p>
<p>Let me know if you need any screenshots/info/output whatever… I have been struggling to get this running for a week now.</p>
<p>And thanks again fo course.</p>

Spas: <p>Can you please share your setup … libraries/install directories/versions anything.</p>
<p>I don’t care to run the DCGAN demo per se, I just need to get C++ Torch Api to find my CUDA. and to run on the gpu.</p>
<p>I have ran multiple Python models on this setup and they all run fine on the gpu with some to no effort from my end.</p>

ptrblck: <p>Sure!</p>
<ul>
<li>CUDA version: 10.1</li>
<li>CUDA driver: 418.56</li>
<li>TITAN V</li>
<li>
<code>nvcc</code> in <code>/usr/local/cuda-10.1/bin/nvcc</code>
</li>
<li>
<code>libtorch</code> unzipped in a <code>~/libs</code>
</li>
</ul>
<p>Does cmake find your CUDA install at all?</p>

Spas: <p>Yes, Cmake finds cuda successfully … I think my Cuda driver is 4.10 though, do you think that might be an issue?</p>

Spas: <p>I have Nvidia Quadro P5000 and the driver is 4.10… do you think might have something to do with the issue?</p>

Spas: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/8/88fdbc474bd8a17f4756d67d80ad8f6d3df05051.png" data-download-href="https://discuss.pytorch.org/uploads/default/88fdbc474bd8a17f4756d67d80ad8f6d3df05051" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/8/88fdbc474bd8a17f4756d67d80ad8f6d3df05051_2_690x120.png" alt="image" data-base62-sha1="jxSyPbAs59IWPue4U9jhe9hyDEB" width="690" height="120" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/8/88fdbc474bd8a17f4756d67d80ad8f6d3df05051_2_690x120.png, https://discuss.pytorch.org/uploads/default/original/2X/8/88fdbc474bd8a17f4756d67d80ad8f6d3df05051.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/8/88fdbc474bd8a17f4756d67d80ad8f6d3df05051.png 2x" data-dominant-color="0F0F0E"><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">image</span><span class="informations">925×162 26.2 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>

ptrblck: <p>Might be the reason.<br>
<a href="https://docs.nvidia.com/deploy/cuda-compatibility/index.html#binary-compatibility__table-toolkit-driver" rel="nofollow noopener">This table</a> gives you the compatible driver versions.<br>
For CUDA10.1, <code>&gt;=418.39</code> is recommended.</p>
<p>Could you try to update the driver and run the example again?</p>

Spas: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/1/124d72fd1c8817e8b54f1d9416a2c4fcb226697a.png" alt="image" data-base62-sha1="2BUwLMxoKkXgoqpJ2M1PS6yNzNU" width="543" height="335"></p>
<p>:X … I am planning on decomissioning my machine and requesting a new one to set up from scratch. Do you mind sharing the resource from which you setup your environment, because it seems the issue is with the linking on the libraries I think…</p>
<p>Thanks again for your time and for the quick responses I am desperate at this point.</p>

Spas: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/3/399968b7adb3fbc4a2dbe19872993d256b77ff0b.png" alt="image" data-base62-sha1="8dxTnrqcCGEhRsXZWFcF7gl9Kcz" width="327" height="109"></p>

Spas: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/d/daedbf73906fec695282ba5d8ef98199be139edd.png" data-download-href="https://discuss.pytorch.org/uploads/default/daedbf73906fec695282ba5d8ef98199be139edd" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/d/daedbf73906fec695282ba5d8ef98199be139edd_2_690x248.png" alt="image" data-base62-sha1="veJsA3r8S6ukpVU9X0uOl4aY35r" width="690" height="248" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/d/daedbf73906fec695282ba5d8ef98199be139edd_2_690x248.png, https://discuss.pytorch.org/uploads/default/optimized/2X/d/daedbf73906fec695282ba5d8ef98199be139edd_2_1035x372.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/d/daedbf73906fec695282ba5d8ef98199be139edd.png 2x" data-dominant-color="0C0D0E"><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">image</span><span class="informations">1071×386 92.6 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>

Spas: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/d/da5da28d3e1cff09b54610f19eff733a8f86c20e.png" alt="image" data-base62-sha1="v9KHqUUmV3RgsowQ0E9KhLKTHEG" width="598" height="198"></p>

Spas: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/5/5cbbbd01cc446f5f8354301444d6aae5d8ee6f64.png" alt="image" data-base62-sha1="dem9ARSDHXbmmH5P9whiF2TOJo0" width="553" height="168"></p>
<p>that is the only code that I run…</p>

ptrblck: <p>Your Python install is not really related to this issue in libtorch.<br>
E.g. I used my base conda environment without PyTorch installed, and could successfully build the C++ example.</p>
<p>Have you built PyTorch from source before?<br>
If so, did you see any issues?</p>
<p>Maybe Peter Goldsborough (one of the PyTorch core devs) will have any idea about this specific issue. CC <a class="mention" href="/u/goldsborough">@goldsborough</a></p>

Spas: <p>Hey <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>Sorry to bother you again with this, but I have just got a completely new and clean machine and want to follow the steps of installing</p>
<ol>
<li>Conda.</li>
<li>Pytorch from source (as you mentioned)</li>
</ol>
<p>Do you mind providing me with resource links where I can follow the procedure you did,  since I can find 50 different ones online and am nos sure which one of them will work out.</p>
<p>Thanks in advance.</p>

ptrblck: <p>I’m not sure, if I’m the right person to ask as I’m installing everything in a pragmatic (and thus maybe not the best?) way.<br>
Anyway,</p>
<ul>
<li>download the latest Conda package <a href="https://www.anaconda.com/distribution/" rel="nofollow noopener">link</a> (Use Python 3.7)</li>
<li>Select the NVIDIA driver from “Software &amp; Updates -&gt; Additional Driver” on Ubuntu (I’m using 418)</li>
<li>Download and install <a href="https://developer.nvidia.com/cuda-downloads" rel="nofollow noopener">CUDA</a>
</li>
</ul>
<p>Let me know, if you get stuck somewhere.</p>

Spas: <p>Hey,</p>
<p>I have followed EXACTLY what you said but still I don’t get the “cuda” device showing up whilst running my code…</p>
<p>Should I build pytorch from source ?<br>
And what environment variables should I set to make it work ?</p>
<p>Thanks again for your time and for your patience.</p>

ptrblck: <p>Did you install some PyTorch binaries and are you able to create CUDATensors using Python?</p>

Spas: <p>Yes I can create and run anything I want with  through Python and it is working like charm, but when I run the example GAN C++ project I get false for cuda::is_available()…</p>
<p>That is so bizarre I have tried multiple environments with multiple versions of Cuda / Cudnn / Torch on multiple machines - they ALL work fine with Python installs and not with C++…</p>