".idea/inspectionProfiles/Project_Default.xml" did not exist on "687f3d89b4384f4749cfa8af62c53deba76f7637"
utils.py 1.37 KB
Newer Older
Woosuk Kwon's avatar
Woosuk Kwon committed
1
import enum
2
import socket
Zhuohan Li's avatar
Zhuohan Li committed
3
import uuid
4
from platform import uname
Zhuohan Li's avatar
Zhuohan Li committed
5

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

9
from vllm._C import cuda_utils
10

Woosuk Kwon's avatar
Woosuk Kwon committed
11
12
13
14
15
16
17
18
19
20
21

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

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

30

31
32
33
34
def is_hip() -> bool:
    return torch.version.hip is not None


35
36
37
def get_max_shared_memory_bytes(gpu: int = 0) -> int:
    """Returns the maximum shared memory per thread block in bytes."""
    # https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html
38
    cudaDevAttrMaxSharedMemoryPerBlockOptin = 97 if not is_hip() else 74
39
40
41
42
43
    max_shared_mem = cuda_utils.get_device_attribute(
        cudaDevAttrMaxSharedMemoryPerBlockOptin, gpu)
    return int(max_shared_mem)


44
def get_cpu_memory() -> int:
45
    """Returns the total CPU memory of the node in bytes."""
46
    return psutil.virtual_memory().total
Zhuohan Li's avatar
Zhuohan Li committed
47
48
49
50


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

52

53
54
55
def in_wsl() -> bool:
    # Reference: https://github.com/microsoft/WSL/issues/4071
    return "microsoft" in " ".join(uname()).lower()
56
57
58
59
60
61


def get_open_port():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("", 0))
        return s.getsockname()[1]