@infernetprotocol/training 0.1.89 → 0.1.90

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@infernetprotocol/training",
3
3
  "private": false,
4
- "version": "0.1.89",
4
+ "version": "0.1.90",
5
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.",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -19,8 +19,8 @@
19
19
  "node": ">=18"
20
20
  },
21
21
  "dependencies": {
22
- "@infernetprotocol/engine": "0.1.89",
23
- "@infernetprotocol/liquid-agent": "0.1.89"
22
+ "@infernetprotocol/engine": "0.1.90",
23
+ "@infernetprotocol/liquid-agent": "0.1.90"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",
@@ -12,11 +12,21 @@ infernet.text.train). `job.json` is that package's job config:
12
12
  "kind": "lora",
13
13
  "base_model": "HuggingFaceTB/SmolLM2-135M-Instruct",
14
14
  "system_prompt": "You are ...", optional
15
- "dataset": { "url": "corpus.jsonl", "sha256": "<hex>"? }, jsonl of {title, text}
15
+ "dataset": { "url": "corpus.jsonl", "sha256": "<hex>"? }, jsonl, see below
16
16
  "hyperparams": { "lr", "batch_size", "epochs", "max_steps", "seq_len", "lora_r", "threads", "seed" },
17
17
  "output": { "destination_url": "out/adapter" }
18
18
  }
19
19
 
20
+ Dataset records (one JSON object per line, mixing allowed):
21
+
22
+ {"title"?: "...", "text": "..."} one "Brief me on: <title>" exchange,
23
+ loss on the whole exchange (as before)
24
+ {"messages": [{"role", "content"}...]} the whole conversation through the
25
+ model's chat template, loss on the
26
+ assistant turns only; the job's
27
+ system prompt is added when the
28
+ conversation has none
29
+
20
30
  Relative paths resolve against the job file's directory. Progress goes to
21
31
  stdout as training-protocol v1 NDJSON (meta / step / eval / checkpoint /
22
32
  done / error); human-readable logs go to stderr. The output dir holds a PEFT
@@ -58,6 +68,33 @@ def resolve(base_dir, p):
58
68
  return Path(p) if os.path.isabs(p) else base_dir / p
59
69
 
60
70
 
71
+ # Dataset record shapes this runner reads. `infernet model runtime status
72
+ # --peft --json` reports them, so callers know what they may send.
73
+ DATASET_FORMATS = ("text", "messages")
74
+ ROLES = ("system", "user", "assistant")
75
+ IGNORE = -100
76
+
77
+
78
+ def parse_record(d):
79
+ """One dataset line as a doc, or None to skip it. Raises on a malformed
80
+ conversation so a bad dataset fails loudly instead of training on less."""
81
+ msgs = d.get("messages")
82
+ if isinstance(msgs, list) and msgs:
83
+ turns = []
84
+ for m in msgs:
85
+ role = m.get("role") if isinstance(m, dict) else None
86
+ content = m.get("content") if isinstance(m, dict) else None
87
+ if role not in ROLES or not isinstance(content, str):
88
+ raise ValueError("every message needs a role of system, user or assistant and a string content")
89
+ turns.append({"role": role, "content": content})
90
+ if not any(t["role"] == "assistant" and t["content"].strip() for t in turns):
91
+ return None
92
+ return {"messages": turns}
93
+ if d.get("text"):
94
+ return {"title": d.get("title") or "", "text": d["text"]}
95
+ return None
96
+
97
+
61
98
  def load_dataset(spec, base_dir):
62
99
  # Local files only: the runner never fetches from the network. A remote
63
100
  # corpus is downloaded and hash-checked by the caller (c0mpute's text
@@ -75,14 +112,81 @@ def load_dataset(spec, base_dir):
75
112
  for line in raw.decode().splitlines():
76
113
  if not line.strip():
77
114
  continue
78
- d = json.loads(line)
79
- if d.get("text"):
80
- docs.append({"title": d.get("title") or "", "text": d["text"]})
115
+ doc = parse_record(json.loads(line))
116
+ if doc:
117
+ docs.append(doc)
81
118
  if len(docs) < 2:
82
119
  raise ValueError("dataset needs at least 2 documents with text")
83
120
  return docs, digest
84
121
 
85
122
 
123
+ def conversation(doc, system):
124
+ """The chat a doc trains as."""
125
+ if "messages" in doc:
126
+ msgs = doc["messages"]
127
+ return msgs if msgs[0]["role"] == "system" else [{"role": "system", "content": system}, *msgs]
128
+ # Each text document becomes one briefing exchange, so the tune keeps the
129
+ # instruct model's chat format instead of drifting into raw prose.
130
+ return [
131
+ {"role": "system", "content": system},
132
+ {"role": "user", "content": f"Brief me on: {doc['title']}" if doc["title"] else "Brief me."},
133
+ {"role": "assistant", "content": doc["text"]},
134
+ ]
135
+
136
+
137
+ def encode(tok, doc, system):
138
+ """(input_ids, labels) for one doc.
139
+
140
+ Text docs learn the whole exchange, as this runner always did.
141
+ Conversations learn only what the assistant says: the template is rendered
142
+ for growing prefixes of the chat, and the tokens each assistant turn adds
143
+ after the generation prompt keep their labels; everything else is IGNORE.
144
+ A template whose prefixes do not nest falls back to learning the whole
145
+ conversation rather than guessing where the turns are."""
146
+ msgs = conversation(doc, system)
147
+
148
+ def ids_of(text):
149
+ return tok(text, add_special_tokens=False)["input_ids"]
150
+
151
+ if "messages" not in doc:
152
+ ids = ids_of(tok.apply_chat_template(msgs, tokenize=False))
153
+ return ids, list(ids)
154
+ ids, labels, done = [], [], ""
155
+ for k, m in enumerate(msgs):
156
+ full = tok.apply_chat_template(msgs[: k + 1], tokenize=False)
157
+ if m["role"] == "assistant":
158
+ prompt = tok.apply_chat_template(msgs[:k], tokenize=False, add_generation_prompt=True)
159
+ else:
160
+ prompt = full
161
+ if not (prompt.startswith(done) and full.startswith(prompt)):
162
+ ids = ids_of(tok.apply_chat_template(msgs, tokenize=False))
163
+ return ids, list(ids)
164
+ head = ids_of(prompt[len(done):]) if len(prompt) > len(done) else []
165
+ ids += head
166
+ labels += [IGNORE] * len(head)
167
+ if m["role"] == "assistant" and len(full) > len(prompt):
168
+ said = ids_of(full[len(prompt):])
169
+ ids += said
170
+ labels += said
171
+ done = full
172
+ return ids, labels
173
+
174
+
175
+ def pack_ids(encoded, seq_len):
176
+ """Concatenate (ids, labels) pairs into seq_len blocks, dropping any block
177
+ with nothing to learn (all IGNORE), which would give a NaN loss."""
178
+ ids, labels = [], []
179
+ for i, l in encoded:
180
+ ids.extend(i)
181
+ labels.extend(l)
182
+ blocks = []
183
+ for s in range(0, len(ids) - seq_len + 1, seq_len):
184
+ lab = labels[s : s + seq_len]
185
+ if any(x != IGNORE for x in lab):
186
+ blocks.append((ids[s : s + seq_len], lab))
187
+ return blocks
188
+
189
+
86
190
  def main():
87
191
  import torch
88
192
  from peft import LoraConfig, get_peft_model
@@ -105,31 +209,21 @@ def main():
105
209
  torch.manual_seed(hp["seed"])
106
210
  torch.set_num_threads(int(hp["threads"]))
107
211
 
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
212
  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)
213
+ # (input_ids, labels), each [blocks, seq_len].
214
+ blocks = pack_ids([encode(tok, d, system) for d in docs], seq_len)
215
+ return (
216
+ torch.tensor([b[0] for b in blocks], dtype=torch.long).reshape(-1, seq_len),
217
+ torch.tensor([b[1] for b in blocks], dtype=torch.long).reshape(-1, seq_len),
218
+ )
126
219
 
