File size: 2,325 Bytes
49ef597
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Export cablepilot's released world model (weights/stringman_g1.pt) to JSON for the browser, plus test fixtures.

    cd ~/Projects/pilothub/cablepilot && .venv/bin/python ../mission/tools/export_model.py

Writes mission/core/model.json (weights, normaliser, config) and mission/tools/fixtures.json (a rollout computed by
PyTorch on the mission room's mounts, that tools/test.mjs checks the JS port against).
"""
import json
import sys
from pathlib import Path

import numpy as np
import torch
import yaml

CP = Path.cwd()
sys.path.insert(0, str(CP))
from cablepilot.model.learned import LearnedCDPRModel  # noqa: E402

OUT = Path(__file__).resolve().parents[1]
ckpt = torch.load(CP / "weights/stringman_g1.pt", weights_only=True)


def t(v):
    return np.asarray(v.detach().float().cpu()).round(7).tolist()


model = {
    "config": ckpt["config"],
    "jepa": {k: t(v) for k, v in ckpt["jepa"].items()},
    "prober": {k: t(v) for k, v in ckpt["prober"].items()},
    "norm": {k: t(v) for k, v in ckpt["norm"].items()},
    "source": "MacroSwan/cablepilot, the released world model",
}
(OUT / "core/model.json").write_text(json.dumps(model))

# the mission's fictional room (core/mission.js ROOM.mounts)
M = np.asarray([[2.22, 1.92, 2.52], [-2.22, 1.92, 2.48], [-2.22, -1.92, 2.55], [2.22, -1.92, 2.50]], np.float32)
wm = LearnedCDPRModel(ckpt, dt=0.2, mounts=M)
rng = np.random.default_rng(0)
x = np.zeros(14, np.float32)
x[0:3] = [0.1, 0.2, 1.3]
x[3:6] = [0.05, -0.02, 0.0]
x[6:8] = [0.02, -0.01]
x[8:10] = [0.05, 0.03]
cmd = np.array([0.05, -0.02, 0.0, 0.0, 0.0], np.float32)
wm.reset_history(x, cmd)
xs, cs = [], []
for k in range(3):                     # a little real history, so the encoder sees motion
    x2 = x.copy()
    x2[0:3] += (k + 1) * 0.1 * x[3:6]
    wm.observe(x2, cmd)
fut = (rng.normal(size=(3, 10, 5)) * [0.1, 0.1, 0.05, 0.3, 0.0]).astype(np.float32)
fut[..., 4] = [0.0, 0.5, 1.0][0]
out = wm.rollout(None, None, fut).numpy()
fx = {
    "mounts": M.tolist(),
    "states": np.stack(wm.states).tolist(),
    "cmds": np.stack(wm.cmds).tolist(),
    "future": fut.tolist(),
    "rollout": out.tolist(),
}
(OUT / "tools/fixtures.json").write_text(json.dumps(fx))
print("model.json", (OUT / "core/model.json").stat().st_size // 1024, "KB; fixture rollout", out.shape,
      "final p", out[0, -1, :3])