Google is committed to advancing racial equity for Black communities. See how.

GPU 支持

TensorFlow GPU 支持需要各种驱动程序和库。为了简化安装并避免库冲突,建议您使用支持 GPU 的 TensorFlow Docker 映像(仅限 Linux)。此设置仅需要 NVIDIA® GPU 驱动程序

这些安装说明适用于最新版 TensorFlow。如需了解可用于旧版 TensorFlow 的 CUDA 和 cuDNN 版本,请参阅经过测试的构建配置

pip 软件包

您可以参阅 pip 安装指南,了解可用的软件包、系统要求和说明。如需使用 pip 安装支持 GPU 的 TensorFlow 软件包,请选择稳定版或开发软件包:

pip install tensorflow  # stable

pip install tf-nightly  # preview

旧版 TensorFlow

对于 1.15 及更早版本,CPU 和 GPU 软件包是分开的:

pip install tensorflow==1.15      # CPU
pip install tensorflow-gpu==1.15  # GPU

硬件要求

支持以下带有 GPU 的设备:

软件要求

必须在系统中安装以下 NVIDIA® 软件:

Linux 设置

要在 Ubuntu 上安装所需的 NVIDIA 软件,最简单的方法是使用下面的 apt 指令。但是,如果从源代码构建 TensorFlow,请手动安装上述软件要求中列出的软件,并考虑以 -devel TensorFlow Docker 映像作为基础。

安装 CUDA® 工具包附带的 CUPTI,并将其安装目录附加到 $LD_LIBRARY_PATH 环境变量中:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/extras/CUPTI/lib64

对于 CUDA 计算能力为 3.0 的 GPU,或不同版本的 NVIDIA 库,请参阅在 Linux 下从源代码构建指南。

使用 apt 安装 CUDA

本部分将介绍如何针对 Ubuntu 16.04 和 18.04 安装 CUDA 10(TensorFlow 1.13.0 及更高版本)和 CUDA 9。这些说明可能适用于其他 Debian 系发行版。

Ubuntu 18.04 (CUDA 10.1)

# Add NVIDIA package repositories
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
sudo apt-get update
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt-get update

# Install NVIDIA driver
sudo apt-get install --no-install-recommends nvidia-driver-430
# Reboot. Check that GPUs are visible using the command: nvidia-smi

# Install development and runtime libraries (~4GB)
sudo apt-get install --no-install-recommends \
    cuda-10-1 \
    libcudnn7=7.6.4.38-1+cuda10.1  \
    libcudnn7-dev=7.6.4.38-1+cuda10.1


# Install TensorRT. Requires that libcudnn7 is installed above.
sudo apt-get install -y --no-install-recommends libnvinfer6=6.0.1-1+cuda10.1 \
    libnvinfer-dev=6.0.1-1+cuda10.1 \
    libnvinfer-plugin6=6.0.1-1+cuda10.1

Ubuntu 16.04 (CUDA 10.1)

# Add NVIDIA package repositories
# Add HTTPS support for apt-key
sudo apt-get install gnupg-curl
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_10.1.243-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1604_10.1.243-1_amd64.deb
sudo apt-get update
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt-get update

# Install NVIDIA driver
# Issue with driver install requires creating /usr/lib/nvidia
sudo mkdir /usr/lib/nvidia
sudo apt-get install --no-install-recommends nvidia-418
# Reboot. Check that GPUs are visible using the command: nvidia-smi

# Install development and runtime libraries (~4GB)
sudo apt-get install --no-install-recommends \
    cuda-10-1 \
    libcudnn7=7.6.4.38-1+cuda10.1  \
    libcudnn7-dev=7.6.4.38-1+cuda10.1


# Install TensorRT. Requires that libcudnn7 is installed above.
sudo apt-get install -y --no-install-recommends \
    libnvinfer6=6.0.1-1+cuda10.1 \
    libnvinfer-dev=6.0.1-1+cuda10.1 \
    libnvinfer-plugin6=6.0.1-1+cuda10.1

Ubuntu 16.04(CUDA 9.0,TensorFlow 1.13.0 以下版本)

# Add NVIDIA package repository
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
sudo apt install ./cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt update

# Install the NVIDIA driver
# Issue with driver install requires creating /usr/lib/nvidia
sudo mkdir /usr/lib/nvidia
sudo apt-get install --no-install-recommends nvidia-410
# Reboot. Check that GPUs are visible using the command: nvidia-smi

# Install CUDA and tools. Include optional NCCL 2.x
sudo apt install cuda9.0 cuda-cublas-9-0 cuda-cufft-9-0 cuda-curand-9-0 \
    cuda-cusolver-9-0 cuda-cusparse-9-0 libcudnn7=7.2.1.38-1+cuda9.0 \
    libnccl2=2.2.13-1+cuda9.0 cuda-command-line-tools-9-0

# Optional: Install the TensorRT runtime (must be after CUDA install)
sudo apt update
sudo apt install libnvinfer4=4.1.2-1+cuda9.0

Windows 设置

请参阅上面列出的硬件要求软件要求,并阅读适用于 Windows 的 CUDA® 安装指南

确保安装的 NVIDIA 软件包与上面列出的版本一致。特别是,如果没有 cuDNN64_7.dll 文件,TensorFlow 将无法加载。如需使用其他版本,请参阅在 Windows 下从源代码构建指南。

将 CUDA、CUPTI 和 cuDNN 安装目录添加到 %PATH% 环境变量中。例如,如果 CUDA 工具包安装到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1,同时 cuDNN 安装到 C:\tools\cuda,请更新 %PATH% 以匹配路径:

SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\libx64;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include;%PATH%
SET PATH=C:\tools\cuda\bin;%PATH%