如何在 tensorflow 2.0b 中检查/释放 GPU 内存?

Bar*_*den 6 gpu python-3.x tensorflow2.0

在我的 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 中不再可用

小智 5

您可能有兴趣将此Python 3 绑定用于 NVIDIA 管理库

我会尝试这样的事情:

import nvidia_smi

nvidia_smi.nvmlInit()

handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
# card id 0 hardcoded here, there is also a call to get all available card ids, so we could iterate

info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)

print("Total memory:", info.total)
print("Free memory:", info.free)
print("Used memory:", info.used)

nvidia_smi.nvmlShutdown()
Run Code Online (Sandbox Code Playgroud)