|
| 1 | +"""Vendor + verify ALL 130 LIBERO tasks as native MetaSim bundles. |
| 2 | +
|
| 3 | +For every task: remote-read its model_file + a demo's initial/final states + |
| 4 | +recorded done from HF (range requests, no full download); read the BDDL goal |
| 5 | +locally; write a native bundle (model.xml + goal.json + init.npz); load the model |
| 6 | +and check that the native BDDL success at the demo's final state equals the |
| 7 | +recorded done. One pass vendors all bundles AND validates the native predicates. |
| 8 | +
|
| 9 | + python -m tools.libero_integration.vendor_all_native --out <report>/metasim_1to1/native130.json |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import argparse |
| 15 | +import json |
| 16 | +import os |
| 17 | +import time |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +os.environ.setdefault("MUJOCO_GL", "egl") |
| 21 | +os.environ.setdefault("HF_HUB_OFFLINE", "0") |
| 22 | + |
| 23 | +import fsspec # noqa: E402 |
| 24 | +import h5py # noqa: E402 |
| 25 | +import mujoco # noqa: E402 |
| 26 | +import numpy as np # noqa: E402 |
| 27 | + |
| 28 | +from roboverse_pack.tasks.libero._native_util import check_bddl_success, parse_goal, remap_libero_model # noqa: E402 |
| 29 | + |
| 30 | +_HF = "https://huggingface.co/datasets/yifengzhu-hf/LIBERO-datasets/resolve/main" |
| 31 | +_BDDL = "/venv/roboverse/lib/python3.11/site-packages/libero/libero/bddl_files" |
| 32 | +_BUNDLES = Path(__file__).resolve().parents[2] / "roboverse_pack/tasks/libero/native_bundles" |
| 33 | +_ASSETS = str(Path(__file__).resolve().parents[2] / "third_party/LIBERO/libero/libero/assets") |
| 34 | +_LOCAL = { |
| 35 | + ("libero_object", "pick_up_the_butter_and_place_it_in_the_basket"): "datasets/libero/butter_demo.hdf5", |
| 36 | + ("libero_goal", "open_the_middle_drawer_of_the_cabinet"): "datasets/libero/goal_drawer_demo.hdf5", |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +def _read(suite, stem): |
| 41 | + local = _LOCAL.get((suite, stem)) |
| 42 | + f = h5py.File(local, "r") if local and os.path.exists(local) else h5py.File(fsspec.open(f"{_HF}/{suite}/{stem}_demo.hdf5", "rb").open(), "r") |
| 43 | + g = f["data"]["demo_0"] |
| 44 | + mf = g.attrs["model_file"] |
| 45 | + s0 = g["states"][0] |
| 46 | + sN = g["states"][-1] |
| 47 | + done = int(g["dones"][()][-1]) if "dones" in g else int(g["rewards"][()][-1] >= 1.0) |
| 48 | + f.close() |
| 49 | + return mf, s0, sN, done |
| 50 | + |
| 51 | + |
| 52 | +def main() -> None: |
| 53 | + ap = argparse.ArgumentParser() |
| 54 | + ap.add_argument("--out", type=Path, required=True) |
| 55 | + ap.add_argument("--tasks-json", default="datasets/libero/all_tasks.json") |
| 56 | + args = ap.parse_args() |
| 57 | + tasks = json.loads(Path(args.tasks_json).read_text()) |
| 58 | + |
| 59 | + results, vendored, matched, errs = [], 0, 0, 0 |
| 60 | + by_suite_match: dict[str, list[bool]] = {} |
| 61 | + for i, (suite, pf, stem) in enumerate(tasks): |
| 62 | + name = f"{suite.replace('libero_', '')}__{stem}"[:120] |
| 63 | + try: |
| 64 | + mf, s0, sN, done = _read(suite, stem) |
| 65 | + xml = remap_libero_model(mf, _ASSETS) |
| 66 | + m = mujoco.MjModel.from_xml_string(xml) |
| 67 | + d = mujoco.MjData(m) |
| 68 | + nq, nv = m.nq, m.nv |
| 69 | + goal = parse_goal(Path(f"{_BDDL}/{pf}/{stem}.bddl").read_text()) |
| 70 | + # write bundle |
| 71 | + out = _BUNDLES / name |
| 72 | + out.mkdir(parents=True, exist_ok=True) |
| 73 | + (out / "model.xml").write_text(mf) |
| 74 | + (out / "goal.json").write_text(json.dumps(goal)) |
| 75 | + np.savez(out / "init.npz", qpos=s0[1 : 1 + nq], qvel=s0[1 + nq : 1 + nq + nv]) |
| 76 | + vendored += 1 |
| 77 | + # verify native success at demo-final == recorded done |
| 78 | + d.qpos[:] = sN[1 : 1 + nq] |
| 79 | + d.qvel[:] = sN[1 + nq : 1 + nq + nv] |
| 80 | + native = check_bddl_success(m, d, goal) |
| 81 | + ok = native == bool(done) |
| 82 | + matched += ok |
| 83 | + by_suite_match.setdefault(suite, []).append(ok) |
| 84 | + rec = {"name": name, "suite": suite, "goal": goal, "native_success": native, "recorded_done": done, "match": ok} |
| 85 | + print(f" [{i + 1:3d}/130] {name[:60]:60s} goal={[g[0] for g in goal]} native={native} done={done} match={ok}", flush=True) |
| 86 | + except Exception as e: # noqa: BLE001 |
| 87 | + errs += 1 |
| 88 | + rec = {"name": name, "suite": suite, "error": str(e)[:140]} |
| 89 | + print(f" [{i + 1:3d}/130] {name[:60]:60s} ERROR {str(e)[:60]}", flush=True) |
| 90 | + results.append(rec) |
| 91 | + time.sleep(0.25) |
| 92 | + |
| 93 | + summary = { |
| 94 | + "n": len(tasks), "vendored": vendored, "success_match": f"{matched}/{len(tasks)}", "errors": errs, |
| 95 | + "per_suite_match": {s: f"{sum(v)}/{len(v)}" for s, v in by_suite_match.items()}, |
| 96 | + "tasks": results, |
| 97 | + } |
| 98 | + args.out.parent.mkdir(parents=True, exist_ok=True) |
| 99 | + args.out.write_text(json.dumps(summary, indent=2)) |
| 100 | + print(f"\nNATIVE 130: vendored {vendored}/{len(tasks)} | success-match {summary['success_match']} | errors {errs}") |
| 101 | + print("per-suite match:", summary["per_suite_match"]) |
| 102 | + print(f"wrote {args.out}") |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments