Created
November 12, 2021 11:35
-
-
Save mcvarer/ded4cd26a54bbf144be80e490f99caa8 to your computer and use it in GitHub Desktop.
Pytorch GPU memory watch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def get_gpu_memory_map(): | |
"""Get the current gpu usage. | |
Returns | |
------- | |
usage: dict | |
Keys are device ids as integers. | |
Values are memory usage as integers in MB. | |
""" | |
result = subprocess.check_output( | |
[ | |
'nvidia-smi', '--query-gpu=memory.used', | |
'--format=csv,nounits,noheader' | |
], encoding='utf-8') | |
# Convert lines into a dictionary | |
gpu_memory = [int(x) for x in result.strip().split('\n')] | |
gpu_memory_map = dict(zip(range(len(gpu_memory)), gpu_memory)) | |
return gpu_memory_map |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment