AQIT
0.1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
device_info.py
Go to the documentation of this file.
1
# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2
"""Auto-detect GPU host label, hardware, and coarse location for engine registration."""
3
4
from
__future__
import
annotations
5
6
import
json
7
import
os
8
import
platform
9
import
re
10
import
socket
11
import
urllib.request
12
from
typing
import
Any
13
14
15
def
_fetch_aws_placement
() -> str | None:
16
try
:
17
req = urllib.request.Request(
18
"http://169.254.169.254/latest/meta-data/placement/availability-zone"
,
19
headers={
"Metadata-Flavor"
:
"Amazon"
},
20
)
21
with
urllib.request.urlopen(req, timeout=0.4)
as
resp:
22
text = resp.read().decode().strip()
23
return
text
or
None
24
except
Exception:
25
return
None
26
27
28
def
_sanitize_label
(text: str, *, max_len: int = 64) -> str:
29
cleaned = re.sub(
r"[^\w.\-]+"
,
"-"
, text.strip().lower()).strip(
"-"
)
30
return
(cleaned
or
"gpu-host"
)[:max_len]
31
32
33
def
detect_gpu_info
() -> dict[str, Any]:
34
try
:
35
from
aquin.compute.device
import
probe_backend
36
37
probe = probe_backend()
38
info: dict[str, Any] = {
39
"cuda_available"
: probe.get(
"cuda_available"
,
False
),
40
"mps_available"
: probe.get(
"mps_available"
,
False
),
41
"backend"
: probe.get(
"backend"
),
42
"selected_device"
: probe.get(
"selected"
),
43
"rocm"
: probe.get(
"rocm"
,
False
),
44
"accelerator_available"
: probe.get(
"accelerator_available"
,
False
),
45
"gpus"
: probe.get(
"gpus"
)
or
[],
46
}
47
return
info
48
except
Exception:
49
return
{
50
"cuda_available"
:
False
,
51
"mps_available"
:
False
,
52
"backend"
:
"cpu"
,
53
"selected_device"
:
"cpu"
,
54
"rocm"
:
False
,
55
"accelerator_available"
:
False
,
56
"gpus"
: [],
57
}
58
59
60
def
detect_location
() -> dict[str, Any]:
61
hostname = socket.gethostname()
62
aws_az =
_fetch_aws_placement
()
63
region = (
64
os.environ.get(
"AWS_REGION"
)
65
or
os.environ.get(
"AWS_DEFAULT_REGION"
)
66
or
os.environ.get(
"GCP_REGION"
)
67
or
os.environ.get(
"MODAL_REGION"
)
68
or
os.environ.get(
"RUNPOD_POD_HOSTNAME"
)
69
or
aws_az
70
)
71
72
provider: str |
None
=
None
73
if
os.environ.get(
"AWS_REGION"
)
or
os.environ.get(
"AWS_DEFAULT_REGION"
)
or
aws_az:
74
provider =
"aws"
75
elif
os.environ.get(
"MODAL_TASK_ID"
)
or
os.environ.get(
"MODAL_REGION"
):
76
provider =
"modal"
77
elif
os.environ.get(
"RUNPOD_POD_ID"
):
78
provider =
"runpod"
79
80
parts = [p
for
p
in
(region, hostname)
if
p]
81
return
{
82
"hostname"
: hostname,
83
"region"
: region,
84
"provider"
: provider,
85
"platform"
: platform.system(),
86
"location_label"
:
" · "
.join(parts)
if
parts
else
hostname,
87
}
88
89
90
def
detect_device_label
() -> str:
91
hostname = socket.gethostname()
92
gpu =
detect_gpu_info
()
93
gpus = gpu.get(
"gpus"
)
or
[]
94
if
gpus:
95
short_gpu = re.sub(
r"\s+"
,
"-"
, str(gpus[0].get(
"name"
,
""
)).lower())
96
short_gpu = re.sub(
r"[^a-z0-9\-]"
,
""
, short_gpu)[:24]
97
if
short_gpu:
98
return
_sanitize_label
(f
"{hostname}-{short_gpu}"
)
99
if
gpu.get(
"mps_available"
):
100
return
_sanitize_label
(f
"{hostname}-metal"
)
101
return
_sanitize_label
(hostname)
102
103
104
def
build_engine_profile
() -> dict[str, Any]:
105
gpu =
detect_gpu_info
()
106
location =
detect_location
()
107
device =
detect_device_label
()
108
return
{
109
"device"
: device,
110
"gpu"
: gpu,
111
"location"
: location,
112
"gpu_info"
: json.dumps({**gpu, **location}, separators=(
","
,
":"
)),
113
}
114
115
116
def
format_engine_profile_summary
(profile: dict[str, Any]) -> str:
117
loc = profile.get(
"location"
)
or
{}
118
gpu = profile.get(
"gpu"
)
or
{}
119
gpus = gpu.get(
"gpus"
)
or
[]
120
backend = gpu.get(
"backend"
)
or
"cpu"
121
selected = gpu.get(
"selected_device"
)
or
"cpu"
122
123
if
gpus:
124
g = gpus[0]
125
gpu_line = f
"{g.get('name', 'GPU')} · {g.get('vram_gb', '?')} GB ({backend})"
126
if
len(gpus) > 1:
127
gpu_line += f
" (+{len(gpus) - 1} more)"
128
elif
gpu.get(
"mps_available"
)
or
selected ==
"mps"
:
129
gpu_line =
"Apple Metal (MPS)"
130
elif
gpu.get(
"cuda_available"
):
131
gpu_line = f
"CUDA/ROCm ({backend})"
132
else
:
133
gpu_line =
"no GPU accelerator"
134
135
loc_line = loc.get(
"location_label"
)
or
loc.get(
"hostname"
)
or
"unknown host"
136
return
f
"{loc_line} · {gpu_line}"
aquin.compute.device
Definition
device.py:1
aquin.engine.device_info.format_engine_profile_summary
str format_engine_profile_summary(dict[str, Any] profile)
Definition
device_info.py:120
aquin.engine.device_info.build_engine_profile
dict[str, Any] build_engine_profile()
Definition
device_info.py:108
aquin.engine.device_info.detect_device_label
str detect_device_label()
Definition
device_info.py:94
aquin.engine.device_info._sanitize_label
str _sanitize_label(str text, *, int max_len=64)
Definition
device_info.py:32
aquin.engine.device_info._fetch_aws_placement
str|None _fetch_aws_placement()
Definition
device_info.py:19
aquin.engine.device_info.detect_location
dict[str, Any] detect_location()
Definition
device_info.py:64
aquin.engine.device_info.detect_gpu_info
dict[str, Any] detect_gpu_info()
Definition
device_info.py:37
aquin
engine
device_info.py
AQIT · Aquin Labs Private Limited · Apache 2.0 · Generated by
1.18.0