Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vardaan123/53a49a789b27bf829bb3799c60e26705 to your computer and use it in GitHub Desktop.
Save vardaan123/53a49a789b27bf829bb3799c60e26705 to your computer and use it in GitHub Desktop.
GPU memory consumption
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))
print(gpu_memory_map)
return gpu_memory_map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment