前言:我們一般使用深度學習框架都是GPU版本的,如何檢測我們的安裝是否成功,GPU、CUDA、CUDNN等信息呢?本文做了一個歸納總結:
一、tensorflow版本的GPU信息查看
(1)tensorflow版本
tf.__version__ tensorflow版本
tf.__xxxxx__ 一些信息的查看
tf.verison.xxxx 一些信息
tf.test.is_built_with_cuda()
tf.test.gpu_device_name() 默認獲得第一塊GPU設備,打印出 "/device:gpu:0"
tf.test.is_built_with_gpu_support()
tf.test.is_built_with_rocm(): Returns whether TensorFlow was built with ROCm (GPU) support.
tf.test.is_gpu_available(): 快要被取代了,被後面的方法所替代tf.config.list_physical_devices('GPU')
注意:tensorflow2.x版本以及較新的幾個tensorflow1.x版本,關於一些基本信息的查看和配置都主要在以下兩個包中
tf.test.xxxx
tf.comfig.xxxx
只不過有一些名稱可能不太一樣。
(2)pytorch關於GPU的信息查看
torch.__version__
torch.version.cuda #9.0
torch.cuda.is_available()
torch.cuda.get_device_name(0)
torch.cuda.get_device_properties(0)
torch.cuda.device(0)
torch.cuda.device_count()
torch.cuda.current_device() # 當前正在使用的設備
torch.backend.cudnn.version() # 7005,查看cudnn的版本
注意torch的GPU信息查看主要是在 torch.cuda.xxxx 這個包裏面。
注意:torch裏面有一個驗證tensor是否被cudnn支持的方法。
import torch
from torch.backends import cudnn
x = torch.Tensor([1.0])
xx = x.cuda()
print(xx)
# CUDNN TEST 驗證cudnn
print(cudnn.is_acceptable(xx)) # xx是一個torch的tensor
(3)清理GPU顯存的幾種方法
方法一:torch版本
torch.cuda.empty()
# 方法二
ps aux | grep python
kill -9 [pid]
# 方法三
nvidia-smi --gpu-reset -i [gpu-id] # id 是0,1,2,3,等等,這需要管理員權限