常用AI框架GPU测试方法

现在工作中经常接触pytorch,tensorflow等AI框架,记录下安装注意事项GPU测试方法

1.tensorflow

可以在https://tensorflow.google.cn/install页面查看安装说明,注意GPU的机器需要先安装CUDA,每个版本的CUDA都有对应的tensorflow的版本

pip install tensorflow==2.6.0

不指定即安装最新版本,在2.0以后版本无需区分CPU版本和GPU版本

如果是以前的版本需要这样安装GPU版本

pip install rensorflow-gpu==1.15.0

安装完以后可以通过这个代码来测试GPU是否可用

#!/usr/bin/python3
import tensorflow as tf
print("Tensorflow Version:{}".format(tf.__version__))
print("Support GPU:{}".format(tf.test.is_gpu_available()))
''''高级版本不支持list_physical_devices 方法'''
try:
    cpus = tf.config.list_physical_devices(device_type="GPU")
    gpus = tf.config.list_physical_devices(device_type="CPU")
    print(cpus)
    print(gpus)
except Exception as e:
    print("Current version not support list_physical_devices function !")

以下是我安装的1.15版本打印的结果

Tensorflow Version: 1.15.3
2022-04-26 07:16:05.127623: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.
2022-04-26 07:16:05.137984: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3099995000 Hz
2022-04-26 07:16:05.139056: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5ded840 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2022-04-26 07:16:05.139080: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2022-04-26 07:16:05.141319: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2022-04-26 07:16:05.744664: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-26 07:16:05.744977: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x8077030 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2022-04-26 07:16:05.745012: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Quadro P2200, Compute Capability 6.1
2022-04-26 07:16:05.761296: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-26 07:16:05.761357: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1639] Found device 0 with properties:
name: Quadro P2200 major: 6 minor: 1 memoryClockRate(GHz): 1.493
pciBusID: 0000:00:0c.0
2022-04-26 07:16:05.766236: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2022-04-26 07:16:05.767164: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2022-04-26 07:16:05.768054: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2022-04-26 07:16:05.768964: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2022-04-26 07:16:05.769960: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2022-04-26 07:16:05.770872: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2022-04-26 07:16:05.771887: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2022-04-26 07:16:05.772031: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-26 07:16:05.772102: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-26 07:16:05.772134: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1767] Adding visible gpu devices: 0
2022-04-26 07:16:05.772986: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2022-04-26 07:16:05.773062: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1180] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-04-26 07:16:05.773077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1186]      0
2022-04-26 07:16:05.773086: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1199] 0:   N
2022-04-26 07:16:05.773168: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-26 07:16:05.773243: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-26 07:16:05.773319: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1325] Created TensorFlow device (/device:GPU:0 with 4627 MB memory) -> physical GPU (device: 0, name: Quadro P2200, pci bus id: 0000:00:0c.0, compute capability: 6.1)
Support GPU: True
Current version not support list_physical_devices function !

tensorflow中获取cuda版本的代码

import tensorflow as tf
sys_details = tf.sysconfig.get_build_info()
print(sys_details["cuda_version"])
#返回
10.1

2.pytoch

这个可以在首页https://pytorch.org/,直接选择对应的版本,它都会给出安装命令,直接复制到本地进行安装就行,注意GPU的机器需要先安装CUDA,每个版本的CUDA都有对应的PyTorch的版本

微信截图_20220427151000.png

安装好以后我们可以跑下代码看支不支持GPU,我这里只有CPU机器可以测试

#!/usr/bin/python3
#coding:utf-8
import torch

print("PyTorch Version: {}".format(torch.__version__))
print("Support GPU:{}".format(torch.cuda.is_available()))
ngpu= 1
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
print("Current used {}".format(device))

打印返回

PyTorch Version: 1.8.0+cpu
Support GPU:False
Current used cpu

torch获取cuda版本代码

import torch
print(torch.version.cuda)


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/836.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。