kernelvet 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kernelvet/__init__.py +10 -0
- kernelvet/__main__.py +3 -0
- kernelvet/benchmark/__init__.py +1 -0
- kernelvet/benchmark/engine.py +180 -0
- kernelvet/benchmark/workloads.py +153 -0
- kernelvet/cli/__init__.py +1 -0
- kernelvet/cli/app.py +64 -0
- kernelvet/cli/commands/__init__.py +1 -0
- kernelvet/cli/commands/diagnose.py +244 -0
- kernelvet/cli/commands/migrate.py +334 -0
- kernelvet/cli/commands/misc.py +216 -0
- kernelvet/cli/output.py +110 -0
- kernelvet/compatibility/__init__.py +1 -0
- kernelvet/compatibility/data/rocm_matrix.json +123 -0
- kernelvet/compatibility/engine.py +24 -0
- kernelvet/compatibility/matrix.py +128 -0
- kernelvet/compatibility/validators.py +405 -0
- kernelvet/config/__init__.py +10 -0
- kernelvet/config/settings.py +112 -0
- kernelvet/diagnose/__init__.py +1 -0
- kernelvet/diagnose/engine.py +296 -0
- kernelvet/diagnose/hardware.py +336 -0
- kernelvet/diagnose/runtime.py +332 -0
- kernelvet/diagnose/software.py +221 -0
- kernelvet/diagnose/system.py +67 -0
- kernelvet/execution/__init__.py +1 -0
- kernelvet/execution/compiler.py +275 -0
- kernelvet/execution/runner.py +125 -0
- kernelvet/execution/sandbox.py +51 -0
- kernelvet/history/__init__.py +3 -0
- kernelvet/history/store.py +157 -0
- kernelvet/main.py +19 -0
- kernelvet/migration/__init__.py +17 -0
- kernelvet/migration/analyzer.py +78 -0
- kernelvet/migration/auditor.py +89 -0
- kernelvet/migration/hipify.py +268 -0
- kernelvet/migration/planner.py +128 -0
- kernelvet/migration/rules.py +564 -0
- kernelvet/migration/scanner.py +234 -0
- kernelvet/migration/workflow.py +135 -0
- kernelvet/models/__init__.py +35 -0
- kernelvet/models/common.py +102 -0
- kernelvet/models/migration.py +236 -0
- kernelvet/models/report.py +126 -0
- kernelvet/models/system.py +89 -0
- kernelvet/reporting/__init__.py +80 -0
- kernelvet/reporting/common.py +74 -0
- kernelvet/reporting/html.py +149 -0
- kernelvet/reporting/json.py +20 -0
- kernelvet/reporting/loader.py +48 -0
- kernelvet/reporting/markdown.py +256 -0
- kernelvet/reporting/sarif.py +79 -0
- kernelvet/reporting/terminal.py +276 -0
- kernelvet/tui/__init__.py +1 -0
- kernelvet/tui/app.py +177 -0
- kernelvet/tui/screens/__init__.py +1 -0
- kernelvet/tui/screens/base.py +34 -0
- kernelvet/tui/screens/devices.py +211 -0
- kernelvet/tui/screens/diagnose.py +212 -0
- kernelvet/tui/screens/help.py +47 -0
- kernelvet/tui/screens/migration.py +573 -0
- kernelvet/tui/screens/overview.py +195 -0
- kernelvet/tui/screens/tools.py +357 -0
- kernelvet/tui/state.py +55 -0
- kernelvet/tui/widgets/__init__.py +1 -0
- kernelvet/tui/widgets/common.py +82 -0
- kernelvet/tui/workers/__init__.py +25 -0
- kernelvet/utils/__init__.py +1 -0
- kernelvet/utils/paths.py +48 -0
- kernelvet/utils/proc.py +178 -0
- kernelvet/utils/redact.py +47 -0
- kernelvet/utils/versions.py +34 -0
- kernelvet-0.1.0.dist-info/METADATA +275 -0
- kernelvet-0.1.0.dist-info/RECORD +77 -0
- kernelvet-0.1.0.dist-info/WHEEL +4 -0
- kernelvet-0.1.0.dist-info/entry_points.txt +2 -0
- kernelvet-0.1.0.dist-info/licenses/LICENSE +197 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"""Diagnostic orchestration shared by the CLI and the TUI."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
from kernelvet.compatibility.engine import evaluate
|
|
8
|
+
from kernelvet.compatibility.matrix import load_matrix
|
|
9
|
+
from kernelvet.diagnose.hardware import HardwareProbe, discover_gpus
|
|
10
|
+
from kernelvet.diagnose.runtime import run_hip_probe, run_torch_probe
|
|
11
|
+
from kernelvet.diagnose.software import collect_software
|
|
12
|
+
from kernelvet.diagnose.system import collect_host
|
|
13
|
+
from kernelvet.models.common import CheckResult, CheckStatus, Evidence
|
|
14
|
+
from kernelvet.models.report import DiagnosticReport
|
|
15
|
+
from kernelvet.models.system import GpuDevice, HostInfo, SoftwareInfo
|
|
16
|
+
from kernelvet.utils.proc import HostContext, ProgressCallback, noop_progress
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class DiagnoseOptions:
|
|
21
|
+
runtime: bool = True # compile + run the HIP probe
|
|
22
|
+
torch: bool = True # detect PyTorch and run tensor ops on the GPU
|
|
23
|
+
compatibility: bool = True
|
|
24
|
+
python: str | None = None
|
|
25
|
+
matrix_path: str | None = None
|
|
26
|
+
extra_notes: list[str] = field(default_factory=list)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _c(
|
|
30
|
+
cid: str,
|
|
31
|
+
name: str,
|
|
32
|
+
category: str,
|
|
33
|
+
status: CheckStatus,
|
|
34
|
+
summary: str,
|
|
35
|
+
evidence: Evidence = Evidence.DETECTED,
|
|
36
|
+
remediation: str | None = None,
|
|
37
|
+
detail: str = "",
|
|
38
|
+
) -> CheckResult:
|
|
39
|
+
return CheckResult(
|
|
40
|
+
id=cid,
|
|
41
|
+
name=name,
|
|
42
|
+
category=category,
|
|
43
|
+
status=status,
|
|
44
|
+
evidence=evidence,
|
|
45
|
+
summary=summary,
|
|
46
|
+
remediation=remediation,
|
|
47
|
+
detail=detail,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def static_checks(host: HostInfo, hw: HardwareProbe, sw: SoftwareInfo) -> list[CheckResult]:
|
|
52
|
+
checks: list[CheckResult] = []
|
|
53
|
+
linux = host.os_name not in ("macOS", "Windows", "Darwin")
|
|
54
|
+
checks.append(
|
|
55
|
+
_c(
|
|
56
|
+
"system.platform",
|
|
57
|
+
"Supported Platform",
|
|
58
|
+
"system",
|
|
59
|
+
CheckStatus.PASS if linux else CheckStatus.FAIL,
|
|
60
|
+
f"{host.os_pretty or host.os_name} ({host.architecture})",
|
|
61
|
+
remediation=None if linux else "ROCm runs on Linux; use a supported distribution",
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
amd = [g for g in hw.gpus if g.vendor_is_amd]
|
|
66
|
+
if amd:
|
|
67
|
+
sources = sorted({s for g in amd for s in g.sources})
|
|
68
|
+
checks.append(
|
|
69
|
+
_c(
|
|
70
|
+
"hardware.gpu_detected",
|
|
71
|
+
"GPU Detection",
|
|
72
|
+
"hardware",
|
|
73
|
+
CheckStatus.PASS,
|
|
74
|
+
f"{len(amd)} AMD GPU(s) detected via {', '.join(sources)}",
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
else:
|
|
78
|
+
others = len(hw.gpus) - len(amd)
|
|
79
|
+
why = "No AMD GPU detected"
|
|
80
|
+
if not linux:
|
|
81
|
+
why += f" ({host.os_name} host; ROCm GPU discovery needs Linux)"
|
|
82
|
+
elif hw.rocminfo is None and hw.amd_smi is None:
|
|
83
|
+
why += " (rocminfo and amd-smi not installed, and no AMD device in /sys/class/drm)"
|
|
84
|
+
if others:
|
|
85
|
+
why += f"; {others} non-AMD GPU(s) listed"
|
|
86
|
+
checks.append(
|
|
87
|
+
_c(
|
|
88
|
+
"hardware.gpu_detected",
|
|
89
|
+
"GPU Detection",
|
|
90
|
+
"hardware",
|
|
91
|
+
CheckStatus.FAIL,
|
|
92
|
+
why,
|
|
93
|
+
remediation="Check the GPU is installed, the amdgpu driver is loaded, and ROCm is installed",
|
|
94
|
+
detail="\n".join(hw.notes),
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
if hw.kfd_accessible is None:
|
|
99
|
+
checks.append(
|
|
100
|
+
_c(
|
|
101
|
+
"hardware.device_access",
|
|
102
|
+
"GPU Device Access",
|
|
103
|
+
"hardware",
|
|
104
|
+
CheckStatus.SKIPPED,
|
|
105
|
+
"Not a Linux host",
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
elif hw.kfd_accessible:
|
|
109
|
+
checks.append(
|
|
110
|
+
_c(
|
|
111
|
+
"hardware.device_access",
|
|
112
|
+
"GPU Device Access",
|
|
113
|
+
"hardware",
|
|
114
|
+
CheckStatus.PASS,
|
|
115
|
+
"/dev/kfd is readable and writable by this user",
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
else:
|
|
119
|
+
checks.append(
|
|
120
|
+
_c(
|
|
121
|
+
"hardware.device_access",
|
|
122
|
+
"GPU Device Access",
|
|
123
|
+
"hardware",
|
|
124
|
+
CheckStatus.FAIL,
|
|
125
|
+
"/dev/kfd is missing or not accessible",
|
|
126
|
+
remediation="Load the amdgpu driver and add your user to the `render` and `video` groups: "
|
|
127
|
+
"`sudo usermod -aG render,video $USER` then log in again",
|
|
128
|
+
)
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
if sw.rocm_version:
|
|
132
|
+
checks.append(
|
|
133
|
+
_c(
|
|
134
|
+
"software.rocm",
|
|
135
|
+
"ROCm Installation",
|
|
136
|
+
"software",
|
|
137
|
+
CheckStatus.PASS,
|
|
138
|
+
f"ROCm {sw.rocm_version} at {sw.rocm_path}",
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
elif sw.rocm_path:
|
|
142
|
+
checks.append(
|
|
143
|
+
_c(
|
|
144
|
+
"software.rocm",
|
|
145
|
+
"ROCm Installation",
|
|
146
|
+
"software",
|
|
147
|
+
CheckStatus.WARNING,
|
|
148
|
+
f"{sw.rocm_path} exists but its version could not be read",
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
else:
|
|
152
|
+
checks.append(
|
|
153
|
+
_c(
|
|
154
|
+
"software.rocm",
|
|
155
|
+
"ROCm Installation",
|
|
156
|
+
"software",
|
|
157
|
+
CheckStatus.FAIL,
|
|
158
|
+
"ROCm not found (checked ROCM_PATH, HIP_PATH and /opt/rocm)",
|
|
159
|
+
remediation="Install ROCm: https://rocm.docs.amd.com/projects/install-on-linux/en/latest/",
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
rocminfo = sw.tool("rocminfo")
|
|
164
|
+
if rocminfo and rocminfo.available:
|
|
165
|
+
if hw.rocminfo is not None and not hw.rocminfo.ok:
|
|
166
|
+
checks.append(
|
|
167
|
+
_c(
|
|
168
|
+
"software.rocminfo",
|
|
169
|
+
"rocminfo",
|
|
170
|
+
"software",
|
|
171
|
+
CheckStatus.FAIL,
|
|
172
|
+
hw.rocminfo.describe_failure(),
|
|
173
|
+
remediation="rocminfo failed; this usually means /dev/kfd is not accessible "
|
|
174
|
+
"or the amdgpu driver is not loaded",
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
else:
|
|
178
|
+
checks.append(
|
|
179
|
+
_c(
|
|
180
|
+
"software.rocminfo",
|
|
181
|
+
"rocminfo",
|
|
182
|
+
"software",
|
|
183
|
+
CheckStatus.PASS,
|
|
184
|
+
f"available at {rocminfo.path}",
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
else:
|
|
188
|
+
checks.append(
|
|
189
|
+
_c(
|
|
190
|
+
"software.rocminfo",
|
|
191
|
+
"rocminfo",
|
|
192
|
+
"software",
|
|
193
|
+
CheckStatus.WARNING,
|
|
194
|
+
"not installed",
|
|
195
|
+
remediation="rocminfo ships with ROCm (package `rocminfo`)",
|
|
196
|
+
)
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
for tool, label, fix in (
|
|
200
|
+
("amd-smi", "AMD SMI", "Install AMD SMI (package `amd-smi-lib`)"),
|
|
201
|
+
("hipcc", "HIP Compiler", "Install the HIP SDK (package `hip-dev` / `rocm-hip-sdk`)"),
|
|
202
|
+
):
|
|
203
|
+
t = sw.tool(tool)
|
|
204
|
+
if t and t.available:
|
|
205
|
+
checks.append(
|
|
206
|
+
_c(
|
|
207
|
+
f"software.{tool}",
|
|
208
|
+
label,
|
|
209
|
+
"software",
|
|
210
|
+
CheckStatus.PASS,
|
|
211
|
+
f"{t.version or 'version unknown'} at {t.path}",
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
else:
|
|
215
|
+
checks.append(
|
|
216
|
+
_c(
|
|
217
|
+
f"software.{tool}",
|
|
218
|
+
label,
|
|
219
|
+
"software",
|
|
220
|
+
CheckStatus.WARNING,
|
|
221
|
+
"not installed",
|
|
222
|
+
remediation=fix,
|
|
223
|
+
)
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
if linux:
|
|
227
|
+
if sw.amdgpu_module_loaded:
|
|
228
|
+
checks.append(
|
|
229
|
+
_c(
|
|
230
|
+
"software.amdgpu",
|
|
231
|
+
"amdgpu Kernel Module",
|
|
232
|
+
"software",
|
|
233
|
+
CheckStatus.PASS,
|
|
234
|
+
f"loaded{f' (version {sw.amdgpu_driver_version})' if sw.amdgpu_driver_version else ''}",
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
else:
|
|
238
|
+
checks.append(
|
|
239
|
+
_c(
|
|
240
|
+
"software.amdgpu",
|
|
241
|
+
"amdgpu Kernel Module",
|
|
242
|
+
"software",
|
|
243
|
+
CheckStatus.FAIL,
|
|
244
|
+
"not loaded",
|
|
245
|
+
remediation="Install amdgpu-dkms or load the module: `sudo modprobe amdgpu`",
|
|
246
|
+
)
|
|
247
|
+
)
|
|
248
|
+
return checks
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def run_diagnosis(
|
|
252
|
+
ctx: HostContext | None = None,
|
|
253
|
+
options: DiagnoseOptions | None = None,
|
|
254
|
+
progress: ProgressCallback = noop_progress,
|
|
255
|
+
) -> DiagnosticReport:
|
|
256
|
+
ctx = ctx or HostContext()
|
|
257
|
+
opts = options or DiagnoseOptions()
|
|
258
|
+
|
|
259
|
+
# load (and validate) the matrix first, so a bad matrix file fails before the slow probes run
|
|
260
|
+
matrix = load_matrix(opts.matrix_path) if opts.compatibility else None
|
|
261
|
+
|
|
262
|
+
progress("Collecting host information", 0.05)
|
|
263
|
+
host = collect_host(ctx)
|
|
264
|
+
progress("Discovering GPUs (amd-smi, rocminfo, sysfs)", 0.15)
|
|
265
|
+
hw = discover_gpus(ctx)
|
|
266
|
+
progress("Inspecting ROCm, HIP, drivers and tools", 0.35)
|
|
267
|
+
sw = collect_software(ctx, python=opts.python, torch=opts.torch)
|
|
268
|
+
checks = static_checks(host, hw, sw)
|
|
269
|
+
|
|
270
|
+
if opts.runtime:
|
|
271
|
+
progress("Compiling and running the HIP probe", 0.55)
|
|
272
|
+
targets = [g.gfx_target for g in hw.gpus if g.gfx_target]
|
|
273
|
+
checks.extend(run_hip_probe(ctx, gfx_targets=[t for t in targets if t]))
|
|
274
|
+
if opts.torch:
|
|
275
|
+
progress("Running PyTorch GPU checks", 0.75)
|
|
276
|
+
checks.extend(run_torch_probe(ctx, sw.torch, opts.python))
|
|
277
|
+
|
|
278
|
+
compat = None
|
|
279
|
+
if matrix is not None:
|
|
280
|
+
progress("Evaluating compatibility matrix", 0.9)
|
|
281
|
+
compat = evaluate(host, hw.gpus, sw, matrix)
|
|
282
|
+
|
|
283
|
+
report = DiagnosticReport(host=host, gpus=hw.gpus, software=sw, checks=checks, compatibility=compat)
|
|
284
|
+
progress("Done", 1.0)
|
|
285
|
+
return report.finalize()
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def collect_inventory(
|
|
289
|
+
ctx: HostContext | None = None, *, torch: bool = False, python: str | None = None
|
|
290
|
+
) -> tuple[HostInfo, list[GpuDevice], SoftwareInfo]:
|
|
291
|
+
"""Static inventory only (used by `gpu list`, `system info`, `compatibility`)."""
|
|
292
|
+
ctx = ctx or HostContext()
|
|
293
|
+
host = collect_host(ctx)
|
|
294
|
+
hw = discover_gpus(ctx)
|
|
295
|
+
sw = collect_software(ctx, python=python, torch=torch)
|
|
296
|
+
return host, hw.gpus, sw
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"""GPU discovery from ``amd-smi``, ``rocminfo`` and ``/sys``.
|
|
2
|
+
|
|
3
|
+
Sources are merged by PCI bus address when available, otherwise by order. A GPU
|
|
4
|
+
is only reported when some source actually lists it; nothing is invented.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from kernelvet.models.common import Evidence
|
|
16
|
+
from kernelvet.models.system import GpuDevice
|
|
17
|
+
from kernelvet.utils.proc import CommandResult, HostContext
|
|
18
|
+
|
|
19
|
+
AMD_PCI_VENDOR = "0x1002"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# --------------------------------------------------------------------------- rocminfo
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class RocminfoAgent:
|
|
27
|
+
name: str | None = None
|
|
28
|
+
marketing_name: str | None = None
|
|
29
|
+
vendor: str | None = None
|
|
30
|
+
device_type: str | None = None
|
|
31
|
+
compute_units: int | None = None
|
|
32
|
+
node: int | None = None
|
|
33
|
+
uuid: str | None = None
|
|
34
|
+
chip_id: str | None = None
|
|
35
|
+
bdf: str | None = None
|
|
36
|
+
pool_bytes: list[int] = field(default_factory=list)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
_KV = re.compile(r"^\s*([A-Za-z][A-Za-z0-9 ()/_.-]*?):\s+(.*\S)\s*$")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def parse_rocminfo(text: str) -> list[RocminfoAgent]:
|
|
43
|
+
"""Parse ``rocminfo`` output into agents (CPU and GPU).
|
|
44
|
+
|
|
45
|
+
Agent blocks start with ``Agent N`` and are separated by asterisk rules.
|
|
46
|
+
Memory pools are nested in each agent; we collect the GLOBAL pool sizes
|
|
47
|
+
so VRAM can be inferred when amd-smi is unavailable.
|
|
48
|
+
"""
|
|
49
|
+
agents: list[RocminfoAgent] = []
|
|
50
|
+
current: RocminfoAgent | None = None
|
|
51
|
+
in_pool = False
|
|
52
|
+
pool_segment: str | None = None
|
|
53
|
+
for raw in text.splitlines():
|
|
54
|
+
line = raw.rstrip()
|
|
55
|
+
if re.match(r"^\s*Agent \d+\s*$", line):
|
|
56
|
+
current = RocminfoAgent()
|
|
57
|
+
agents.append(current)
|
|
58
|
+
in_pool = False
|
|
59
|
+
continue
|
|
60
|
+
if current is None:
|
|
61
|
+
continue
|
|
62
|
+
if re.match(r"^\s*Pool \d+\s*$", line):
|
|
63
|
+
in_pool = True
|
|
64
|
+
pool_segment = None
|
|
65
|
+
continue
|
|
66
|
+
if re.match(r"^\s*ISA Info:", line):
|
|
67
|
+
in_pool = False
|
|
68
|
+
continue
|
|
69
|
+
m = _KV.match(line)
|
|
70
|
+
if not m:
|
|
71
|
+
continue
|
|
72
|
+
key, value = m.group(1).strip(), m.group(2).strip()
|
|
73
|
+
if in_pool:
|
|
74
|
+
if key == "Segment":
|
|
75
|
+
pool_segment = value
|
|
76
|
+
elif key == "Size" and pool_segment and pool_segment.startswith("GLOBAL"):
|
|
77
|
+
size = _parse_size(value)
|
|
78
|
+
if size:
|
|
79
|
+
current.pool_bytes.append(size)
|
|
80
|
+
continue
|
|
81
|
+
if key == "Name" and current.name is None:
|
|
82
|
+
current.name = value
|
|
83
|
+
elif key == "Marketing Name":
|
|
84
|
+
current.marketing_name = value
|
|
85
|
+
elif key == "Vendor Name":
|
|
86
|
+
current.vendor = value
|
|
87
|
+
elif key == "Device Type":
|
|
88
|
+
current.device_type = value
|
|
89
|
+
elif key == "Compute Unit":
|
|
90
|
+
current.compute_units = _int(value)
|
|
91
|
+
elif key == "Node":
|
|
92
|
+
current.node = _int(value)
|
|
93
|
+
elif key == "Uuid":
|
|
94
|
+
current.uuid = value
|
|
95
|
+
elif key == "Chip ID":
|
|
96
|
+
current.chip_id = value.split("(")[-1].rstrip(")") if "(" in value else value
|
|
97
|
+
elif key == "BDFID":
|
|
98
|
+
current.bdf = _bdf_from_id(value)
|
|
99
|
+
return agents
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _int(value: str) -> int | None:
|
|
103
|
+
m = re.match(r"-?\d+", value)
|
|
104
|
+
return int(m.group(0)) if m else None
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _parse_size(value: str) -> int | None:
|
|
108
|
+
m = re.match(r"(\d+)\s*\(?\s*0x[0-9a-fA-F]+\s*\)?\s*(KB|MB|GB)?", value)
|
|
109
|
+
if not m:
|
|
110
|
+
m = re.match(r"(\d+)\s*(KB|MB|GB)?", value)
|
|
111
|
+
if not m:
|
|
112
|
+
return None
|
|
113
|
+
n = int(m.group(1))
|
|
114
|
+
unit = (m.group(2) or "KB").upper()
|
|
115
|
+
return n * {"KB": 1024, "MB": 1024**2, "GB": 1024**3}[unit]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _bdf_from_id(value: str) -> str | None:
|
|
119
|
+
"""rocminfo prints BDFID as a decimal int: bus<<8 | device<<3 | function."""
|
|
120
|
+
n = _int(value)
|
|
121
|
+
if n is None:
|
|
122
|
+
return None
|
|
123
|
+
bus, dev, fn = (n >> 8) & 0xFF, (n >> 3) & 0x1F, n & 0x7
|
|
124
|
+
return f"{bus:02x}:{dev:02x}.{fn}"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def gpu_agents(agents: list[RocminfoAgent]) -> list[RocminfoAgent]:
|
|
128
|
+
return [a for a in agents if (a.device_type or "").upper() == "GPU"]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# --------------------------------------------------------------------------- amd-smi
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _unwrap(value: Any) -> Any:
|
|
135
|
+
"""amd-smi wraps numbers as {"value": 12, "unit": "MB"} in newer releases."""
|
|
136
|
+
if isinstance(value, dict) and "value" in value:
|
|
137
|
+
return value["value"], value.get("unit")
|
|
138
|
+
return value, None
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _mem_bytes(value: Any) -> int | None:
|
|
142
|
+
v, unit = _unwrap(value)
|
|
143
|
+
if v in (None, "N/A"):
|
|
144
|
+
return None
|
|
145
|
+
try:
|
|
146
|
+
n = float(str(v).split()[0])
|
|
147
|
+
except ValueError:
|
|
148
|
+
return None
|
|
149
|
+
unit = (unit or (str(v).split()[1] if len(str(v).split()) > 1 else "MB")).upper()
|
|
150
|
+
mult = {"B": 1, "KB": 1024, "MB": 1024**2, "GB": 1024**3, "KIB": 1024, "MIB": 1024**2, "GIB": 1024**3}
|
|
151
|
+
return int(n * mult.get(unit, 1024**2))
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def parse_amd_smi_static(text: str) -> list[dict[str, Any]]:
|
|
155
|
+
"""Parse ``amd-smi static --json`` (list or ``{"gpu_data": [...]}``)."""
|
|
156
|
+
data = json.loads(text)
|
|
157
|
+
if isinstance(data, dict):
|
|
158
|
+
data = data.get("gpu_data") or data.get("gpus") or [data]
|
|
159
|
+
out: list[dict[str, Any]] = []
|
|
160
|
+
for i, entry in enumerate(data):
|
|
161
|
+
if not isinstance(entry, dict):
|
|
162
|
+
continue
|
|
163
|
+
asic = entry.get("asic") or {}
|
|
164
|
+
bus = entry.get("bus") or {}
|
|
165
|
+
vram = entry.get("vram") or {}
|
|
166
|
+
board = entry.get("board") or {}
|
|
167
|
+
out.append(
|
|
168
|
+
{
|
|
169
|
+
"index": entry.get("gpu", i),
|
|
170
|
+
"marketing_name": asic.get("market_name") or board.get("product_name"),
|
|
171
|
+
"vendor": asic.get("vendor_name"),
|
|
172
|
+
"vendor_id": str(asic.get("vendor_id") or "").lower() or None,
|
|
173
|
+
"device_id": asic.get("device_id"),
|
|
174
|
+
"gfx_target": asic.get("target_graphics_version"),
|
|
175
|
+
"compute_units": _unwrap(asic.get("num_compute_units"))[0],
|
|
176
|
+
"uuid": asic.get("asic_serial") or entry.get("uuid"),
|
|
177
|
+
"pci_bus": _norm_bdf(bus.get("bdf")),
|
|
178
|
+
"vram_bytes": _mem_bytes(vram.get("size")),
|
|
179
|
+
}
|
|
180
|
+
)
|
|
181
|
+
return out
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _norm_bdf(bdf: str | None) -> str | None:
|
|
185
|
+
if not bdf:
|
|
186
|
+
return None
|
|
187
|
+
bdf = bdf.lower()
|
|
188
|
+
# "0000:03:00.0" -> "03:00.0"
|
|
189
|
+
return bdf.split(":", 1)[1] if bdf.count(":") == 2 else bdf
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
# --------------------------------------------------------------------------- /sys fallback
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def sysfs_amd_gpus(ctx: HostContext) -> list[dict[str, Any]]:
|
|
196
|
+
"""AMD display/compute devices visible in /sys/class/drm (no ROCm required)."""
|
|
197
|
+
found: list[dict[str, Any]] = []
|
|
198
|
+
for card in ctx.listdir("/sys/class/drm"):
|
|
199
|
+
if not re.fullmatch(r"card\d+", card):
|
|
200
|
+
continue
|
|
201
|
+
base = f"/sys/class/drm/{card}/device"
|
|
202
|
+
vendor = (ctx.read_text(f"{base}/vendor") or "").strip().lower()
|
|
203
|
+
if vendor != AMD_PCI_VENDOR:
|
|
204
|
+
continue
|
|
205
|
+
vram = (ctx.read_text(f"{base}/mem_info_vram_total") or "").strip()
|
|
206
|
+
uevent = ctx.read_text(f"{base}/uevent") or ""
|
|
207
|
+
slot = re.search(r"PCI_SLOT_NAME=(\S+)", uevent)
|
|
208
|
+
found.append(
|
|
209
|
+
{
|
|
210
|
+
"card": card,
|
|
211
|
+
"device_id": (ctx.read_text(f"{base}/device") or "").strip() or None,
|
|
212
|
+
"vram_bytes": int(vram) if vram.isdigit() else None,
|
|
213
|
+
"pci_bus": _norm_bdf(slot.group(1)) if slot else None,
|
|
214
|
+
}
|
|
215
|
+
)
|
|
216
|
+
return found
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# --------------------------------------------------------------------------- merge
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
@dataclass
|
|
223
|
+
class HardwareProbe:
|
|
224
|
+
gpus: list[GpuDevice]
|
|
225
|
+
rocminfo: CommandResult | None = None
|
|
226
|
+
amd_smi: CommandResult | None = None
|
|
227
|
+
sysfs_count: int = 0
|
|
228
|
+
kfd_accessible: bool | None = None
|
|
229
|
+
notes: list[str] = field(default_factory=list)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _device_access(ctx: HostContext) -> bool | None:
|
|
233
|
+
"""Can this user open /dev/kfd and a render node? None when not on Linux."""
|
|
234
|
+
if ctx.system != "Linux":
|
|
235
|
+
return None
|
|
236
|
+
kfd = ctx.path("/dev/kfd")
|
|
237
|
+
if not kfd.exists():
|
|
238
|
+
return False
|
|
239
|
+
return os.access(kfd, os.R_OK | os.W_OK)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def discover_gpus(ctx: HostContext) -> HardwareProbe:
|
|
243
|
+
probe = HardwareProbe(gpus=[])
|
|
244
|
+
smi_entries: list[dict[str, Any]] = []
|
|
245
|
+
if ctx.which("amd-smi"):
|
|
246
|
+
res = ctx.run(["amd-smi", "static", "--json"])
|
|
247
|
+
probe.amd_smi = res
|
|
248
|
+
if res.ok and res.stdout.strip():
|
|
249
|
+
try:
|
|
250
|
+
smi_entries = parse_amd_smi_static(res.stdout)
|
|
251
|
+
except (json.JSONDecodeError, TypeError) as exc:
|
|
252
|
+
probe.notes.append(f"amd-smi output could not be parsed: {exc}")
|
|
253
|
+
elif not res.ok:
|
|
254
|
+
probe.notes.append(res.describe_failure())
|
|
255
|
+
|
|
256
|
+
agents: list[RocminfoAgent] = []
|
|
257
|
+
if ctx.which("rocminfo"):
|
|
258
|
+
res = ctx.run(["rocminfo"])
|
|
259
|
+
probe.rocminfo = res
|
|
260
|
+
if res.ok:
|
|
261
|
+
agents = gpu_agents(parse_rocminfo(res.stdout))
|
|
262
|
+
else:
|
|
263
|
+
probe.notes.append(res.describe_failure())
|
|
264
|
+
|
|
265
|
+
sysfs = sysfs_amd_gpus(ctx) if ctx.system == "Linux" else []
|
|
266
|
+
probe.sysfs_count = len(sysfs)
|
|
267
|
+
probe.kfd_accessible = _device_access(ctx)
|
|
268
|
+
|
|
269
|
+
by_bus: dict[str, GpuDevice] = {}
|
|
270
|
+
ordered: list[GpuDevice] = []
|
|
271
|
+
|
|
272
|
+
def upsert(bus: str | None, idx: int) -> GpuDevice:
|
|
273
|
+
if bus and bus in by_bus:
|
|
274
|
+
return by_bus[bus]
|
|
275
|
+
if not bus and idx < len(ordered):
|
|
276
|
+
return ordered[idx]
|
|
277
|
+
dev = GpuDevice(index=len(ordered), pci_bus=bus, evidence=Evidence.DETECTED)
|
|
278
|
+
ordered.append(dev)
|
|
279
|
+
if bus:
|
|
280
|
+
by_bus[bus] = dev
|
|
281
|
+
return dev
|
|
282
|
+
|
|
283
|
+
for i, e in enumerate(smi_entries):
|
|
284
|
+
d = upsert(e.get("pci_bus"), i)
|
|
285
|
+
d.marketing_name = d.marketing_name or e.get("marketing_name")
|
|
286
|
+
d.vendor = d.vendor or e.get("vendor")
|
|
287
|
+
d.gfx_target = d.gfx_target or e.get("gfx_target")
|
|
288
|
+
d.compute_units = d.compute_units or _safe_int(e.get("compute_units"))
|
|
289
|
+
d.vram_bytes = d.vram_bytes or e.get("vram_bytes")
|
|
290
|
+
d.device_id = d.device_id or e.get("device_id")
|
|
291
|
+
d.uuid = d.uuid or e.get("uuid")
|
|
292
|
+
d.sources.append("amd-smi")
|
|
293
|
+
vid = e.get("vendor_id") or ""
|
|
294
|
+
if (
|
|
295
|
+
vid in ("0x1002", "1002")
|
|
296
|
+
or "amd" in (d.vendor or "").lower()
|
|
297
|
+
or "advanced micro" in (d.vendor or "").lower()
|
|
298
|
+
):
|
|
299
|
+
d.vendor_is_amd = True
|
|
300
|
+
|
|
301
|
+
for i, a in enumerate(agents):
|
|
302
|
+
d = upsert(a.bdf, i)
|
|
303
|
+
d.marketing_name = d.marketing_name or a.marketing_name
|
|
304
|
+
d.vendor = d.vendor or a.vendor
|
|
305
|
+
d.gfx_target = d.gfx_target or a.name
|
|
306
|
+
d.compute_units = d.compute_units or a.compute_units
|
|
307
|
+
d.node_id = a.node
|
|
308
|
+
d.uuid = d.uuid or a.uuid
|
|
309
|
+
if d.vram_bytes is None and a.pool_bytes:
|
|
310
|
+
d.vram_bytes = max(a.pool_bytes)
|
|
311
|
+
d.sources.append("rocminfo")
|
|
312
|
+
if (a.vendor or "").upper() == "AMD":
|
|
313
|
+
d.vendor_is_amd = True
|
|
314
|
+
|
|
315
|
+
for i, s in enumerate(sysfs):
|
|
316
|
+
sd = upsert(s.get("pci_bus"), i) if s.get("pci_bus") else None
|
|
317
|
+
if sd is None:
|
|
318
|
+
continue
|
|
319
|
+
d = sd
|
|
320
|
+
d.vendor_is_amd = True
|
|
321
|
+
d.vendor = d.vendor or "AMD"
|
|
322
|
+
d.device_id = d.device_id or s.get("device_id")
|
|
323
|
+
d.vram_bytes = d.vram_bytes or s.get("vram_bytes")
|
|
324
|
+
d.sources.append("sysfs")
|
|
325
|
+
|
|
326
|
+
for d in ordered:
|
|
327
|
+
d.accessible = probe.kfd_accessible
|
|
328
|
+
probe.gpus = ordered
|
|
329
|
+
return probe
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _safe_int(v: Any) -> int | None:
|
|
333
|
+
try:
|
|
334
|
+
return int(v)
|
|
335
|
+
except (TypeError, ValueError):
|
|
336
|
+
return None
|