Example App with GPU support

I am trying to compile the example app described at cpp installation docs with GPU support.

I changed the download link to https://download.pytorch.org/libtorch/cu90/libtorch-shared-with-deps-latest.zip and the program to:

int main() {
  torch::Tensor tensor = torch::randn({3, 4}, torch::dtype(torch::kFloat32).device(torch::kCUDA, 1).requires_grad(true));
  std::cout << tensor << std::endl;

It compiles fine, but when executing I have the following error:

terminate called after throwing an instance of 'c10::Error'
  what():  p ASSERT FAILED at /home/dani/libtorch/include/c10/impl/DeviceGuardImplInterface.h:125, please report a bug to PyTorch. DeviceGuardImpl for cuda is not available (getDeviceGuardImpl at /home/dani/libtorch/include/c10/impl/DeviceGuardImplInterface.h:125)
frame #0: std::function<std::string ()>::operator()() const + 0x11 (0x7f2360b7cfe1 in /home/dani/libtorch/lib/libc10.so)
frame #1: c10::Error::Error(c10::SourceLocation, std::string const&) + 0x2a (0x7f2360b7cdfa in /home/dani/libtorch/lib/libc10.so)
frame #2: c10::impl::getDeviceGuardImpl(c10::DeviceType) + 0xe8 (0x40e288 in ./example-app)
frame #3: c10::impl::VirtualGuardImpl::VirtualGuardImpl(c10::DeviceType) + 0x36 (0x40e3b6 in ./example-app)
frame #4: c10::impl::InlineDeviceGuard<c10::impl::VirtualGuardImpl>::InlineDeviceGuard(c10::Device) + 0x2c (0x410844 in ./example-app)
frame #5: c10::DeviceGuard::DeviceGuard(c10::Device) + 0x20 (0x40e56c in ./example-app)
frame #6: ./example-app() [0x40cea0]
frame #7: torch::randn(c10::ArrayRef<long>, at::TensorOptions const&) + 0x1fb (0x40f99e in ./example-app)
frame #8: main + 0xf1 (0x40d246 in ./example-app)
frame #9: __libc_start_main + 0xf0 (0x7f2360228830 in /lib/x86_64-linux-gnu/libc.so.6)
frame #10: _start + 0x29 (0x40ca59 in ./example-app)

I am on Ubuntu and I have cuda 9.0 and cudnn 7 installed and I was able to fully compile pytorch from source on the same machine.

Any ideas?

how many GPUs do you have in your working station?
I believe your first (any maybe sole) GPU would be indexed with 0 or just omit the explicit mentioning of the GPU alltogether

1 Like

That really could be this! I just tested with 0 or ommiting the GPU index and got the same error :frowning:

See if this link helps you: https://pytorch.org/tutorials/advanced/cpp_frontend.html#moving-to-the-gpu

I still have the same issue. It seems that libtorch doesn’t find the GPU.

Finaly, my problem was a wrong installation of CuDNN.

1 Like

could you explain your solution and what do you mean by wrong installation of cudnn.

Yes. I had an older version of CuDNN installed on /usr/local/cuda/. I fixed by reinstalling cuDNN following the instructions here.