fix(qualification): parse nvidia-smi topology through ANSI escapes

nvidia-smi topo -m underlines its header with ANSI SGR escapes even when
piped (\x1b[4mGPU0 ...\x1b[0m). parse_topology matched cells[0] against
GPU\d+, never found the header and silently returned {}, so every link was
"unknown" and --require-link nvlink failed on a real NV12 pair (2x A100).
Strip ANSI CSI sequences before parsing, and raise on nonempty output with
no recognizable header so smi_diagnostics records an error instead of an
empty topology.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Joseph MaglyandClaude Opus 5.5 committed 2026-10-22 14:05:29 -04:00
1 parent 368660ecc3
commit f28d02a17a
2 files changed
+54 -2

No files matched your search

+9 -1
View File
@@ -55,7 +55,13 @@ def parse_gpu_inventory(raw: str) -> list[dict]:
def parse_topology(raw: str) -> dict[str, dict[str, str]]:
"""Retain NVLink counts and PCIe path types; ignore affinity/legend columns."""
"""Retain NVLink counts and PCIe path types; ignore affinity/legend columns.
ANSI CSI sequences are ignored. Empty or whitespace-only output returns an
empty mapping. Nonempty output without a recognizable GPU header raises
ValueError.
"""
raw = re.sub(r"\x1b\[[0-?]*[ -/]*[@-~]", "", raw)
header = None
result = {}
for line in raw.splitlines():
@@ -67,6 +73,8 @@ def parse_topology(raw: str) -> dict[str, dict[str, str]]:
header = [cell for cell in cells if re.fullmatch(r"GPU\d+", cell)]
elif header and cells[0] in header and len(cells) >= len(header) + 1:
result[cells[0]] = dict(zip(header, cells[1:len(header) + 1]))
if header is None and raw.strip():
raise ValueError("nvidia-smi topology header not found")
return result
+45 -1
View File
@@ -31,6 +31,17 @@ Legend:
"""
CAPTURED_TOPOLOGY = (
"\t\x1b[4mGPU0\tGPU1\tGPU2\tGPU3\tCPU Affinity\tNUMA Affinity\tGPU NUMA ID\x1b[0m\n"
"GPU0\t X \tNODE\tNV12\tNODE\t0-47\t0\t\tN/A\n"
"GPU1\tNODE\t X \tPHB\tNODE\t0-47\t0\t\tN/A\n"
"GPU2\tNV12\tPHB\t X \tNODE\t48-95\t1\t\tN/A\n"
"GPU3\tNODE\tNODE\tNODE\t X \t48-95\t1\t\tN/A\n"
"Legend:\n"
" NV# = Connection traversing a bonded set of # NVLinks\n"
)
FIRST_UUID = "3538758b-6ba7-fa1b-a80c-e315a606c3f0"
SECOND_UUID = "9e1069b2-f073-4ab7-8066-37a04bf9e112"
THIRD_UUID = "aa3b656a-138f-4682-b22d-dff0b43006bc"
@@ -126,12 +137,45 @@ def test_topology_preserves_nvlink_and_pcie_paths_with_visible_reordering():
assert qualification.link_kind("X") == "self"
@pytest.mark.parametrize("ansi", [True, False], ids=["ansi-sgr", "plain"])
def test_captured_topology_preserves_nvlink_with_or_without_ansi(ansi):
raw = CAPTURED_TOPOLOGY
if not ansi:
raw = raw.replace("\x1b[4m", "").replace("\x1b[0m", "")
topology = qualification.parse_topology(raw)
assert topology == {
"GPU0": {"GPU0": "X", "GPU1": "NODE", "GPU2": "NV12", "GPU3": "NODE"},
"GPU1": {"GPU0": "NODE", "GPU1": "X", "GPU2": "PHB", "GPU3": "NODE"},
"GPU2": {"GPU0": "NV12", "GPU1": "PHB", "GPU2": "X", "GPU3": "NODE"},
"GPU3": {"GPU0": "NODE", "GPU1": "NODE", "GPU2": "NODE", "GPU3": "X"},
}
assert topology["GPU0"]["GPU2"] == "NV12"
assert qualification.link_kind(topology["GPU0"]["GPU2"]) == "nvlink"
@pytest.mark.parametrize("raw", ["", " \t\n"])
def test_empty_topology_returns_empty_mapping(raw):
assert qualification.parse_topology(raw) == {}
@pytest.mark.parametrize("raw", ["unavailable", "GPU0\t X \tNODE\tNV12\tNODE\t0-47\t0\t\tN/A\n"])
def test_topology_without_header_raises_and_records_diagnostics_error(raw):
with pytest.raises(ValueError, match="^nvidia-smi topology header not found$"):
qualification.parse_topology(raw)
inventory = f"0,GPU-{FIRST_UUID},pci,Example GPU,580.10,81920,40960\n"
run = Mock(side_effect=[SimpleNamespace(stdout=inventory), SimpleNamespace(stdout=raw)])
diagnostics = qualification.smi_diagnostics(run=run)
assert diagnostics["inventory"] == qualification.parse_gpu_inventory(inventory)
assert diagnostics["topology"] == {}
assert diagnostics["errors"] == ["topology: ValueError: nvidia-smi topology header not found"]
assert run.call_count == 2
def test_missing_diagnostics_never_guesses_uuid_to_physical_ordinal():
records = qualification.visible_device_records([{"uuid": "GPU-" + SECOND_UUID}], {"inventory": []})
assert records[0]["physical_ordinal"] is None
assert qualification.topology_links(records * 2, {})[0]["kind"] == "unknown"
assert qualification.parse_topology("GPU0 CPU Affinity\nGPU0 X 0-3") == {"GPU0": {"GPU0": "X"}}
assert qualification.parse_topology("unavailable") == {}
assert qualification.parse_topology("GPU2 GPU5 CPU Affinity\nGPU2 X NV4 0-7\nGPU5 NV4 X 0-7")["GPU2"]["GPU5"] == "NV4"
run = Mock(side_effect=FileNotFoundError("nvidia-smi unavailable"))
diagnostics = qualification.smi_diagnostics(run=run)