在没有 sudo 的 Ubuntu 20.04 上使用 GPU 设置 Tensorflow 2.4

Posted

技术标签:

【中文标题】在没有 sudo 的 Ubuntu 20.04 上使用 GPU 设置 Tensorflow 2.4【英文标题】:Setup Tensorflow 2.4 on Ubuntu 20.04 with GPU without sudo 【发布时间】:2021-08-01 15:15:00 【问题描述】:

我可以访问具有 Ubuntu 20.04 设置和 GPU 的虚拟机。系统管理员已经安装了最新的 Cuda 驱动程序,但不幸的是,这还不足以在 Tensorflow 中使用 GPU,因为当涉及到特定的 Cuda Toolkit + CuDNN 版本集时,TF 的每个版本都可能非常挑剔。我没有 sudo 权限,所以我需要在本地安装所有东西。

nvidia-smi

返回驱动程序版本:465.19.01 CUDA 版本:11.3

python -c "import tensorflow as tf, logging; logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s'); tf.config.list_physical_devices('GPU');"

返回

2021-05-11 10:56:26.737279:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcudart.so.11.0”; dlerror:libcudart.so.11.0:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:26.737338: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] 如果您的机器上没有设置 GPU,请忽略上面的 cudart dlerror。 2021-05-11 10:56:28.313896:我 tensorflow/compiler/jit/xla_cpu_device.cc:41] 未创建 XLA 设备,未设置 tf_xla_enable_xla_devices 2021-05-11 10:56:28.315540: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] 成功打开动态库 libcuda.so.1 2021-05-11 10:56:28.324232: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:941] 从 SysFS 读取的成功 NUMA 节点具有负值 (-1),但必须至少有一个 NUMA 节点,因此返回NUMA 节点零 2021-05-11 10:56:28.324707:我 tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] 找到具有属性的设备 0: pciBusID:0000:00:05.0 名称:NVIDIA Tesla P100-PCIE-12GB 计算能力:6.0 coreClock:1.3285GHz coreCount:56 deviceMemorySize:11.91GiB deviceMemoryBandwidth:511.41GiB/s 2021-05-11 10:56:28.324867: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:941] 从 SysFS 读取的成功 NUMA 节点具有负值 (-1),但必须至少有一个 NUMA 节点,因此返回NUMA 节点零 2021-05-11 10:56:28.325293: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] 找到具有属性的设备 1: pciBusID:0000:00:06.0 名称:NVIDIA Tesla P100-PCIE-12GB 计算能力:6.0 coreClock:1.3285GHz coreCount:56 deviceMemorySize:11.91GiB deviceMemoryBandwidth:511.41GiB/s 2021-05-11 10:56:28.325438:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcudart.so.11.0”; dlerror:libcudart.so.11.0:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.325563:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcublas.so.11”; dlerror:libcublas.so.11:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.325706:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcublasLt.so.11”; dlerror:libcublasLt.so.11:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.325820:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcufft.so.10”; dlerror:libcufft.so.10:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.325931:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcurand.so.10”; dlerror:libcurand.so.10:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.326028:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcusolver.so.10”; dlerror:libcusolver.so.10:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.326117:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcusparse.so.11”; dlerror:libcusparse.so.11:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.326215:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 无法加载动态库“libcudnn.so.8”; dlerror:libcudnn.so.8:无法打开共享对象文件:没有这样的文件或目录 2021-05-11 10:56:28.326230: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1757] 无法打开某些 GPU 库。如果您想使用 GPU,请确保正确安装了上述缺少的库。按照https://www.tensorflow.org/install/gpu 的指南,了解如何为您的平台下载和设置所需的库。 跳过注册 GPU 设备...

这表明 GPU 不会在 TF 应用程序中使用。

我不得不花一些时间设置虚拟机,所以我将在下面逐步发布我的解决方案。

【问题讨论】:

【参考方案1】:

在没有管理员权限的 Ubuntu 20.04 环境中设置 Tensorflow 2.4.x(针对 2.4.1 测试)的说明。假设系统管理员已经安装了最新的 Cuda 驱动程序。它由安装 Cuda 11.0 工具包 + CuDNN 8.2.0 组成。

以下说明将在目录 /home/pherath/cuda_toolkits/cuda-11.0 下安装 CUDA 11.0(经测试可用于 Tensorflow 2.4.1),无需 sudo 权限。

第 1 步。下载 CUDA 11.0

wget http://developer.download.nvidia.com/compute/cuda/11.0.2/local_installers/cuda_11.0.2_450.51.05_linux.run
chmod +x cuda_11.0.2_450.51.05_linux.run

第 2 步,选项 1:对于快速自动化表单,请使用以下方法

./cuda_11.0.2_450.51.05_linux.run --silent --tmpdir=. --toolkit --toolkitpath=/home/pherath/cuda_toolkits/cuda-11.0

第 2 步,选项 2:这是一个直观的分步指南

./cuda_11.0.2_450.51.05_linux.run

Continue, then accept the EULA.

Leave only Cuda Toolkit checked, uncheck everything else. Then go to Options.

Go into Toolkit Options.

Uncheck everything, then go to Change Toolkit Install Path and replace it with /home/pherath/cuda_toolkits/cuda-11.0 After this step, proceed with Install.

第 3 步。下载 CUDA 11.0 补丁

wget https://developer.download.nvidia.com/compute/cuda/11.0.3/local_installers/cuda_11.0.3_450.51.06_linux.run
chmod +x cuda_11.0.3_450.51.06_linux.run

第 4 步。选项 1:快速静音模式

./cuda_11.0.3_450.51.06_linux.run --silent --tmpdir=. --toolkit --toolkitpath=/home/pherath/cuda_toolkits/cuda-11.0

第 4 步。选项 2:GUI 模式 重复步骤 2,选项 2 的确切步骤。

安装可能会出错。 When checking the logs, the error I saw suggests that there might be a bug in the installation script. The only offending term is the symbolic link of one file.

[错误]:boost::filesystem::create_symlink:文件存在:“libcuinj64.so.11.0”、“/home/pherath/cuda_toolkits/cuda-11.0/targets/x86_64-linux/lib/libcuinj64.so”

我在各种分发尝试中遇到了其他几个单一错误(例如,在 Ubuntu 16.04 上): libcuinj64.so.11.0、libaccinj64.so.11.0、libnvrtc-builtins.so.11.0

这个错误可以用下面两行来修复

cd /home/pherath/cuda_toolkits/cuda-11.0/targets/x86_64-linux/lib # move to the dir of the offending line
ln -s libaccinj64.so.11.0 libaccinj64.so #reorder such that symbolic link and target are in correct order (we need libaccinj64.so -> libaccinj64.so.11.0)

第 5 步。下载 CuDNN 8.2.0

cd /home/pherath/cuda_toolkits # move back to the parent of previous dir

您需要从CuDNN archives 下载 CuDNN .tgz 文件,我使用的是 v8.2.0。此步骤将要求您在 CuDNN 创建一个帐户并通过 Web 界面下载。如果您在设置 tensorflow 的机器上没有 GUI,我建议使用“链接重定向跟踪”插件来跟踪文件将从 (here is a google chrome add-on link) 下载的确切链接。您可以使用带有 GUI 的本地计算机跟踪链接,然后使用 wget 在 VM 上下载跟踪的链接。请注意,此跟踪链接的生命周期相对较短。

下载后名称仍会加密,重命名为.tgz by

mv $some_ambiguous_name cudnn-11.3-linux-x64-v8.2.0.53.tgz

现在我们在 cuda 安装目录的父级解压

tar -xvzf cudnn-11.3-linux-x64-v8.2.0.53.tgz # this will extract things under a dir called 'cuda'

现在我们需要将所有lib64和include复制到cuda工具包安装下的对应目录

cp -fv cuda/lib64/*.* cuda-11.0/lib64/.
cp -fv cuda/include/*.* cuda-11.0/include/.

第 6 步。创建/附加/前置 PATH 和 LD_LIBRARY_PATH 环境变量。

将以下行添加到 ~/.bashrc 的末尾(否则,请确保为您将运行 TF 脚本的每个 bash 扩展相应的环境变量)。

导出 CUDA11=/home/pherath/cuda_toolkits/cuda-11.0 导出 PATH=$CUDA11/bin:$PATH 导出 LD_LIBRARY_PATH=$CUDA11/lib64:$CUDA11/extras/CUPTI/lib64:$LD_LIBRARY_PATH

启动新终端或

source ~/.bashrc 

在每个现有终端中。

检查安装是否成功

您可以运行以下几行来测试 TF 2.4.1 + profiler 是否有效:

conda create -n tf python==3.7 -y  # create a python environment
conda activate tf #activate the virtual environment (here conda)
pip install tensorflow==2.4.1 # install tf 2.4.1
python -c "import tensorflow as tf, logging; logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s'); tf.config.list_physical_devices('GPU'); tf.profiler.experimental.start('.'); tf.profiler.experimental.stop()" # test to see if TF with GPU works

############################################## ###########################

如果您想在 Ubuntu 20.04 LTS 上安装 Cuda Toolkit 10.2,则单行安装代码会相应更改(需要添加 library_path,并覆盖 gcc 版本不匹配的抱怨)。

./cuda_10.2.89_440.33.01_linux.run --silent --tmpdir=. --toolkit --toolkitpath=/home/pherath/cuda_toolkits/cuda-10.2 --librarypath=/home/pherath/cuda_toolkits/cuda-10.2 --override

请记住,您还需要对 cuda 工具包 10.2 的补丁重复此过程。之后,您需要下载相应的 cuDNN 并将 lib64 & include 复制到 cuda 工具包的目录中(与上面的说明相同)。

############################################## ###########################

如果仍然出现错误,则很有可能您没有安装正确的 cuda/nvidia 驱动程序。要解决此问题,您将需要 sudo 权限!

1.

首先,清除所有 cuda/nvidia 内容(由于声誉有限,我无法添加引用..);基本上使用 sudo 权限运行以下行。 apt clean; apt update; apt purge cuda; apt purge nvidia-*; apt autoremove; apt install cuda

2.

按照谷歌https://cloud.google.com/compute/docs/gpus/install-drivers-gpu#ubuntu-driver-steps的说明进行操作

3.

重启机器。

【讨论】:

以上是关于在没有 sudo 的 Ubuntu 20.04 上使用 GPU 设置 Tensorflow 2.4的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu 20.04 上安装 Nginx 转载

如何在 Ubuntu 20.04 上安装启用 SSH

如何在Ubuntu 20.04上安装MySQL 8.0

暗影精灵8 pro安装ubuntu20.04后,没有wifi驱动

ubuntu 20.04部署elasticsearch 8.*(安装es)

当 Ubuntu 20.04 上的 sudo -iu bob /opt/builder/bin/start.sh 时,LD_LIBRARY_PATH 不是来自 .bashrc