Created
November 24, 2021 01:46
-
-
Save malfet/38cf2ab16f8d708aa1e312573295f5c6 to your computer and use it in GitHub Desktop.
Query PyTorch memory utilisation
This file contains 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
#!/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