GPU版本pytorch和tensorflow部署(cuda、cudnn)

概述

部署前建议简单了解显卡、显卡驱动、cuda、cudnn。
部署GPU版本pytorch和tensorflow的可用流程如下:
在这里插入图片描述
当前软硬件环境:aarch64架构麒麟V10系统,两块NVIDIA-A100系列显卡。
经过多次不同方案的尝试,最终选择适合当前需求的部署方案:
1、 宿主机安装显卡驱动;
2、 使用nvidia/cuda官方docker镜像;
3、 下载pytorch源码离线安装,下载tensorflow源码在线安装。
选择部署方案的一些建议:
1、 安装cuda和cudnn:
选择下载官方镜像,在容器中进行后续操作的优势:
a) 安装方便,docker pull即可。
b) 后续容器移植到别的机器也较为方便
选择nvidia官网下载安装包在宿主机安装的优势:
a) 安装在宿主机上,无需依赖docker容器。
2、 安装pytorch和tensorflow:
x86架构的win和linux,以及arm64架构的win建议使用pip/conda/whl包安装,方便省力,但是要注意安装gpu版本。
arm64架构的linux建议使用源码安装,因为网上找到的arm64-linux的whl包都是cpu版本!
选择源码安装pytorch和tensorflow不挑pytorch和tensorflow的版本,任何版本都可以编译安装出gpu版本库,还可以定制选择支持的功能。缺点:较为复杂,pytorch还好,主要是tensorflow。tensorflow对网络还有要求,网络不好下载三方库会失败,需要手动下载然后修改bazel配置文件。
附上aarch64-麒麟V10系统,A100系列显卡的机器,部署nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04容器+torch-gpu-1.12+tensorflow-gpu2.12过程。

1 安装显卡驱动

1.1 禁用系统默认驱动nouveau

首先输入lsmod | grep nouveau检查是否已经禁用了,如果没内容输出,则无需禁用,否则需要禁用。如果lsmod提示“command not found”,则需要安装module-init-tools,例如ubuntu使用apt-get install module-init-tools安装。
禁用系统默认驱动nouveau,网上教程很多,搜索对应系统的禁用nouveau教程操作即可,这里不多介绍。
最后再使用lsmod | grep nouveau测试是否输出为空,禁用成功。

1.2 安装显卡驱动

首先在nvidia官网驱动程序下载页面输入自己的显卡型号搜索,官网会推荐给你驱动版本,然后下载。网址:https://www.nvidia.cn/Download/index.aspx?lang=cn
在这里插入图片描述
在这里插入图片描述
linux下载完的文件如下:
在这里插入图片描述
给该文件执行权限:

chmod 744 NVIDIA-Linux-aarch64-515.65.01.run

执行安装:

sh NVIDIA-Linux-aarch64-515.65.01.run

1.3 显卡驱动测试

输入“nvidia-smi”命令,输出如下内容即为显卡驱动安装成功。
在这里插入图片描述
其中右上角的Cuda Version版本有人说是支持的cuda最大版本,也有说是支持的cuda版本。建议安装Cuda Version显示的cuda版本。

2 安装cuda和cudnn

2.1 方法1:使用nvidia/cuda的docker镜像

2.1.1 部署docker环境

部署docker方法1(不建议):
docker版本小于19.03时,需要额外使用nvidia-docker2软件包,配置方案较为繁琐,可自行搜索,例如:https://www.cnblogs.com/ytwang/p/14809112.html。建议直接升级docker,使用以下方案2。
部署docker方法2(建议):
Docker 19.03的发布,不赞成使用nvidia-docker2软件包,因为Docker运行时已将NVIDIA GPU作为设备本地支持,所以安装(或升级)docker版本>=19.03,再安装nvidia-container-toolkit。
安装(或升级)docker教程参考:
https://www.cnblogs.com/haolb123/p/16553001.html
安装nvidia-container-toolkit:
yum install -y nvidia-container-toolkit,安装即可,可以看到依赖关系如下:
在这里插入图片描述

2.1.2 拉取nvidia/cuda镜像

根据nvidia-smi输出的内容,确定自己想安装的cuda版本,在网址:https://gitlab.com/nvidia/container-images/cuda/blob/master/doc/supported-tags.md中找到对应的镜像标签,然后输入命令docker pull nvidia/cuda:tagID即可。
例如:
在这里插入图片描述
“devel” 涵盖了开发所需的所有工具,比“runtime”齐全,优先安装devel。
查到我想下载的镜像标签为“11.7.1-cudnn8-devel-ubuntu22.04”,在终端输入:

docker pull nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04

将镜像下载下来。

2.1.3 启动容器

用上一步下载的镜像启动容器,初次启动需要使用–gpus选项指定GPU设备,例如–gpus all,或者–gpus '"device=1,2"'。我将容器启动并后台运行容器,完整命令如下:

docker run --gpus all --name "test" -itd nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04 /bin/bash

