Skip to content

Instantly share code, notes, and snippets.

@mcvarer
Created November 12, 2021 11:35
Show Gist options
  • Save mcvarer/ded4cd26a54bbf144be80e490f99caa8 to your computer and use it in GitHub Desktop.
Save mcvarer/ded4cd26a54bbf144be80e490f99caa8 to your computer and use it in GitHub Desktop.
Pytorch GPU memory watch
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