utils.py 893 Bytes
Newer Older
Woosuk Kwon's avatar
Woosuk Kwon committed
1
import enum
2
from platform import uname
Zhuohan Li's avatar
Zhuohan Li committed
3
import uuid
Zhuohan Li's avatar
Zhuohan Li committed
4

5
import psutil
Zhuohan Li's avatar
Zhuohan Li committed
6
7
import torch

Woosuk Kwon's avatar
Woosuk Kwon committed
8
9
10
11
12
13
14
15
16
17
18

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
19
    def __next__(self) -> int:
20
        i = self.counter
Woosuk Kwon's avatar
Woosuk Kwon committed
21
        self.counter += 1
22
        return i
Woosuk Kwon's avatar
Woosuk Kwon committed
23
24
25

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

27
28

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


def get_cpu_memory() -> int:
34
    """Returns the total CPU memory of the node in bytes."""
35
    return psutil.virtual_memory().total
Zhuohan Li's avatar
Zhuohan Li committed
36
37
38
39


def random_uuid() -> str:
    return str(uuid.uuid4().hex)
40

41

42
43
44
def in_wsl() -> bool:
    # Reference: https://github.com/microsoft/WSL/issues/4071
    return "microsoft" in " ".join(uname()).lower()