启动后,docker ps可以看到运行中的容器,刚刚启动的容器被命名为了“test”。
后续使用docker exec命令进容器。例如:

docker exec -it test /bin/bash

进入容器后输入nvidia-smi可以看到有显卡信息输出,说明容器中可以使用显卡和显卡驱动。
输入nvcc -V可以看到有cuda版本信息输出,说明已经安装了cuda,cuda安装目录在/usr/local/cuda下。
cudnn的头文件和库文件分别在/usr/include和/usr/lib/aarch64-linux-gnu/目录下,可以看到也是安装好的。
至此我们已经有了一个配好cuda和cudnn的镜像、容器。
按ctrl+D返回宿主机终端。

2.2 方法2:nvidia官网下载cuda和cudnn安装

由于本文最终选择了使用nvidia/cuda的docker镜像,所以宿主机安装只简单介绍一些注意事项,网上详细安装教程很多。
在nvidia官网https://developer.nvidia.com/cuda-downloads下载对应cuda版本,下载“.run”文件。首页是最新的cuda,如果想下载历史cuda点击这里:
在这里插入图片描述
安装cuda需要有gcc环境。
安装cdua时可以选择是否安装显卡驱动,如果已经安装了则无需安装显卡驱动。
cudnn下载地址https://developer.nvidia.cn/zh-cn/cudnn,下载cudnn需要有nvidia账户,没有注册一个即可。
cudnn优先下载tar或者zip包,解压后对应文件放到cuda安装目录的bin、include、lib64下即可。
较新的cudnn-arm64-linux版本可能没有压缩包,需要下载deb或rpm安装包安装。
deb版本cudnn安装顺序如下:
在这里插入图片描述
安装完后测试cudnn方法:
进入/usr/src/cudnn_samples_v8目录,使用如下方法测试:
https://zhuanlan.zhihu.com/p/126997172

make clean
make

这一步可能会提示fatal error: FreeImage.h: No such file or directory,安装apt-get install libfreeimage3 libfreeimage-dev重新make即可。
运行./mnistCUDNN,出现 Test passed!代表 cudnn 正常使用。

3 安装pytorch和tensorflow

3.1 方法1:pip/conda/whl包安装

x86的机器以及arm64的win机器推荐使用该方式安装,需注意使用gpu版本。
安装教程网上也比较多,自行搜索即可。

3.2 方法2:源码安装

arm64的linux机器,网上的whl包基本都是cpu版本的,所以在此使用了源码安装。

3.2.1 源码安装pytorch
下载pytorch源码及三方库源码

首先下载源码,并且下载所需所有三方库!
源码网址:https://github.com/pytorch/pytorch/
git下载命令:

git clone pytorch
git submodules update --init –recursive

如果不使用git命令而在网页下载,较为繁琐,操作步骤如下(本文就是这么下载的):
网页选择对应tag的pytorch源码,右上角点击下载zip包,解压,此时下载的源码没有三方库。
然后网页点击进入“third-party”,进去一个一个下载,解压到刚才下载的源码包的third-party对应目录下,部分third-party还会再有一层自己的third-party,要把里面的三方库都下全。

安装pytorch

安装时进入pytorch目录输入python setup.py install即可。

测试pytorch

pytorch安装完后的测试命令如下:
在这里插入图片描述

3.2.1源码装tensorflow
部署bazel

tensorflow需要使用bazel编译,所以首先部署bazel。bazel部署方式有多种,这里选择了最简单的方式:直接下载别人编译好的Bazel,其他部署bazel方式可以自行搜索部署。
国内镜像源下载地址https://mirrors.huaweicloud.com/bazel/
选择下面三个文件下载:
在这里插入图片描述
下载完成后,创建软连接:ln -s /bazel的目录/bazel-5.3.2-linux-arm64 /usr/bin/bazel
此时bazel即可使用,输入bazel –help测试。

下载tensorflow源码

接下来下载tensorflow源码,下载地址:
https://github.com/tensorflow/tensorflow ,或者
https://gitee.com/patterson/tensorflow

编译安装tensorflow

tensorflow安装官网指南如下:
https://tensorflow.google.cn/install
配置 build:
执行tensorflow源码目录下的./configure
执行过程中会有一些选项供选择,其中cuda是否使用选择了“Y”,其他内容按照自己需求选择,本文其他内容选择了“N”。
编译构建支持GPU的tensorflow软件包,输入如下命令:

bazel build --config=cuda [--config=option] //tensorflow/tools/pip_package:build_pip_package

编译过程需要联网并能访问git上的仓库,否则下载三方库失败。下载失败建议再次尝试。如果网络不通在后文介绍两个解决思路。
此处编译时间较长,产生的缓存文件在/~/.cache/bazel下,其中包括下载的三方库。从源代码构建 TensorFlow 会消耗大量 RAM。如果您的系统受内存限制,请将 Bazel 的 RAM 使用量限制为:–local_ram_resources=2048

构建whl包并安装

