对于矢量量化 (k-means) 程序,我想知道当前 GPU 上的可用内存量(如果有的话)。这需要选择最佳批量大小,以便在整个数据集上运行尽可能少的批次。
我编写了以下测试程序:
import tensorflow as tf
import numpy as np
from kmeanstf import KMeansTF
print("GPU Available: ", tf.test.is_gpu_available())
nn=1000
dd=250000
print("{:,d} bytes".format(nn*dd*4))
dic = {}
for x in "ABCD":
dic[x]=tf.random.normal((nn,dd))
print(x,dic[x][:1,:2])
print("done...")
Run Code Online (Sandbox Code Playgroud)
这是我的系统上的典型输出(ubuntu 18.04 LTS,GTX-1060 6GB)。请注意核心转储。
python misc/maxmem.py
GPU Available: True
1,000,000,000 bytes
A tf.Tensor([[-0.23787294 -2.0841186 ]], shape=(1, 2), dtype=float32)
B tf.Tensor([[ 0.23762687 -1.1229591 ]], shape=(1, 2), dtype=float32)
C tf.Tensor([[-1.2672468 0.92139906]], shape=(1, 2), dtype=float32)
2020-01-02 17:35:05.988473: W tensorflow/core/common_runtime/bfc_allocator.cc:419] Allocator (GPU_0_bfc) ran out of memory trying to …Run Code Online (Sandbox Code Playgroud) 最近我使用Tensorflow和PyTorch实现了VGG-16网络,数据集是CIFAR-10.每张图片为32*32 RGB.
我在开始时使用64批量大小,而我发现PyTorch使用的GPU内存比tensorflow少得多.然后我做了一些实验,得到了一个数字,发布在下面.

经过一番研究,我知道使用BFC算法管理内存的张量流.因此,它可以解释为什么tensorflow的内存使用减少或增加2048,1024,... MB,有时内存使用不会增加批量大小.
但我仍然感到困惑,为什么当批量大小为512时,内存使用率低于批量大小为384,448等,批量大小较小.与批量大小为1024到1408,批量大小为2048到2688时相同.
这是我的源代码:
PyTorch:https://github.com/liupeng3425/tesorflow-vgg/blob/master/vgg-16-pytorch.py
Tensorflow:https://github.com/liupeng3425/tesorflow-vgg/blob/master/vgg-16.py
编辑:我的计算机上有两个Titan XP,操作系统:Linux Mint 18.2 64位.
我用命令确定GPU内存使用情况nvidia-smi.
我的代码在GPU1上运行,GPU1在我的代码中定义:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
Run Code Online (Sandbox Code Playgroud)
我确信只有一个使用GPU1的应用程序.
GPU内存使用情况可以通过下面的应用程序列表确定.例如,就像下面发布的屏幕截图一样,进程名称是/usr/bin/python3,其GPU内存使用量为1563 MiB.

在我的 tensorflow2.0b 程序中,我确实收到了这样的错误
ResourceExhaustedError: OOM when allocating tensor with shape[727272703] and type int8 on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc [Op:TopKV2]
Run Code Online (Sandbox Code Playgroud)
在此程序中的许多基于 GPU 的操作已成功执行后,会出现该错误。
我喜欢释放与这些过去的操作相关的所有 GPU 内存,以避免上述错误。我怎样才能在 tensorflow-2.0b 中做到这一点?如何从我的程序中检查内存使用情况?
我只能使用 tf.session() 找到相关信息,这在 tensorflow2.0 中不再可用