127
220
  @torch.no_grad()
128
221
  def perplexity(model, blocks, batch_size):
222
+ ids, labels = blocks
129
223
  model.eval()
130
224
  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)
225
+ model(input_ids=ids[i : i + batch_size].to(device), labels=labels[i : i + batch_size].to(device)).loss.item()
226
+ for i in range(0, len(ids), batch_size)
133
227
  ]
134
228
  model.train()
135
229
  return math.exp(sum(losses) / max(len(losses), 1))
@@ -140,15 +234,15 @@ def main():
140
234
  tok = AutoTokenizer.from_pretrained(job["base_model"])
141
235
  train_blocks = pack(tok, docs[held:], int(hp["seq_len"]))
142
236
  eval_blocks = pack(tok, docs[:held], int(hp["seq_len"]))
143
- if len(train_blocks) == 0:
237
+ if len(train_blocks[0]) == 0:
144
238
  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"]))
239
+ steps_per_epoch = math.ceil(len(train_blocks[0]) / int(hp["batch_size"]))
146
240
  total = int(hp["max_steps"] or steps_per_epoch * int(hp["epochs"]))
147
241
 
148
242
  emit(job_id, "meta", started_at=now(), backend="peft", world_size=1, kind=job.get("kind", "lora"),
149
243
  base_model=job["base_model"], device=device, total_steps=total, documents=len(docs),
150
244
  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}")
245
+ log(f"{len(docs)} docs -> {len(train_blocks[0])} train / {len(eval_blocks[0])} eval blocks of {hp['seq_len']} tokens; {total} steps on {device}")
152
246
 
153
247
  model = AutoModelForCausalLM.from_pretrained(job["base_model"], dtype=torch.float32).to(device)
154
248
  # Recompute activations in the backward pass: roughly halves peak RAM, so
@@ -165,16 +259,16 @@ def main():
165
259
  opt = torch.optim.AdamW([p for p in model.parameters() if p.requires_grad], lr=float(hp["lr"]))
166
260
  sched = torch.optim.lr_scheduler.LambdaLR(opt, lambda s: min(1.0, (s + 1) / 10) * max(0.0, 1 - s / total))
167
261
 
168
- if len(eval_blocks):
262
+ if len(eval_blocks[0]):
169
263
  emit(job_id, "eval", step=0, metrics={"perplexity": perplexity(model, eval_blocks, int(hp["batch_size"]))})
170
264
 
171
265
  step, t0 = 0, time.time()
172
266
  model.train()
173
267
  while step < total:
174
- order = torch.randperm(len(train_blocks))
268
+ order = torch.randperm(len(train_blocks[0]))
175
269
  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
270
+ pick = order[i : i + int(hp["batch_size"])]
271
+ loss = model(input_ids=train_blocks[0][pick].to(device), labels=train_blocks[1][pick].to(device)).loss
178
272
  loss.backward()
179
273
  grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0).item()
180
274
  opt.step(); sched.step(); opt.zero_grad()
@@ -185,7 +279,7 @@ def main():
185
279
  if step >= total:
186
280
  break
187
281
 
188
- if len(eval_blocks):
282
+ if len(eval_blocks[0]):
189
283
  emit(job_id, "eval", step=step, metrics={"perplexity": perplexity(model, eval_blocks, int(hp["batch_size"]))})
190
284
 
191
285
  out_dir.mkdir(parents=True, exist_ok=True)
@@ -200,6 +294,7 @@ def main():
200
294
  "adapter_sha256": digest,
201
295
  "dataset_sha256": dataset_sha,
202
296
  "documents": len(docs),
297
+ "conversations": sum(1 for d in docs if "messages" in d),
203
298
  "steps": step,
204
299
  "system_prompt": system,
205
300
  "hyperparams": hp,
