2023-02-09 11:26:50 +00:00
|
|
|
import enum
|
2023-05-23 21:39:50 -07:00
|
|
|
import uuid
|
2023-03-22 04:45:42 +08:00
|
|
|
|
2023-05-09 15:30:12 -07:00
|
|
|
import psutil
|
2023-03-22 04:45:42 +08:00
|
|
|
import torch
|
|
|
|
|
2023-02-09 11:26:50 +00:00
|
|
|
|
|
|
|
class Device(enum.Enum):
|
|
|
|
GPU = enum.auto()
|
|
|
|
CPU = enum.auto()
|
|
|
|
|
|
|
|
|
|
|
|
class Counter:
|
|
|
|
|
|
|
|
def __init__(self, start: int = 0) -> None:
|
|
|
|
self.counter = start
|
|
|
|
|
2023-02-14 01:19:27 +00:00
|
|
|
def __next__(self) -> int:
|
2023-02-09 11:26:50 +00:00
|
|
|
id = self.counter
|
|
|
|
self.counter += 1
|
|
|
|
return id
|
|
|
|
|
|
|
|
def reset(self) -> None:
|
|
|
|
self.counter = 0
|
2023-03-22 04:45:42 +08:00
|
|
|
|
2023-03-29 14:48:56 +08:00
|
|
|
|
|
|
|
def get_gpu_memory(gpu: int = 0) -> int:
|
2023-05-23 18:22:26 -07:00
|
|
|
"""Returns the total memory of the GPU in bytes."""
|
2023-03-29 14:48:56 +08:00
|
|
|
return torch.cuda.get_device_properties(gpu).total_memory
|
|
|
|
|
|
|
|
|
|
|
|
def get_cpu_memory() -> int:
|
2023-05-23 18:22:26 -07:00
|
|
|
"""Returns the total CPU memory of the node in bytes."""
|
2023-03-29 14:48:56 +08:00
|
|
|
return psutil.virtual_memory().total
|
2023-05-23 21:39:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
def random_uuid() -> str:
|
|
|
|
return str(uuid.uuid4().hex)
|