@infernetprotocol/training 0.1.89 → 0.1.91
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 +3 -3
- package/runners/peft_train.py +180 -34
- package/runners/test_peft_train.py +130 -0
- package/src/backends/peft.js +9 -1
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infernetprotocol/training",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.91",
|
|
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.
|
|
23
|
-
"@infernetprotocol/liquid-agent": "0.1.
|
|
22
|
+
"@infernetprotocol/engine": "0.1.91",
|
|
23
|
+
"@infernetprotocol/liquid-agent": "0.1.91"
|
|
24
24
|
},
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
package/runners/peft_train.py
CHANGED
|
@@ -12,11 +12,28 @@ 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
|
|
16
|
-
"hyperparams": { "lr", "batch_size", "epochs", "max_steps", "seq_len", "lora_r", "threads", "seed"
|
|
15
|
+
"dataset": { "url": "corpus.jsonl", "sha256": "<hex>"? }, jsonl, see below
|
|
16
|
+
"hyperparams": { "lr", "batch_size", "epochs", "max_steps", "seq_len", "lora_r", "threads", "seed",
|
|
17
|
+
"dtype" }, float32 | bfloat16 | float16 | auto (default)
|
|
17
18
|
"output": { "destination_url": "out/adapter" }
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
Dataset records (one JSON object per line, mixing allowed):
|
|
22
|
+
|
|
23
|
+
{"title"?: "...", "text": "..."} one "Brief me on: <title>" exchange,
|
|
24
|
+
loss on the whole exchange (as before)
|
|
25
|
+
{"messages": [{"role", "content"}...]} the whole conversation through the
|
|
26
|
+
model's chat template, loss on the
|
|
27
|
+
assistant turns only; the job's
|
|
28
|
+
system prompt is added when the
|
|
29
|
+
conversation has none
|
|
30
|
+
|
|
31
|
+
`dtype` is what the base model's weights load in; the LoRA weights always
|
|
32
|
+
train in float32. `auto` picks bfloat16 where the hardware does it natively
|
|
33
|
+
(CUDA with bf16, CPUs with avx512_bf16 / amx_bf16 / Arm bf16) and float32
|
|
34
|
+
elsewhere: bf16 is several times faster and needs half the RAM on those CPUs,
|
|
35
|
+
but emulated bf16 on an older CPU is slower than float32.
|
|
36
|
+
|
|
20
37
|
Relative paths resolve against the job file's directory. Progress goes to
|
|
21
38
|
stdout as training-protocol v1 NDJSON (meta / step / eval / checkpoint /
|
|
22
39
|
done / error); human-readable logs go to stderr. The output dir holds a PEFT
|
|
@@ -58,6 +75,64 @@ def resolve(base_dir, p):
|
|
|
58
75
|
return Path(p) if os.path.isabs(p) else base_dir / p
|
|
59
76
|
|
|
60
77
|
|
|
78
|
+
# Dataset record shapes this runner reads. `infernet model runtime status
|
|
79
|
+
# --peft --json` reports them, so callers know what they may send.
|
|
80
|
+
DATASET_FORMATS = ("text", "messages")
|
|
81
|
+
ROLES = ("system", "user", "assistant")
|
|
82
|
+
IGNORE = -100
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
DTYPES = ("float32", "bfloat16", "float16")
|
|
86
|
+
DTYPE_ALIASES = {"fp32": "float32", "bf16": "bfloat16", "fp16": "float16", "half": "float16"}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def cpu_has_bf16(cpuinfo=None):
|
|
90
|
+
"""Whether this CPU does bfloat16 natively (x86 avx512_bf16 / amx_bf16, Arm bf16)."""
|
|
91
|
+
if cpuinfo is None:
|
|
92
|
+
try:
|
|
93
|
+
cpuinfo = Path("/proc/cpuinfo").read_text()
|
|
94
|
+
except OSError:
|
|
95
|
+
return False
|
|
96
|
+
flags = set()
|
|
97
|
+
for line in cpuinfo.splitlines():
|
|
98
|
+
key, _, val = line.partition(":")
|
|
99
|
+
if key.strip().lower() in ("flags", "features"):
|
|
100
|
+
flags.update(val.split())
|
|
101
|
+
return bool(flags & {"avx512_bf16", "amx_bf16", "bf16"})
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def resolve_dtype(requested, device, cuda_bf16=False, cpu_bf16=False):
|
|
105
|
+
"""The dtype name to load the base model in: `hyperparams.dtype` if set,
|
|
106
|
+
else (auto) bfloat16 where it is native and float32 elsewhere."""
|
|
107
|
+
want = DTYPE_ALIASES.get(str(requested or "auto").lower(), str(requested or "auto").lower())
|
|
108
|
+
if want == "auto":
|
|
109
|
+
native = cuda_bf16 if device == "cuda" else cpu_bf16
|
|
110
|
+
return "bfloat16" if native else "float32"
|
|
111
|
+
if want not in DTYPES:
|
|
112
|
+
raise ValueError(f"hyperparams.dtype must be one of {', '.join(DTYPES)} or auto, not {requested!r}")
|
|
113
|
+
return want
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def parse_record(d):
|
|
117
|
+
"""One dataset line as a doc, or None to skip it. Raises on a malformed
|
|
118
|
+
conversation so a bad dataset fails loudly instead of training on less."""
|
|
119
|
+
msgs = d.get("messages")
|
|
120
|
+
if isinstance(msgs, list) and msgs:
|
|
121
|
+
turns = []
|
|
122
|
+
for m in msgs:
|
|
123
|
+
role = m.get("role") if isinstance(m, dict) else None
|
|
124
|
+
content = m.get("content") if isinstance(m, dict) else None
|
|
125
|
+
if role not in ROLES or not isinstance(content, str):
|
|
126
|
+
raise ValueError("every message needs a role of system, user or assistant and a string content")
|
|
127
|
+
turns.append({"role": role, "content": content})
|
|
128
|
+
if not any(t["role"] == "assistant" and t["content"].strip() for t in turns):
|
|
129
|
+
return None
|
|
130
|
+
return {"messages": turns}
|
|
131
|
+
if d.get("text"):
|
|
132
|
+
return {"title": d.get("title") or "", "text": d["text"]}
|
|
133
|
+
return None
|
|
134
|
+
|
|
135
|
+
|
|
61
136
|
def load_dataset(spec, base_dir):
|
|
62
137
|
# Local files only: the runner never fetches from the network. A remote
|
|
63
138
|
# corpus is downloaded and hash-checked by the caller (c0mpute's text
|
|
@@ -75,14 +150,81 @@ def load_dataset(spec, base_dir):
|
|
|
75
150
|
for line in raw.decode().splitlines():
|
|
76
151
|
if not line.strip():
|
|
77
152
|
continue
|
|
78
|
-
|
|
79
|
-
if
|
|
80
|
-
docs.append(
|
|
153
|
+
doc = parse_record(json.loads(line))
|
|
154
|
+
if doc:
|
|
155
|
+
docs.append(doc)
|
|
81
156
|
if len(docs) < 2:
|
|
82
157
|
raise ValueError("dataset needs at least 2 documents with text")
|
|
83
158
|
return docs, digest
|
|
84
159
|
|
|
85
160
|
|
|
161
|
+
def conversation(doc, system):
|
|
162
|
+
"""The chat a doc trains as."""
|
|
163
|
+
if "messages" in doc:
|
|
164
|
+
msgs = doc["messages"]
|
|
165
|
+
return msgs if msgs[0]["role"] == "system" else [{"role": "system", "content": system}, *msgs]
|
|
166
|
+
# Each text document becomes one briefing exchange, so the tune keeps the
|
|
167
|
+
# instruct model's chat format instead of drifting into raw prose.
|
|
168
|
+
return [
|
|
169
|
+
{"role": "system", "content": system},
|
|
170
|
+
{"role": "user", "content": f"Brief me on: {doc['title']}" if doc["title"] else "Brief me."},
|
|
171
|
+
{"role": "assistant", "content": doc["text"]},
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def encode(tok, doc, system):
|
|
176
|
+
"""(input_ids, labels) for one doc.
|
|
177
|
+
|
|
178
|
+
Text docs learn the whole exchange, as this runner always did.
|
|
179
|
+
Conversations learn only what the assistant says: the template is rendered
|
|
180
|
+
for growing prefixes of the chat, and the tokens each assistant turn adds
|
|
181
|
+
after the generation prompt keep their labels; everything else is IGNORE.
|
|
182
|
+
A template whose prefixes do not nest falls back to learning the whole
|
|
183
|
+
conversation rather than guessing where the turns are."""
|
|
184
|
+
msgs = conversation(doc, system)
|
|
185
|
+
|
|
186
|
+
def ids_of(text):
|
|
187
|
+
return tok(text, add_special_tokens=False)["input_ids"]
|
|
188
|
+
|
|
189
|
+
if "messages" not in doc:
|
|
190
|
+
ids = ids_of(tok.apply_chat_template(msgs, tokenize=False))
|
|
191
|
+
return ids, list(ids)
|
|
192
|
+
ids, labels, done = [], [], ""
|
|
193
|
+
for k, m in enumerate(msgs):
|
|
194
|
+
full = tok.apply_chat_template(msgs[: k + 1], tokenize=False)
|
|
195
|
+
if m["role"] == "assistant":
|
|
196
|
+
prompt = tok.apply_chat_template(msgs[:k], tokenize=False, add_generation_prompt=True)
|
|
197
|
+
else:
|
|
198
|
+
prompt = full
|
|
199
|
+
if not (prompt.startswith(done) and full.startswith(prompt)):
|
|
200
|
+
ids = ids_of(tok.apply_chat_template(msgs, tokenize=False))
|
|
201
|
+
return ids, list(ids)
|
|
202
|
+
head = ids_of(prompt[len(done):]) if len(prompt) > len(done) else []
|
|
203
|
+
ids += head
|
|
204
|
+
labels += [IGNORE] * len(head)
|
|
205
|
+
if m["role"] == "assistant" and len(full) > len(prompt):
|
|
206
|
+
said = ids_of(full[len(prompt):])
|
|
207
|
+
ids += said
|
|
208
|
+
labels += said
|
|
209
|
+
done = full
|
|
210
|
+
return ids, labels
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def pack_ids(encoded, seq_len):
|
|
214
|
+
"""Concatenate (ids, labels) pairs into seq_len blocks, dropping any block
|
|
215
|
+
with nothing to learn (all IGNORE), which would give a NaN loss."""
|
|
216
|
+
ids, labels = [], []
|
|
217
|
+
for i, l in encoded:
|
|
218
|
+
ids.extend(i)
|
|
219
|
+
labels.extend(l)
|
|
220
|
+
blocks = []
|
|
221
|
+
for s in range(0, len(ids) - seq_len + 1, seq_len):
|
|
222
|
+
lab = labels[s : s + seq_len]
|
|
223
|
+
if any(x != IGNORE for x in lab):
|
|
224
|
+
blocks.append((ids[s : s + seq_len], lab))
|
|
225
|
+
return blocks
|
|
226
|
+
|
|
227
|
+
|
|
86
228
|
def main():
|
|
87
229
|
import torch
|
|
88
230
|
from peft import LoraConfig, get_peft_model
|
|
@@ -100,36 +242,34 @@ def main():
|
|
|
100
242
|
system = job.get("system_prompt") or DEFAULT_SYSTEM
|
|
101
243
|
out_dir = resolve(base_dir, job["output"]["destination_url"])
|
|
102
244
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
245
|
+
dtype = resolve_dtype(
|
|
246
|
+
hp.get("dtype"), device,
|
|
247
|
+
cuda_bf16=device == "cuda" and torch.cuda.is_bf16_supported(),
|
|
248
|
+
cpu_bf16=cpu_has_bf16(),
|
|
249
|
+
)
|
|
250
|
+
hp["dtype"] = dtype
|
|
251
|
+
if dtype == "float16" and device == "cpu":
|
|
252
|
+
log("warning: float16 on CPU is slow and can overflow; bfloat16 or float32 is the better choice")
|
|
103
253
|
|
|
104
254
|
random.seed(hp["seed"])
|
|
105
255
|
torch.manual_seed(hp["seed"])
|
|
106
256
|
torch.set_num_threads(int(hp["threads"]))
|
|
107
257
|
|
|
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
258
|
def pack(tok, docs, seq_len):
|
|
121
|
-
|
|
122
|
-
for d in docs
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
259
|
+
# (input_ids, labels), each [blocks, seq_len].
|
|
260
|
+
blocks = pack_ids([encode(tok, d, system) for d in docs], seq_len)
|
|
261
|
+
return (
|
|
262
|
+
torch.tensor([b[0] for b in blocks], dtype=torch.long).reshape(-1, seq_len),
|
|
263
|
+
torch.tensor([b[1] for b in blocks], dtype=torch.long).reshape(-1, seq_len),
|
|
264
|
+
)
|
|
126
265
|
|
|
127
266
|
@torch.no_grad()
|
|
128
267
|
def perplexity(model, blocks, batch_size):
|
|
268
|
+
ids, labels = blocks
|
|
129
269
|
model.eval()
|
|
130
270
|
losses = [
|
|
131
|
-
model(input_ids=
|
|
132
|
-
for i in range(0, len(
|
|
271
|
+
model(input_ids=ids[i : i + batch_size].to(device), labels=labels[i : i + batch_size].to(device)).loss.item()
|
|
272
|
+
for i in range(0, len(ids), batch_size)
|
|
133
273
|
]
|
|
134
274
|
model.train()
|
|
135
275
|
return math.exp(sum(losses) / max(len(losses), 1))
|
|
@@ -140,17 +280,17 @@ def main():
|
|
|
140
280
|
tok = AutoTokenizer.from_pretrained(job["base_model"])
|
|
141
281
|
train_blocks = pack(tok, docs[held:], int(hp["seq_len"]))
|
|
142
282
|
eval_blocks = pack(tok, docs[:held], int(hp["seq_len"]))
|
|
143
|
-
if len(train_blocks) == 0:
|
|
283
|
+
if len(train_blocks[0]) == 0:
|
|
144
284
|
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"]))
|
|
285
|
+
steps_per_epoch = math.ceil(len(train_blocks[0]) / int(hp["batch_size"]))
|
|
146
286
|
total = int(hp["max_steps"] or steps_per_epoch * int(hp["epochs"]))
|
|
147
287
|
|
|
148
288
|
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),
|
|
289
|
+
base_model=job["base_model"], device=device, dtype=dtype, total_steps=total, documents=len(docs),
|
|
150
290
|
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}")
|
|
291
|
+
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} in {dtype}")
|
|
152
292
|
|
|
153
|
-
model = AutoModelForCausalLM.from_pretrained(job["base_model"], dtype=torch
|
|
293
|
+
model = AutoModelForCausalLM.from_pretrained(job["base_model"], dtype=getattr(torch, dtype)).to(device)
|
|
154
294
|
# Recompute activations in the backward pass: roughly halves peak RAM, so
|
|
155
295
|
# the job fits beside other work on a shared box.
|
|
156
296
|
model.gradient_checkpointing_enable()
|
|
@@ -160,21 +300,26 @@ def main():
|
|
|
160
300
|
target_modules=hp.get("target_modules") or ["q_proj", "k_proj", "v_proj", "o_proj"],
|
|
161
301
|
task_type="CAUSAL_LM",
|
|
162
302
|
))
|
|
303
|
+
# The adapter trains in float32 whatever the base is in: bf16 optimizer
|
|
304
|
+
# updates on small LoRA weights lose most of their precision.
|
|
305
|
+
for p in model.parameters():
|
|
306
|
+
if p.requires_grad and p.dtype != torch.float32:
|
|
307
|
+
p.data = p.data.float()
|
|
163
308
|
log(f"trainable params: {sum(p.numel() for p in model.parameters() if p.requires_grad):,}")
|
|
164
309
|
|
|
165
310
|
opt = torch.optim.AdamW([p for p in model.parameters() if p.requires_grad], lr=float(hp["lr"]))
|
|
166
311
|
sched = torch.optim.lr_scheduler.LambdaLR(opt, lambda s: min(1.0, (s + 1) / 10) * max(0.0, 1 - s / total))
|
|
167
312
|
|
|
168
|
-
if len(eval_blocks):
|
|
313
|
+
if len(eval_blocks[0]):
|
|
169
314
|
emit(job_id, "eval", step=0, metrics={"perplexity": perplexity(model, eval_blocks, int(hp["batch_size"]))})
|
|
170
315
|
|
|
171
316
|
step, t0 = 0, time.time()
|
|
172
317
|
model.train()
|
|
173
318
|
while step < total:
|
|
174
|
-
order = torch.randperm(len(train_blocks))
|
|
319
|
+
order = torch.randperm(len(train_blocks[0]))
|
|
175
320
|
for i in range(0, len(order), int(hp["batch_size"])):
|
|
176
|
-
|
|
177
|
-
loss = model(input_ids=
|
|
321
|
+
pick = order[i : i + int(hp["batch_size"])]
|
|
322
|
+
loss = model(input_ids=train_blocks[0][pick].to(device), labels=train_blocks[1][pick].to(device)).loss
|
|
178
323
|
loss.backward()
|
|
179
324
|
grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0).item()
|
|
180
325
|
opt.step(); sched.step(); opt.zero_grad()
|
|
@@ -185,7 +330,7 @@ def main():
|
|
|
185
330
|
if step >= total:
|
|
186
331
|
break
|
|
187
332
|
|
|
188
|
-
if len(eval_blocks):
|
|
333
|
+
if len(eval_blocks[0]):
|
|
189
334
|
emit(job_id, "eval", step=step, metrics={"perplexity": perplexity(model, eval_blocks, int(hp["batch_size"]))})
|
|
190
335
|
|
|
191
336
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
@@ -200,6 +345,7 @@ def main():
|
|
|
200
345
|
"adapter_sha256": digest,
|
|
201
346
|
"dataset_sha256": dataset_sha,
|
|
202
347
|
"documents": len(docs),
|
|
348
|
+
"conversations": sum(1 for d in docs if "messages" in d),
|
|
203
349
|
"steps": step,
|
|
204
350
|
"system_prompt": system,
|
|
205
351
|
"hyperparams": hp,
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
|
|
107
|
+
class DtypeTests(unittest.TestCase):
|
|
108
|
+
def test_explicit_dtype_is_honoured(self):
|
|
109
|
+
for want in ("float32", "bfloat16", "float16"):
|
|
110
|
+
self.assertEqual(pt.resolve_dtype(want, "cpu"), want)
|
|
111
|
+
self.assertEqual(pt.resolve_dtype("bf16", "cpu"), "bfloat16")
|
|
112
|
+
self.assertEqual(pt.resolve_dtype("FP32", "cuda", cuda_bf16=True), "float32")
|
|
113
|
+
|
|
114
|
+
def test_auto_picks_bf16_only_where_native(self):
|
|
115
|
+
self.assertEqual(pt.resolve_dtype(None, "cpu", cpu_bf16=True), "bfloat16")
|
|
116
|
+
self.assertEqual(pt.resolve_dtype("auto", "cpu", cpu_bf16=False), "float32")
|
|
117
|
+
self.assertEqual(pt.resolve_dtype(None, "cuda", cuda_bf16=True), "bfloat16")
|
|
118
|
+
self.assertEqual(pt.resolve_dtype(None, "cuda", cuda_bf16=False, cpu_bf16=True), "float32")
|
|
119
|
+
|
|
120
|
+
def test_unknown_dtype_fails_loudly(self):
|
|
121
|
+
with self.assertRaises(ValueError):
|
|
122
|
+
pt.resolve_dtype("int8", "cpu")
|
|
123
|
+
|
|
124
|
+
def test_cpu_bf16_detection(self):
|
|
125
|
+
self.assertTrue(pt.cpu_has_bf16("flags\t\t: fpu sse avx2 avx512f avx512_bf16\n"))
|
|
126
|
+
self.assertTrue(pt.cpu_has_bf16("Features\t: fp asimd bf16 i8mm\n"))
|
|
127
|
+
self.assertFalse(pt.cpu_has_bf16("flags\t\t: fpu sse avx2 avx512f\n"))
|
|
128
|
+
|
|
129
|
+
if __name__ == "__main__":
|
|
130
|
+
unittest.main()
|
package/src/backends/peft.js
CHANGED
|
@@ -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
|
}
|