Files
OBLITERATUS/obliteratus/qualification.py
Joseph MaglyandClaude Opus 5.5 5f99489b80 fix(qualification): reject malformed topology matrices
Review of f28d02a: rows were accepted by minimum length, so a row missing
its diagonal X could map GPU0->GPU1 as NV12 and satisfy --require-link
nvlink while the reciprocal row said PHB. parse_topology now requires
exactly one complete row per header GPU, an X diagonal, known link tokens
(NV#, PIX, PXB, PHB, NODE, SYS) and symmetric links, and raises
otherwise so diagnostics record an error instead of a false pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:32:50 -04:00

443 lines
22 KiB
Python

"""Portable planning, diagnostics and evidence for ordinary GPU qualification.
This module deliberately imports no accelerator or model runtime. Visibility can
be resolved in a disposable child before selecting UUIDs for the model workers.
"""
from __future__ import annotations
import csv
import hashlib
import io
import json
import math
import os
import re
import subprocess
import tempfile
from pathlib import Path
PHASES = ("load", "baseline", "activations", "direction", "edit", "verify", "save")
def canonical_json(value) -> bytes:
return json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()
def digest_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def file_inventory(directory: Path) -> dict:
return {str(path.relative_to(directory)): {"bytes": path.stat().st_size,
"sha256": digest_file(path)}
for path in sorted(directory.rglob("*")) if path.is_file()}
def parse_gpu_inventory(raw: str) -> list[dict]:
"""Parse nvidia-smi's explicitly requested CSV fields, without ordinal guesses."""
result = []
for row in csv.reader(io.StringIO(raw), skipinitialspace=True):
if not row:
continue
if len(row) != 7:
raise ValueError("expected seven GPU inventory fields")
index, uuid, pci, name, driver, total, free = (value.strip() for value in row)
result.append(dict(physical_ordinal=int(index), uuid=uuid, pci_bus_id=pci,
name=name, driver=driver, total_mib=int(total), free_mib=int(free)))
if len({normalize_uuid(item["uuid"]) for item in result}) != len(result):
raise ValueError("duplicate GPU UUID in inventory")
return result
def parse_topology(raw: str) -> dict[str, dict[str, str]]:
"""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 or with
an incomplete, invalid or asymmetric GPU matrix raises ValueError.
"""
raw = re.sub(r"\x1b\[[0-?]*[ -/]*[@-~]", "", raw)
header = None
result = {}
row_counts = {}
for line in raw.splitlines():
cells = line.split()
if not cells:
continue
if (header is None and re.fullmatch(r"GPU\d+", cells[0])
and ("CPU" in cells or (len(cells) > 1 and cells[1].startswith(("GPU", "NIC"))))):
header = [cell for cell in cells if re.fullmatch(r"GPU\d+", cell)]
elif header and re.fullmatch(r"GPU\d+", cells[0]):
row_counts[cells[0]] = row_counts.get(cells[0], 0) + 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")
if header is not None:
if len(set(header)) != len(header):
raise ValueError("invalid nvidia-smi topology matrix: duplicate GPU in header")
if set(result) - set(header):
raise ValueError("invalid nvidia-smi topology matrix: unexpected GPU row")
for gpu in header:
if row_counts.get(gpu, 0) != 1:
raise ValueError(
f"invalid nvidia-smi topology matrix: expected exactly one row for {gpu}")
if len(result[gpu]) != len(header):
raise ValueError(f"invalid nvidia-smi topology matrix: incomplete row for {gpu}")
if result[gpu][gpu] != "X":
raise ValueError(f"invalid nvidia-smi topology matrix: diagonal for {gpu} must be X")
for peer, token in result[gpu].items():
if peer != gpu and not re.fullmatch(r"NV\d+|PIX|PXB|PHB|NODE|SYS", token):
raise ValueError(
f"invalid nvidia-smi topology matrix: unknown link {gpu}->{peer}: {token}")
for index, gpu in enumerate(header):
for peer in header[index + 1:]:
if result[gpu][peer] != result[peer][gpu]:
raise ValueError(
f"invalid nvidia-smi topology matrix: asymmetric link {gpu}<->{peer}")
return result
def link_kind(token: str | None) -> str:
if token and re.fullmatch(r"NV\d+", token):
return "nvlink"
if token in {"PIX", "PXB", "PHB", "NODE", "SYS"}:
return "pcie"
return "self" if token == "X" else "unknown"
def smi_diagnostics(run=subprocess.run) -> dict:
result = {"inventory": [], "topology": {}, "errors": []}
for key, command in (
("inventory", ["nvidia-smi", "--query-gpu=index,uuid,pci.bus_id,name,driver_version,memory.total,memory.free",
"--format=csv,noheader,nounits"]),
("topology", ["nvidia-smi", "topo", "-m"]),
):
try:
completed = run(command, capture_output=True, text=True, timeout=15, check=True)
result[key] = (parse_gpu_inventory if key == "inventory" else parse_topology)(completed.stdout)
except (OSError, ValueError, subprocess.SubprocessError) as exc:
result["errors"].append(f"{key}: {type(exc).__name__}: {exc}")
return result
def normalize_uuid(value: str | None) -> str:
"""Validate and canonicalize GPU and supported MIG identifiers.
GPU UUIDs use GPU-<lowercase 8-4-4-4-12 hex>, accepting bare UUIDs too.
MIG UUIDs stay distinct as MIG-<lowercase UUID> or the legacy form
MIG-GPU-<lowercase UUID>/<gpu instance>/<compute instance>. Prefixes are
case-insensitive; legacy indices are nonnegative decimal integers and are
returned without leading zeroes. Surrounding whitespace is stripped.
All other inputs raise ValueError.
"""
if not isinstance(value, str) or not value.strip():
raise ValueError("every requested GPU needs a resolved UUID")
value = value.strip().lower()
uuid_pattern = r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
if value.startswith("mig-"):
if re.fullmatch(uuid_pattern, value[4:]):
return "MIG-" + value[4:]
legacy = re.fullmatch(rf"mig-gpu-({uuid_pattern})/([0-9]+)/([0-9]+)", value)
if legacy:
body, gpu_instance, compute_instance = legacy.groups()
return f"MIG-GPU-{body}/{int(gpu_instance)}/{int(compute_instance)}"
raise ValueError("invalid MIG UUID")
if value.startswith("gpu-"):
value = value[4:]
if not value:
raise ValueError("every requested GPU needs a resolved UUID")
if not re.fullmatch(uuid_pattern, value):
raise ValueError("invalid GPU UUID")
return "GPU-" + value
def visible_device_records(properties: list[dict], diagnostics: dict) -> list[dict]:
by_uuid = {normalize_uuid(item["uuid"]): item for item in diagnostics["inventory"]}
result = []
for ordinal, properties_record in enumerate(properties):
uuid = properties_record.get("uuid")
uuid = normalize_uuid(uuid) if uuid else None
physical = by_uuid.get(uuid, {})
result.append({**physical, **properties_record, "uuid": uuid, "visible_ordinal": ordinal,
"physical_ordinal": physical.get("physical_ordinal"),
"pci_bus_id": physical.get("pci_bus_id")})
return result
def topology_links(devices: list[dict], topology: dict) -> list[dict]:
links = []
for index, first in enumerate(devices):
for second in devices[index + 1:]:
token = topology.get(f"GPU{first['physical_ordinal']}", {}).get(
f"GPU{second['physical_ordinal']}")
links.append(dict(source=first["visible_ordinal"], target=second["visible_ordinal"],
path=token, kind=link_kind(token)))
return links
def select_uuids(visible: list[dict], count: int, requested: list[str] | None = None) -> list[str]:
if count < 1 or len(visible) < count:
raise ValueError(f"required {count} visible GPUs; discovered {len(visible)}")
available = [normalize_uuid(item["uuid"]) if item.get("uuid") else None for item in visible]
selected = requested if requested is not None else available[:count]
if len(selected) != count or any(not value for value in selected):
raise ValueError("every requested GPU needs a resolved UUID")
selected = [normalize_uuid(value) for value in selected]
if len(set(selected)) != count:
raise ValueError("requested GPUs must have distinct UUIDs")
if not set(selected).issubset(available):
raise ValueError("requested UUIDs are outside the current visible allocation")
return selected
def normalize_device(value) -> str:
if isinstance(value, int) and not isinstance(value, bool):
return f"cuda:{value}"
value = str(value)
if value.isdigit():
return f"cuda:{value}"
if value in {"cpu", "disk", "meta"} or re.fullmatch(r"cuda:\d+", value):
return value
raise ValueError(f"unresolved placement device {value!r}")
def parameter_device(name: str, device_map: dict) -> str:
candidates = [prefix for prefix in device_map if not prefix or name == prefix or name.startswith(prefix + ".")]
if not candidates:
raise ValueError(f"placement does not cover {name}")
return normalize_device(device_map[max(candidates, key=len)])
def plan_modules(blocks: list[str], other_modules: list[str], devices: list[str],
offload: str = "none", output_module: str | None = None,
tied_parameters: list[list[str]] | None = None) -> dict:
"""Place consecutive complete decoder blocks, including a forced offload block."""
if not devices or len(set(devices)) != len(devices):
raise ValueError("placement needs distinct execution devices")
if offload not in {"none", "cpu", "disk"}:
raise ValueError("offload must be none, cpu or disk")
targets = [normalize_device(device) for device in devices]
if offload != "none" and offload not in targets:
targets.append(offload)
if len(blocks) < len(targets):
raise ValueError("not enough complete decoder blocks to occupy every placement target")
device_map = {module: targets[0] for module in other_modules}
device_map.update({block: targets[index * len(targets) // len(blocks)]
for index, block in enumerate(blocks)})
if output_module:
device_map[output_module] = devices[-1]
for group in tied_parameters or []:
target = min((parameter_device(name, device_map) for name in group), key=targets.index)
for name in group:
device_map[name.rpartition(".")[0]] = target
return device_map
def validate_plan(parameter_sizes: dict[str, int], blocks: list[str], device_map: dict,
requested: list[str], budgets: dict[str, int] | None = None,
tied_parameters: list[list[str]] | None = None) -> dict:
for group in tied_parameters or []:
if len({parameter_device(name, device_map) for name in group}) != 1:
raise ValueError(f"tied parameters must share one placement device: {group}")
totals = {}
block_devices = {}
for name, size in parameter_sizes.items():
device = parameter_device(name, device_map)
if device not in {*requested, "cpu", "disk"}:
raise ValueError(f"placement uses unrequested device {device}")
totals[device] = totals.get(device, 0) + size
for block in blocks:
if name.startswith(block + "."):
previous = block_devices.setdefault(block, device)
if previous != device:
raise ValueError(f"decoder block {block} must remain complete on one device")
break
for device in requested:
if not totals.get(device):
raise ValueError(f"placement leaves requested device {device} empty")
for device, budget in (budgets or {}).items():
if totals.get(device, 0) > budget:
raise ValueError(f"placement exceeds {device} free-memory budget: {totals[device]} > {budget}")
return {"bytes_by_device": totals, "block_devices": block_devices,
"device_map": {name: normalize_device(value) for name, value in device_map.items()}}
def enforce_occupancy(model, requested: list[str], blocks: list[str]) -> dict:
"""Count real resident storage; hooks and hf_device_map are not occupancy."""
modules = {}
block_devices = {}
for name, tensor in model.named_parameters():
if tensor.numel() == 0:
continue
device = normalize_device(tensor.device)
module = name.rpartition(".")[0]
owned = modules.setdefault(device, {})
owned[module] = owned.get(module, 0) + tensor.numel() * tensor.element_size()
for block in blocks:
if name.startswith(block + "."):
previous = block_devices.setdefault(block, device)
if previous != device:
raise ValueError(f"realized decoder block {block} is split across devices")
break
for device in requested:
if not modules.get(device):
raise ValueError(f"requested device {device} holds no resident modules")
extra = set(modules) - {*requested, "cpu", "meta"}
if extra:
raise ValueError(f"model occupied unrequested devices: {sorted(extra)}")
return {"modules": modules, "block_devices": block_devices}
def _nonnegative(value) -> bool:
return type(value) in {int, float} and math.isfinite(value) and value >= 0
def validate_capacity_evidence(plan: dict, *, allow_unbounded_export: bool = False) -> None:
if (not isinstance(plan, dict) or plan.get("status") != "feasible"
or plan.get("claim") != "config_only_capacity_estimate"
or plan.get("runtime_check_required") is not True):
raise ValueError("evidence lacks an admitted config-only capacity estimate")
export = plan.get("export", {})
fallback = allow_unbounded_export and export.get("operator_allowed_fallback") is True
if not fallback and (export.get("eligible") is not True or export.get("bounded") is not True):
raise ValueError("capacity evidence lacks bounded export eligibility")
snapshot = plan.get("snapshot", {})
if snapshot.get("status") not in {"disabled_with_reason", "disk_backed"} or not snapshot.get("reason"):
raise ValueError("capacity evidence lacks an explicit snapshot policy")
for phase in (*PHASES, "reload"):
rows = plan.get("phases", {}).get(phase, {})
if not rows or not {"cpu", "disk"} <= rows.keys():
raise ValueError(f"capacity evidence lacks {phase} host/disk budgets")
for row in rows.values():
components = row.get("components", {})
if (not components or not all(type(value) is int and value >= 0 for value in components.values())
or type(row.get("peak_bytes")) is not int
or type(row.get("budget_bytes")) is not int
or row["peak_bytes"] != sum(components.values())
or row.get("headroom_bytes") != row["budget_bytes"] - row["peak_bytes"]
or row["headroom_bytes"] < 0):
raise ValueError(f"invalid capacity arithmetic or shortfall for {phase}")
def validate_evidence(payload: dict) -> None:
if not isinstance(payload, dict):
raise ValueError("evidence payload must be an object")
if payload.get("status") not in {"passed", "failed"}:
raise ValueError("evidence needs a terminal status")
for field in ("run_id", "started_at", "configuration", "identity", "workers"):
if field not in payload:
raise ValueError(f"evidence missing {field}")
if any(not isinstance(payload[field], dict) for field in ("configuration", "identity", "workers")):
raise ValueError("evidence configuration, identity and workers must be objects")
if payload["status"] != "passed":
if not payload.get("error"):
raise ValueError("failed evidence needs an error")
return
identity = payload["identity"]
if (not identity.get("git_sha") or not identity.get("source_files")
or identity.get("source_sha256") != hashlib.sha256(canonical_json(identity["source_files"])).hexdigest()):
raise ValueError("evidence lacks a valid implementation fingerprint")
prepare = payload["workers"].get("prepare", {})
if prepare.get("status") != "passed":
raise ValueError("required preparation did not pass")
if "capacity_plan" in payload["configuration"]:
capacity = payload.get("capacity_plan")
validate_capacity_evidence(capacity, allow_unbounded_export=payload["configuration"].get("allow_unbounded_export", False))
if capacity != payload["configuration"]["capacity_plan"] or capacity != prepare.get("capacity_plan"):
raise ValueError("frozen capacity plan differs across evidence and workers")
runtime = prepare.get("runtime", {})
if not runtime.get("python") or not runtime.get("torch") or not runtime.get("packages"):
raise ValueError("evidence lacks runtime package identities")
checkpoint = payload.get("source_checkpoint")
if not checkpoint or payload.get("source_checkpoint_sha256") != hashlib.sha256(canonical_json(checkpoint)).hexdigest():
raise ValueError("source checkpoint inventory digest is missing or invalid")
reference = payload.get("reference_policy", {}).get("selected")
if reference not in {"cpu", "cuda:0", "none"}:
raise ValueError("evidence lacks an explicit reference decision")
if reference != "none" and (payload["workers"].get("reference", {}).get("status") != "passed"
or payload.get("reference_comparison", {}).get("passed") is not True):
raise ValueError("selected reference did not pass")
requested = payload["configuration"].get("requested_devices", [])
if requested:
selected = [normalize_uuid(value) for value in payload.get("granted_uuids", [])]
mapping = payload.get("device_mapping", [])
if (len(selected) != len(requested)
or len(set(selected)) != len(selected)
or [normalize_uuid(record.get("uuid")) for record in mapping] != selected
or [record.get("visible_ordinal") for record in mapping] != list(range(len(requested)))):
raise ValueError("evidence lacks the granted UUID/ordinal mapping")
if "topology_links" not in payload:
raise ValueError("evidence lacks topology diagnostics")
edit = payload["workers"].get("edit", {})
reload = payload["workers"].get("reload", {})
if "capacity_plan" in payload["configuration"]:
bounded = edit.get("bounded_export", {})
fallback = (payload["configuration"].get("allow_unbounded_export") is True
and payload["capacity_plan"]["export"].get("operator_allowed_fallback") is True)
if not fallback and (bounded.get("eligible") is not True or bounded.get("bounded") is not True):
raise ValueError("edit worker lacks realized bounded export evidence")
if edit.get("snapshot_policy") != payload["capacity_plan"]["snapshot"]:
raise ValueError("edit worker snapshot policy differs from the capacity plan")
for worker, required in ((edit, PHASES), (reload, ("reload",))):
if worker.get("status") != "passed":
raise ValueError("required worker did not pass")
if type(worker.get("pid")) is not int or worker["pid"] <= 0:
raise ValueError("worker PID is missing or invalid")
if requested and [normalize_uuid(record.get("uuid"))
for record in worker.get("hardware", {}).get("devices", [])] != selected:
raise ValueError("worker UUIDs differ from the granted allocation")
for phase in required:
metrics = worker.get("phases", {}).get(phase, {})
for field in ("seconds", "host_ram_peak_sampled_bytes", "disk_peak_sampled_bytes"):
if not _nonnegative(metrics.get(field)):
raise ValueError(f"invalid or missing {phase} metric {field}")
if metrics.get("sampling_errors"):
raise ValueError(f"incomplete {phase} telemetry")
for device in payload["configuration"].get("requested_devices", []):
gpu = metrics.get("gpu", {}).get(device, {})
if not all(_nonnegative(gpu.get(field)) for field in
("allocated_peak_bytes", "reserved_peak_bytes", "used_peak_sampled_bytes")):
raise ValueError(f"missing {phase} GPU peak for {device}")
if edit.get("pid") == reload.get("pid") or payload.get("edit_worker_exited_before_reload") is not True:
raise ValueError("reload must run after the edit process exits")
if reload.get("comparison", {}).get("passed") is not True:
raise ValueError("reload parity did not pass")
for device in payload["configuration"].get("requested_devices", []):
if not edit.get("occupancy", {}).get("modules", {}).get(device):
raise ValueError(f"evidence has no occupancy for {device}")
if not reload.get("occupancy", {}).get("modules", {}).get(device):
raise ValueError(f"reload evidence has no occupancy for {device}")
def read_evidence(path: Path) -> dict:
envelope = json.loads(path.read_text())
if (envelope.get("schema_version") != 1
or envelope.get("sha256") != hashlib.sha256(canonical_json(envelope.get("payload"))).hexdigest()):
raise ValueError("evidence envelope digest or schema is invalid")
validate_evidence(envelope["payload"])
return envelope["payload"]
def write_evidence(directory: Path, payload: dict) -> Path:
"""Publish a content-addressed, read-only JSON envelope without replacement."""
validate_evidence(payload)
digest = hashlib.sha256(canonical_json(payload)).hexdigest()
envelope = {"schema_version": 1, "sha256": digest, "payload": payload}
path = directory / f"evidence-{digest}.json"
descriptor, temporary = tempfile.mkstemp(prefix=".evidence-", dir=directory)
try:
with os.fdopen(descriptor, "wb") as stream:
stream.write(canonical_json(envelope) + b"\n")
stream.flush()
os.fsync(stream.fileno())
os.fchmod(stream.fileno(), 0o444)
os.link(temporary, path)
finally:
Path(temporary).unlink(missing_ok=True)
return path