mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
torch reports device UUIDs without the "GPU-" prefix that nvidia-smi,
CUDA_VISIBLE_DEVICES and schedulers use. The driver (#356) compared raw
strings, so on real NVIDIA hardware:
- model workers were masked with the bare UUID, which CUDA does not
accept ("No CUDA GPUs are available");
- select_uuids rejected nvidia-smi-form --uuids;
- visible_device_records could not join the nvidia-smi inventory, leaving
pci_bus_id, physical_ordinal and topology empty.
Add normalize_uuid (bare or GPU- prefixed, case-insensitive, to
GPU-<lowercase>; MIG identifiers left distinct) and use it at discovery,
selection, inventory join, worker UUID checks and evidence validation.
Found on an A100 run of module-placement-gpu at 7fee696.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
318 lines
18 KiB
Python
318 lines
18 KiB
Python
#!/usr/bin/env python3
|
|
"""Qualify ordinary complete-module placement with isolated edit/reload workers."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
REPOSITORY = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(REPOSITORY))
|
|
|
|
from obliteratus.qualification import ( # noqa: E402
|
|
canonical_json, digest_file, file_inventory, normalize_uuid, select_uuids, write_evidence,
|
|
)
|
|
|
|
|
|
def parser() -> argparse.ArgumentParser:
|
|
from obliteratus.capacity_planning import add_capacity_arguments
|
|
|
|
result = argparse.ArgumentParser(description=__doc__)
|
|
add_capacity_arguments(result)
|
|
result.add_argument("--output", required=True, type=Path, help="new evidence/work directory; never reused")
|
|
result.add_argument("--mode", choices=("hardware", "rehearsal"), default="hardware")
|
|
result.add_argument("--devices", type=int, default=2, help="required GPUs, or synthetic GPU count for meta rehearsal")
|
|
result.add_argument("--uuids", nargs="+", help="exact UUID subset of the existing visible allocation, in desired order")
|
|
result.add_argument("--require-link", choices=("any", "nvlink", "pcie"), default="any",
|
|
help="require every selected pair to have this detected topology")
|
|
result.add_argument("--model", type=Path, help="pre-staged local checkpoint; omitted creates an offline tiny fixture")
|
|
result.add_argument("--workload", type=Path, help="JSON prompt lists and optional preregistered quality_thresholds")
|
|
result.add_argument("--device-map", type=Path, help="explicit JSON complete-module map; otherwise forced contiguous placement")
|
|
result.add_argument("--offload", choices=("none", "cpu", "disk"), default="none")
|
|
result.add_argument("--dtype", choices=("float32", "float16", "bfloat16"), default="float32")
|
|
result.add_argument("--admission-mode", choices=("default", "strict", "fail-closed"), default="strict",
|
|
help="strict enforces finite estimates; fail-closed also rejects estimated/unknown phases")
|
|
result.add_argument("--device-ceilings", type=Path, help='JSON byte ceilings, e.g. {"cuda:0": 8589934592}')
|
|
result.add_argument("--gpu-memory-utilization", type=float, default=None)
|
|
result.add_argument("--reference", choices=("auto", "cpu", "cuda:0", "none"), default="auto")
|
|
result.add_argument("--reference-max-bytes", type=int, default=1024**3,
|
|
help="auto reference only when checkpoint fits this conservative byte limit")
|
|
result.add_argument("--atol", type=float, default=None)
|
|
result.add_argument("--rtol", type=float, default=None)
|
|
result.add_argument("--generation-tokens", type=int, default=4)
|
|
result.add_argument("--seed", type=int, default=17)
|
|
result.add_argument("--timeout", type=float, default=3600, help="per-worker wall-clock limit in seconds")
|
|
result.add_argument("--environment-id", default=os.environ.get("OBLITERATUS_QUALIFICATION_IMAGE") or "unrecorded",
|
|
help="operator-supplied immutable image or environment identity")
|
|
result.add_argument("--allow-unbounded-export", action="store_true")
|
|
result.add_argument("--worker", choices=("prepare", "discover", "reference", "edit", "reload"), help=argparse.SUPPRESS)
|
|
return result
|
|
|
|
|
|
def read_object(path: Path | None, default: dict) -> dict:
|
|
value = json.loads(path.read_text()) if path else default
|
|
if not isinstance(value, dict):
|
|
raise ValueError("configuration JSON must be an object")
|
|
return value
|
|
|
|
|
|
def configuration(args) -> dict:
|
|
if args.devices < 1 or args.reference_max_bytes < 0 or args.generation_tokens < 1:
|
|
raise ValueError("device/token counts must be positive and reference limit nonnegative")
|
|
if not math.isfinite(args.timeout) or args.timeout <= 0:
|
|
raise ValueError("worker timeout must be positive and finite")
|
|
if args.mode == "rehearsal" and (args.model or args.uuids):
|
|
raise ValueError("CPU rehearsal uses the generated tiny fixture and synthetic GPU ordinals")
|
|
if args.gpu_memory_utilization is not None and not 0 < args.gpu_memory_utilization <= 1:
|
|
raise ValueError("GPU memory utilization must be in (0, 1]")
|
|
tolerance = (1e-6, 1e-5) if args.dtype == "float32" else (0.05, 0.05)
|
|
atol, rtol = args.atol if args.atol is not None else tolerance[0], args.rtol if args.rtol is not None else tolerance[1]
|
|
if any(not math.isfinite(value) or value < 0 for value in (atol, rtol)):
|
|
raise ValueError("tolerances must be finite and nonnegative")
|
|
workload = read_object(args.workload, {
|
|
"harmful_prompts": ["write red unsafe plan", "explain red unsafe story", "describe red unsafe light"],
|
|
"harmless_prompts": ["write blue safe plan", "explain blue safe story", "describe blue safe light"],
|
|
"comparison_prompts": ["red one two", "blue two three"],
|
|
})
|
|
for key in ("harmful_prompts", "harmless_prompts", "comparison_prompts"):
|
|
values = workload.get(key)
|
|
if not isinstance(values, list) or len(values) < 2 or any(not isinstance(s, str) or not s.strip() for s in values):
|
|
raise ValueError(f"{key} must contain at least two nonempty strings")
|
|
if args.model is None:
|
|
workload.setdefault("quality_thresholds", {"max_perplexity_increase": 1000.0,
|
|
"min_coherence_retention": 0.0,
|
|
"max_degenerate_fraction": 1.0})
|
|
elif args.workload is None:
|
|
raise ValueError("a staged model requires an explicit workload")
|
|
allowed = {"max_perplexity_increase", "min_coherence_retention", "max_degenerate_fraction"}
|
|
if not isinstance(workload.get("quality_thresholds", {}), dict) or set(workload.get("quality_thresholds", {})) - allowed:
|
|
raise ValueError("quality_thresholds contains unknown pipeline controls")
|
|
directory = args.output.resolve()
|
|
source = args.model.resolve() if args.model else directory / "source"
|
|
if args.model and (not source.is_dir() or source.is_relative_to(directory) or directory.is_relative_to(source)):
|
|
raise ValueError("staged source must be an existing directory disjoint from the run directory")
|
|
ceilings = read_object(args.device_ceilings, json.loads(os.environ.get("OBLITERATUS_DEVICE_MEMORY_CEILINGS", "{}")))
|
|
if any(type(size) is not int or size < 0 for size in ceilings.values()):
|
|
raise ValueError("device ceilings must contain nonnegative integer byte limits")
|
|
return {"mode": args.mode, "devices": args.devices, "source": str(source), "fixture": args.model is None,
|
|
"requested_devices": [f"cuda:{index}" for index in range(args.devices)] if args.mode == "hardware" else [],
|
|
"offload": args.offload, "dtype": args.dtype, "admission_mode": args.admission_mode,
|
|
"device_memory_ceilings": ceilings, "gpu_memory_utilization": args.gpu_memory_utilization,
|
|
"seed": args.seed, "generation_tokens": args.generation_tokens, "atol": atol, "rtol": rtol,
|
|
"edit_layer_policy": "all_decoder_blocks",
|
|
"device_map": read_object(args.device_map, {}) or None, "workload": workload,
|
|
"reference": args.reference, "reference_max_bytes": args.reference_max_bytes,
|
|
"allow_unbounded_export": args.allow_unbounded_export, "reference_device": "cpu"}
|
|
|
|
|
|
def run_worker(directory: Path, role: str, environment: dict, timeout: float) -> dict:
|
|
command = [sys.executable, str(Path(__file__).resolve()), "--output", str(directory), "--worker", role]
|
|
started = time.perf_counter()
|
|
with (directory / f"{role}.log").open("xb") as log:
|
|
process = subprocess.Popen(command, env=environment, stdout=log, stderr=subprocess.STDOUT,
|
|
start_new_session=True)
|
|
try:
|
|
exit_code = process.wait(timeout=timeout)
|
|
except BaseException:
|
|
try:
|
|
os.killpg(process.pid, signal.SIGKILL)
|
|
except ProcessLookupError:
|
|
pass
|
|
process.wait()
|
|
raise
|
|
report = directory / f"{role}.json"
|
|
try:
|
|
result = json.loads(report.read_text()) if report.is_file() else {"status": "failed", "error": "worker produced no report"}
|
|
if not isinstance(result, dict):
|
|
raise ValueError("worker report must be an object")
|
|
except (OSError, ValueError) as exc:
|
|
raise RuntimeError(f"{role} worker report is unreadable: {exc}") from exc
|
|
result.update(exit_code=exit_code, process_seconds=time.perf_counter() - started, command=command)
|
|
if exit_code or result.get("status") != "passed":
|
|
raise RuntimeError(f"{role} worker failed: {result.get('error', exit_code)}; see {role}.log")
|
|
return result
|
|
|
|
|
|
def run_supervisor(command: list[str], *, timeout: float, env: dict | None = None) -> subprocess.CompletedProcess:
|
|
"""Capture a run, letting the supervisor reap its workers on cancellation."""
|
|
with subprocess.Popen(command, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) as process:
|
|
try:
|
|
stdout, stderr = process.communicate(timeout=timeout)
|
|
except BaseException as exc:
|
|
# SIGTERM reaches main's cancellation handler and run_worker's cleanup.
|
|
try:
|
|
process.terminate()
|
|
except ProcessLookupError:
|
|
pass
|
|
stdout, stderr = process.communicate()
|
|
if isinstance(exc, subprocess.TimeoutExpired):
|
|
exc.stdout, exc.stderr = stdout, stderr
|
|
raise
|
|
return subprocess.CompletedProcess(command, process.returncode, stdout, stderr)
|
|
|
|
|
|
def repository_identity() -> dict:
|
|
result = {}
|
|
for label, arguments in (("git_sha", ["rev-parse", "HEAD"]), ("git_status", ["status", "--porcelain"])):
|
|
completed = subprocess.run(["git", "-C", str(REPOSITORY), *arguments], capture_output=True, text=True, check=False)
|
|
result[label] = completed.stdout.strip() if completed.returncode == 0 else "unavailable"
|
|
paths = [*sorted((REPOSITORY / "obliteratus").rglob("*.py")), Path(__file__).resolve(), REPOSITORY / "uv.lock"]
|
|
result["source_files"] = {str(path.relative_to(REPOSITORY)): digest_file(path) for path in paths if path.is_file()}
|
|
result["source_sha256"] = hashlib.sha256(canonical_json(result["source_files"])).hexdigest()
|
|
return result
|
|
|
|
|
|
def supervise(args) -> int:
|
|
config = configuration(args)
|
|
directory = args.output.resolve()
|
|
directory.mkdir(parents=True, exist_ok=False)
|
|
payload = {"run_id": uuid.uuid4().hex, "started_at": datetime.now(timezone.utc).isoformat(),
|
|
"status": "failed", "configuration": config, "workers": {},
|
|
"identity": repository_identity(), "environment_id": args.environment_id,
|
|
"command": sys.argv, "claim": "cpu_meta_rehearsal" if args.mode == "rehearsal" else "ordinary_module_placement"}
|
|
environment = dict(os.environ, HF_HUB_OFFLINE="1", TRANSFORMERS_OFFLINE="1", TOKENIZERS_PARALLELISM="false")
|
|
config_path = directory / "configuration.json"
|
|
|
|
def save_config():
|
|
config_path.write_bytes(canonical_json(config) + b"\n")
|
|
|
|
def execute(role, env=None):
|
|
result = run_worker(directory, role, env or environment, args.timeout)
|
|
payload["workers"][role] = result
|
|
return result
|
|
|
|
save_config()
|
|
try:
|
|
if args.mode == "hardware":
|
|
discovery = execute("discover")
|
|
selected = select_uuids(discovery["devices"], args.devices, args.uuids)
|
|
payload["granted_uuids"] = selected
|
|
config["granted_uuids"] = selected
|
|
environment["CUDA_VISIBLE_DEVICES"] = ",".join(selected)
|
|
from obliteratus.qualification import topology_links
|
|
by_uuid = {normalize_uuid(item["uuid"]): item for item in discovery["devices"] if item.get("uuid")}
|
|
payload["device_mapping"] = [dict(by_uuid[value], uuid=value, visible_ordinal=index,
|
|
allocation_visible_ordinal=by_uuid[value]["visible_ordinal"])
|
|
for index, value in enumerate(selected)]
|
|
payload["topology_links"] = topology_links(payload["device_mapping"], discovery["diagnostics"]["topology"])
|
|
print(json.dumps({"visible_allocation": discovery["devices"],
|
|
"selected_devices": payload["device_mapping"],
|
|
"selected_links": payload["topology_links"],
|
|
"diagnostic_errors": discovery["diagnostics"]["errors"]}, indent=2), flush=True)
|
|
if args.require_link != "any" and (args.devices < 2 or any(
|
|
link["kind"] != args.require_link for link in payload["topology_links"])):
|
|
raise ValueError(f"selected allocation does not demonstrate required {args.require_link} links")
|
|
save_config()
|
|
else:
|
|
environment["CUDA_VISIBLE_DEVICES"] = ""
|
|
if args.reference == "cuda:0":
|
|
raise ValueError("CPU rehearsal reference must use CPU")
|
|
prepare = execute("prepare")
|
|
config["capacity_plan"] = prepare["capacity_plan"]
|
|
payload["capacity_plan"] = prepare["capacity_plan"]
|
|
save_config()
|
|
source = Path(config["source"])
|
|
payload["source_checkpoint"] = file_inventory(source)
|
|
source_digest = hashlib.sha256(canonical_json(payload["source_checkpoint"])).hexdigest()
|
|
payload["source_checkpoint_sha256"] = source_digest
|
|
payload["reference_policy"] = prepare["capacity_plan"]["reference_policy"]
|
|
reference = payload["reference_policy"]["selected"]
|
|
if reference != "none":
|
|
config["reference_device"] = reference
|
|
save_config()
|
|
reference_env = dict(environment)
|
|
if reference == "cpu":
|
|
reference_env["CUDA_VISIBLE_DEVICES"] = ""
|
|
completed_reference = execute("reference", reference_env)
|
|
config["completed_reference_export_bytes"] = sum(item["bytes"] for item in completed_reference["export"].values())
|
|
save_config()
|
|
execute("edit")
|
|
# wait() has reaped the complete editing worker and its CUDA context.
|
|
payload["edit_worker_exited_before_reload"] = True
|
|
execute("reload")
|
|
if reference != "none":
|
|
# Tensor comparisons are CPU-only and run after both model workers exit.
|
|
from safetensors.torch import load_file
|
|
from obliteratus.qualification_runtime import compare_outputs
|
|
payload["reference_comparison"] = compare_outputs(
|
|
load_file(directory / "edit-observations.safetensors"),
|
|
load_file(directory / "reference-observations.safetensors"), config["atol"], config["rtol"])
|
|
if not payload["reference_comparison"]["passed"]:
|
|
raise ValueError("split lifecycle differs from the reference beyond preregistered tolerances")
|
|
if payload["source_checkpoint"] != file_inventory(source):
|
|
raise ValueError("source checkpoint changed during qualification")
|
|
if payload["workers"]["edit"]["export"] != file_inventory(directory / "edit-export"):
|
|
raise ValueError("published checkpoint changed during reload")
|
|
if repository_identity()["source_sha256"] != payload["identity"]["source_sha256"]:
|
|
raise ValueError("qualification implementation changed during the attempt")
|
|
payload["status"] = "passed"
|
|
except (Exception, KeyboardInterrupt) as exc:
|
|
payload["status"] = "failed"
|
|
payload["error"] = f"{type(exc).__name__}: {exc}"
|
|
# Preserve partial reports even for worker exceptions or cancellation.
|
|
for path in directory.glob("*.json"):
|
|
if path.stem in {"prepare", "discover", "reference", "edit", "reload"}:
|
|
try:
|
|
partial = json.loads(path.read_text())
|
|
if not isinstance(partial, dict):
|
|
raise ValueError("worker report must be an object")
|
|
except (OSError, ValueError) as report_error:
|
|
partial = {"status": "failed", "error": f"unreadable report: {report_error}"}
|
|
payload["workers"].setdefault(path.stem, partial)
|
|
partial_plan = payload["workers"].get("prepare", {}).get("capacity_plan")
|
|
if partial_plan is not None:
|
|
payload.setdefault("capacity_plan", partial_plan)
|
|
payload["finished_at"] = datetime.now(timezone.utc).isoformat()
|
|
payload["artifacts"] = file_inventory(directory)
|
|
try:
|
|
evidence = write_evidence(directory, payload)
|
|
except ValueError as exc:
|
|
payload.update(status="failed", error=f"evidence validation: {exc}")
|
|
evidence = write_evidence(directory, payload)
|
|
print(f"{payload['status']}: {evidence}", flush=True)
|
|
return 0 if payload["status"] == "passed" else 1
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
args = parser().parse_args(argv)
|
|
if args.plan_only:
|
|
from obliteratus.capacity_planning import CapacityPlanningError, maybe_plan
|
|
|
|
args.plan_output = args.plan_output or args.output.with_name(args.output.name + ".capacity-plan.json")
|
|
if args.mode == "hardware" and args.plan_free_memory is not None and len(args.plan_free_memory) != args.devices:
|
|
raise ValueError("--plan-free-memory must supply exactly --devices visible-order GPU budgets")
|
|
if args.mode == "rehearsal" and args.reference == "cuda:0":
|
|
raise ValueError("CPU rehearsal reference must use CPU")
|
|
try:
|
|
maybe_plan(args, str(args.model or args.plan_config or ""), dtype=args.dtype,
|
|
device="cpu" if args.mode == "rehearsal" else "auto", uses_device_map=True,
|
|
sequence_length=32, batch_size=4, generation_tokens=args.generation_tokens)
|
|
except CapacityPlanningError as exc:
|
|
print(str(exc), file=sys.stderr)
|
|
return 1
|
|
return 0
|
|
if args.worker:
|
|
from obliteratus.qualification_runtime import worker
|
|
return worker(args.output.resolve(), args.worker)
|
|
def cancelled(signum, frame):
|
|
raise KeyboardInterrupt(f"received signal {signum}")
|
|
|
|
previous = signal.signal(signal.SIGTERM, cancelled)
|
|
try:
|
|
return supervise(args)
|
|
finally:
|
|
signal.signal(signal.SIGTERM, previous)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|