Tensorflow的CUDA升级,以及相关配置

本文详细介绍如何在Ubuntu系统中升级和配置TensorFlow的CUDA环境,包括禁用GPU使用、CudaSDK与cuDNN的安装及配置步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍Tensorflow在unbuntu下的cuda升级以及一些相关配置–GPU,cuDNN,Cuda SDK。目的是为了解决安装完成tensorflow之后在使用过程中想调整上述相关设置,比如禁用GPU、升级cuda等。

本文内容要求tensorflow已经安装完成,主要涉及GPU版本相关设置

解决的问题:
1、安装了GPU版本的tensorflow,想禁用tensorflow对GPU的使用
2、升级Cuda SDK与cuDNN,以及设置相关配置

1、安装Cuda SDK

官网下载和安装Cuda Toolkit,选择合适的系统版本下载,本文为介绍基于ubuntu系统的版本,个人建议下载runfile(local)的Installer Type。关于显卡驱动的安装可以参见链接-安装最新Nvidia显卡驱动

如果下载文件runfile(local),大小约为1G+,文件名称格式为‘cuda_x.x.xx_linux.run’(其中x为数字,表示版本),按照下载页面安装指导安装即可。

#shell 
sudo Run `sudo sh cuda_x.x.xx_linux.run`

安装过程中会设置安装路径,如果是7.5版本,那么默认安装在路径‘/usr/local/cuda-7.5’,并在‘/usr/local/cuda’做一份映射,此项为可选项目,如果不想覆盖前版,可以不做映射。

安装完成需要添加路径,特别是在安装不同版本的cuda共存情况下。需要在profile或.bashrc中添加一下内容:

#添加cuda路径
PATH=$PATH:/usr/local/cuda-7.5 (CUDA安装路径)
#添加lib路径
LD_LIBRARY_PATH=$LA_LIBRARY_PATH:/usr/local/cuda-7.5/lib64
#-----------#
#如果做了映射,也可以使用以下内容
PATH=$PATH:/usr/local/cuda
#添加lib路径
LD_LIBRARY_PATH=$LA_LIBRARY_PATH:/usr/local/cuda/lib64

2、安装cuDNN

官网下载和安装cuDNN,择合适的系统版本下载,需要注意的是,版本的选择与已经安装的Cuda Toolkit版本有关,否则会报错。

#版本不对可能引发的错误提示
E tensorflow/stream_executor/cuda/cuda_dnn.cc:286] Loaded cudnn library: 5005 but source was compiled against 4007. If using a binary install, upgrade your cudnn library to match. If building from sources, make sure the library loaded matches the version you specified during compile configuration.

下载的文件名为,‘cudnn-#.#-linux-x64-v*.tgz’,大约80M左右,其中#.#为版本号–如‘8.0’,v*为版本–如‘v5’。

tar xvzf cudnn-7.0-linux-x64-v4.tgz
#注意cuda路径,与之前安装路径一致
sudo cp cudnn-7.0-linux-x64-v4/cudnn.h /usr/local/cuda/include
sudo cp cudnn-7.0-linux-x64-v4/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

cuDNN安装完成

3、配置tensorflow

如果tensorflow是使用whl文件安装,需要下载源码进行配置,官网推荐下载地址Tensorflow
或者使用命令下载

#shell
git clone https://github.com/tensorflow/tensorflow

进入下载的tensorflow目录,以root权限运行文件‘configure’

$ sudo ./configure
#开始设置或更改配置
#设置与tensorflow关联的python路径
Please specify the location of python. [Default is /usr/bin/python]:

#设置是否开启GPU支持
Do you wish to build TensorFlow with GPU support? [y/N] y
GPU support will be enabled for TensorFlow

#设置所安装的Cuda SDK版本
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave
empty to use system default]: 7.5

#设置关联的cuda sdk安装路径
Please specify the location where CUDA 7.5 toolkit is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda

#设置cuDNN的版本
Please specify the Cudnn version you want to use. [Leave empty to use system
default]: 7.0

#设置cudnn的库文件所存放的路径,即与之前设置的相同,
#确认后将会根据路径与版本号自动扫描搜索是否有相关库文件
Please specify the location where the cuDNN 7.0 library is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda-7.5

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.
#选择gpu的计算能力,可以去官网查看相应型号GPU的计算能力<https://developer.nvidia.com/cuda-gpus>
Please note that each additional compute capability significantly increases your
build time and binary size. [Default is: \"3.5,5.2\"]: 3.5

Setting up Cuda include
Setting up Cuda lib64
Setting up Cuda bin
Setting up Cuda nvvm
Configuration finished

对tensorflow的cuda升级,配置完成



相关链接:

[1] 安装Tensorflow(Linux ubuntu) http://blog.csdn.net/lenbow/article/details/51203526
[2] ubuntu下CUDA编译的GCC降级安装 http://blog.csdn.net/lenbow/article/details/51596706
[3] ubuntu手动安装最新Nvidia显卡驱动 http://blog.csdn.net/lenbow/article/details/51683783

自编译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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值