文章目录
1. 问题
今天运行代码时发现一个怪现象,在使用TensorFlow时,通过os.environ["CUDA_VISIBLE_DEVICES"] = "0"
指定使用GPU 0,但是使用nvidia-smi
命令查看GPU的使用情况时,发现还是没有使用GPU,而是使用的CPU进行计算,导致计算速度非常慢,跑了一个晚上,才跑出来一点点数据,见下:
使用下面的代码检测TensorFlow能使用的设备的情况:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
发现可用设备只有两块CPU:
2. Solution
查找资料,发现可能是TensorFlow CPU版本(1.14)比GPU版本(1.13.1)高的原因,导致两个版本有代差,而tensorflow默认选择版本高的CPU版本来计算了。
升级GPU版本到1.14版本,注意:这里一定要指定升级的版本,因为cuda版本和TensorFlow版本有一定对应关系,如果升级到最新GPU版本,可能会导致不能使用。
使用如下命令升级GPU版本到1.14:
pip3 install --index-url http://pypi.douban.com/simple --trusted-host pypi.douban.com --upgrade tensorflow-gpu==1.14.0
升级后查看TensorFlow版本:
pip3 list| grep tensorflow
结果为:
再次使用下面的命令进行检测:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
结果如下所示,GPU已经赫然在列。
3. other
-
解决了上述问题,在运行的过程中出现了下面的
Could not create cudnn handle
错误:I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcublas.so.10.0 I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudnn.so.7 E tensorflow/stream_executor/cuda/cuda_dnn.cc:338] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR E tensorflow/stream_executor/cuda/cuda_dnn.cc:338] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
在我的环境上,通过进行如下的修改,即可修复该问题。
from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config)
用GPU跑程序就是快:
-
Linux 文件按时间排序
ls -alt
按照修改时间排序,等价于ls --sort=time -la
ls -alc
按照创建时间排序ls -alu
按照访问时间排序- 以上命令均可使用
-r
实现逆序排序
-
另一种 测试tf是否能使用GPU的代码
import tensorflow as tf tf.test.is_gpu_available() tf.test.gpu_device_name() #输出gpu的名称
4. 更新
在pyenv、conda环境下,TensorFlow CPU、GPU版本一样,同时和cuda的版本也匹配,但运行代码还是不能使用GPU。
在TensorFlow issue - 423893319中有类似的问题,不清楚是不是该原因导致的:
We (tensorflow team) do not maintain the packages available through anaconda.
Please reach out to anaconda with issues with those packages.
看issue问题提出者的回答应该是解决该问题了:不过还是通过卸载老的版本这个操作。
自己还未解决情况下GPU不能使用的问题。待续…
5. 参考