问题描述
我给出一个结果示例。我想问一下如何获得像这张图这样的数据。
解决方法
您可以使用诸如torch.cuda.memory_stats
之类的pytorch命令来获取有关当前GPU内存使用情况的信息,然后根据这些报告创建时间图。
另一种方法是使用看起来像这样的nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.66 Driver Version: 450.66 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 GeForce GTX 1080 Off | 00000000:01:00.0 On | N/A |
| 0% 50C P8 12W / 215W | 1088MiB / 8113MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1091 G /usr/lib/xorg/Xorg 24MiB |
| 0 N/A N/A 1158 G /usr/bin/gnome-shell 48MiB |
+-----------------------------------------------------------------------------+
并使用subprocess
来获取字符串
import subprocess
import re
command = 'nvidia-smi'
while True:
p = subprocess.check_output(command)
ram_using = re.findall(r'\b\d+MiB+ /',str(p))[0][:-5]
ram_total = re.findall(r'/ \b\d+MiB',str(p))[0][3:-3]
ram_percent = int(ram_using) / int(ram_total)
或者只需用str(p).split('\n')
分隔行并计算字符串长度