From f28d02a17afa1faedb550ef1e30783cede630a18 Mon Sep 17 00:00:00 2001 From: Joseph Magly <1159087+jmagly@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:05:29 -0400 Subject: [PATCH] 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 --- obliteratus/qualification.py | 10 ++++++- tests/test_gpu_qualification.py | 46 ++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/obliteratus/qualification.py b/obliteratus/qualification.py index 5e78b349..9c748be4 100644 --- a/obliteratus/qualification.py +++ b/obliteratus/qualification.py @@ -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 diff --git a/tests/test_gpu_qualification.py b/tests/test_gpu_qualification.py index 6d6f7d58..89775b29 100644 --- a/tests/test_gpu_qualification.py +++ b/tests/test_gpu_qualification.py @@ -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)