Virtual-LPU / lpu /cli.py
iparaco's picture
Upload 118 files
e59bd9d verified
Raw History Blame
10.2 kB
"""LPU command line interface.
python -m lpu <command> [args]
Commands:
run [args...] lpu-run: launch a project under LPU supervision
doctor hardware + environment diagnosis
bench [--tier ...] A/B/C benchmark (baseline vs manual vs LPU)
audit <path> audit a legacy LPU project tree
start start the background controller (foreground)
stop request graceful controller stop
status controller + sessions + GPU snapshot
config [show|validate] configuration inspection
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from typing import List, Optional
def _build_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(prog="lpu", description="LPU Virtual Runtime V3")
sub = p.add_subparsers(dest="cmd")
pr = sub.add_parser("run", help="launch a project: lpu run python train.py")
pr.add_argument("argv", nargs=argparse.REMAINDER, help="command to launch")
pr.add_argument("--python", default=None, help="target interpreter")
pr.add_argument("--cwd", default=None)
pr.add_argument("--no-hooks", action="store_true", help="no PYTHONPATH bootstrap")
pr.add_argument("--intercept", action="store_true",
help="enable level-2 guarded interception (needs config too)")
d = sub.add_parser("doctor", help="diagnose hardware + environment")
d.add_argument("--json", action="store_true", help="machine-readable output")
b = sub.add_parser("bench", help="A/B/C benchmark")
b.add_argument("--tier", default=None, choices=["quick", "extended"])
b.add_argument("--variants", nargs="*", default=None,
help="subset e.g. a_baseline c_lpu")
b.add_argument("--out", default=None, help="report output dir")
a = sub.add_parser("audit", help="audit a legacy LPU tree (read-only)")
a.add_argument("path", help="path to the legacy project")
a.add_argument("--out", default=None, help="write audit markdown here")
sub.add_parser("start", help="start background controller (foreground mode)")
s = sub.add_parser("stop", help="gracefully stop the controller")
sub.add_parser("status", help="show controller + session status")
c = sub.add_parser("config", help="configuration utilities")
c.add_argument("action", nargs="?", default="show",
choices=["show", "validate", "defaults"])
c.add_argument("--path", default=None)
return p
def cmd_doctor(args) -> int:
from .compat import TORCH_AVAILABLE, torch_version, optional_import
from .config import load_config
from .monitoring.gpu_monitor import GPUMonitor
from .integration.project_detector import interpreter_report
cfg = load_config()
mon = GPUMonitor(sample_seconds=0.5, enabled=True)
report = {
"lpu_version": __import__("lpu.version", fromlist=["__version__"]).__version__,
"platform": sys.platform,
"python": sys.version.split()[0],
"torch_importable": TORCH_AVAILABLE,
"torch_version": torch_version(),
"torch_cuda": None,
"gpu_count": mon.device_count,
"driver_version": mon.driver_version(),
"gpus": [s.as_dict() for s in mon.sample_all(force=True)],
"config_path": str(cfg.path) if cfg.path else "<defaults>",
"interpreter_probe": interpreter_report(sys.executable),
}
if TORCH_AVAILABLE:
torch = optional_import("torch")
report["torch_cuda"] = bool(torch.cuda.is_available())
if torch.cuda.is_available():
report["torch_gpus"] = [
{"index": i, "name": torch.cuda.get_device_name(i),
"cc": ".".join(map(str, torch.cuda.get_device_capability(i))),
"total_mb": torch.cuda.get_device_properties(i).total_memory / (1024 * 1024)}
for i in range(torch.cuda.device_count())
]
report["p2p"] = {}
n = torch.cuda.device_count()
for i in range(n):
for j in range(n):
if i != j:
try:
report["p2p"][f"{i}->{j}"] = bool(
torch.cuda.can_device_access_peer(i, j)
)
except Exception: # noqa: BLE001
report["p2p"][f"{i}->{j}"] = "query failed"
else:
report["hint"] = ("torch is CPU-only in THIS interpreter. lpu doctor "
"reports sandbox facts; run it with your project's "
"interpreter for CUDA facts (LPU never installs torch).")
if args.json:
print(json.dumps(report, indent=2, default=str))
else:
print("== LPU doctor ==")
for k in ("lpu_version", "platform", "python", "torch_version",
"torch_cuda", "gpu_count", "driver_version", "config_path"):
print(f" {k:18}: {report.get(k)}")
for g in report.get("gpus", []):
print(f" GPU {g['index']}: {g['name']} util={g['utilization_pct']}% "
f"mem={g['mem_used_mb']}/{g['mem_total_mb']} MB")
for g in report.get("torch_gpus", []):
print(f" torch GPU {g['index']}: {g['name']} cc={g['cc']} {g['total_mb']:.0f} MB")
if report.get("p2p"):
print(f" P2P: {report['p2p']}")
if report.get("hint"):
print(f" NOTE: {report['hint']}")
mon.close()
return 0
def cmd_run(args) -> int:
from .integration import launch
argv = list(args.argv)
if argv and argv[0] == "--":
argv = argv[1:]
if not argv:
print("usage: lpu run [--python EXE] -- python train.py [args...]",
file=sys.stderr)
return 2
result = launch(argv, python=args.python, cwd=args.cwd,
hooks=not args.no_hooks,
intercept=args.intercept)
print(json.dumps(result.summary(), indent=2, default=str))
return result.returncode
def cmd_bench(args) -> int:
from .compat import require_torch
from .benchmarks import BenchmarkRunner, write_reports
from .monitoring.gpu_monitor import GPUMonitor
import threading
import time as _t
require_torch() # benchmarks need torch (the project's interpreter)
cfg_src = None
runner = BenchmarkRunner(tier=args.tier)
mon = GPUMonitor(sample_seconds=2.0, enabled=True)
gpu_samples: list = []
stop_flag = threading.Event()
def sampler():
# sample DURING the run, not one post-hoc snapshot
while not stop_flag.is_set():
for s in mon.sample_all():
gpu_samples.append(s.as_dict())
stop_flag.wait(1.0)
th = threading.Thread(target=sampler, daemon=True)
t0 = _t.time()
th.start()
try:
results = runner.run(variants=args.variants)
finally:
stop_flag.set()
th.join(timeout=3)
duration = _t.time() - t0
summary = runner.summary()
summary["wall_time_s"] = round(duration, 1)
md_path = write_reports(summary, output_dir=args.out, gpu_samples=gpu_samples)
mon.close()
print(f"benchmark complete in {duration:.0f}s -> {md_path}")
for name, v in summary["variants"].items():
if v.get("error"):
print(f" {name}: FAILED {v['error'][:80]}")
else:
print(f" {name}: {v['images_per_second_mean']} img/s "
f"(iter {v['iteration_time_mean_s']} s)")
return 0
def cmd_audit(args) -> int:
from .audit import audit_tree, audit_markdown
from .monitoring.logger import lpu_home
result = audit_tree(Path(args.path))
md = audit_markdown(result)
# NEVER write into the audited project or the user's cwd by default
out = Path(args.out) if args.out else (lpu_home() / "reports" / "AUDIT_REPORT.md")
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(md, encoding="utf-8")
print(f"audited {result['python_files']} files -> {out}")
print(json.dumps(result["summary_counts"], indent=2))
return 0
def cmd_start(_args) -> int:
from .runtime.controller import main as controller_main
return controller_main()
def cmd_stop(_args) -> int:
from .runtime.lifecycle import find_controller_pid
from .monitoring.logger import lpu_home
pid = find_controller_pid()
if pid is None:
# no controller: writing a stop flag would BRICK the next start
print("no running controller found (nothing to stop)")
return 0
stop = lpu_home() / "controller.stop"
stop.write_text("cli", encoding="utf-8")
print(f"stop requested (controller pid {pid}); it will exit within its poll interval")
return 0
def cmd_status(_args) -> int:
from .runtime.background_worker import full_report
print(json.dumps(full_report(), indent=2, default=str))
return 0
def cmd_config(args) -> int:
from .config import load_config, validate_config
import yaml
if args.action == "validate":
try:
if args.path:
data = yaml.safe_load(Path(args.path).read_text(encoding="utf-8"))
validate_config(data or {})
else:
load_config() # full validation happens in load
print("configuration VALID")
return 0
except Exception as exc: # noqa: BLE001
print(f"configuration INVALID: {exc}", file=sys.stderr)
return 1
cfg = load_config(args.path)
print(yaml.safe_dump(cfg.data, sort_keys=False, allow_unicode=True))
return 0
def main(argv: Optional[List[str]] = None) -> int:
parser = _build_parser()
args = parser.parse_args(argv)
if not args.cmd:
parser.print_help()
return 2
handlers = {
"doctor": cmd_doctor, "run": cmd_run, "bench": cmd_bench,
"audit": cmd_audit, "start": cmd_start, "stop": cmd_stop,
"status": cmd_status, "config": cmd_config,
}
return handlers[args.cmd](args)
if __name__ == "__main__":
raise SystemExit(main())