@infernetprotocol/liquid-agent 0.0.0-stage → 0.1.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +15 -2
- package/fixtures/contract.json +6 -0
- package/fixtures/invalid/episode-extra-field.json +275 -0
- package/fixtures/invalid/episode-no-capture.json +265 -0
- package/fixtures/invalid/episode-unknown-action.json +274 -0
- package/fixtures/invalid/model-manifest-bad-hash.json +92 -0
- package/fixtures/invalid/predict-negative-seq.json +48 -0
- package/fixtures/invalid/proposal-bad-abstain.json +31 -0
- package/fixtures/invalid/proposal-confidence-range.json +31 -0
- package/fixtures/invalid/session-bad-seq.json +17 -0
- package/fixtures/invalid/train-job-bad-family.json +44 -0
- package/fixtures/invalid/train-job-zero-lr.json +44 -0
- package/fixtures/valid/action-proposal.json +27 -0
- package/fixtures/valid/dataset-bundle.json +3747 -0
- package/fixtures/valid/dataset-manifest.json +126 -0
- package/fixtures/valid/episode.json +270 -0
- package/fixtures/valid/inference-session.json +13 -0
- package/fixtures/valid/model-manifest.json +88 -0
- package/fixtures/valid/predict-request.json +44 -0
- package/fixtures/valid/train-job.json +40 -0
- package/package.json +28 -3
- package/runtime/infernet_liquid/__init__.py +6 -0
- package/runtime/infernet_liquid/__main__.py +95 -0
- package/runtime/infernet_liquid/bench.py +125 -0
- package/runtime/infernet_liquid/bundle.py +63 -0
- package/runtime/infernet_liquid/canonical.py +75 -0
- package/runtime/infernet_liquid/data.py +121 -0
- package/runtime/infernet_liquid/errors.py +13 -0
- package/runtime/infernet_liquid/evaluate.py +145 -0
- package/runtime/infernet_liquid/export.py +136 -0
- package/runtime/infernet_liquid/features.py +105 -0
- package/runtime/infernet_liquid/models.py +63 -0
- package/runtime/infernet_liquid/schema.py +155 -0
- package/runtime/infernet_liquid/secretscan.py +64 -0
- package/runtime/infernet_liquid/server.py +251 -0
- package/runtime/infernet_liquid/sessions.py +279 -0
- package/runtime/infernet_liquid/synth.py +194 -0
- package/runtime/infernet_liquid/train.py +136 -0
- package/runtime/pyproject.toml +14 -0
- package/runtime/requirements-cuda.lock +461 -0
- package/runtime/requirements.lock +344 -0
- package/schemas/action-proposal.schema.json +40 -0
- package/schemas/common.schema.json +88 -0
- package/schemas/dataset-bundle.schema.json +15 -0
- package/schemas/dataset-manifest.schema.json +100 -0
- package/schemas/episode.schema.json +71 -0
- package/schemas/inference-session.schema.json +23 -0
- package/schemas/model-manifest.schema.json +103 -0
- package/schemas/predict-request.schema.json +20 -0
- package/schemas/train-job.schema.json +75 -0
- package/src/canonical.js +49 -0
- package/src/contract.js +62 -0
- package/src/dataset.js +133 -0
- package/src/index.js +13 -0
- package/src/runtime.js +206 -0
- package/src/secrets.js +79 -0
- package/src/validate.js +161 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""Synthetic mock-desktop traces for three workflows, with UI layout variants.
|
|
2
|
+
|
|
3
|
+
Variants 0-2 are the "seen" layouts (train and test draw from them); variant 3
|
|
4
|
+
renames key controls and reorders everything, as a layout-shift holdout. Every
|
|
5
|
+
trace is generated from a seed, so a bundle is reproducible byte for byte.
|
|
6
|
+
Nothing here touches a real site: apps are mock names and URLs end in .mock.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import random
|
|
10
|
+
from datetime import datetime, timedelta, timezone
|
|
11
|
+
|
|
12
|
+
from .bundle import build_bundle, seal_episode
|
|
13
|
+
|
|
14
|
+
PACE_MS = 3000
|
|
15
|
+
TASKS = ["email.reply_draft", "reddit.post_compose", "linkedin.notifications_review"]
|
|
16
|
+
PEOPLE = ["Dana", "Sam", "Lee", "Ari", "Kim", "Noor", "Jo", "Pat", "Ravi", "Mei", "Uma", "Tom"]
|
|
17
|
+
SUBJECTS = ["Lunch?", "Invoice 1042", "Re: roadmap", "Weekend plans", "Quick question", "Design review", "Flight details", "Team offsite", "Contract draft", "Hiring update"]
|
|
18
|
+
SUBS = ["r/selfhosted", "r/rust", "r/homelab", "r/linux", "r/python", "r/webdev", "r/sysadmin", "r/devops"]
|
|
19
|
+
EVENTS = ["commented on your post", "liked your comment", "viewed your profile", "mentioned you", "shared your post", "endorsed you"]
|
|
20
|
+
|
|
21
|
+
EMAIL_V = [
|
|
22
|
+
dict(inbox="Inbox", unread="Unread", reply="Reply", draft="Save draft", body="Message body", compose="Compose"),
|
|
23
|
+
dict(inbox="Mail", unread="New", reply="Reply", draft="Save as draft", body="Write your reply", compose="New message"),
|
|
24
|
+
dict(inbox="All inboxes", unread="Unread", reply="Reply to sender", draft="Save draft", body="Body", compose="Compose"),
|
|
25
|
+
dict(inbox="Messages", unread="Unseen", reply="Respond", draft="Keep as draft", body="Your message", compose="Write"),
|
|
26
|
+
]
|
|
27
|
+
REDDIT_V = [
|
|
28
|
+
dict(create="Create post", title="Title", body="Body text (optional)", preview="Preview", post="Post", joined="joined"),
|
|
29
|
+
dict(create="New post", title="Title", body="Text", preview="Preview", post="Publish", joined="joined"),
|
|
30
|
+
dict(create="Create a post", title="Post title", body="Body", preview="Preview post", post="Post", joined="member"),
|
|
31
|
+
dict(create="Start a thread", title="Headline", body="Post text", preview="See preview", post="Submit post", joined="subscribed"),
|
|
32
|
+
]
|
|
33
|
+
LINKEDIN_V = [
|
|
34
|
+
dict(notif="Notifications", new="New", end="No more new notifications"),
|
|
35
|
+
dict(notif="Notifications", new="Unread", end="You're all caught up"),
|
|
36
|
+
dict(notif="Alerts", new="New", end="No more new notifications"),
|
|
37
|
+
dict(notif="Activity", new="Fresh", end="Nothing new"),
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Trace:
|
|
42
|
+
def __init__(self, rng):
|
|
43
|
+
self.rng = rng
|
|
44
|
+
self.t = 0
|
|
45
|
+
self.steps = []
|
|
46
|
+
|
|
47
|
+
def add(self, obs, action, dt=None):
|
|
48
|
+
if self.steps:
|
|
49
|
+
self.t += dt if dt is not None else int(self.rng.lognormvariate(6.7, 0.45))
|
|
50
|
+
self.steps.append({"t_ms": self.t, "observation": obs, "action": action, "result": {"status": "ok"}})
|
|
51
|
+
|
|
52
|
+
def maybe_loading(self, app, url, p=0.45):
|
|
53
|
+
if self.rng.random() < p:
|
|
54
|
+
self.add({"app": app, "url_pattern": url, "loading": True, "elements": [{"role": "progressbar", "name": "Loading"}]},
|
|
55
|
+
{"type": "wait"}, dt=self.rng.randint(120, 400))
|
|
56
|
+
return self.rng.randint(500, 1600)
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _el(role, name):
|
|
61
|
+
return {"role": role, "name": name}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _shuffle(rng, xs):
|
|
65
|
+
xs = list(xs)
|
|
66
|
+
rng.shuffle(xs)
|
|
67
|
+
return xs
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def gen_email(rng, v):
|
|
71
|
+
V = EMAIL_V[v]
|
|
72
|
+
tr = Trace(rng)
|
|
73
|
+
home = [_el("link", V["inbox"]), _el("link", "Sent"), _el("link", "Drafts"), _el("button", V["compose"])]
|
|
74
|
+
home += [_el("link", n) for n in rng.sample(["Spam", "Archive", "Settings", "Calendar", "Contacts"], rng.randint(0, 3))]
|
|
75
|
+
inbox = _el("link", V["inbox"])
|
|
76
|
+
tr.add({"app": "mail", "url_pattern": "mail://home", "elements": _shuffle(rng, home)}, {"type": "click", "target": inbox})
|
|
77
|
+
wait = tr.maybe_loading("mail", "mail://inbox")
|
|
78
|
+
people = rng.sample(PEOPLE, rng.randint(3, 7))
|
|
79
|
+
rows = [_el("row", f"From {p}: {rng.choice(SUBJECTS)}") for p in people]
|
|
80
|
+
k = rng.randrange(len(rows))
|
|
81
|
+
rows[k] = _el("row", f"{V['unread']} · {rows[k]['name']}")
|
|
82
|
+
chrome = [_el("button", V["compose"]), inbox]
|
|
83
|
+
if rng.random() < 0.2:
|
|
84
|
+
# The unread message starts below the fold: the visible rows hold no unread marker, so scroll first.
|
|
85
|
+
above = [_el("row", f"From {p}: {rng.choice(SUBJECTS)}") for p in rng.sample(PEOPLE, 4)]
|
|
86
|
+
tr.add({"app": "mail", "url_pattern": "mail://inbox", "elements": above + chrome}, {"type": "scroll", "amount": 3}, dt=wait)
|
|
87
|
+
wait = None
|
|
88
|
+
tr.add({"app": "mail", "url_pattern": "mail://inbox", "elements": rows + chrome}, {"type": "click", "target": rows[k]}, dt=wait)
|
|
89
|
+
msg = _shuffle(rng, [_el("button", V["reply"]), _el("button", "Forward"), _el("button", "Archive"), _el("button", "Delete"), _el("button", "Mark unread")])
|
|
90
|
+
tr.add({"app": "mail", "url_pattern": "mail://message", "elements": msg + [_el("text", f"From {people[k]}")]}, {"type": "click", "target": _el("button", V["reply"])})
|
|
91
|
+
body = _el("textbox", V["body"])
|
|
92
|
+
compose = _shuffle(rng, [body, _el("button", "Send"), _el("button", V["draft"]), _el("button", "Discard"), _el("button", "Attach")])
|
|
93
|
+
tr.add({"app": "mail", "url_pattern": "mail://compose", "focus": body, "elements": compose},
|
|
94
|
+
{"type": "type", "target": body, "text_redacted": True}, dt=rng.randint(300, 900))
|
|
95
|
+
tr.add({"app": "mail", "url_pattern": "mail://compose", "focus": body, "elements": compose},
|
|
96
|
+
{"type": "click", "target": _el("button", V["draft"])}, dt=rng.randint(4000, 30000))
|
|
97
|
+
tr.add({"app": "mail", "url_pattern": "mail://compose", "elements": [_el("text", "Draft saved"), inbox]}, {"type": "done"})
|
|
98
|
+
return tr.steps, "low", "Reply to the unread message and save the reply as a draft"
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def gen_reddit(rng, v):
|
|
102
|
+
V = REDDIT_V[v]
|
|
103
|
+
tr = Trace(rng)
|
|
104
|
+
subs = rng.sample(SUBS, rng.randint(3, 5))
|
|
105
|
+
j = rng.randrange(len(subs))
|
|
106
|
+
links = [_el("link", s if i != j else f"{s} ({V['joined']})") for i, s in enumerate(subs)]
|
|
107
|
+
tr.add({"app": "browser", "url_pattern": "reddit.mock/", "elements": _shuffle(rng, links + [_el("textbox", "Search")])}, {"type": "click", "target": links[j]})
|
|
108
|
+
wait = tr.maybe_loading("browser", "reddit.mock/r/*")
|
|
109
|
+
posts = [_el("link", f"Post: {rng.choice(SUBJECTS)}") for _ in range(rng.randint(2, 4))]
|
|
110
|
+
sub_page = _shuffle(rng, [_el("button", V["create"]), _el("button", "Joined"), _el("link", "Hot"), _el("link", "New"), _el("link", "Top")] + posts)
|
|
111
|
+
tr.add({"app": "browser", "url_pattern": "reddit.mock/r/*", "elements": sub_page}, {"type": "click", "target": _el("button", V["create"])}, dt=wait)
|
|
112
|
+
title, body = _el("textbox", V["title"]), _el("textbox", V["body"])
|
|
113
|
+
form = _shuffle(rng, [title, body, _el("button", V["preview"]), _el("button", V["post"]), _el("button", "Save draft"), _el("link", "Rules")])
|
|
114
|
+
tr.add({"app": "browser", "url_pattern": "reddit.mock/r/*/submit", "focus": title, "elements": form}, {"type": "type", "target": title, "text_redacted": True})
|
|
115
|
+
tr.add({"app": "browser", "url_pattern": "reddit.mock/r/*/submit", "focus": title, "elements": form}, {"type": "type", "target": body, "text_redacted": True}, dt=rng.randint(2000, 9000))
|
|
116
|
+
tr.add({"app": "browser", "url_pattern": "reddit.mock/r/*/submit", "focus": body, "elements": form}, {"type": "click", "target": _el("button", V["preview"])}, dt=rng.randint(5000, 40000))
|
|
117
|
+
prev = _shuffle(rng, [_el("button", V["post"]), _el("button", "Edit"), _el("text", "Preview")])
|
|
118
|
+
# Pacing rule: post only once PACE_S seconds have passed since opening the preview. The screen
|
|
119
|
+
# does not change while waiting, so only accumulated elapsed time (irregularly sampled) decides.
|
|
120
|
+
obs = {"app": "browser", "url_pattern": "reddit.mock/r/*/submit", "elements": prev}
|
|
121
|
+
gap = int(rng.uniform(250, 2600))
|
|
122
|
+
elapsed = gap
|
|
123
|
+
while elapsed < PACE_MS:
|
|
124
|
+
tr.add(obs, {"type": "wait"}, dt=gap)
|
|
125
|
+
gap = int(rng.uniform(250, 2600))
|
|
126
|
+
elapsed += gap
|
|
127
|
+
tr.add(obs, {"type": "post", "target": _el("button", V["post"])}, dt=gap)
|
|
128
|
+
tr.add({"app": "browser", "url_pattern": "reddit.mock/r/*/comments/*", "elements": [_el("text", "Posted"), _el("link", "Hot")]}, {"type": "done"})
|
|
129
|
+
return tr.steps, "medium", "Compose a post in the joined subreddit, preview it, then publish (needs approval)"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def gen_linkedin(rng, v):
|
|
133
|
+
V = LINKEDIN_V[v]
|
|
134
|
+
tr = Trace(rng)
|
|
135
|
+
notif = _el("link", V["notif"])
|
|
136
|
+
feed = [_el("link", "Home"), _el("link", "My Network"), _el("link", "Jobs"), notif, _el("link", "Messaging")]
|
|
137
|
+
feed += [_el("article", f"{rng.choice(PEOPLE)} posted: {rng.choice(SUBJECTS)}") for _ in range(rng.randint(2, 3))]
|
|
138
|
+
tr.add({"app": "browser", "url_pattern": "linkedin.mock/feed", "elements": _shuffle(rng, feed)}, {"type": "click", "target": notif})
|
|
139
|
+
wait = tr.maybe_loading("browser", "linkedin.mock/notifications")
|
|
140
|
+
pages = rng.randint(1, 4)
|
|
141
|
+
for _ in range(pages):
|
|
142
|
+
items = [_el("listitem", f"{rng.choice(PEOPLE)} {rng.choice(EVENTS)}") for _ in range(rng.randint(3, 4))]
|
|
143
|
+
tr.add({"app": "browser", "url_pattern": "linkedin.mock/notifications", "elements": items + [notif]}, {"type": "scroll", "amount": 5}, dt=wait)
|
|
144
|
+
wait = None
|
|
145
|
+
items = [_el("listitem", f"{rng.choice(PEOPLE)} {rng.choice(EVENTS)}") for _ in range(rng.randint(2, 3))]
|
|
146
|
+
k = rng.randrange(len(items))
|
|
147
|
+
items[k] = _el("listitem", f"{V['new']} · {items[k]['name']}")
|
|
148
|
+
tr.add({"app": "browser", "url_pattern": "linkedin.mock/notifications", "elements": items + [_el("text", V["end"]), notif]}, {"type": "click", "target": items[k]})
|
|
149
|
+
tr.add({"app": "browser", "url_pattern": "linkedin.mock/notifications/*", "elements": [_el("button", "Like"), _el("button", "Reply"), _el("text", items[k]["name"])]}, {"type": "done"})
|
|
150
|
+
return tr.steps, "low", "Open notifications, scroll to the end of the new ones and open the newest"
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
GENERATORS = {"email.reply_draft": gen_email, "reddit.post_compose": gen_reddit, "linkedin.notifications_review": gen_linkedin}
|
|
154
|
+
APPS = {"email.reply_draft": "mail", "reddit.post_compose": "browser", "linkedin.notifications_review": "browser"}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def make_episodes(per_task=160, seed=7, variants=(0, 1, 2), prefix="syn"):
|
|
158
|
+
base = datetime(2026, 10, 25, 12, 0, tzinfo=timezone.utc)
|
|
159
|
+
out = []
|
|
160
|
+
for ti, task in enumerate(TASKS):
|
|
161
|
+
rng = random.Random(f"{seed}:{task}:{prefix}")
|
|
162
|
+
for i in range(per_task):
|
|
163
|
+
v = variants[i % len(variants)]
|
|
164
|
+
steps, risk, goal = GENERATORS[task](rng, v)
|
|
165
|
+
short = task.split(".")[0]
|
|
166
|
+
ep = {
|
|
167
|
+
"episode_id": f"ep_{prefix}_{short}_{seed}_{i:04d}",
|
|
168
|
+
"task": task,
|
|
169
|
+
"created_at": (base + timedelta(minutes=ti * 10000 + i)).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
|
170
|
+
"split_group": f"{short}-s{i // 4}",
|
|
171
|
+
"variant": f"v{v}",
|
|
172
|
+
"capture": {"consent_id": "synthetic", "redaction_profile": "strict", "device_local": True, "source": "synthetic", "app_scope": [APPS[task]]},
|
|
173
|
+
"steps": steps,
|
|
174
|
+
"labels": {"success": True, "reviewed": True, "risk": risk, "goal": goal},
|
|
175
|
+
}
|
|
176
|
+
out.append(seal_episode(ep))
|
|
177
|
+
return out
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def make_bundle(per_task=160, seed=7, shifted=False):
|
|
181
|
+
variants = (3,) if shifted else (0, 1, 2)
|
|
182
|
+
prefix = "shift" if shifted else "syn"
|
|
183
|
+
eps = make_episodes(per_task, seed, variants, prefix)
|
|
184
|
+
consent = {"consent_id": "synthetic", "purposes": ["model_training", "evaluation"], "granted_at": "2026-10-25T12:00:00Z"}
|
|
185
|
+
return build_bundle(
|
|
186
|
+
eps,
|
|
187
|
+
dataset_id=f"ds_{prefix}_{seed}_{per_task}",
|
|
188
|
+
created_at="2026-10-25T12:00:00Z",
|
|
189
|
+
consent=consent,
|
|
190
|
+
name="Synthetic mock desktop traces" + (" (layout shift)" if shifted else ""),
|
|
191
|
+
seed=seed,
|
|
192
|
+
validation=0.0 if shifted else 0.15,
|
|
193
|
+
test=1.0 if shifted else 0.25,
|
|
194
|
+
)
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""Training loop: identical splits, seed and budget for every family."""
|
|
2
|
+
|
|
3
|
+
import random
|
|
4
|
+
import time
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
from torch import nn
|
|
9
|
+
|
|
10
|
+
from .data import collate, encode_episode, split_episodes
|
|
11
|
+
from .errors import LiquidError
|
|
12
|
+
from .models import TemporalPolicy
|
|
13
|
+
|
|
14
|
+
DEFAULTS = {
|
|
15
|
+
"hidden_units": 64,
|
|
16
|
+
"epochs": 40,
|
|
17
|
+
"learning_rate": 0.005,
|
|
18
|
+
"batch_size": 16,
|
|
19
|
+
"seed": 0,
|
|
20
|
+
"feature_dim": 256,
|
|
21
|
+
"backbone_units": 64,
|
|
22
|
+
"early_stop_patience": 8,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def now_iso():
|
|
27
|
+
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def pick_device(want):
|
|
31
|
+
if want in (None, "auto", "cpu"):
|
|
32
|
+
if want == "auto" and torch.cuda.is_available():
|
|
33
|
+
return "cuda"
|
|
34
|
+
return "cpu"
|
|
35
|
+
if want == "cuda" and not torch.cuda.is_available():
|
|
36
|
+
raise LiquidError("E_BAD_REQUEST", "cuda requested but no CUDA device is available")
|
|
37
|
+
if want == "mps" and not (getattr(torch.backends, "mps", None) and torch.backends.mps.is_available()):
|
|
38
|
+
raise LiquidError("E_BAD_REQUEST", "mps requested but not available")
|
|
39
|
+
return want
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def forward_batch(model, batch):
|
|
43
|
+
B, T, _ = batch["x"].shape
|
|
44
|
+
h = model.init_state(B).to(batch["x"].device)
|
|
45
|
+
type_logits, ptr_logits = [], []
|
|
46
|
+
for t in range(T):
|
|
47
|
+
out, h_new = model.step(batch["x"][:, t], h, batch["dt"][:, t])
|
|
48
|
+
keep = batch["smask"][:, t].unsqueeze(-1)
|
|
49
|
+
h = torch.where(keep, h_new, h)
|
|
50
|
+
tl, pl = model.heads(out, batch["elems"][:, t], batch["emask"][:, t])
|
|
51
|
+
type_logits.append(tl)
|
|
52
|
+
ptr_logits.append(pl)
|
|
53
|
+
return torch.stack(type_logits, 1), torch.stack(ptr_logits, 1)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def loss_of(type_logits, ptr_logits, batch):
|
|
57
|
+
ce = nn.CrossEntropyLoss(ignore_index=-100)
|
|
58
|
+
lt = ce(type_logits.reshape(-1, type_logits.shape[-1]), batch["y_type"].reshape(-1))
|
|
59
|
+
yp = batch["y_ptr"]
|
|
60
|
+
if (yp != -100).any():
|
|
61
|
+
lp = ce(ptr_logits.reshape(-1, ptr_logits.shape[-1]), yp.reshape(-1))
|
|
62
|
+
else:
|
|
63
|
+
lp = torch.zeros(())
|
|
64
|
+
return lt + lp
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _batches(items, size, rng):
|
|
68
|
+
idx = list(range(len(items)))
|
|
69
|
+
rng.shuffle(idx)
|
|
70
|
+
for i in range(0, len(idx), size):
|
|
71
|
+
yield [items[j] for j in idx[i:i + size]]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def train(bundle, family, hyper=None, device="auto", max_threads=None, on_event=None, cancelled=None, deadline=None):
|
|
75
|
+
"""Train one model. Returns (model, info). Raises E_CANCELLED/E_TIMEOUT."""
|
|
76
|
+
from .evaluate import evaluate_encoded
|
|
77
|
+
|
|
78
|
+
hp = {**DEFAULTS, **(hyper or {})}
|
|
79
|
+
dev = pick_device(device)
|
|
80
|
+
if max_threads:
|
|
81
|
+
torch.set_num_threads(int(max_threads))
|
|
82
|
+
seed = int(hp["seed"])
|
|
83
|
+
torch.manual_seed(seed)
|
|
84
|
+
rng = random.Random(seed)
|
|
85
|
+
splits = split_episodes(bundle)
|
|
86
|
+
if not splits["train"]:
|
|
87
|
+
raise LiquidError("E_SCHEMA", "dataset has no train episodes")
|
|
88
|
+
enc = {k: [encode_episode(e, hp["feature_dim"]) for e in v] for k, v in splits.items()}
|
|
89
|
+
model = TemporalPolicy(family, hp["feature_dim"], hp["hidden_units"], hp["backbone_units"]).to(dev)
|
|
90
|
+
opt = torch.optim.Adam(model.parameters(), lr=hp["learning_rate"])
|
|
91
|
+
started, t0 = now_iso(), time.monotonic()
|
|
92
|
+
best, best_state, bad = -1.0, None, 0
|
|
93
|
+
val_set = enc["validation"] or enc["train"]
|
|
94
|
+
epochs_run = 0
|
|
95
|
+
for epoch in range(1, hp["epochs"] + 1):
|
|
96
|
+
model.train()
|
|
97
|
+
total, n = 0.0, 0
|
|
98
|
+
for chunk in _batches(enc["train"], hp["batch_size"], rng):
|
|
99
|
+
if cancelled and cancelled():
|
|
100
|
+
raise LiquidError("E_CANCELLED", "training cancelled")
|
|
101
|
+
if deadline and time.monotonic() > deadline:
|
|
102
|
+
raise LiquidError("E_TIMEOUT", "training exceeded its timeout")
|
|
103
|
+
batch = {k: v.to(dev) for k, v in collate(chunk).items()}
|
|
104
|
+
tl, pl = forward_batch(model, batch)
|
|
105
|
+
loss = loss_of(tl, pl, batch)
|
|
106
|
+
opt.zero_grad()
|
|
107
|
+
loss.backward()
|
|
108
|
+
nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
|
109
|
+
opt.step()
|
|
110
|
+
total += loss.item() * len(chunk)
|
|
111
|
+
n += len(chunk)
|
|
112
|
+
epochs_run = epoch
|
|
113
|
+
val = evaluate_encoded(model, val_set, device=dev, timing=False)
|
|
114
|
+
if on_event:
|
|
115
|
+
on_event({"event": "epoch", "family": family, "epoch": epoch, "loss": round(total / max(n, 1), 6), "val_step_accuracy": val["step_accuracy"]})
|
|
116
|
+
score = val["step_accuracy"] + 1e-3 * val["episode_success"]
|
|
117
|
+
if score > best + 1e-9:
|
|
118
|
+
best, bad = score, 0
|
|
119
|
+
best_state = {k: v.detach().clone() for k, v in model.state_dict().items()}
|
|
120
|
+
else:
|
|
121
|
+
bad += 1
|
|
122
|
+
if hp["early_stop_patience"] and bad >= hp["early_stop_patience"]:
|
|
123
|
+
break
|
|
124
|
+
if best_state is not None:
|
|
125
|
+
model.load_state_dict(best_state)
|
|
126
|
+
model.to("cpu").eval()
|
|
127
|
+
info = {
|
|
128
|
+
"hyperparameters": hp,
|
|
129
|
+
"device": dev,
|
|
130
|
+
"started_at": started,
|
|
131
|
+
"finished_at": now_iso(),
|
|
132
|
+
"train_seconds": round(time.monotonic() - t0, 3),
|
|
133
|
+
"epochs_run": epochs_run,
|
|
134
|
+
"encoded": enc,
|
|
135
|
+
}
|
|
136
|
+
return model, info
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "infernet-liquid"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Infernet temporal-policy runtime: CfC/LTC liquid networks and a GRU baseline over a stdio JSON contract."
|
|
5
|
+
requires-python = ">=3.11"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
# Exact pins. requirements.lock (uv pip compile --universal --generate-hashes)
|
|
8
|
+
# is what installs; these lines exist so the lock can be regenerated.
|
|
9
|
+
dependencies = [
|
|
10
|
+
"torch==2.14.1",
|
|
11
|
+
"ncps==1.0.1",
|
|
12
|
+
"safetensors==0.8.0",
|
|
13
|
+
"numpy==2.4.6",
|
|
14
|
+
]
|