bazel build 命令会创建一个名为 build_pip_package 的可执行文件,此文件是用于构建 pip 软件包的程序。如下所示运行该可执行文件,在 /tmp/tensorflow_pkg 目录中构建 .whl 软件包:
如需从某个版本分支构建,请使用如下命令:

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

如需从 master 分支构建,请使用 --nightly_flag 获取合适的依赖项:

./bazel-bin/tensorflow/tools/pip_package/build_pip_package --nightly_flag /tmp/tensorflow_pkg

本文是从master分支构建的。
生成的whl包在/tmp/tensorflow_pkg下,如下:
在这里插入图片描述
安装该whl包:

pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl

此时,TensorFlow 已安装完毕。

测试tensorflow

测试TensorFlow命令如下:
在这里插入图片描述

tensorflow三方库下载总是失败的两种解决思路

思路1:
复制网址到浏览器手动下载三方库压缩包,然后修改tensorflow中的.bazel文件,改为本地路径,这样做较为耗时耗力。具体操作参考:https://zhuanlan.zhihu.com/p/134019025
在这里插入图片描述
思路2:
通过一些软件配置网络,将国外git、google等仓库替换(或者说映射?)为国内镜像源,下载时候将转到国内镜像源下载,这样不一定能下载全。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自编译tensorflow: 1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3; 3.无mkl支持; 软硬件硬件环境:Ubuntu16.04,GeForce GTX 1080 TI 配置信息: hp@dla:~/work/ts_compile/tensorflow$ ./configure WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown". You have bazel 0.19.1 installed. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 Found possible Python library paths: /usr/local/lib/python3.5/dist-packages /usr/lib/python3/dist-packages Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] Do you wish to build TensorFlow with XLA JIT support? [Y/n]: XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: No OpenCL SYCL support will be enabled for TensorFlow. Do you wish to build TensorFlow with ROCm support? [y/N]: No ROCm support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: y CUDA support will be enabled for TensorFlow. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10.0]: Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-10.0 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-10.0]: Do you wish to build TensorFlow with TensorRT support? [y/N]: y TensorRT support will be enabled for TensorFlow. Please specify the location where TensorRT is installed. [Default is /usr/lib/x86_64-linux-gnu]://home/hp/bin/TensorRT-5.0.2.6-cuda10.0-cudnn7.3/targets/x86_64-linux-gnu Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1,6.1,6.1]: Do you want to use clang as CUDA compiler? [y/N]: nvcc will be used as CUDA compiler. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: Not configuring the WORKSPACE for Android builds. Preconfigured Bazel build configs. You can use any of the below by adding "--config=" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. --config=gdr # Build with GDR support. --config=verbs # Build with libverbs support. --config=ngraph # Build with Intel nGraph support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=noignite # Disable Apacha Ignite support. --config=nokafka # Disable Apache Kafka support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished 编译: bazel build --config=opt --verbose_failures //tensorflow/tools/pip_package:build_pip_package 卸载已有tensorflow: hp@dla:~/temp$ sudo pip3 uninstall tensorflow 安装自己编译的成果: hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl
自编译tensorflow: 1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3; 3.支持mkl,无MPI; 软硬件硬件环境:Ubuntu16.04,GeForce GTX 1080 配置信息: hp@dla:~/work/ts_compile/tensorflow$ ./configure WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown". You have bazel 0.19.1 installed. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 Found possible Python library paths: /usr/local/lib/python3.5/dist-packages /usr/lib/python3/dist-packages Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] Do you wish to build TensorFlow with XLA JIT support? [Y/n]: XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: No OpenCL SYCL support will be enabled for TensorFlow. Do you wish to build TensorFlow with ROCm support? [y/N]: No ROCm support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: y CUDA support will be enabled for TensorFlow. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10.0]: Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-10.0 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-10.0]: Do you wish to build TensorFlow with TensorRT support? [y/N]: y TensorRT support will be enabled for TensorFlow. Please specify the location where TensorRT is installed. [Default is /usr/lib/x86_64-linux-gnu]:/home/hp/bin/TensorRT-5.0.2.6-cuda10.0-cudnn7.3/targets/x86_64-linux-gnu Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1,6.1,6.1]: Do you want to use clang as CUDA compiler? [y/N]: nvcc will be used as CUDA compiler. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: Not configuring the WORKSPACE for Android builds. Preconfigured Bazel build configs. You can use any of the below by adding "--config=" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. --config=gdr # Build with GDR support. --config=verbs # Build with libverbs support. --config=ngraph # Build with Intel nGraph support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=noignite # Disable Apacha Ignite support. --config=nokafka # Disable Apache Kafka support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished 编译: hp@dla:~/work/ts_compile/tensorflow$ bazel build --config=opt --config=mkl --verbose_failures //tensorflow/tools/pip_package:build_pip_package 卸载已有tensorflow: hp@dla:~/temp$ sudo pip3 uninstall tensorflow 安装自己编译的成果: hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值