Skip to content

utils

get_cpu_id()

Fetches the CPU ID based on the operating system.

Attempts to get the CPU ID for Windows, Linux, and MacOS. In case of any error or an unsupported OS, returns None.

Returns:

Type Description

Optional[str]: CPU ID string if available, None otherwise.

Source code in inference/core/devices/utils.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def get_cpu_id():
    """Fetches the CPU ID based on the operating system.

    Attempts to get the CPU ID for Windows, Linux, and MacOS.
    In case of any error or an unsupported OS, returns None.

    Returns:
        Optional[str]: CPU ID string if available, None otherwise.
    """
    try:
        if platform.system() == "Windows":
            return os.popen("wmic cpu get ProcessorId").read().strip()
        elif platform.system() == "Linux":
            return (
                open("/proc/cpuinfo").read().split("processor")[0].split(":")[1].strip()
            )
        elif platform.system() == "Darwin":
            import subprocess

            return (
                subprocess.check_output(["sysctl", "-n", "machdep.cpu.brand_string"])
                .strip()
                .decode()
            )
    except Exception as e:
        return None

get_device_hostname()

Fetches the device's hostname.

Returns:

Name Type Description
str

The device's hostname.

Source code in inference/core/devices/utils.py
107
108
109
110
111
112
113
def get_device_hostname():
    """Fetches the device's hostname.

    Returns:
        str: The device's hostname.
    """
    return platform.node()

get_gpu_id()

Fetches the GPU ID if a GPU is present.

Tries to import and use the pynvml (delivered by nvidia-ml-py) module to retrieve the GPU information.

Returns:

Type Description

Optional[int]: GPU ID if available, None otherwise.

Source code in inference/core/devices/utils.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def get_gpu_id():
    """Fetches the GPU ID if a GPU is present.

    Tries to import and use the `pynvml` (delivered by nvidia-ml-py) module to retrieve the GPU information.

    Returns:
        Optional[int]: GPU ID if available, None otherwise.
    """
    try:
        from pynvml import nvmlDeviceGetCount, nvmlInit

        nvmlInit()
        gpus_count = nvmlDeviceGetCount()
        if gpus_count:
            return 0
    except ImportError:
        return None
    except Exception:
        return None

get_inference_server_id()

Fetches a unique device ID.

Tries to get the GPU ID first, then falls back to CPU ID. If the application is running inside Docker, the Docker container ID is appended to the hostname.

Returns:

Name Type Description
str

A unique string representing the device. If unable to determine, returns "UNKNOWN".

Source code in inference/core/devices/utils.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def get_inference_server_id():
    """Fetches a unique device ID.

    Tries to get the GPU ID first, then falls back to CPU ID.
    If the application is running inside Docker, the Docker container ID is appended to the hostname.

    Returns:
        str: A unique string representing the device. If unable to determine, returns "UNKNOWN".
    """
    try:
        if INFERENCE_SERVER_ID is not None:
            return INFERENCE_SERVER_ID
        id = random_string(6)
        gpu_id = get_gpu_id()
        if gpu_id is not None:
            return f"{id}-GPU-{gpu_id}"
        jetson_id = get_jetson_id()
        if jetson_id is not None:
            return f"{id}-JETSON-{jetson_id}"
        return id
    except Exception as e:
        return "UNKNOWN"

get_jetson_id()

Fetches the Jetson device's serial number.

Attempts to read the serial number from the device tree. In case of any error, returns None.

Returns:

Type Description

Optional[str]: Jetson device serial number if available, None otherwise.

Source code in inference/core/devices/utils.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def get_jetson_id():
    """Fetches the Jetson device's serial number.

    Attempts to read the serial number from the device tree.
    In case of any error, returns None.

    Returns:
        Optional[str]: Jetson device serial number if available, None otherwise.
    """
    try:
        # Fetch the device's serial number
        if not os.path.exists("/proc/device-tree/serial-number"):
            return None
        serial_number = os.popen("cat /proc/device-tree/serial-number").read().strip()
        if serial_number == "":
            return None
        return serial_number
    except Exception as e:
        return None

is_running_in_docker()

Checks if the current process is running inside a Docker container.

Returns:

Name Type Description
bool

True if running inside a Docker container, False otherwise.

Source code in inference/core/devices/utils.py
10
11
12
13
14
15
16
def is_running_in_docker():
    """Checks if the current process is running inside a Docker container.

    Returns:
        bool: True if running inside a Docker container, False otherwise.
    """
    return os.path.exists("/.dockerenv")