파이썬

[파이썬] GPU 정보 출력

IntegerString 2023. 4. 5. 17:30
import GPUtil

필요한 라이브러리 임포트 (https://github.com/anderskm/gputil)

 

def add_unit(mem: float) -> str:
    if mem > 1024:
        mem = round(mem / 1024, 2)
        mem = f"{mem}GiB"
    else:
        mem = round(mem, 2)
        mem = f"{mem}MiB"
    return mem

데이터 단위를 붙여주기 위한 함수

 

for gpu in GPUtil.getGPUs():
    gpu_util = f"{gpu.load}%"
    mem_total = add_unit(gpu.memoryTotal)
    mem_used = add_unit(gpu.memoryUsed)
    mem_used_percent = f"{round(gpu.memoryUtil * 100, 2)}%"
    print(f"ID: {gpu.id}, Util: {gpu_util}, Memory: {mem_used} / {mem_total} ({mem_used_percent})")

출력 결과


GPU 클래스 정보: https://github.com/anderskm/gputil#helper-functions