Download scripts/benchmark_inference_components.py from zhicao/tract: direct link, hf CLI and curl.
- Browser
- Download file 6.07 kB
-
https://huggingface.co/zhicao/tract/resolve/main/scripts/benchmark_inference_components.py
- Command line
-
hf download hf://zhicao/tract/scripts/benchmark_inference_components.py
-
curl -L -o benchmark_inference_components.py https://huggingface.co/zhicao/tract/resolve/main/scripts/benchmark_inference_components.py
6.07 kB
| #!/usr/bin/env python3 | |
| """Benchmark per-replan inference components: VLAT(policy) vs Video(WM) vs VLAC. | |
| Writes a JSON summary under results/timing/. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import logging | |
| import os | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| OPENPI_ROOT = Path(__file__).resolve().parents[1] | |
| sys.path[:0] = [ | |
| str(OPENPI_ROOT), | |
| str(OPENPI_ROOT / "src"), | |
| str(OPENPI_ROOT / "packages" / "openpi-client" / "src"), | |
| str(OPENPI_ROOT / "scripts"), | |
| ] | |
| os.environ.setdefault("LIBERO_CONFIG_PATH", str(OPENPI_ROOT / ".libero_local")) | |
| os.environ.setdefault("MUJOCO_GL", "glx") | |
| os.environ.setdefault("OPENPI_MUJOCO_GL", "glx") | |
| def main() -> None: | |
| logging.basicConfig(level=logging.INFO, force=True) | |
| import tyro | |
| from dataclasses import dataclass | |
| class Args: | |
| n_candidates: int = 10 | |
| n_replans: int = 3 | |
| warmup: int = 1 | |
| wm_num_inference_steps: int = 10 | |
| policy_gpu: str = "1" | |
| wm_gpu: str = "2" | |
| vlac_gpu: str = "3" | |
| out_json: str = str(OPENPI_ROOT / "results" / "timing" / "inference_components.json") | |
| args = tyro.cli(Args) | |
| os.environ["CUDA_VISIBLE_DEVICES"] = ",".join( | |
| dict.fromkeys([args.policy_gpu, args.wm_gpu, args.vlac_gpu]) | |
| ) | |
| # Remap to local cuda indices after CUDA_VISIBLE_DEVICES. | |
| visible = list(dict.fromkeys([args.policy_gpu, args.wm_gpu, args.vlac_gpu])) | |
| idx = {p: i for i, p in enumerate(visible)} | |
| policy_dev = f"cuda:{idx[args.policy_gpu]}" | |
| wm_dev = f"cuda:{idx[args.wm_gpu]}" | |
| vlac_dev = f"cuda:{idx[args.vlac_gpu]}" | |
| from scripts.vlac_pipeline.libero_action_selection import LiberoActionSelector | |
| selector = LiberoActionSelector( | |
| n_candidates=args.n_candidates, | |
| wm_num_inference_steps=args.wm_num_inference_steps, | |
| policy_mem_fraction=0.3, | |
| ) | |
| logging.info("Loading selector devices policy=%s wm=%s vlac=%s", policy_dev, wm_dev, vlac_dev) | |
| selector.load(policy_device=policy_dev, wm_device=wm_dev, vlac_device=vlac_dev) | |
| H = W = 224 | |
| rng = np.random.RandomState(0) | |
| agent = rng.randint(0, 255, (H, W, 3), dtype=np.uint8) | |
| wrist = rng.randint(0, 255, (H, W, 3), dtype=np.uint8) | |
| # Same key layout as evaluate.py::_prepare_raw_item. | |
| zeros7 = np.zeros((7, 2), dtype=np.float32) | |
| zeros32 = np.zeros((32, 2), dtype=np.float32) | |
| raw_item = { | |
| "image": agent, | |
| "wrist_image": wrist, | |
| "state": np.zeros(8, dtype=np.float32), | |
| "prompt": "pick the alphabet soup and place it in the basket", | |
| "agentview_mesh_vertices_2d": zeros7, | |
| "wrist_mesh_vertices_2d": zeros7, | |
| "wrist_tracks": zeros32, | |
| "wrist_grid_mesh_32": zeros32, | |
| "actions": np.zeros((16, 7), dtype=np.float32), | |
| } | |
| # Also time single VLAT infer (direct policy, no candidates/WM/VLAC). | |
| vlat_ms: list[float] = [] | |
| assert selector._policy is not None | |
| for i in range(args.warmup + args.n_replans): | |
| t0 = time.perf_counter() | |
| _ = selector._policy.infer(raw_item, return_tracks=True) | |
| dt = (time.perf_counter() - t0) * 1000.0 | |
| if i >= args.warmup: | |
| vlat_ms.append(dt) | |
| logging.info("VLAT single infer #%d: %.1f ms", i - args.warmup, dt) | |
| rows = [] | |
| for i in range(args.warmup + args.n_replans): | |
| sel = selector.select_best_candidate( | |
| raw_item, | |
| agent_img=agent, | |
| wrist_img=wrist, | |
| prompt=str(raw_item["prompt"]), | |
| n_candidates=args.n_candidates, | |
| ) | |
| if i < args.warmup: | |
| continue | |
| rows.append( | |
| { | |
| "replan": i - args.warmup, | |
| "n_candidates": sel.n_candidates, | |
| "policy_ms": float(sel.policy_ms), | |
| "wm_ms": float(sel.wm_ms), | |
| "vlac_ms": float(sel.vlac_ms), | |
| "total_ms": float(sel.policy_ms + sel.wm_ms + sel.vlac_ms), | |
| "wm_ms_per_cand": float(sel.wm_ms / max(1, sel.n_candidates)), | |
| "vlac_ms_per_cand": float(sel.vlac_ms / max(1, sel.n_candidates)), | |
| } | |
| ) | |
| logging.info( | |
| "replan %d: policy=%.0f wm=%.0f vlac=%.0f total=%.0f", | |
| rows[-1]["replan"], | |
| rows[-1]["policy_ms"], | |
| rows[-1]["wm_ms"], | |
| rows[-1]["vlac_ms"], | |
| rows[-1]["total_ms"], | |
| ) | |
| def _mean(xs: list[float]) -> float: | |
| return float(np.mean(xs)) if xs else float("nan") | |
| summary = { | |
| "config": { | |
| "n_candidates": args.n_candidates, | |
| "n_replans": args.n_replans, | |
| "warmup": args.warmup, | |
| "wm_num_inference_steps": args.wm_num_inference_steps, | |
| "devices": {"policy": policy_dev, "wm": wm_dev, "vlac": vlac_dev}, | |
| "note": ( | |
| "VLAT = single policy.infer (direct). " | |
| "policy_ms = candidate generation for TrAct. " | |
| "Video = Track DualView WM (sum over candidates). " | |
| "VLAC = dual-view critic (sum over candidates)." | |
| ), | |
| }, | |
| "vlat_single_infer_ms": { | |
| "mean": _mean(vlat_ms), | |
| "samples": vlat_ms, | |
| }, | |
| "per_replan_selection": rows, | |
| "means_ms": { | |
| "vlat_single_infer": _mean(vlat_ms), | |
| "policy_candidates": _mean([r["policy_ms"] for r in rows]), | |
| "video_wm_total": _mean([r["wm_ms"] for r in rows]), | |
| "vlac_total": _mean([r["vlac_ms"] for r in rows]), | |
| "video_wm_per_candidate": _mean([r["wm_ms_per_cand"] for r in rows]), | |
| "vlac_per_candidate": _mean([r["vlac_ms_per_cand"] for r in rows]), | |
| "tract_selection_total": _mean([r["total_ms"] for r in rows]), | |
| }, | |
| } | |
| out = Path(args.out_json) | |
| out.parent.mkdir(parents=True, exist_ok=True) | |
| out.write_text(json.dumps(summary, indent=2), encoding="utf-8") | |
| print(json.dumps(summary["means_ms"], indent=2)) | |
| print("wrote", out) | |
| if __name__ == "__main__": | |
| main() | |