Running Torch for R with CUDA in a Docker container

[2024-04-11 Thu] #permalink

Torch for R makes deep learning in R easy, only when installed

This post gives a simple instruction to build a Docker container that runs Torch for R (called torch hereafter) with CUDA devices on a Linux host. We have found that the image rocker/cuda, provided by the Rocker Project, suitable for a base image on which we add the layers of torch. The build procedure is summarized in the following five steps.

Confirm that your host has GPUs with appropriate CUDA Compute Capability

torch's current release version 0.12.0 supports CUDA 11.7 and CUDA 11.8. Make sure that your GPUs enjoy compatible compute capability.

Install the Docker Engine

Install the NVIDIA Container Toolkit

Read Nvidia's documentation on installation. Do not forget configuration. Especially using the Rootless mode requres additional settings of no-cgroups.

Save a Dockerfile

Save the following text in a Dockerfile.

FROM rocker/cuda@sha256:0125b935baee1a9d4aab1746622e851ceac040a64e7b820a88b66661f426ab9e
RUN R -e 'install.packages("torch")'
RUN R -e 'torch::install_torch()'

Build your image

The following command will build a image named foobar/torch:latest:

docker build --no-cache -t foobar/torch:latest -t Dockerfile

If all are done,

docker run --rm -it --gpus all foobar/torch:latest

will start a container with torch installed. Example:

% docker run --rm -it --gpus all foobar/torch:latest R -e "torch::cuda_is_available()"

== CUDA ==

CUDA Version 11.8.0

Container image Copyright (c) 2016-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:

A copy of this license is made available in this container at /NGC-DL-CONTAINER-LICENSE for your convenience.

R version 4.3.3 (2024-02-29) -- "Angel Food Cake"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> torch::cuda_is_available()
[1] TRUE

Bonus: remarks

  • Note that the disk size of the resulting image is larger than 10GB. Watch the storage space left if you saw a strange error during the build.
  • Consider using rocker/ml instead of rocker/cuda if you also want tidyverse or other R libraries pre-installed.

Happy deep learning!

© 2006-2024