Search code examples
rgpurstudiotorch

How to activate R's torch package with GPU in RStudio


I wanted to run the tutorial here. https://blogs.rstudio.com/ai/posts/2020-09-29-introducing-torch-for-r/

After installation with

install.packages("torch")

and then when I activated it in my RStudio, I got the following error

> library(torch)

Attaching package: ‘torch’

The following object is masked from ‘package:reticulate’:

    as_iterator

> Sys.setenv(CUDA="/usr/local/cuda-10.2") 
> torch_tensor(1, device = "cuda")
Error: PyTorch is not linked with support for cuda devices
Exception raised from getDeviceGuardImpl at ../c10/core/impl/DeviceGuardImplInterface.h:319 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x6b (0x7fc04426c9cb in /home/ubuntu/R/x86_64-pc-linux-gnu-library/4.1/torch/lib/./libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) + 0xce (0x7fc04426837e in /home/ubuntu/R/x86_64-pc-linux-gnu-library/4.1/torch/lib/./libc10.so)
frame #2: <unknown function> + 0x120448d (0x7fc000b3d48d in /home/ubuntu/R/x86_64-pc-linux-gnu-library/4.1/torch/lib/./libtorch_cpu.so)
frame #3: at::native::to(at::Tensor const&, c10::optional<c10::ScalarType>, c10::optional<c10::Layout>, c10::optional<c10::Device>, c10::optional<bool>, bool, bool, c10::optional<c10::MemoryFormat>

I expect it to return:

torch_tensor 
 1
[ CUDAFloatType{1} ]

I know I've got several CUDA engines in my machine:

$ ls -lhSt /usr/local/
total 60K
drwxr-xr-x 17 root root 4.0K Sep 29 15:49 share
drwxr-xr-x  4 root root 4.0K Feb 19  2022 bin
drwxr-xr-x  7 root root 4.0K Dec 26  2021 lib
drwxr-xr-x  3 root root 4.0K Dec 26  2021 include
drwxr-xr-x  3 root root 4.0K Oct 19  2021 dcgm
drwxr-xr-x 18 root root 4.0K Oct 19  2021 cuda-10.0
lrwxrwxrwx  1 root root   20 Oct 19  2021 cuda -> /usr/local/cuda-10.0
drwxr-xr-x 20 root root 4.0K Oct 19  2021 cuda-10.2
drwxr-xr-x 19 root root 4.0K Oct 19  2021 cuda-10.1
drwxr-xr-x 25 root root 4.0K Oct 19  2021 cuda-11.0
drwxr-xr-x 19 root root 4.0K Oct 19  2021 cuda-11.1
drwxr-xr-x  4 root root 4.0K Oct 19  2021 init
drwxr-xr-x  3 root root 4.0K Oct 19  2021 etc
lrwxrwxrwx  1 root root    9 Feb 25  2021 man -> share/man
drwxr-xr-x  2 root root 4.0K Feb 25  2021 games
drwxr-xr-x  2 root root 4.0K Feb 25  2021 sbin
drwxr-xr-x  2 root root 4.0K Feb 25  2021 src

And

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85

Or

$ nvidia-smi
Mon Jan 23 21:34:31 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.161.03   Driver Version: 470.161.03   CUDA Version: 11.4

But why it didn't work? What's the way to resolve it?


Solution

  • Only the cuda-10.2 library from your list of CUDA libraries is supported by torch version 0.91. To ensure proper installation, it is necessary to specify this version during the installation process.

    Step 1: Initial Installation

    source("https://raw.githubusercontent.com/mlverse/torch/master/R/install.R")
    install.packages("torch")
    

    Step 2: Reinstall with CUDA 10.2

    library(torch)
    install_torch(type = "10.2")
    

    Step 3: Verify CUDA Recognition

    torch_tensor(1, device = "cuda")
    #torch_tensor 
    # 1
    #[ CUDAFloatType{1} ]
    

    Alternatively, you can use the command watch -n0.1 nvidia-smi to confirm that CUDA is recognized.