# Copyright (C) 2020-2022, François-Guillaume Fernandez.# This program is licensed under the Apache License 2.0.# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.importreimportsubprocessimportwarningsimporttorch__all__=["get_process_gpu_ram"]
[docs]defget_process_gpu_ram(pid:int)->float:"""Gets the amount of RAM used by a given process on GPU devices Args: pid: process ID Returns: RAM usage in Megabytes """# PyTorch is not responsible for GPU usageifnottorch.cuda.is_available():warnings.warn("CUDA is unavailable to PyTorch.")return0.0# Query the running processes on GPUstry:res=subprocess.run(["nvidia-smi","-q","-d","PIDS"],capture_output=True).stdout.decode()# Try to locate the processpids=re.findall(r"Process ID\s+:\s([^\D]*)",res)foridx,_pidinenumerate(pids):ifint(_pid)==pid:returnfloat(re.findall(r"Used GPU Memory\s+:\s([^\D]*)",res)[idx])# Query total memory used by nvidiares=subprocess.run(["nvidia-smi","--query-gpu=memory.used","--format=csv"],capture_output=True).stdout.decode()returnfloat(res.split("\n")[1].split()[0])exceptExceptionase:warnings.warn(f"raised: {e}. Parsing NVIDIA-SMI failed.")# Default to overall RAM usage for this process on the GPUram_str=torch.cuda.list_gpu_processes().split("\n")# Take the first process running on the GPUifram_str[1].startswith("process"):returnfloat(ram_str[1].split()[3])# Otherwise assume the process is running exclusively on CPUreturn0.0