Last active
June 30, 2022 11:02
-
-
Save m3hrdadfi/5efdbd23bcaa379c538471a3e4377cee to your computer and use it in GitHub Desktop.
GPU Memory
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(): | |
# source: https://stackoverflow.com/a/59571639 | |
command = "nvidia-smi --query-gpu=memory.free --format=csv" | |
memory_free_info = subprocess.check_output(command.split()).decode('ascii').split('\n')[:-1][1:] | |
memory_free_values = [round(int(x.split()[0]) / 1000) for i, x in enumerate(memory_free_info)] | |
return memory_free_values | |
def check_gpu_memory(min_num_gpus=3, min_memory=30): | |
gpus = get_gpu_memory() | |
if not len(gpus) > min_num_gpus or not all([True if gpu > min_memory else False for gpu in gpus]): | |
raise Exception("There is something wrong with GPU allocation on the server side!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment