[CPP] C++ tensor cannot move from cpu to gpu

Weihao_Yuan: <pre><code class="lang-auto">module-&gt;to(at::kCUDA);
</code></pre>
<p>or</p>
<pre><code class="lang-auto">input_tensor.to(at::kCUDA);
</code></pre>
<p>When I try to move the model to gpu and use gpu to do the forward, it returns me an error and lets me report a bug to PyTorch. The error message is like following:</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'c10::Error'
  what():  p ASSERT FAILED at /pytorch/c10/core/impl/DeviceGuardImplInterface.h:130, please report a bug to PyTorch. DeviceGuardImpl for cuda is not available (getDeviceGuardImpl at /pytorch/c10/core/impl/DeviceGuardImplInterface.h:130)
frame #0: std::function&lt;std::string ()&gt;::operator()() const + 0x11 (0x7f623ebd50f1 in /home/will/Softwares/libtorch/lib/libc10.so)
frame #1: c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x2a (0x7f623ebd4a2a in /home/will/Softwares/libtorch/lib/libc10.so)
frame #2: at::native::to(at::Tensor const&amp;, c10::Device, c10::ScalarType, bool, bool) + 0x18d9 (0x7f623f73fd99 in /home/will/Softwares/libtorch/lib/libcaffe2.so)
frame #3: at::TypeDefault::to(at::Tensor const&amp;, c10::Device, c10::ScalarType, bool, bool) const + 0x1b (0x7f623f8ea7eb in /home/will/Softwares/libtorch/lib/libcaffe2.so)
frame #4: torch::jit::script::Module::to_impl(c10::optional&lt;c10::Device&gt; const&amp;, c10::optional&lt;c10::ScalarType&gt; const&amp;, bool) + 0x21b (0x7f624930affb in /home/will/Softwares/libtorch/lib/libtorch.so.1)
frame #5: torch::jit::script::Module::to_impl(c10::optional&lt;c10::Device&gt; const&amp;, c10::optional&lt;c10::ScalarType&gt; const&amp;, bool) + 0x69 (0x7f624930ae49 in /home/will/Softwares/libtorch/lib/libtorch.so.1)
frame #6: torch::jit::script::Module::to_impl(c10::optional&lt;c10::Device&gt; const&amp;, c10::optional&lt;c10::ScalarType&gt; const&amp;, bool) + 0x69 (0x7f624930ae49 in /home/will/Softwares/libtorch/lib/libtorch.so.1)
frame #7: torch::jit::script::Module::to(c10::Device, bool) + 0x26 (0x7f624930b356 in /home/will/Softwares/libtorch/lib/libtorch.so.1)
frame #8: main + 0x181 (0x42d277 in /home/will/Softwares/libtorch/example/project/cmake-build-debug/example-app)
frame #9: __libc_start_main + 0xf0 (0x7f623e273830 in /lib/x86_64-linux-gnu/libc.so.6)
frame #10: _start + 0x29 (0x42c859 in /home/will/Softwares/libtorch/example/project/cmake-build-debug/example-app)
</code></pre>
<p>The <code>torch::cuda::is_available()</code> returns me 0. But I do have 2 gpu and can use it with pytorch in python well.</p>

Weihao_Yuan: <p>solved…</p>

asa008: <p>Would you please tell me that how did you solved this problem?</p>

Weihao_Yuan: <p>It is a problem due to the version of cuda, cudnn. It is solved after I installed a new cuda and cudnn (cuda 9.0 and cudnn 7). I guess currently pytorch cpp library can only work on some version. It is not stable.</p>

Weihao_Yuan: <p>by the way, how to delete this question?</p>

asa008: <p>thank you very much, some trouble occured on my win10, cuda10, libtorch 1.0.1</p>

mhubii: <p>hm may the problem actually be that libtorch does not know onto which gpu to move the tensor? If that is the case, you could move your tensors to a certain gpu like so</p>
<pre><code class="lang-auto">module-&gt;to(torch::Device("cuda:0"));
</code></pre>
<p>where all following gpus are indexed in incrementing order, <code>cuda:1</code>, <code>cuda:2</code> and so on</p>

asa008: <p>tried, it’s not the truth</p>

NickKao: <p>May same with me.</p><aside class="quote quote-modified" data-post="2" data-topic="56029">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nickkao/48/15567_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/torch-cudnn-is-available-got-false/56029/2">Torch::cuda::cudnn_is_available() got false</a> <a class="badge-category__wrapper " href="/c/c/11"><span data-category-id="11" data-drop-close="true" class="badge-category " title="Topics related to the C++ Frontend, C++ API or C++ Extensions"><span class="badge-category__name">C++</span></span></a>
  </div>
  <blockquote>
    My Cmake Message: 
D:\WS\WS_CPP\torch_begin\libtorch-gpu\build&gt;cmake -G "Visual Studio 14 2015 Win64" ../
-- Selecting Windows SDK version  to target Windows 10.0.15063.
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe --…
  </blockquote>
</aside>
<p>
Use wrong libtorch lib.</p>