Local GPU

The default build of TensorFlow will use an NVIDIA® GPU if it is available and the appropriate drivers are installed, and otherwise fallback to using the CPU only.

The prerequisites for the GPU version of TensorFlow on each platform are covered below. Note that on all platforms (except macOS) you must be running an NVIDIA® GPU with CUDA® Compute Capability 3.5 or higher. See the list of CUDA-enabled GPU cards.

To enable TensorFlow to use a local NVIDIA® GPU, you can install the following:

Note that the following instructions require the development version of the tensorflow R package installed:

remotes::install_github("rstudio/tensorflow")

To install the required NVIDIA components on Ubuntu 22.04, you can run the following at the terminal:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-toolkit-11-8

cuDNN 8.6 is automatically installed by install_tensorflow() via pip if a GPU is detected.

Note that Tensorflow 2.10 was the last version of TensorFlow to natively support GPUs on Windows. To use a GPU on widows, it is recomended to run TensorFlow under WSL. See here for details. To use the RStudio IDE with WSL, see here

The specifics of installing required software differ by Linux version so please review the NVIDIA® documentation carefully to ensure you install everything correctly.

Note that cuDNN 8.6 can easily be installed via pip now, and then can be found by TensorFlow if the CUDNN_PATH and LD_LIBRARY_PATH variables are set appropriately. install_tensorflow() takes care of this for you.

Beginning with TensorFlow version 2.13, the ‘tensorflow’ pip package has native support for Arm Macs, including GPUs.

Make sure that an x86_64 build of R is not running under Rosetta. You can test if you have an Arm build of R like this:

R.version$arch

If this returns "aarch64", you’re using the correct build of R. If instead it returns "x86_64", then you need to install a different build of R. You can install an Arm build of R by navigating to https://cloud.r-project.org/bin/macosx/ and selecting the appropriate package (e.g., “R-4.2.1-arm64.pkg”) or using an installation manager like rig and running rig add release. If you have brew configured, then a command brew install --cask r at the terminal will install the correct version of R.

You can install Python for reticulate to use like this:

reticulate::install_python()

Alternatively, you can download and install python from www.python.org/downloads.

Test

You can test if TensorFlow can use a local GPU by running the following:

library(tensorflow)
tf$config$list_physical_devices("GPU")

If you see a list like this:

[[1]]
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')

Then that means TensorFlow is configured properly. If instead you see an empty list(), then TensorFlow is not using a GPU.

For support with installation, open a Github issue