Spaces:
Running
Running
File size: 10,207 Bytes
e59bd9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | """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())
|