Download closedloop/scripts/run_pdm_acceptance_suite.py from YongshuoLiu/LANTERN: direct link, hf CLI and curl.
- Browser
- Download file 8.66 kB
-
https://huggingface.co/datasets/YongshuoLiu/LANTERN/resolve/main/closedloop/scripts/run_pdm_acceptance_suite.py
- Command line
-
hf download hf://datasets/YongshuoLiu/LANTERN/closedloop/scripts/run_pdm_acceptance_suite.py
-
curl -L -o run_pdm_acceptance_suite.py https://huggingface.co/datasets/YongshuoLiu/LANTERN/resolve/main/closedloop/scripts/run_pdm_acceptance_suite.py
8.66 kB
| #!/usr/bin/env python3 | |
| """Run sequential PDM-Lite acceptance over the frozen CoDrive routes.""" | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| import os | |
| import shutil | |
| import subprocess | |
| import sys | |
| import time | |
| import xml.etree.ElementTree as ET | |
| from collections import Counter | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[2] | |
| RUN_ONE = ROOT / "closed_dataset/scripts/run_pdm_acceptance_route.sh" | |
| VALIDATE_ONE = ROOT / "closed_dataset/scripts/validate_pdm_acceptance.py" | |
| FAMILY_ORDER = ( | |
| "occluded_pedestrian", | |
| "lane_cut_in", | |
| "reveal_obstacle", | |
| "ghost_driver_left_turn", | |
| "ghost_driver_right_turn", | |
| "right_turn_on_red", | |
| ) | |
| DEFAULT_GRAPHICS_ADAPTER = {0: 1, 1: 2, 2: 0, 3: 3} | |
| def _route_family(route: Path) -> str: | |
| return route.parent.name | |
| def _route_id(route: Path) -> str: | |
| return ET.parse(route).getroot().find("route").attrib["id"] | |
| def _sha256(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 _quarantine_incomplete(run_dir: Path) -> None: | |
| if not run_dir.exists() or not any(run_dir.iterdir()): | |
| return | |
| suffix = time.strftime("%Y%m%d_%H%M%S") | |
| destination = run_dir.with_name(f"{run_dir.name}_incomplete_{suffix}") | |
| counter = 1 | |
| while destination.exists(): | |
| destination = run_dir.with_name( | |
| f"{run_dir.name}_incomplete_{suffix}_{counter:02d}" | |
| ) | |
| counter += 1 | |
| shutil.move(str(run_dir), str(destination)) | |
| print(f"quarantined incomplete artifact: {destination}", flush=True) | |
| def _select_routes(route_root: Path, families: list[str], one_per_family: bool) -> list[Path]: | |
| routes = [] | |
| for family in FAMILY_ORDER: | |
| if families and family not in families: | |
| continue | |
| family_routes = sorted((route_root / family).glob("*.xml")) | |
| if one_per_family: | |
| family_routes = family_routes[:1] | |
| routes.extend(family_routes) | |
| return routes | |
| def main() -> int: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument( | |
| "--route-root", | |
| type=Path, | |
| default=ROOT / "closed_dataset/routes", | |
| ) | |
| parser.add_argument( | |
| "--run-root", | |
| type=Path, | |
| default=ROOT / "closed_dataset/acceptance_runs", | |
| ) | |
| parser.add_argument("--family", action="append", choices=FAMILY_ORDER) | |
| parser.add_argument("--one-per-family", action="store_true") | |
| parser.add_argument("--max-routes", type=int, default=0) | |
| parser.add_argument("--gpu", type=int, default=2) | |
| parser.add_argument("--graphics-adapter", type=int) | |
| parser.add_argument("--port", type=int, default=4897) | |
| parser.add_argument("--tm-port", type=int, default=10897) | |
| parser.add_argument("--walltime", type=int, default=480) | |
| parser.add_argument("--rerun", action="store_true") | |
| parser.add_argument( | |
| "--revalidate-cache", | |
| action="store_true", | |
| help="Re-run the current validator before accepting a hash-matched cache.", | |
| ) | |
| parser.add_argument("--fail-fast", action="store_true") | |
| args = parser.parse_args() | |
| route_root = args.route_root.resolve() | |
| run_root = args.run_root.resolve() | |
| run_root.mkdir(parents=True, exist_ok=True) | |
| routes = _select_routes(route_root, args.family or [], args.one_per_family) | |
| if args.max_routes > 0: | |
| routes = routes[: args.max_routes] | |
| if not routes: | |
| raise SystemExit(f"No routes found under {route_root}") | |
| summaries = [] | |
| total = len(routes) | |
| for index, route in enumerate(routes, 1): | |
| route_id = _route_id(route) | |
| family = _route_family(route) | |
| run_dir = run_root / family / route_id | |
| report_path = run_dir / "acceptance_report.json" | |
| route_sha256 = _sha256(route) | |
| if report_path.is_file() and not args.rerun: | |
| report = json.loads(report_path.read_text(encoding="utf-8")) | |
| cache_matches = report.get("route_sha256") == route_sha256 | |
| if cache_matches and args.revalidate_cache: | |
| validate_status = subprocess.run( | |
| [ | |
| sys.executable, | |
| str(VALIDATE_ONE), | |
| str(run_dir), | |
| "--output", | |
| str(report_path), | |
| ], | |
| start_new_session=True, | |
| ).returncode | |
| report = json.loads(report_path.read_text(encoding="utf-8")) | |
| report["validator_status"] = validate_status | |
| report["route_id"] = route_id | |
| report["family"] = family | |
| report["route_sha256"] = route_sha256 | |
| if report.get("runner_status", 0) != 0: | |
| report["accepted"] = False | |
| report["errors"] = list(report.get("errors", [])) | |
| report["errors"].append( | |
| f"acceptance runner exited with status " | |
| f"{report['runner_status']}" | |
| ) | |
| report_path.write_text( | |
| json.dumps(report, indent=2, sort_keys=True) + "\n" | |
| ) | |
| cache_matches = report["accepted"] | |
| if cache_matches: | |
| print( | |
| f"[{index}/{total}] cached {route_id}: " | |
| f"accepted={report['accepted']} hash={route_sha256[:12]}", | |
| flush=True, | |
| ) | |
| summaries.append(report) | |
| if args.fail_fast and not report["accepted"]: | |
| break | |
| continue | |
| print( | |
| f"[{index}/{total}] stale cache for {route_id}; rerunning", | |
| flush=True, | |
| ) | |
| _quarantine_incomplete(run_dir) | |
| run_dir.mkdir(parents=True, exist_ok=True) | |
| env = os.environ.copy() | |
| env.update( | |
| { | |
| "ROUTE": str(route), | |
| "RUN_DIR": str(run_dir), | |
| "GPU_RANK": str(args.gpu), | |
| "CARLA_GRAPHICS_ADAPTER": str( | |
| args.graphics_adapter | |
| if args.graphics_adapter is not None | |
| else DEFAULT_GRAPHICS_ADAPTER.get(args.gpu, args.gpu) | |
| ), | |
| "PORT": str(args.port), | |
| "TM_PORT": str(args.tm_port), | |
| "EVAL_WALLTIME_SECONDS": str(args.walltime), | |
| } | |
| ) | |
| print(f"[{index}/{total}] running {route_id}", flush=True) | |
| run_status = subprocess.run( | |
| ["bash", str(RUN_ONE)], | |
| env=env, | |
| start_new_session=True, | |
| ).returncode | |
| validate_status = subprocess.run( | |
| [ | |
| sys.executable, | |
| str(VALIDATE_ONE), | |
| str(run_dir), | |
| "--output", | |
| str(report_path), | |
| ], | |
| start_new_session=True, | |
| ).returncode | |
| report = json.loads(report_path.read_text(encoding="utf-8")) | |
| report["runner_status"] = run_status | |
| report["validator_status"] = validate_status | |
| report["route_id"] = route_id | |
| report["family"] = family | |
| report["route_sha256"] = route_sha256 | |
| if run_status != 0: | |
| report["accepted"] = False | |
| report["errors"] = list(report.get("errors", [])) | |
| report["errors"].append( | |
| f"acceptance runner exited with status {run_status}" | |
| ) | |
| report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n") | |
| summaries.append(report) | |
| print( | |
| f"[{index}/{total}] {route_id}: accepted={report['accepted']} " | |
| f"errors={report['errors']}", | |
| flush=True, | |
| ) | |
| if args.fail_fast and not report["accepted"]: | |
| break | |
| counts = Counter("accepted" if item["accepted"] else "rejected" for item in summaries) | |
| suite_report = { | |
| "route_root": str(route_root), | |
| "run_root": str(run_root), | |
| "route_count": len(summaries), | |
| "accepted": counts["accepted"], | |
| "rejected": counts["rejected"], | |
| "routes": summaries, | |
| } | |
| (run_root / "acceptance_summary.json").write_text( | |
| json.dumps(suite_report, indent=2, sort_keys=True) + "\n" | |
| ) | |
| print(json.dumps({key: value for key, value in suite_report.items() if key != "routes"}, indent=2)) | |
| return 0 if counts["rejected"] == 0 else 1 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |