@infernetprotocol/training 0.1.29 → 0.1.80
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/package.json +5 -2
- package/runners/peft_generate.py +83 -0
- package/runners/peft_train.py +221 -0
- package/src/backends/liquid.js +82 -0
- package/src/backends/peft.js +223 -0
- package/src/index.js +24 -0
- package/src/protocol.js +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infernetprotocol/training",
|
|
3
|
-
"
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.1.80",
|
|
4
5
|
"description": "Training orchestration for Infernet — pluggable backends (DeepSpeed/FSDP for trusted clusters, OpenDiLoCo for cross-provider async, Petals for trustless P2P fine-tunes) over a stable async-iterator API.",
|
|
5
6
|
"license": "MIT",
|
|
6
7
|
"type": "module",
|
|
@@ -11,13 +12,15 @@
|
|
|
11
12
|
},
|
|
12
13
|
"files": [
|
|
13
14
|
"src/**/*.js",
|
|
15
|
+
"runners/*.py",
|
|
14
16
|
"README.md"
|
|
15
17
|
],
|
|
16
18
|
"engines": {
|
|
17
19
|
"node": ">=18"
|
|
18
20
|
},
|
|
19
21
|
"dependencies": {
|
|
20
|
-
"@infernetprotocol/engine": "0.1.
|
|
22
|
+
"@infernetprotocol/engine": "0.1.80",
|
|
23
|
+
"@infernetprotocol/liquid-agent": "0.1.80"
|
|
21
24
|
},
|
|
22
25
|
"repository": {
|
|
23
26
|
"type": "git",
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Answer chat messages with a base model plus a LoRA adapter (workload infernet.text.infer).
|
|
3
|
+
|
|
4
|
+
python peft_generate.py request.json
|
|
5
|
+
|
|
6
|
+
request.json:
|
|
7
|
+
{
|
|
8
|
+
"adapter_dir": "adapter", local dir: adapter_config.json, adapter_model.safetensors, manifest.json
|
|
9
|
+
"base_model": "HuggingFaceTB/SmolLM2-135M-Instruct", optional; else the adapter manifest's
|
|
10
|
+
"messages": [{"role": "user", "content": "..."}], a system message is added from the manifest if absent
|
|
11
|
+
"max_new_tokens": 350,
|
|
12
|
+
"threads": 4
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
Relative paths resolve against the request file's directory. Local files only.
|
|
16
|
+
Prints one JSON object on stdout:
|
|
17
|
+
{"v": 1, "type": "answer", "text": "...", "model": "<base>", "adapter": "sha256:<hex>", "tokens": N, "seconds": S}
|
|
18
|
+
or {"v": 1, "type": "error", "message": "..."} and exits 1. Logs go to stderr.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
import hashlib
|
|
22
|
+
import json
|
|
23
|
+
import os
|
|
24
|
+
import sys
|
|
25
|
+
import time
|
|
26
|
+
import traceback
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def resolve(base_dir, p):
|
|
31
|
+
p = str(p).removeprefix("file://")
|
|
32
|
+
if "://" in p:
|
|
33
|
+
raise ValueError("adapter_dir must be a local path")
|
|
34
|
+
return Path(p) if os.path.isabs(p) else base_dir / p
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def main():
|
|
38
|
+
import torch
|
|
39
|
+
from peft import PeftModel
|
|
40
|
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
41
|
+
|
|
42
|
+
req_path = Path(sys.argv[1] if len(sys.argv) > 1 else "request.json").resolve()
|
|
43
|
+
req = json.loads(req_path.read_text())
|
|
44
|
+
adapter = resolve(req_path.parent, req["adapter_dir"])
|
|
45
|
+
manifest = {}
|
|
46
|
+
if (adapter / "manifest.json").exists():
|
|
47
|
+
manifest = json.loads((adapter / "manifest.json").read_text())
|
|
48
|
+
base = req.get("base_model") or manifest.get("base_model")
|
|
49
|
+
if not base:
|
|
50
|
+
raise ValueError("base_model is missing (not in the request or the adapter manifest)")
|
|
51
|
+
weights = adapter / "adapter_model.safetensors"
|
|
52
|
+
digest = "sha256:" + hashlib.sha256(weights.read_bytes()).hexdigest()
|
|
53
|
+
|
|
54
|
+
messages = [m for m in req.get("messages") or [] if isinstance(m, dict) and isinstance(m.get("content"), str)]
|
|
55
|
+
if not messages:
|
|
56
|
+
raise ValueError("messages[] is required")
|
|
57
|
+
if messages[0].get("role") != "system" and manifest.get("system_prompt"):
|
|
58
|
+
messages = [{"role": "system", "content": manifest["system_prompt"]}] + messages
|
|
59
|
+
|
|
60
|
+
torch.set_num_threads(int(req.get("threads") or min(4, os.cpu_count() or 1)))
|
|
61
|
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
62
|
+
t0 = time.time()
|
|
63
|
+
tok = AutoTokenizer.from_pretrained(adapter if (adapter / "tokenizer_config.json").exists() else base)
|
|
64
|
+
model = AutoModelForCausalLM.from_pretrained(base, dtype=torch.float32).to(device)
|
|
65
|
+
model = PeftModel.from_pretrained(model, adapter).eval()
|
|
66
|
+
prompt = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
67
|
+
ids = tok(prompt, return_tensors="pt").to(device)
|
|
68
|
+
with torch.no_grad():
|
|
69
|
+
# Greedy: sampling on a small model echoes the question and rambles.
|
|
70
|
+
out = model.generate(**ids, max_new_tokens=int(req.get("max_new_tokens") or 350), do_sample=False, repetition_penalty=1.1)
|
|
71
|
+
new = out[0][ids["input_ids"].shape[1]:]
|
|
72
|
+
text = tok.decode(new, skip_special_tokens=True).strip()
|
|
73
|
+
print(json.dumps({"v": 1, "type": "answer", "text": text, "model": base, "adapter": digest,
|
|
74
|
+
"tokens": int(new.shape[0]), "seconds": round(time.time() - t0, 2)}), flush=True)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if __name__ == "__main__":
|
|
78
|
+
try:
|
|
79
|
+
main()
|
|
80
|
+
except Exception as e:
|
|
81
|
+
traceback.print_exc(file=sys.stderr)
|
|
82
|
+
print(json.dumps({"v": 1, "type": "error", "message": str(e)}), flush=True)
|
|
83
|
+
sys.exit(1)
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""LoRA-tune a causal language model from a job file, on CPU (or CUDA if present).
|
|
3
|
+
|
|
4
|
+
python peft_train.py job.json
|
|
5
|
+
|
|
6
|
+
The runner for @infernetprotocol/training's `peft` backend (workload
|
|
7
|
+
infernet.text.train). `job.json` is that package's job config:
|
|
8
|
+
|
|
9
|
+
{
|
|
10
|
+
"id": "...", optional
|
|
11
|
+
"name": "business-strategist-agent",
|
|
12
|
+
"kind": "lora",
|
|
13
|
+
"base_model": "HuggingFaceTB/SmolLM2-135M-Instruct",
|
|
14
|
+
"system_prompt": "You are ...", optional
|
|
15
|
+
"dataset": { "url": "corpus.jsonl", "sha256": "<hex>"? }, jsonl of {title, text}
|
|
16
|
+
"hyperparams": { "lr", "batch_size", "epochs", "max_steps", "seq_len", "lora_r", "threads", "seed" },
|
|
17
|
+
"output": { "destination_url": "out/adapter" }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Relative paths resolve against the job file's directory. Progress goes to
|
|
21
|
+
stdout as training-protocol v1 NDJSON (meta / step / eval / checkpoint /
|
|
22
|
+
done / error); human-readable logs go to stderr. The output dir holds a PEFT
|
|
23
|
+
adapter (adapter_model.safetensors + adapter_config.json), the tokenizer and
|
|
24
|
+
manifest.json; the checkpoint event's `model` is sha256 of the adapter weights.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import hashlib
|
|
28
|
+
import json
|
|
29
|
+
import math
|
|
30
|
+
import os
|
|
31
|
+
import random
|
|
32
|
+
import sys
|
|
33
|
+
import time
|
|
34
|
+
import traceback
|
|
35
|
+
import uuid
|
|
36
|
+
from datetime import datetime, timezone
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
|
|
39
|
+
DEFAULT_SYSTEM = (
|
|
40
|
+
"You are a concise, practical assistant grounded in what independent writers are publishing."
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def now():
|
|
45
|
+
return datetime.now(timezone.utc).isoformat()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def emit(job_id, type_, **fields):
|
|
49
|
+
print(json.dumps({"v": 1, "type": type_, "id": job_id, **fields}), flush=True)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def log(msg):
|
|
53
|
+
print(msg, file=sys.stderr, flush=True)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def resolve(base_dir, p):
|
|
57
|
+
p = p.removeprefix("file://")
|
|
58
|
+
return Path(p) if os.path.isabs(p) else base_dir / p
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def load_dataset(spec, base_dir):
|
|
62
|
+
# Local files only: the runner never fetches from the network. A remote
|
|
63
|
+
# corpus is downloaded and hash-checked by the caller (c0mpute's text
|
|
64
|
+
# worker) and handed over as a path, so a job cannot point this process at
|
|
65
|
+
# an arbitrary URL.
|
|
66
|
+
url = spec["url"]
|
|
67
|
+
if "://" in url and not url.startswith("file://"):
|
|
68
|
+
raise ValueError("dataset.url must be a local path; download remote corpora first")
|
|
69
|
+
raw = resolve(base_dir, url).read_bytes()
|
|
70
|
+
digest = hashlib.sha256(raw).hexdigest()
|
|
71
|
+
want = (spec.get("sha256") or "").removeprefix("sha256:")
|
|
72
|
+
if want and digest != want:
|
|
73
|
+
raise ValueError(f"dataset sha256 {digest} differs from the job's {want}")
|
|
74
|
+
docs = []
|
|
75
|
+
for line in raw.decode().splitlines():
|
|
76
|
+
if not line.strip():
|
|
77
|
+
continue
|
|
78
|
+
d = json.loads(line)
|
|
79
|
+
if d.get("text"):
|
|
80
|
+
docs.append({"title": d.get("title") or "", "text": d["text"]})
|
|
81
|
+
if len(docs) < 2:
|
|
82
|
+
raise ValueError("dataset needs at least 2 documents with text")
|
|
83
|
+
return docs, digest
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def main():
|
|
87
|
+
import torch
|
|
88
|
+
from peft import LoraConfig, get_peft_model
|
|
89
|
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
90
|
+
|
|
91
|
+
job_path = Path(sys.argv[1] if len(sys.argv) > 1 else "job.json").resolve()
|
|
92
|
+
job = json.loads(job_path.read_text())
|
|
93
|
+
base_dir = job_path.parent
|
|
94
|
+
job_id = job.get("id") or str(uuid.uuid4())
|
|
95
|
+
hp = {
|
|
96
|
+
"lr": 2e-4, "batch_size": 4, "epochs": 1, "max_steps": None,
|
|
97
|
+
"seq_len": 256, "lora_r": 8, "threads": min(4, os.cpu_count() or 1), "seed": 0,
|
|
98
|
+
**(job.get("hyperparams") or {}),
|
|
99
|
+
}
|
|
100
|
+
system = job.get("system_prompt") or DEFAULT_SYSTEM
|
|
101
|
+
out_dir = resolve(base_dir, job["output"]["destination_url"])
|
|
102
|
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
103
|
+
|
|
104
|
+
random.seed(hp["seed"])
|
|
105
|
+
torch.manual_seed(hp["seed"])
|
|
106
|
+
torch.set_num_threads(int(hp["threads"]))
|
|
107
|
+
|
|
108
|
+
def to_chat(tok, doc):
|
|
109
|
+
# Each document becomes one briefing exchange, so the tune keeps the
|
|
110
|
+
# instruct model's chat format instead of drifting into raw prose.
|
|
111
|
+
return tok.apply_chat_template(
|
|
112
|
+
[
|
|
113
|
+
{"role": "system", "content": system},
|
|
114
|
+
{"role": "user", "content": f"Brief me on: {doc['title']}" if doc["title"] else "Brief me."},
|
|
115
|
+
{"role": "assistant", "content": doc["text"]},
|
|
116
|
+
],
|
|
117
|
+
tokenize=False,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
def pack(tok, docs, seq_len):
|
|
121
|
+
ids = []
|
|
122
|
+
for d in docs:
|
|
123
|
+
ids.extend(tok(to_chat(tok, d), add_special_tokens=False)["input_ids"])
|
|
124
|
+
blocks = [ids[i : i + seq_len] for i in range(0, len(ids) - seq_len + 1, seq_len)]
|
|
125
|
+
return torch.tensor(blocks, dtype=torch.long)
|
|
126
|
+
|
|
127
|
+
@torch.no_grad()
|
|
128
|
+
def perplexity(model, blocks, batch_size):
|
|
129
|
+
model.eval()
|
|
130
|
+
losses = [
|
|
131
|
+
model(input_ids=blocks[i : i + batch_size].to(device), labels=blocks[i : i + batch_size].to(device)).loss.item()
|
|
132
|
+
for i in range(0, len(blocks), batch_size)
|
|
133
|
+
]
|
|
134
|
+
model.train()
|
|
135
|
+
return math.exp(sum(losses) / max(len(losses), 1))
|
|
136
|
+
|
|
137
|
+
docs, dataset_sha = load_dataset(job["dataset"], base_dir)
|
|
138
|
+
random.shuffle(docs)
|
|
139
|
+
held = max(1, len(docs) // 20)
|
|
140
|
+
tok = AutoTokenizer.from_pretrained(job["base_model"])
|
|
141
|
+
train_blocks = pack(tok, docs[held:], int(hp["seq_len"]))
|
|
142
|
+
eval_blocks = pack(tok, docs[:held], int(hp["seq_len"]))
|
|
143
|
+
if len(train_blocks) == 0:
|
|
144
|
+
raise ValueError("dataset too small for one training block; lower seq_len or add documents")
|
|
145
|
+
steps_per_epoch = math.ceil(len(train_blocks) / int(hp["batch_size"]))
|
|
146
|
+
total = int(hp["max_steps"] or steps_per_epoch * int(hp["epochs"]))
|
|
147
|
+
|
|
148
|
+
emit(job_id, "meta", started_at=now(), backend="peft", world_size=1, kind=job.get("kind", "lora"),
|
|
149
|
+
base_model=job["base_model"], device=device, total_steps=total, documents=len(docs),
|
|
150
|
+
dataset_sha256=dataset_sha)
|
|
151
|
+
log(f"{len(docs)} docs -> {len(train_blocks)} train / {len(eval_blocks)} eval blocks of {hp['seq_len']} tokens; {total} steps on {device}")
|
|
152
|
+
|
|
153
|
+
model = AutoModelForCausalLM.from_pretrained(job["base_model"], dtype=torch.float32).to(device)
|
|
154
|
+
# Recompute activations in the backward pass: roughly halves peak RAM, so
|
|
155
|
+
# the job fits beside other work on a shared box.
|
|
156
|
+
model.gradient_checkpointing_enable()
|
|
157
|
+
model.enable_input_require_grads()
|
|
158
|
+
model = get_peft_model(model, LoraConfig(
|
|
159
|
+
r=int(hp["lora_r"]), lora_alpha=int(hp["lora_r"]) * 2, lora_dropout=0.05,
|
|
160
|
+
target_modules=hp.get("target_modules") or ["q_proj", "k_proj", "v_proj", "o_proj"],
|
|
161
|
+
task_type="CAUSAL_LM",
|
|
162
|
+
))
|
|
163
|
+
log(f"trainable params: {sum(p.numel() for p in model.parameters() if p.requires_grad):,}")
|
|
164
|
+
|
|
165
|
+
opt = torch.optim.AdamW([p for p in model.parameters() if p.requires_grad], lr=float(hp["lr"]))
|
|
166
|
+
sched = torch.optim.lr_scheduler.LambdaLR(opt, lambda s: min(1.0, (s + 1) / 10) * max(0.0, 1 - s / total))
|
|
167
|
+
|
|
168
|
+
if len(eval_blocks):
|
|
169
|
+
emit(job_id, "eval", step=0, metrics={"perplexity": perplexity(model, eval_blocks, int(hp["batch_size"]))})
|
|
170
|
+
|
|
171
|
+
step, t0 = 0, time.time()
|
|
172
|
+
model.train()
|
|
173
|
+
while step < total:
|
|
174
|
+
order = torch.randperm(len(train_blocks))
|
|
175
|
+
for i in range(0, len(order), int(hp["batch_size"])):
|
|
176
|
+
batch = train_blocks[order[i : i + int(hp["batch_size"])]].to(device)
|
|
177
|
+
loss = model(input_ids=batch, labels=batch).loss
|
|
178
|
+
loss.backward()
|
|
179
|
+
grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0).item()
|
|
180
|
+
opt.step(); sched.step(); opt.zero_grad()
|
|
181
|
+
step += 1
|
|
182
|
+
emit(job_id, "step", step=step, total_steps=total, loss=round(loss.item(), 4), lr=sched.get_last_lr()[0],
|
|
183
|
+
grad_norm=round(grad_norm, 3),
|
|
184
|
+
throughput=round(step * int(hp["batch_size"]) * int(hp["seq_len"]) / (time.time() - t0), 1))
|
|
185
|
+
if step >= total:
|
|
186
|
+
break
|
|
187
|
+
|
|
188
|
+
if len(eval_blocks):
|
|
189
|
+
emit(job_id, "eval", step=step, metrics={"perplexity": perplexity(model, eval_blocks, int(hp["batch_size"]))})
|
|
190
|
+
|
|
191
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
192
|
+
model.save_pretrained(out_dir)
|
|
193
|
+
tok.save_pretrained(out_dir)
|
|
194
|
+
weights = out_dir / "adapter_model.safetensors"
|
|
195
|
+
digest = hashlib.sha256(weights.read_bytes()).hexdigest()
|
|
196
|
+
(out_dir / "manifest.json").write_text(json.dumps({
|
|
197
|
+
"name": job.get("name") or "adapter",
|
|
198
|
+
"kind": job.get("kind", "lora"),
|
|
199
|
+
"base_model": job["base_model"],
|
|
200
|
+
"adapter_sha256": digest,
|
|
201
|
+
"dataset_sha256": dataset_sha,
|
|
202
|
+
"documents": len(docs),
|
|
203
|
+
"steps": step,
|
|
204
|
+
"system_prompt": system,
|
|
205
|
+
"hyperparams": hp,
|
|
206
|
+
"finished_at": now(),
|
|
207
|
+
}, indent=2) + "\n")
|
|
208
|
+
emit(job_id, "checkpoint", step=step, path=str(out_dir), size_bytes=weights.stat().st_size,
|
|
209
|
+
model=f"sha256:{digest}")
|
|
210
|
+
emit(job_id, "done", reason="complete", final_step=step, finished_at=now())
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
if __name__ == "__main__":
|
|
214
|
+
job_id = "unknown"
|
|
215
|
+
try:
|
|
216
|
+
main()
|
|
217
|
+
except Exception as e: # report as protocol events so callers never parse a traceback
|
|
218
|
+
traceback.print_exc(file=sys.stderr)
|
|
219
|
+
emit(job_id, "error", message=str(e), code="E_RUNTIME")
|
|
220
|
+
emit(job_id, "done", reason="error", finished_at=now())
|
|
221
|
+
sys.exit(1)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Liquid backend — temporal-policy training (CfC / LTC liquid networks, GRU
|
|
3
|
+
* baseline) on one provider, CPU or GPU. Workload `infernet.temporal.train`.
|
|
4
|
+
*
|
|
5
|
+
* Runs the isolated infernet-liquid runtime from @infernetprotocol/liquid-agent
|
|
6
|
+
* and maps its events onto this package's v1 training protocol:
|
|
7
|
+
* epoch → step (step = epoch, loss, val_step_accuracy)
|
|
8
|
+
* result → eval + checkpoint (the exported model dir) + done:complete
|
|
9
|
+
* error → error + done:error (done:cancel for E_CANCELLED)
|
|
10
|
+
*
|
|
11
|
+
* Job config:
|
|
12
|
+
* { kind: "temporal-policy", dataset: { url: <bundle path>, sha256? },
|
|
13
|
+
* output: { destination_url: <dir> }, family?: "cfc"|"ltc"|"gru",
|
|
14
|
+
* hyperparams?: {...}, job?: TrainJobV1 }
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { randomUUID } from "node:crypto";
|
|
18
|
+
import { AsyncQueue } from "@infernetprotocol/engine";
|
|
19
|
+
import { LiquidRuntime } from "@infernetprotocol/liquid-agent";
|
|
20
|
+
import { TMSG, TRAINING_PROTOCOL_VERSION } from "../protocol.js";
|
|
21
|
+
|
|
22
|
+
export function createLiquidBackend({ runtimeFactory = () => new LiquidRuntime() } = {}) {
|
|
23
|
+
let runtime = null;
|
|
24
|
+
return {
|
|
25
|
+
kind: "liquid",
|
|
26
|
+
start({ config = {}, id } = {}) {
|
|
27
|
+
const jobId = id ?? randomUUID();
|
|
28
|
+
const stream = new AsyncQueue();
|
|
29
|
+
const push = (msg) => stream.push({ v: TRAINING_PROTOCOL_VERSION, id: jobId, ...msg });
|
|
30
|
+
let cancelled = false;
|
|
31
|
+
let reqId = null;
|
|
32
|
+
|
|
33
|
+
(async () => {
|
|
34
|
+
const family = config.job?.model_type ?? config.family ?? "cfc";
|
|
35
|
+
push({ type: TMSG.META, backend: "liquid", started_at: new Date().toISOString(), world_size: 1, kind: "temporal-policy", family });
|
|
36
|
+
try {
|
|
37
|
+
runtime ??= runtimeFactory();
|
|
38
|
+
const params = {
|
|
39
|
+
bundle_path: config.dataset?.url,
|
|
40
|
+
output_dir: config.output?.destination_url,
|
|
41
|
+
family,
|
|
42
|
+
hyperparameters: config.hyperparams ?? {},
|
|
43
|
+
...(config.job ? { job: config.job } : {})
|
|
44
|
+
};
|
|
45
|
+
if (cancelled) throw Object.assign(new Error("cancelled"), { code: "E_CANCELLED" });
|
|
46
|
+
const res = await runtime.request("train", params, {
|
|
47
|
+
onId: (rid) => {
|
|
48
|
+
reqId = rid;
|
|
49
|
+
},
|
|
50
|
+
onEvent: (ev) => {
|
|
51
|
+
if (ev.event === "epoch") push({ type: TMSG.STEP, step: ev.epoch, loss: ev.loss, val_step_accuracy: ev.val_step_accuracy });
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
if (config.dataset?.sha256 && res.eval.dataset_hash !== config.dataset.sha256) {
|
|
55
|
+
throw Object.assign(new Error("trained dataset hash differs from the job's"), { code: "E_HASH_MISMATCH" });
|
|
56
|
+
}
|
|
57
|
+
push({ type: TMSG.EVAL, step: res.manifest.training.epochs, metrics: res.eval.metrics });
|
|
58
|
+
push({ type: TMSG.CHECKPOINT, step: res.manifest.training.epochs, path: res.model_dir, size_bytes: res.manifest.weights.size_bytes, model_hash: res.manifest.weights.weights_hash });
|
|
59
|
+
push({ type: TMSG.DONE, reason: "complete", final_step: res.manifest.training.epochs, finished_at: new Date().toISOString(), manifest: res.manifest });
|
|
60
|
+
} catch (err) {
|
|
61
|
+
const reason = cancelled || err?.code === "E_CANCELLED" ? "cancel" : "error";
|
|
62
|
+
if (reason === "error") push({ type: TMSG.ERROR, message: err?.message ?? String(err), code: err?.code ?? "E_RUNTIME" });
|
|
63
|
+
push({ type: TMSG.DONE, reason, finished_at: new Date().toISOString() });
|
|
64
|
+
}
|
|
65
|
+
stream.end();
|
|
66
|
+
})();
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
id: jobId,
|
|
70
|
+
stream,
|
|
71
|
+
cancel: () => {
|
|
72
|
+
cancelled = true;
|
|
73
|
+
if (reqId) runtime?.cancel(reqId).catch(() => {});
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
async shutdown() {
|
|
78
|
+
await runtime?.close();
|
|
79
|
+
runtime = null;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PEFT backend — LoRA fine-tunes of causal language models on one provider,
|
|
3
|
+
* CPU or CUDA. Workload `infernet.text.train`.
|
|
4
|
+
*
|
|
5
|
+
* Spawns `runners/peft_train.py <job.json>` in its own Python venv and relays
|
|
6
|
+
* its stdout, which is already training-protocol v1 NDJSON (meta / step /
|
|
7
|
+
* eval / checkpoint / done / error). Anything else on stdout is ignored;
|
|
8
|
+
* stderr is the runner's human log.
|
|
9
|
+
*
|
|
10
|
+
* Job config (see runners/peft_train.py):
|
|
11
|
+
* { kind: "lora", base_model, system_prompt?, dataset: { url, sha256? },
|
|
12
|
+
* hyperparams?: {...}, output: { destination_url } }
|
|
13
|
+
*
|
|
14
|
+
* The venv is installed only on request (`infernet model runtime setup --peft
|
|
15
|
+
* --yes`): CPU PyTorch + transformers + peft, about 1 GB.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { randomUUID } from "node:crypto";
|
|
19
|
+
import { spawn } from "node:child_process";
|
|
20
|
+
import { existsSync } from "node:fs";
|
|
21
|
+
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
|
|
22
|
+
import os from "node:os";
|
|
23
|
+
import path from "node:path";
|
|
24
|
+
import { fileURLToPath } from "node:url";
|
|
25
|
+
import { AsyncQueue } from "@infernetprotocol/engine";
|
|
26
|
+
import { TMSG, TRAINING_PROTOCOL_VERSION } from "../protocol.js";
|
|
27
|
+
|
|
28
|
+
export const PEFT_KINDS = Object.freeze(["lora", "peft"]);
|
|
29
|
+
export const RUNNER_PATH = fileURLToPath(new URL("../../runners/peft_train.py", import.meta.url));
|
|
30
|
+
export const PEFT_REQUIREMENTS = Object.freeze(["transformers>=4.46", "peft>=0.13", "safetensors>=0.4"]);
|
|
31
|
+
|
|
32
|
+
export function peftRuntimeDir() {
|
|
33
|
+
const base = process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share");
|
|
34
|
+
return path.join(base, "infernet", "peft");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The venv python, or null when the runtime is not installed. */
|
|
38
|
+
export function resolvePeftPython() {
|
|
39
|
+
const explicit = process.env.INFERNET_PEFT_PYTHON;
|
|
40
|
+
if (explicit) return existsSync(explicit) ? explicit : null;
|
|
41
|
+
const py = path.join(peftRuntimeDir(), "venv", "bin", "python");
|
|
42
|
+
return existsSync(py) ? py : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function run(cmd, args, log) {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
const child = spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
48
|
+
const relay = (b) => b.toString().split("\n").filter(Boolean).forEach((l) => log?.(l));
|
|
49
|
+
child.stdout.on("data", relay);
|
|
50
|
+
child.stderr.on("data", relay);
|
|
51
|
+
child.on("error", reject);
|
|
52
|
+
child.on("close", (code) => (code === 0 ? resolve() : reject(new Error(`${cmd} ${args.join(" ")} exited ${code}`))));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Create the venv and install CPU (or CUDA) PyTorch, transformers and peft. */
|
|
57
|
+
export async function setupPeftRuntime({ accelerator = "cpu", python = "python3", log } = {}) {
|
|
58
|
+
const dir = peftRuntimeDir();
|
|
59
|
+
await mkdir(dir, { recursive: true });
|
|
60
|
+
const venv = path.join(dir, "venv");
|
|
61
|
+
if (!existsSync(path.join(venv, "bin", "python"))) await run(python, ["-m", "venv", venv], log);
|
|
62
|
+
const py = path.join(venv, "bin", "python");
|
|
63
|
+
await run(py, ["-m", "pip", "install", "--upgrade", "--quiet", "pip"], log);
|
|
64
|
+
const torchIndex = accelerator === "cuda" ? [] : ["--index-url", "https://download.pytorch.org/whl/cpu"];
|
|
65
|
+
await run(py, ["-m", "pip", "install", "--quiet", "torch", ...torchIndex], log);
|
|
66
|
+
await run(py, ["-m", "pip", "install", "--quiet", ...PEFT_REQUIREMENTS], log);
|
|
67
|
+
return { python: py, accelerator, runner: RUNNER_PATH };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Versions of the installed runtime, or null. */
|
|
71
|
+
export async function peftRuntimeStatus() {
|
|
72
|
+
const py = resolvePeftPython();
|
|
73
|
+
if (!py) return null;
|
|
74
|
+
const code = "import json,torch,transformers,peft;print(json.dumps({'torch':torch.__version__,'transformers':transformers.__version__,'peft':peft.__version__,'cuda':torch.cuda.is_available()}))";
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
const child = spawn(py, ["-c", code], { stdio: ["ignore", "pipe", "ignore"] });
|
|
77
|
+
let out = "";
|
|
78
|
+
child.stdout.on("data", (b) => (out += b));
|
|
79
|
+
child.on("error", () => resolve(null));
|
|
80
|
+
child.on("close", (c) => {
|
|
81
|
+
try {
|
|
82
|
+
resolve(c === 0 ? { python: py, runner: RUNNER_PATH, ...JSON.parse(out) } : null);
|
|
83
|
+
} catch {
|
|
84
|
+
resolve(null);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const GENERATE_RUNNER_PATH = fileURLToPath(new URL("../../runners/peft_generate.py", import.meta.url));
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* One answer from a base model + LoRA adapter (workload infernet.text.infer).
|
|
94
|
+
* `request` is the runner's request object; relative paths resolve against
|
|
95
|
+
* `requestPath`'s directory when given. Resolves to the runner's answer
|
|
96
|
+
* object; rejects with its error message.
|
|
97
|
+
*/
|
|
98
|
+
export async function peftGenerate({ request, requestPath = null, python = null, runner = GENERATE_RUNNER_PATH, timeoutMs = 600000, spawnImpl = spawn } = {}) {
|
|
99
|
+
const py = python ?? resolvePeftPython();
|
|
100
|
+
if (!py) throw Object.assign(new Error("the peft runtime is not installed; run `infernet model runtime setup --peft --yes`"), { code: "E_NO_RUNTIME" });
|
|
101
|
+
let file = requestPath;
|
|
102
|
+
if (!file) {
|
|
103
|
+
const dir = await mkdtemp(path.join(os.tmpdir(), "infernet-generate-"));
|
|
104
|
+
file = path.join(dir, "request.json");
|
|
105
|
+
await writeFile(file, JSON.stringify(request));
|
|
106
|
+
}
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
const child = spawnImpl(py, [runner, file], { stdio: ["ignore", "pipe", "pipe"], env: { ...process.env, PYTHONUNBUFFERED: "1" } });
|
|
109
|
+
let out = "";
|
|
110
|
+
const tail = [];
|
|
111
|
+
child.stdout.on("data", (b) => (out += b));
|
|
112
|
+
child.stderr.on("data", (b) => {
|
|
113
|
+
for (const l of b.toString().split("\n").filter(Boolean)) {
|
|
114
|
+
tail.push(l);
|
|
115
|
+
if (tail.length > 20) tail.shift();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const timer = setTimeout(() => child.kill("SIGTERM"), timeoutMs);
|
|
119
|
+
child.on("error", reject);
|
|
120
|
+
child.on("close", (code) => {
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
const line = out.split("\n").map((l) => l.trim()).filter((l) => l.startsWith("{")).at(-1);
|
|
123
|
+
let res = null;
|
|
124
|
+
try {
|
|
125
|
+
res = line ? JSON.parse(line) : null;
|
|
126
|
+
} catch {
|
|
127
|
+
res = null;
|
|
128
|
+
}
|
|
129
|
+
if (code === 0 && res?.type === "answer") return resolve(res);
|
|
130
|
+
reject(Object.assign(new Error(res?.message ?? `generate exited ${code}: ${tail.at(-1) ?? "no output"}`), { code: "E_RUNTIME" }));
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function createPeftBackend({ python = null, runner = RUNNER_PATH, spawnImpl = spawn } = {}) {
|
|
136
|
+
const children = new Set();
|
|
137
|
+
return {
|
|
138
|
+
kind: "peft",
|
|
139
|
+
/**
|
|
140
|
+
* `config` is the job object; `jobPath` (optional) is where it lives, so
|
|
141
|
+
* the runner resolves relative dataset/output paths against it.
|
|
142
|
+
*/
|
|
143
|
+
start({ config = {}, id, jobPath } = {}) {
|
|
144
|
+
const jobId = id ?? config.id ?? randomUUID();
|
|
145
|
+
const stream = new AsyncQueue();
|
|
146
|
+
const push = (msg) => stream.push({ v: TRAINING_PROTOCOL_VERSION, id: jobId, ...msg });
|
|
147
|
+
let cancelled = false;
|
|
148
|
+
let child = null;
|
|
149
|
+
|
|
150
|
+
(async () => {
|
|
151
|
+
let sawDone = false;
|
|
152
|
+
try {
|
|
153
|
+
const py = python ?? resolvePeftPython();
|
|
154
|
+
if (!py) throw Object.assign(new Error("the peft runtime is not installed; run `infernet model runtime setup --peft --yes`"), { code: "E_NO_RUNTIME" });
|
|
155
|
+
if (!PEFT_KINDS.includes(config.kind)) throw Object.assign(new Error(`job kind ${config.kind} is not a peft job (${PEFT_KINDS.join(", ")})`), { code: "E_BAD_JOB" });
|
|
156
|
+
let file = jobPath;
|
|
157
|
+
if (!file) {
|
|
158
|
+
const dir = await mkdtemp(path.join(os.tmpdir(), "infernet-peft-"));
|
|
159
|
+
file = path.join(dir, "job.json");
|
|
160
|
+
await writeFile(file, JSON.stringify({ ...config, id: jobId }, null, 2));
|
|
161
|
+
}
|
|
162
|
+
child = spawnImpl(py, [runner, file], { stdio: ["ignore", "pipe", "pipe"], env: { ...process.env, PYTHONUNBUFFERED: "1" } });
|
|
163
|
+
children.add(child);
|
|
164
|
+
let buf = "";
|
|
165
|
+
child.stdout.on("data", (b) => {
|
|
166
|
+
buf += b.toString();
|
|
167
|
+
let nl;
|
|
168
|
+
while ((nl = buf.indexOf("\n")) >= 0) {
|
|
169
|
+
const line = buf.slice(0, nl).trim();
|
|
170
|
+
buf = buf.slice(nl + 1);
|
|
171
|
+
if (!line.startsWith("{")) continue;
|
|
172
|
+
try {
|
|
173
|
+
const ev = JSON.parse(line);
|
|
174
|
+
if (ev?.v !== TRAINING_PROTOCOL_VERSION || !ev.type) continue;
|
|
175
|
+
if (ev.type === TMSG.DONE) sawDone = true;
|
|
176
|
+
stream.push({ ...ev, id: jobId });
|
|
177
|
+
} catch {
|
|
178
|
+
/* not an event */
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
const tail = [];
|
|
183
|
+
child.stderr.on("data", (b) => {
|
|
184
|
+
for (const l of b.toString().split("\n").filter(Boolean)) {
|
|
185
|
+
tail.push(l);
|
|
186
|
+
if (tail.length > 20) tail.shift();
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
const code = await new Promise((resolve, reject) => {
|
|
190
|
+
child.on("error", reject);
|
|
191
|
+
child.on("close", resolve);
|
|
192
|
+
});
|
|
193
|
+
if (!sawDone) {
|
|
194
|
+
if (cancelled) push({ type: TMSG.DONE, reason: "cancel", finished_at: new Date().toISOString() });
|
|
195
|
+
else {
|
|
196
|
+
push({ type: TMSG.ERROR, message: `runner exited ${code}: ${tail.at(-1) ?? "no output"}`, code: "E_RUNTIME" });
|
|
197
|
+
push({ type: TMSG.DONE, reason: "error", finished_at: new Date().toISOString() });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} catch (err) {
|
|
201
|
+
push({ type: TMSG.ERROR, message: err?.message ?? String(err), code: err?.code ?? "E_RUNTIME" });
|
|
202
|
+
push({ type: TMSG.DONE, reason: "error", finished_at: new Date().toISOString() });
|
|
203
|
+
} finally {
|
|
204
|
+
if (child) children.delete(child);
|
|
205
|
+
stream.end();
|
|
206
|
+
}
|
|
207
|
+
})();
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
id: jobId,
|
|
211
|
+
stream,
|
|
212
|
+
cancel: () => {
|
|
213
|
+
cancelled = true;
|
|
214
|
+
child?.kill("SIGTERM");
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
async shutdown() {
|
|
219
|
+
for (const c of children) c.kill("SIGTERM");
|
|
220
|
+
children.clear();
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
package/src/index.js
CHANGED
|
@@ -17,6 +17,13 @@
|
|
|
17
17
|
* OpenDiLoCo). Class C: WAN-tolerant delta exchange.
|
|
18
18
|
* - "petals" — pipeline-parallel sharding for fine-tunes across
|
|
19
19
|
* volunteer GPUs. Class C-LLM: BigScience's Petals.
|
|
20
|
+
* - "liquid" — temporal policies (CfC/LTC liquid networks, GRU
|
|
21
|
+
* baseline) via the isolated infernet-liquid runtime.
|
|
22
|
+
* Single provider, CPU or GPU. Workload
|
|
23
|
+
* infernet.temporal.train.
|
|
24
|
+
* - "peft" — LoRA fine-tunes of causal LMs (transformers + peft)
|
|
25
|
+
* in their own venv, CPU or CUDA. Single provider.
|
|
26
|
+
* Workload infernet.text.train.
|
|
20
27
|
* - "stub" — synthetic training run, no GPU required. Default.
|
|
21
28
|
*
|
|
22
29
|
* Auto-selection precedence (only if `opts.backend` is not set):
|
|
@@ -25,12 +32,25 @@
|
|
|
25
32
|
*/
|
|
26
33
|
|
|
27
34
|
import { createDeepspeedBackend } from "./backends/deepspeed.js";
|
|
35
|
+
import { createLiquidBackend } from "./backends/liquid.js";
|
|
28
36
|
import { createOpenDiLoCoBackend } from "./backends/opendiloco.js";
|
|
29
37
|
import { createOpenRLHFBackend } from "./backends/openrlhf.js";
|
|
30
38
|
import { createPetalsBackend } from "./backends/petals.js";
|
|
39
|
+
import { createPeftBackend } from "./backends/peft.js";
|
|
31
40
|
import { createStubTrainer } from "./backends/stub.js";
|
|
32
41
|
|
|
33
42
|
export * from "./protocol.js";
|
|
43
|
+
export {
|
|
44
|
+
createPeftBackend,
|
|
45
|
+
peftRuntimeDir,
|
|
46
|
+
peftGenerate,
|
|
47
|
+
peftRuntimeStatus,
|
|
48
|
+
GENERATE_RUNNER_PATH as PEFT_GENERATE_RUNNER_PATH,
|
|
49
|
+
PEFT_KINDS,
|
|
50
|
+
resolvePeftPython,
|
|
51
|
+
RUNNER_PATH as PEFT_RUNNER_PATH,
|
|
52
|
+
setupPeftRuntime
|
|
53
|
+
} from "./backends/peft.js";
|
|
34
54
|
|
|
35
55
|
export async function createTrainer(opts = {}) {
|
|
36
56
|
const backend = opts.backend ?? autoSelectBackend();
|
|
@@ -43,6 +63,10 @@ export async function createTrainer(opts = {}) {
|
|
|
43
63
|
return createOpenDiLoCoBackend(opts);
|
|
44
64
|
case "petals":
|
|
45
65
|
return createPetalsBackend(opts);
|
|
66
|
+
case "liquid":
|
|
67
|
+
return createLiquidBackend(opts);
|
|
68
|
+
case "peft":
|
|
69
|
+
return createPeftBackend(opts);
|
|
46
70
|
case "stub":
|
|
47
71
|
return createStubTrainer(opts);
|
|
48
72
|
default:
|
package/src/protocol.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
*
|
|
22
22
|
* Job config shape (extend as needed; backends ignore fields they don't use):
|
|
23
23
|
* {
|
|
24
|
-
* kind: "fine-tune" | "lora" | "rlhf-rollout" | "pretrain",
|
|
24
|
+
* kind: "fine-tune" | "lora" | "rlhf-rollout" | "pretrain" | "temporal-policy",
|
|
25
25
|
* base_model: string,
|
|
26
26
|
* dataset: { url, sha256?, format?, split? },
|
|
27
27
|
* hyperparams: { lr?, batch_size?, epochs?, max_steps? },
|
|
@@ -48,7 +48,7 @@ export const TMSG = Object.freeze({
|
|
|
48
48
|
LOG: "log"
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
-
export const JOB_KINDS = Object.freeze(["fine-tune", "lora", "rlhf-rollout", "pretrain"]);
|
|
51
|
+
export const JOB_KINDS = Object.freeze(["fine-tune", "lora", "rlhf-rollout", "pretrain", "temporal-policy"]);
|
|
52
52
|
|
|
53
53
|
export const DONE_REASONS = Object.freeze(["complete", "cancel", "error", "not_implemented"]);
|
|
54
54
|
|