簡體   English   中英

在 TensorFlow 中打印 GPU 和 CPU 使用率

[英]Print GPU and CPU usage in TensorFlow

我在 Google Colab 上運行了一些TensorFlow示例(所以我可以有一個 GPU), 就像這個。

有沒有辦法在代碼中為每個訓練步驟打印 CPU 和 GPU 的使用情況,以便查看 GPU 的使用方式以及 CPU-only 和 Z52F9EC21735243AD9917CDA3CA077D23 之間的性能差異?

標准環境中,也許我可以使用nvidia-smi來跟蹤 GPU 的使用情況,但是對於筆記本電腦,我一次只能運行一個單元。

謝謝

我從互聯網上抓取了一個片段代碼。 您可以隨時運行 printm function。

# memory footprint support libraries/code
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize
import psutil
import humanize
import os
import GPUtil as GPU
GPUs = GPU.getGPUs()
# XXX: only one GPU on Colab and isn’t guaranteed
gpu = GPUs[0]
def printm():
 process = psutil.Process(os.getpid())
 print("Gen RAM Free: " + humanize.naturalsize( psutil.virtual_memory().available ), " | Proc size: " + humanize.naturalsize( process.memory_info().rss))
 print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))
printm()

這是來自我的 Google Colab 的 output:

Gen RAM Free: 12.8 GB  | Proc size: 155.7 MB
GPU RAM Free: 11441MB | Used: 0MB | Util   0% | Total 11441MB

您需要啟動一個為您打印此內容的線程。 當此線程運行時,您將僅在其他單元運行時看到 output。

編碼:

!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize
import psutil
import humanize
import os, time
import GPUtil as GPU

GPUs = GPU.getGPUs()
# XXX: only one GPU on Colab and isn’t guaranteed
gpu = GPUs[0]
def worker():
  while True:
    process = psutil.Process(os.getpid())
    print("Gen RAM Free: " + humanize.naturalsize( psutil.virtual_memory().available `enter code here`), " I Proc size: " + humanize.naturalsize( process.memory_info().rss))
    print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))
    time.sleep(6)

import threading
t = threading.Thread(target=worker, name='Monitor')
t.start()

考試: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM