Download scripts/benchmark_tract_speed_knobs.py from zhicao/tract: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/zhicao/tract/resolve/main/scripts/benchmark_tract_speed_knobs.py
- Command line
-
hf download hf://zhicao/tract/scripts/benchmark_tract_speed_knobs.py
-
curl -L -o benchmark_tract_speed_knobs.py https://huggingface.co/zhicao/tract/resolve/main/scripts/benchmark_tract_speed_knobs.py
6.65 kB
| #!/usr/bin/env python3 | |
| """Sweep TrAct selection speed knobs (n_candidates / wm_steps / wm_batch_size). | |
| Loads models once, then times select_best_candidate for each config. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import logging | |
| import os | |
| import sys | |
| import time | |
| from dataclasses import dataclass | |
| 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") | |
| os.environ.setdefault("TRACK_DUALVIEW_PIPELINE_DEBUG", "0") | |
| os.environ.setdefault("TQDM_DISABLE", "1") | |
| class Args: | |
| policy_gpu: str = "4" | |
| wm_gpu: str = "5" | |
| vlac_gpu: str = "6" | |
| warmup: int = 1 | |
| repeats: int = 2 | |
| out_json: str = str(OPENPI_ROOT / "results" / "timing" / "tract_speed_knobs.json") | |
| def main() -> None: | |
| logging.basicConfig(level=logging.INFO, force=True) | |
| import tyro | |
| args = tyro.cli(Args) | |
| visible = list(dict.fromkeys([args.policy_gpu, args.wm_gpu, args.vlac_gpu])) | |
| os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(visible) | |
| 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=20, wm_num_inference_steps=25, wm_batch_size=4, policy_mem_fraction=0.25) | |
| logging.info("Loading selector 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) | |
| logging.basicConfig(level=logging.INFO, force=True) | |
| 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) | |
| 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), | |
| } | |
| configs = [ | |
| {"name": "paper_n20_s25_b1", "n": 20, "steps": 25, "batch": 1}, | |
| {"name": "paper_n20_s25_b4", "n": 20, "steps": 25, "batch": 4}, | |
| {"name": "paper_n20_s25_b8", "n": 20, "steps": 25, "batch": 8}, | |
| {"name": "fast_n20_s10_b4", "n": 20, "steps": 10, "batch": 4}, | |
| {"name": "fast_n20_s10_b8", "n": 20, "steps": 10, "batch": 8}, | |
| {"name": "debug_n8_s10_b4", "n": 8, "steps": 10, "batch": 4}, | |
| {"name": "debug_n4_s10_b4", "n": 4, "steps": 10, "batch": 4}, | |
| ] | |
| results = [] | |
| for cfg in configs: | |
| selector.n_candidates = int(cfg["n"]) | |
| selector.wm_num_inference_steps = int(cfg["steps"]) | |
| selector.wm_batch_size = int(cfg["batch"]) | |
| logging.info("=== %s ===", cfg["name"]) | |
| samples = [] | |
| for i in range(args.warmup + args.repeats): | |
| t0 = time.perf_counter() | |
| try: | |
| sel = selector.select_best_candidate( | |
| raw_item, | |
| agent_img=agent, | |
| wrist_img=wrist, | |
| prompt=str(raw_item["prompt"]), | |
| n_candidates=int(cfg["n"]), | |
| ) | |
| wall_ms = (time.perf_counter() - t0) * 1000.0 | |
| row = { | |
| "ok": True, | |
| "wall_ms": wall_ms, | |
| "policy_ms": float(sel.policy_ms), | |
| "wm_ms": float(sel.wm_ms), | |
| "vlac_ms": float(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)), | |
| } | |
| except Exception as exc: # noqa: BLE001 | |
| logging.exception("config %s failed: %s", cfg["name"], exc) | |
| row = {"ok": False, "error": str(exc), "wall_ms": (time.perf_counter() - t0) * 1000.0} | |
| if i >= args.warmup and row.get("ok"): | |
| samples.append(row) | |
| logging.info( | |
| "%s sample: wall=%.1fs policy=%.0fms wm=%.0fms(%.0f/cand) vlac=%.0fms", | |
| cfg["name"], | |
| row["wall_ms"] / 1000.0, | |
| row["policy_ms"], | |
| row["wm_ms"], | |
| row["wm_ms_per_cand"], | |
| row["vlac_ms"], | |
| ) | |
| if samples: | |
| mean_wall = float(np.mean([s["wall_ms"] for s in samples])) | |
| mean_wm = float(np.mean([s["wm_ms"] for s in samples])) | |
| mean_vlac = float(np.mean([s["vlac_ms"] for s in samples])) | |
| results.append( | |
| { | |
| **cfg, | |
| "mean_wall_s": mean_wall / 1000.0, | |
| "mean_wm_s": mean_wm / 1000.0, | |
| "mean_vlac_s": mean_vlac / 1000.0, | |
| "est_fail_ep_min": (520 / 5) * (mean_wall / 1000.0) / 60.0, | |
| "est_attempt_10fail_h": 10 * (520 / 5) * (mean_wall / 1000.0) / 3600.0, | |
| "samples": samples, | |
| } | |
| ) | |
| else: | |
| results.append({**cfg, "ok": False, "samples": []}) | |
| out = { | |
| "devices": {"policy": policy_dev, "wm": wm_dev, "vlac": vlac_dev}, | |
| "note": "wall = one select_best_candidate (one replan). fail_ep assumes 104 replans.", | |
| "results": results, | |
| } | |
| Path(args.out_json).parent.mkdir(parents=True, exist_ok=True) | |
| Path(args.out_json).write_text(json.dumps(out, indent=2) + "\n", encoding="utf-8") | |
| print("\n=== SUMMARY ===") | |
| for r in results: | |
| if "mean_wall_s" not in r: | |
| print(f"{r['name']}: FAILED") | |
| continue | |
| print( | |
| f"{r['name']}: wall={r['mean_wall_s']:.1f}s " | |
| f"(wm={r['mean_wm_s']:.1f}s vlac={r['mean_vlac_s']:.1f}s) " | |
| f"| fail_ep≈{r['est_fail_ep_min']:.0f}min " | |
| f"| 10fail_attempt≈{r['est_attempt_10fail_h']:.1f}h" | |
| ) | |
| print(f"wrote {args.out_json}") | |
| if __name__ == "__main__": | |
| main() | |