utils.py 657 Bytes
Newer Older
Woosuk Kwon's avatar
Woosuk Kwon committed
1
import enum
Zhuohan Li's avatar
Zhuohan Li committed
2

3
import psutil
Zhuohan Li's avatar
Zhuohan Li committed
4
5
import torch

Woosuk Kwon's avatar
Woosuk Kwon committed
6
7
8
9
10
11
12
13
14
15
16

class Device(enum.Enum):
    GPU = enum.auto()
    CPU = enum.auto()


class Counter:

    def __init__(self, start: int = 0) -> None:
        self.counter = start

Woosuk Kwon's avatar
Woosuk Kwon committed
17
    def __next__(self) -> int:
Woosuk Kwon's avatar
Woosuk Kwon committed
18
19
20
21
22
23
        id = self.counter
        self.counter += 1
        return id

    def reset(self) -> None:
        self.counter = 0
Zhuohan Li's avatar
Zhuohan Li committed
24

25
26

def get_gpu_memory(gpu: int = 0) -> int:
27
    """Returns the total memory of the GPU in bytes."""
28
29
30
31
    return torch.cuda.get_device_properties(gpu).total_memory


def get_cpu_memory() -> int:
32
    """Returns the total CPU memory of the node in bytes."""
33
    return psutil.virtual_memory().total