@@ -0,0 +1,107 @@
1
+ """Unit tests for peft_train's dataset handling (no torch needed).
2
+
3
+ python3 -m unittest packages/training/runners/test_peft_train.py
4
+ """
5
+
6
+ import json
7
+ import os
8
+ import sys
9
+ import tempfile
10
+ import unittest
11
+ from pathlib import Path
12
+
13
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
14
+ import peft_train as pt # noqa: E402
15
+
16
+
17
+ class ChatMLTok:
18
+ """A character-level tokenizer with a ChatML template (Qwen/SmolLM style)."""
19
+
20
+ def __call__(self, text, add_special_tokens=False):
21
+ return {"input_ids": [ord(c) for c in text]}
22
+
23
+ def apply_chat_template(self, msgs, tokenize=False, add_generation_prompt=False):
24
+ out = "".join(f"<|im_start|>{m['role']}\n{m['content']}<|im_end|>\n" for m in msgs)
25
+ return out + ("<|im_start|>assistant\n" if add_generation_prompt else "")
26
+
27
+
28
+ class OddTok(ChatMLTok):
29
+ """A template whose prefixes do not nest (it summarizes the turn count first)."""
30
+
31
+ def apply_chat_template(self, msgs, tokenize=False, add_generation_prompt=False):
32
+ return f"[{len(msgs)}]" + super().apply_chat_template(msgs, tokenize, add_generation_prompt)
33
+
34
+
35
+ def learned(ids, labels):
36
+ return "".join(chr(i) for i, l in zip(ids, labels) if l != pt.IGNORE)
37
+
38
+
39
+ CHAT = {"messages": [
40
+ {"role": "user", "content": "hi"},
41
+ {"role": "assistant", "content": "hello"},
42
+ {"role": "user", "content": "price?"},
43
+ {"role": "assistant", "content": "charge for value"},
44
+ ]}
45
+
46
+
47
+ class DatasetTests(unittest.TestCase):
48
+ def test_parse_record_shapes(self):
49
+ self.assertEqual(pt.parse_record({"text": "body", "title": "T"}), {"title": "T", "text": "body"})
50
+ self.assertEqual(pt.parse_record(CHAT)["messages"][3]["content"], "charge for value")
51
+ self.assertIsNone(pt.parse_record({"messages": [{"role": "user", "content": "only a question"}]}))
52
+ self.assertIsNone(pt.parse_record({"foo": 1}))
53
+ with self.assertRaises(ValueError):
54
+ pt.parse_record({"messages": [{"role": "tool", "content": "x"}, {"role": "assistant", "content": "y"}]})
55
+ with self.assertRaises(ValueError):
56
+ pt.parse_record({"messages": [{"role": "assistant", "content": 3}]})
57
+
58
+ def test_load_dataset_mixes_text_and_conversations(self):
59
+ with tempfile.TemporaryDirectory() as d:
60
+ lines = [json.dumps({"title": "A", "text": "x"}), "", json.dumps(CHAT)]
61
+ Path(d, "c.jsonl").write_text("\n".join(lines) + "\n")
62
+ docs, digest = pt.load_dataset({"url": "c.jsonl"}, Path(d))
63
+ self.assertEqual(len(docs), 2)
64
+ self.assertIn("messages", docs[1])
65
+ self.assertEqual(len(digest), 64)
66
+ with self.assertRaises(ValueError):
67
+ pt.load_dataset({"url": "c.jsonl", "sha256": "0" * 64}, Path(d))
68
+ with self.assertRaises(ValueError):
69
+ pt.load_dataset({"url": "https://evil/x"}, Path(d))
70
+
71
+ def test_every_turn_is_kept_and_only_assistant_turns_learn(self):
72
+ ids, labels = pt.encode(ChatMLTok(), CHAT, "be brief")
73
+ text = "".join(chr(i) for i in ids)
74
+ # The whole conversation, both exchanges, with the job's system prompt added.
75
+ self.assertEqual(text, ChatMLTok().apply_chat_template([{"role": "system", "content": "be brief"}, *CHAT["messages"]]))
76
+ self.assertEqual(len(ids), len(labels))
77
+ self.assertEqual(learned(ids, labels), "hello<|im_end|>\ncharge for value<|im_end|>\n")
78
+
79
+ def test_a_conversation_keeps_its_own_system_prompt(self):
80
+ doc = {"messages": [{"role": "system", "content": "mine"}, *CHAT["messages"]]}
81
+ text = "".join(chr(i) for i in pt.encode(ChatMLTok(), doc, "job default")[0])
82
+ self.assertIn("mine", text)
83
+ self.assertNotIn("job default", text)
84
+
85
+ def test_text_docs_train_as_before(self):
86
+ ids, labels = pt.encode(ChatMLTok(), {"title": "Pricing", "text": "value"}, "sys")
87
+ self.assertEqual(ids, labels)
88
+ self.assertIn("Brief me on: Pricing", "".join(chr(i) for i in ids))
89
+
90
+ def test_a_template_that_does_not_nest_learns_everything(self):
91
+ ids, labels = pt.encode(OddTok(), CHAT, "sys")
92
+ self.assertEqual(ids, labels)
93
+ self.assertTrue("".join(chr(i) for i in ids).startswith("[5]"))
94
+
95
+ def test_pack_drops_blocks_with_nothing_to_learn(self):
96
+ enc = [([1, 2, 3, 4], [pt.IGNORE] * 4), ([5, 6, 7, 8], [5, 6, 7, 8])]
97
+ blocks = pt.pack_ids(enc, 4)
98
+ self.assertEqual(blocks, [([5, 6, 7, 8], [5, 6, 7, 8])])
99
+ # Blocks straddle docs; a partial tail is dropped.
100
+ self.assertEqual(pt.pack_ids(enc, 3), [([4, 5, 6], [pt.IGNORE, 5, 6])])
101
+
102
+ def test_formats_are_advertised(self):
103
+ self.assertEqual(pt.DATASET_FORMATS, ("text", "messages"))
104
+
105
+
106
+ if __name__ == "__main__":
107
+ unittest.main()
@@ -28,6 +28,14 @@ import { TMSG, TRAINING_PROTOCOL_VERSION } from "../protocol.js";
28
28
  export const PEFT_KINDS = Object.freeze(["lora", "peft"]);
