File size: 5,748 Bytes
28b4c87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Run one camera-initialized placement in the local PiPER physics simulator."""
import argparse
import hashlib
import importlib.util
import json
from pathlib import Path
import shutil
import sys
import tempfile
from types import SimpleNamespace
from zipfile import ZipFile

ROOT = Path(__file__).resolve().parent
SKILL = "piper-camera-yaw-skill-0.1.0.zip"
EVIDENCE = "evidence.zip"
SKILL_HASH = "45a8636d2e90db9a4643a73268cf0463b6943d4d63f97ec93fff5ec0b470b1f9"
EVIDENCE_HASH = "cac4c33eccdf0ef95adfccdcd0ac3647c2270c1cdd0562cdfcb5f7cc759e43de"
STUDY = "runs/first-attempt/corner-distilled-yaw"
PLAN = f"{STUDY}/protocol.json"
CALIBRATION = "benchmarks/piper-first-attempt-v01/workspace/nominal.json"
CONTROLLER = "learned_camera_yaw"


def digest(path):
    with path.open("rb") as file:
        return hashlib.file_digest(file, "sha256").hexdigest()


def copy_member(archive, name, destination):
    destination.write_bytes(archive.read(name))
    return destination


def import_file(name, path):
    spec = importlib.util.spec_from_file_location(name, path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def execute(args, evidence, skill, folder, case, reference):
    import dvidia_training

    # Keep installed package data and the native scorer available in the
    # isolated source tree. Leave the installation itself untouched.
    original = Path(dvidia_training.__file__).resolve().parent
    overlay = folder / "dvidia_training"
    shutil.copytree(original, overlay, ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
    for source in ("piper_first_attempt_eval.py", "piper_first_attempt_policy.py",
                   "piper_first_attempt_replay.py", "piper_workspace_calibration.py"):
        copy_member(evidence, f"src/dvidia_training/{source}", overlay / source)
    for source, expected in reference["source_sha256"].items():
        if not (overlay / source).is_file() or digest(overlay / source) != expected:
            raise ValueError(f"Installed simulator source differs from the benchmark: {source}")
    dvidia_training.__path__ = [str(overlay)]

    model = copy_member(skill, "model/camera-xy-primitive.npz", folder / "model.npz")
    policy = copy_member(skill, "policy.py", folder / "policy.py")
    copy_member(skill, "adapter.py", folder / "adapter.py")
    plan = copy_member(evidence, PLAN, folder / "protocol.json")
    calibration = copy_member(evidence, CALIBRATION, folder / "calibration.json")
    driver_path = copy_member(evidence, f"{STUDY}/driver.py", folder / "driver.py")
    sys.path.insert(0, str(folder))
    driver = import_file("benchmark_driver", driver_path)
    setup = SimpleNamespace(assets=args.assets.resolve(), model=model, plan=plan,
                            policy_source=policy, calibration=calibration,
                            policy_class=driver.load_policy(policy))
    output = args.output.resolve()
    output.mkdir(parents=True, exist_ok=False)
    receipt = driver.run(setup, case, CONTROLLER, output)
    receipt["skill_archive_sha256"] = SKILL_HASH
    receipt["matches_registered_commands"] = (
        receipt.get("executed_commands_sha256") is not None
        and receipt.get("executed_commands_sha256") == reference["executed_commands_sha256"])
    with (output / "receipt.json").open("x") as handle:
        json.dump(receipt, handle, allow_nan=False, indent=2, sort_keys=True)
        handle.write("\n")
    print(json.dumps(dict(case=case["id"], status=receipt["status"], strict_success=receipt["success"],
                          reason=receipt["reason"], simulated_completion_s=receipt.get("first_success_time_s"),
                          matches_registered_commands=receipt["matches_registered_commands"],
                          receipt=str(output / "receipt.json")), indent=2))
    return 0 if receipt["status"] == "completed" and receipt["success"] else 1


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--assets", type=Path, help="Pinned native PiPER asset directory")
    parser.add_argument("--output", type=Path, help="Fresh directory for the receipt and actual commands")
    parser.add_argument("--case", default="development-01-g0-yaw+24", help="Registered simulated scene")
    parser.add_argument("--list", action="store_true", help="List available scene IDs")
    args = parser.parse_args()
    if digest(ROOT / EVIDENCE) != EVIDENCE_HASH or digest(ROOT / SKILL) != SKILL_HASH:
        parser.error("Skill or evidence archive differs from the verified local release.")
    with ZipFile(ROOT / EVIDENCE) as evidence, ZipFile(ROOT / SKILL) as skill:
        cases = {entry["id"]: entry for entry in json.loads(evidence.read(PLAN))["cases"]}
        if args.list:
            print("\n".join(cases))
            return 0
        if args.case not in cases:
            parser.error("Unknown case. Use --list to see the 24 registered scene IDs.")
        if args.assets is None or args.output is None:
            parser.error("--assets and --output are required to simulate.")
        if not args.assets.is_dir() or args.output.exists():
            parser.error("Provide an existing asset directory and a fresh output directory.")
        ref_name = f"{STUDY}/development/{args.case}-{CONTROLLER}.json"
        reference = json.loads(evidence.read(ref_name))
        with tempfile.TemporaryDirectory(prefix="piper-local-sim-") as temporary:
            return execute(args, evidence, skill, Path(temporary), cases[args.case], reference)


if __name__ == "__main__":
    try:
        raise SystemExit(main())
    except (ValueError, ImportError, OSError) as error:
        raise SystemExit(f"Local PiPER simulator: {error}")