简体   繁体   English

如何衡量 TensorFlow 模型的 GPU 内存使用情况

[英]How to measure GPU memory usage of TensorFlow model

My goal is to figure out how much GPU memory a TensorFlow model saved as a .pb file uses during inference.我的目标是弄清楚在推理过程中保存为 .pb 文件的 TensorFlow 模型使用了多少 GPU 内存。 By limiting the per_process_gpu_memory_fraction to a value of 0.01 and successfully running the model on an input image, I would expect a memory usage of 120MB (based on a 12,000MB GPU).通过将per_process_gpu_memory_fraction限制为 0.01 的值并在输入图像上成功运行模型,我预计内存使用量为 120MB(基于 12,000MB GPU)。

conf = tf.ConfigProto()
conf.gpu_options.per_process_gpu_memory_fraction=0.01
session = tf.Session(config=conf)

When I observe the actual GPU memory usage via nvidia-smi , I see a utilization of 550MB.当我通过nvidia-smi观察实际的 GPU 内存使用情况时,我看到了 550MB 的利用率。 Based on the answer here ( https://stackoverflow.com/a/55532954 ), I measure the default memory needed to create a TF session, which amounts to 150MB.根据此处的答案 ( https://stackoverflow.com/a/55532954 ),我测量了创建 TF 会话所需的默认内存,总计 150MB。 Now the question is where the remaining 550MB (measured) - 150MB (session) - 120MB (model) = 280MB are from.现在的问题是剩余的 550MB(测量值)- 150MB(会话)- 120MB(模型)= 280MB 来自哪里。 I want to exclude any other factors that use the GPU memory and only quantify how much the model itself uses.我想排除使用 GPU 内存的任何其他因素,只量化模型本身使用了多少。 Can I simply assume 120MB in this case?在这种情况下,我可以简单地假设 120MB 吗?

Try setting TF_FORCE_GPU_ALLOW_GROWTH to true before you run you python code:在运行 python 代码之前,尝试将 TF_FORCE_GPU_ALLOW_GROWTH 设置为 true:

os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'

Put this before you import tensorflow.在导入 tensorflow 之前放置它。

Then measure the GPU Memory utilization.然后测量 GPU 内存利用率。 Also it also depends on the Tensorflow Memory allocator on how the memory on GPU is allocated.此外,它还取决于如何分配 GPU 上的内存的 Tensorflow 内存分配器。 So it is generally going to be a bit larger than the actual model in memory.所以它通常会比内存中的实际模型大一点。

Also try suggestions mentioned here: How to restrict tensorflow GPU memory usage?还可以尝试这里提到的建议: 如何限制 tensorflow GPU 内存使用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM