cuda和tensorflow-gpu对应版本的安装及测试

版本

CUDA、cuDNN 和 tensorflow-gpu 版本的推荐配置:configure page
在这里插入图片描述
在这里插入图片描述
查看自己的 CUDA 和 cudnn 的版本:

1)直接用 nvcc --version 查看:
在这里插入图片描述
2)CUDA 一般安装在 /usr/local/cuda/ 路径下,该路径下有一个version.txt文档,里面记录了 CUDA 的版本信息,执行语句:

cat /usr/local/cuda/version.txt 

3)cuDNN 的信息在其头文件里,执行语句:

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

在这里插入图片描述
这里可以到看到 CUDA 版本是 9.0,cuDNN 版本是 7.0.5,官方推荐的 tensorflow 对应版本是:1.5 / 1.6 / 1.7 / 1.8 / 1.9 / 1.10 / 1.11 / 1.12

如果报错版本不对的话,可以按照下面的解决方法:

pip uninstall tensorflow-gup==1.0.1
pip install tensorflow-gpu==1.5.0

测试

安装好以后,可以进行如下测试,如果没有报错就没问题:

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

查看日志信息若包含gpu信息,就是使用了gpu。

import tensorflow as tf
tf.test.is_gpu_available()

>>>
True

其他方法:跑计算量大的代码,通过 nvidia-smi 命令查看gpu的内存使用量。

多版本CUDA的切换

如果安装了多个版本的 CUDA,可以在 /usr/local/ 目录下看到自己安装了哪些版本:
在这里插入图片描述
cuda 是一个软链接,它指向我们指定的 cuda 版本(在设置环境变量时,也使用的是 cuda,而不是 cuda-具体版本,这主要是为了方便我们切换 cuda 版本,可以让我们不用每次都去该环境变量的值)

进入 local 文件夹下,使用 stat 命令查看当前 cuda 软链接指向的哪个 cuda 版本:
在这里插入图片描述
可以看到,cuda 软链接的文件类型是symbolic link,指向的目录是 /usr/local/cuda-9.0,当我们想使用 cuda-8.0 版本时,只需要删除该软链接,然后重新建立指向 cuda-8.0 版本的软链接即可(注意名称还是cuda,因为要与 bashrc 文件里设置的保持一致)

sudo rm -rf cuda
sudo ln -s /usr/local/cuda-8.0 /usr/local/cuda

需要修改的只是上面的版本号而已。

CUDA驱动程序版本跟CUDA的运行时版本不匹配

如果在测试时报错:

InternalError: cudaGetDevice() failed. Status: CUDA driver version is insufficient for CUDA runtime version

说明 CUDA 驱动程序版本跟 CUDA 的运行时版本不匹配。

(1)查看 CUDA driver version(驱动版本):就是NVIDIA GPU的驱动程序版本

查看命令:nvidia-smi

看以看到 Driver Version:384.111
在这里插入图片描述
(2)查看 CUDA runtime version(运行时版本):在 python 中安装的 cudatoolkit 和cudnn 程序包的版本

查看命令:pip list / conda list

在这里插入图片描述
可以看到 python 安装的 cudatoolkit 和 cudnn 程序包版本是:9.2

(3)nvidia 驱动和 cuda runtime 版本对应关系

来源:https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html

运行时版本驱动版本
CUDA 9.1387.xx
CUDA 9.0384.xx
CUDA 8.0375.xx (GA2)
CUDA 8.0367.4x
CUDA 7.5352.xx
CUDA 7.0346.xx
CUDA 6.5340.xx
CUDA 6.0331.xx
CUDA 5.5319.xx
CUDA 5.0304.xx
CUDA 4.2295.41
CUDA 4.1285.05.33
CUDA 4.0270.41.19
CUDA 3.2260.19.26
CUDA 3.1256.40
CUDA 3.0195.36.15

(4)解决

从驱动和运行时的版本对应关系来看,版本为 384.111 的驱动程序对应的运行时版本是9.0,也就是说目前在 python 中安装的 cudatoolkit 和 cudnn 程序包版本 9.2 是过高了。

先卸载 python 中安装的 cudatoolkit 和 cudnn 程序包:

pip/conda uninstall cudnn
pip/conda uninstall cudatoolkit

然后安装对应版本的 cudatoolkit 和 cudnn 程序包:

pip install cudatoolkit=9.0
pip install cudnn

(5)出现问题的原因
一般出现这种情况是因为在 python 中安装 tensorflow 的 gpu 版本时,pip 会检查 tensorflow 依赖的其他的包,如果依赖的包没有安装,则会先安装最新版本的依赖包,最终导致 gpu 驱动版本和 cuda 运行时版本不匹配。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃饭就会放大招

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值