Skip to content

Instantly share code, notes, and snippets.

@malfet
Created November 24, 2021 01:46
Show Gist options
  • Save malfet/38cf2ab16f8d708aa1e312573295f5c6 to your computer and use it in GitHub Desktop.
Save malfet/38cf2ab16f8d708aa1e312573295f5c6 to your computer and use it in GitHub Desktop.
Query PyTorch memory utilisation
#!/usr/bin/env python3
import os
from subprocess import check_output
def get_pmem():
pmem = check_output(["sh", "-c", f"pmap -d {os.getpid()}|tail -n 1"], encoding="latin1").split()[3]
return pmem
def get_gpumem():
pid = os.getpid()
csv=check_output(["nvidia-smi", "--query-compute-apps=pid,used_memory", "--format=csv"], encoding="latin1").split("\n")
for pid, val in [x.split(",") for x in csv]:
if pid == str(os.getpid()):
return val
return -1
if __name__ == "__main__":
import torch
x=torch.rand(3,3, device="cuda:0")
print(torch.__version__, get_pmem(), get_gpumem())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment