[docs]defget_process_gpu_ram(pid):"""Gets the amount of RAM used by a given process on GPU devices Args: pid (int): process ID Returns: float: RAM usage in Megabytes """# 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("Process ID\s+:\s([^\D]*)",res)foridx,_pidinenumerate(pids):ifint(_pid)==pid:returnfloat(re.findall("Used GPU Memory\s+:\s([^\D]*)",res)[idx])exceptExceptionase:warnings.warn(f"raised: {e}. Assuming no GPU is available.")# Otherwise assume the process is running exclusively on CPUreturn0.