29
29
  export const RUNNER_PATH = fileURLToPath(new URL("../../runners/peft_train.py", import.meta.url));
30
30
  export const PEFT_REQUIREMENTS = Object.freeze(["transformers>=4.46", "peft>=0.13", "safetensors>=0.4"]);
31
+ /**
32
+ * Dataset record shapes runners/peft_train.py reads (its DATASET_FORMATS):
33
+ * `text` ({title?, text}: one briefing exchange) and `messages` (a whole
34
+ * multi-turn chat, loss on the assistant turns). Reported by
35
+ * `infernet model runtime status --peft --json` so callers (c0mpute's text
36
+ * workers) advertise only what this runner can train.
37
+ */
38
+ export const PEFT_DATASET_FORMATS = Object.freeze(["text", "messages"]);
31
39
 
32
40
  export function peftRuntimeDir() {
33
41
  const base = process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share");
@@ -97,7 +105,7 @@ export async function peftRuntimeStatus() {
97
105
  child.on("error", () => resolve(null));
98
106
  child.on("close", (c) => {
99
107
  try {
100
- resolve(c === 0 ? { python: py, runner: RUNNER_PATH, ...JSON.parse(out) } : null);
108
+ resolve(c === 0 ? { python: py, runner: RUNNER_PATH, formats: [...PEFT_DATASET_FORMATS], ...JSON.parse(out) } : null);
101
109
  } catch {
102
110
  resolve(null);
103
111
  }
package/src/index.js CHANGED
@@ -46,6 +46,7 @@ export {
46
46
  peftGenerate,
47
47
  peftRuntimeStatus,
48
48
  GENERATE_RUNNER_PATH as PEFT_GENERATE_RUNNER_PATH,
49
+ PEFT_DATASET_FORMATS,
49
50
  PEFT_KINDS,
50
51
  resolvePeftPython,
51
52
  RUNNER_PATH as PEFT_RUNNER_PATH,