File size: 6,068 Bytes
4341630 | 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 | #!/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
@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()
|