@infernetprotocol/cli 0.1.32 → 0.1.34

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.
@@ -0,0 +1,239 @@
1
+ /**
2
+ * `infernet publish` — push a trained model to HuggingFace and/or Ollama.
3
+ *
4
+ * The Ollama path is what makes a model show up at e.g.
5
+ * https://ollama.com/<your-org>/<model>
6
+ * (similar to ollama.com/rockypod/svelte-coder). We:
7
+ * 1. Locate the safetensors checkpoint in <dir>.
8
+ * 2. Optionally push it to huggingface.co/<HF_ORG>/<name> via huggingface-cli.
9
+ * 3. Convert to GGUF (llama.cpp) — best with the user's local llama.cpp install.
10
+ * 4. Generate a Modelfile + `ollama create` + `ollama push`.
11
+ *
12
+ * Requires:
13
+ * HUGGINGFACE_TOKEN for the HF push
14
+ * OLLAMA_USERNAME for the Ollama push (ollama signin must already be done)
15
+ * llama.cpp cloned to ~/llama.cpp OR override with --llama-cpp-path
16
+ */
17
+
18
+ import fs from "node:fs";
19
+ import fsp from "node:fs/promises";
20
+ import path from "node:path";
21
+ import { spawn } from "node:child_process";
22
+ import { loadConfig } from "../lib/config.js";
23
+
24
+ const HELP = `infernet publish — push a trained model to HuggingFace + Ollama
25
+
26
+ Usage:
27
+ infernet publish <dir> [flags]
28
+
29
+ Required:
30
+ <dir> Path to the trained model directory
31
+ (must contain config.json + *.safetensors)
32
+
33
+ Flags:
34
+ --hf <org>/<name> Push to huggingface.co/<org>/<name>
35
+ --ollama <user>/<name> Push to ollama.com/<user>/<name>
36
+ --base <hf-id> Base model id for the GGUF Modelfile FROM
37
+ (default: read from <dir>/config.json)
38
+ --quant <q> GGUF quantization (default: q4_k_m)
39
+ q2_k | q3_k_m | q4_0 | q4_k_m | q5_k_m | q8_0
40
+ --llama-cpp-path <dir> llama.cpp checkout (default: \$HOME/llama.cpp)
41
+ --skip-hf Skip the HuggingFace push (Ollama only)
42
+ --skip-ollama Skip the Ollama push (HF only)
43
+ --modelfile-only Just generate the Modelfile + GGUF, don't push
44
+ --help
45
+
46
+ Examples:
47
+ infernet publish ./run/checkpoint-final \\
48
+ --hf InfernetProtocol/svelte5-coder \\
49
+ --ollama infernet/svelte5-coder
50
+
51
+ infernet publish ./run --modelfile-only --quant q5_k_m
52
+
53
+ Prereqs:
54
+ HUGGINGFACE_TOKEN env or .env (write scope on the org)
55
+ ollama signin done once, prior to first push
56
+ llama.cpp cloned + built at \$HOME/llama.cpp (for GGUF convert)
57
+ `;
58
+
59
+ async function run(cmd, args, opts = {}) {
60
+ return new Promise((resolve, reject) => {
61
+ const child = spawn(cmd, args, { stdio: "inherit", ...opts });
62
+ child.on("exit", (code) => code === 0 ? resolve() : reject(new Error(`${cmd} ${args.join(" ")} exited ${code}`)));
63
+ child.on("error", reject);
64
+ });
65
+ }
66
+
67
+ async function which(bin) {
68
+ return new Promise((resolve) => {
69
+ const child = spawn("which", [bin], { stdio: ["ignore", "pipe", "ignore"] });
70
+ let out = "";
71
+ child.stdout.on("data", (b) => { out += b.toString(); });
72
+ child.on("exit", (code) => resolve(code === 0 ? out.trim() : null));
73
+ child.on("error", () => resolve(null));
74
+ });
75
+ }
76
+
77
+ async function detectBase(dir) {
78
+ try {
79
+ const cfg = JSON.parse(await fsp.readFile(path.join(dir, "config.json"), "utf8"));
80
+ return cfg?._name_or_path
81
+ ?? cfg?.model_name_or_path
82
+ ?? cfg?.base_model
83
+ ?? null;
84
+ } catch {
85
+ return null;
86
+ }
87
+ }
88
+
89
+ async function pushToHuggingFace({ dir, hfRepo, token }) {
90
+ if (!token) throw new Error("HUGGINGFACE_TOKEN required for HF push");
91
+ const hfCli = (await which("huggingface-cli")) ?? (await which("hf"));
92
+ if (!hfCli) {
93
+ throw new Error("huggingface-cli not found. Install: pip install -U huggingface_hub");
94
+ }
95
+ process.stdout.write(`\n→ Pushing ${dir} → huggingface.co/${hfRepo}\n`);
96
+ // Create the repo (idempotent — won't fail if it exists)
97
+ await run(hfCli, ["repo", "create", hfRepo, "--type", "model", "-y"], {
98
+ env: { ...process.env, HF_TOKEN: token, HUGGINGFACE_TOKEN: token }
99
+ }).catch(() => { /* may already exist */ });
100
+ await run(hfCli, ["upload", hfRepo, dir, ".", "--repo-type", "model"], {
101
+ env: { ...process.env, HF_TOKEN: token, HUGGINGFACE_TOKEN: token }
102
+ });
103
+ process.stdout.write(`✓ HF: https://huggingface.co/${hfRepo}\n`);
104
+ }
105
+
106
+ async function convertToGgufAndPush({ dir, ollamaRepo, base, quant = "q4_k_m", llamaCppPath, modelfileOnly }) {
107
+ const llamaCpp = llamaCppPath ?? path.join(process.env.HOME ?? "", "llama.cpp");
108
+ const convertScript = path.join(llamaCpp, "convert_hf_to_gguf.py");
109
+ if (!fs.existsSync(convertScript)) {
110
+ throw new Error(
111
+ `llama.cpp convert_hf_to_gguf.py not found at ${convertScript}.\n` +
112
+ `Clone + build llama.cpp first:\n` +
113
+ ` git clone https://github.com/ggml-org/llama.cpp ~/llama.cpp\n` +
114
+ ` cd ~/llama.cpp && cmake -B build && cmake --build build -j`
115
+ );
116
+ }
117
+
118
+ const ggufRaw = path.join(dir, "model.f16.gguf");
119
+ const ggufQuant = path.join(dir, `model.${quant}.gguf`);
120
+
121
+ process.stdout.write(`\n→ Converting ${dir} → ${ggufRaw}\n`);
122
+ await run("python3", [convertScript, dir, "--outfile", ggufRaw, "--outtype", "f16"]);
123
+
124
+ const quantBin = path.join(llamaCpp, "build", "bin", "llama-quantize");
125
+ if (fs.existsSync(quantBin)) {
126
+ process.stdout.write(`\n→ Quantizing → ${ggufQuant} (${quant})\n`);
127
+ await run(quantBin, [ggufRaw, ggufQuant, quant.toUpperCase()]);
128
+ } else {
129
+ process.stdout.write(`(skipping quantization — llama-quantize not built; using f16 GGUF)\n`);
130
+ }
131
+
132
+ const finalGguf = fs.existsSync(ggufQuant) ? ggufQuant : ggufRaw;
133
+
134
+ // Generate a Modelfile.
135
+ const modelfilePath = path.join(dir, "Modelfile");
136
+ const modelfile = [
137
+ `# Auto-generated by \`infernet publish\``,
138
+ `FROM ${finalGguf}`,
139
+ ``,
140
+ `# Inherit conversation template from base if provided.`,
141
+ base ? `# Base: ${base}` : `# Base: (unknown)`,
142
+ ``,
143
+ `PARAMETER temperature 0.7`,
144
+ `PARAMETER top_p 0.9`,
145
+ `PARAMETER repeat_penalty 1.1`,
146
+ ``,
147
+ `# ChatML template (works for Qwen / Llama-3 chat / most modern fine-tunes)`,
148
+ `TEMPLATE """{{ if .System }}<|im_start|>system`,
149
+ `{{ .System }}<|im_end|>`,
150
+ `{{ end }}{{ if .Prompt }}<|im_start|>user`,
151
+ `{{ .Prompt }}<|im_end|>`,
152
+ `{{ end }}<|im_start|>assistant`,
153
+ `{{ .Response }}<|im_end|>"""`,
154
+ ``,
155
+ `SYSTEM """You are a helpful assistant fine-tuned by Infernet Protocol."""`
156
+ ].join("\n");
157
+ await fsp.writeFile(modelfilePath, modelfile);
158
+ process.stdout.write(`\n✓ Modelfile: ${modelfilePath}\n`);
159
+
160
+ if (modelfileOnly) {
161
+ process.stdout.write(`\n(--modelfile-only set; skipping ollama create + push)\n`);
162
+ return;
163
+ }
164
+
165
+ if (!ollamaRepo) {
166
+ process.stdout.write(`(no --ollama target; skipping ollama push)\n`);
167
+ return;
168
+ }
169
+
170
+ process.stdout.write(`\n→ ollama create ${ollamaRepo} -f ${modelfilePath}\n`);
171
+ await run("ollama", ["create", ollamaRepo, "-f", modelfilePath]);
172
+
173
+ process.stdout.write(`\n→ ollama push ${ollamaRepo}\n`);
174
+ await run("ollama", ["push", ollamaRepo]);
175
+ process.stdout.write(`✓ Ollama: https://ollama.com/${ollamaRepo}\n`);
176
+ }
177
+
178
+ export default async function publish(args) {
179
+ if (args.has("help") || args.has("h")) {
180
+ process.stdout.write(HELP);
181
+ return 0;
182
+ }
183
+ const dir = args.positional?.[0];
184
+ if (!dir) {
185
+ process.stderr.write("error: model directory required\n\n");
186
+ process.stderr.write(HELP);
187
+ return 2;
188
+ }
189
+ if (!fs.existsSync(dir)) {
190
+ process.stderr.write(`error: ${dir} does not exist\n`);
191
+ return 1;
192
+ }
193
+
194
+ const hfRepo = args.get("hf") ?? null;
195
+ const ollamaRepo = args.get("ollama") ?? null;
196
+ const skipHf = args.has("skip-hf") || !hfRepo;
197
+ const skipOllama = args.has("skip-ollama") || (!ollamaRepo && !args.has("modelfile-only"));
198
+ const modelfileOnly = args.has("modelfile-only");
199
+ const quant = args.get("quant") ?? "q4_k_m";
200
+ const llamaCppPath = args.get("llama-cpp-path") ?? null;
201
+
202
+ if (skipHf && skipOllama && !modelfileOnly) {
203
+ process.stderr.write("error: nothing to do (set --hf <org>/<name> and/or --ollama <user>/<name>, or --modelfile-only)\n");
204
+ return 2;
205
+ }
206
+
207
+ const cfg = await loadConfig().catch(() => null);
208
+ const hfToken = process.env.HUGGINGFACE_TOKEN ?? cfg?.huggingface?.token ?? null;
209
+
210
+ const base = args.get("base") ?? await detectBase(dir);
211
+ if (!base && !skipOllama) {
212
+ process.stdout.write("(could not detect base model from config.json — Modelfile FROM line will reference the GGUF only)\n");
213
+ }
214
+
215
+ if (!skipHf) {
216
+ try {
217
+ await pushToHuggingFace({ dir, hfRepo, token: hfToken });
218
+ } catch (err) {
219
+ process.stderr.write(`HF push failed: ${err?.message ?? err}\n`);
220
+ return 1;
221
+ }
222
+ }
223
+
224
+ if (!skipOllama || modelfileOnly) {
225
+ try {
226
+ await convertToGgufAndPush({
227
+ dir, ollamaRepo, base, quant, llamaCppPath, modelfileOnly
228
+ });
229
+ } catch (err) {
230
+ process.stderr.write(`Ollama path failed: ${err?.message ?? err}\n`);
231
+ return 1;
232
+ }
233
+ }
234
+
235
+ process.stdout.write(`\n✓ publish complete\n`);
236
+ return 0;
237
+ }
238
+
239
+ export { HELP };
package/commands/start.js CHANGED
@@ -520,6 +520,8 @@ async function runDaemon(args, ctx) {
520
520
  result = await ollamaPullWithProgress(modelName, id);
521
521
  } else if (command === 'model_remove') {
522
522
  result = await ollamaSpawn(['rm', String(args?.model)]);
523
+ } else if (command === 'train_shard') {
524
+ result = await runTrainShard(args, id);
523
525
  } else {
524
526
  throw new Error(`unknown command verb: ${command}`);
525
527
  }
@@ -637,6 +639,76 @@ async function runDaemon(args, ctx) {
637
639
  return { stdout: 'pulled via /api/pull', code: 0 };
638
640
  }
639
641
 
642
+ /**
643
+ * Daemon-side handler for the `train_shard` command verb (federated
644
+ * LoRA). Pulls a JSONL shard from a public URL, runs the same Unsloth
645
+ * script the operator's local mode uses, and returns a path to the
646
+ * resulting adapter so the coordinator can FedAvg it.
647
+ *
648
+ * EXPERIMENTAL: assumes Python + Unsloth + datasets + trl are
649
+ * installed on the node. Future: bundle a thin Python venv at setup
650
+ * time so this works out of the box. Adapter upload is currently
651
+ * "report local path" — for cross-machine training the operator needs
652
+ * to provide an args.upload_url that we POST the safetensors to.
653
+ */
654
+ async function runTrainShard(args, _commandId) {
655
+ const fs = await import('node:fs/promises');
656
+ const path = await import('node:path');
657
+ const { buildUnslothScript } = await import('../lib/training/unsloth-script.js');
658
+
659
+ if (!args?.shard_url) throw new Error('train_shard: args.shard_url required');
660
+ if (!args?.base_model) throw new Error('train_shard: args.base_model required');
661
+
662
+ const runId = args.run_id ?? `shard-${Date.now()}`;
663
+ const workDir = path.join(process.env.HOME ?? '/tmp', '.infernet', 'training', runId);
664
+ await fs.mkdir(workDir, { recursive: true });
665
+
666
+ // Download the shard
667
+ const shardPath = path.join(workDir, 'shard.jsonl');
668
+ const res = await fetch(args.shard_url);
669
+ if (!res.ok) throw new Error(`fetch shard: HTTP ${res.status}`);
670
+ const text = await res.text();
671
+ await fs.writeFile(shardPath, text);
672
+
673
+ // Build + run the Unsloth script
674
+ const trainConfig = {
675
+ base_model: args.base_model,
676
+ training: args.training ?? {},
677
+ lora: args.lora ?? {},
678
+ input: { dataset: shardPath }
679
+ };
680
+ const scriptPath = path.join(workDir, 'unsloth_runner.py');
681
+ await fs.writeFile(scriptPath, buildUnslothScript({ trainConfig }));
682
+
683
+ await new Promise((resolve, reject) => {
684
+ const child = spawn('python3', [scriptPath, '--data', shardPath, '--output', workDir], {
685
+ stdio: 'inherit'
686
+ });
687
+ child.on('exit', (code) => code === 0 ? resolve() : reject(new Error(`unsloth_runner exited ${code}`)));
688
+ child.on('error', reject);
689
+ });
690
+
691
+ const adapterDir = path.join(workDir, 'checkpoint-final');
692
+
693
+ // Optional: upload adapter back to the operator-provided URL
694
+ if (args.upload_url) {
695
+ const adapterFile = path.join(adapterDir, 'adapter_model.safetensors');
696
+ const buf = await fs.readFile(adapterFile);
697
+ const up = await fetch(args.upload_url, {
698
+ method: 'PUT',
699
+ body: buf,
700
+ headers: { 'Content-Type': 'application/octet-stream' }
701
+ });
702
+ if (!up.ok) throw new Error(`upload adapter: HTTP ${up.status}`);
703
+ }
704
+
705
+ return {
706
+ adapter_path: adapterDir,
707
+ uploaded: !!args.upload_url,
708
+ shard_url: args.shard_url
709
+ };
710
+ }
711
+
640
712
  function snapshot() {
641
713
  return {
642
714
  pid: process.pid,
package/commands/train.js CHANGED
@@ -17,12 +17,22 @@ import fs from "node:fs/promises";
17
17
  import path from "node:path";
18
18
  import os from "node:os";
19
19
  import { spawn } from "node:child_process";
20
+ import fsSync from "node:fs";
20
21
  import { createWriteStream } from "node:fs";
21
22
  import { loadConfig } from "../lib/config.js";
23
+ import { resolveValueSerpKey, buildTrainingDataset } from "../lib/data-crawler.js";
24
+ import { buildUnslothScript } from "../lib/training/unsloth-script.js";
25
+ import {
26
+ splitJsonl, discoverNodes, queueTrainShard, awaitCommand, fedAvg
27
+ } from "../lib/training/p2p-coordinator.js";
22
28
 
23
29
  const HELP = `infernet train — fine-tune or train models on the P2P GPU network
24
30
 
25
31
  Usage:
32
+ infernet train data --query <q> Crawl ValueSerp + emit JSONL
33
+ [--out <file>] (default: ./data/train.jsonl)
34
+ [--num <n>] (default: 20 results)
35
+ [--domains <a,b,c>] (whitelist filter)
26
36
  infernet train init [--output <dir>] Scaffold infernet.train.yml
27
37
  infernet train run [--config <file>] Run training (local or P2P)
28
38
  [--local] Force local execution
@@ -32,6 +42,7 @@ Usage:
32
42
  infernet train logs [<run-id>] Tail run logs
33
43
 
34
44
  Examples:
45
+ infernet train data --query "svelte 5 framework docs" --domains svelte.dev,github.com
35
46
  infernet train init --output ./my-run
36
47
  infernet train run --local --output ./my-run
37
48
  infernet train run --config infernet.train.yml
@@ -318,22 +329,192 @@ async function cmdRun(args) {
318
329
 
319
330
  const useLocal = forceLocal || workloadClass === "C1";
320
331
 
332
+ // P2P (federated LoRA) — workload_class C2/C3 or explicit --p2p flag.
321
333
  if (!useLocal) {
322
- process.stdout.write(`Submitting to P2P network (workload class ${workloadClass})...\n`);
323
- process.stdout.write(`(P2P training submission not yet implemented — use --local for now)\n`);
324
- await updateRunStatus(runDir, "pending_p2p");
325
- return 0;
334
+ return runP2PFederated({ trainConfig, runDir, runId });
326
335
  }
327
336
 
328
- // Local execution
329
- if (runtime !== "microgpt") {
330
- process.stdout.write(`Note: only microgpt runtime is supported in local mode (v0.1).\n`);
331
- process.stdout.write(`Falling back to microgpt.\n`);
337
+ // Local — pick a real runtime (Unsloth) for QLoRA fine-tuning of a
338
+ // base HF model. microgpt remains for the from-scratch demo path.
339
+ if (runtime === "unsloth" || runtime === "transformers" || runtime === "trl") {
340
+ return runUnslothLocal({ trainConfig, runDir, runId });
332
341
  }
333
-
334
342
  return runMicrogptLocal({ trainConfig, runDir, runId });
335
343
  }
336
344
 
345
+ async function runUnslothLocal({ trainConfig, runDir, runId }) {
346
+ const dataset = trainConfig.input?.dataset;
347
+ if (!dataset) {
348
+ process.stderr.write(`error: input.dataset is required for unsloth runtime\n`);
349
+ return 1;
350
+ }
351
+ if (!fsSync.existsSync(dataset)) {
352
+ process.stderr.write(`error: dataset not found: ${dataset}\n`);
353
+ return 1;
354
+ }
355
+
356
+ const scriptPath = path.join(runDir, "unsloth_runner.py");
357
+ await fs.writeFile(scriptPath, buildUnslothScript({ trainConfig }));
358
+
359
+ const python = await findPython();
360
+ if (!python) {
361
+ process.stderr.write(`error: python3 not found\n`);
362
+ process.stderr.write(`install with: sudo apt install python3 python3-pip && pip install unsloth datasets trl\n`);
363
+ return 1;
364
+ }
365
+
366
+ process.stdout.write(`Starting Unsloth QLoRA training (runtime=unsloth, base=${trainConfig.base_model})...\n\n`);
367
+ const logStream = createWriteStream(path.join(runDir, "run.log"), { flags: "a" });
368
+
369
+ return new Promise((resolve) => {
370
+ const child = spawn(python, [scriptPath, "--data", dataset, "--output", runDir], {
371
+ stdio: ["ignore", "pipe", "pipe"]
372
+ });
373
+ child.stdout.on("data", (chunk) => {
374
+ const lines = chunk.toString().split("\n").filter(Boolean);
375
+ for (const line of lines) {
376
+ logStream.write(line + "\n");
377
+ try {
378
+ const event = JSON.parse(line);
379
+ if (event.event === "step") {
380
+ process.stdout.write(` step ${event.step} loss=${event.loss?.toFixed?.(4) ?? "—"} lr=${event.lr?.toExponential?.(2) ?? "—"}\n`);
381
+ } else if (event.event) {
382
+ process.stdout.write(` ${event.event}${event.path ? ` → ${event.path}` : ""}${event.rows ? ` (${event.rows} rows)` : ""}\n`);
383
+ } else if (event.error) {
384
+ process.stderr.write(` ✗ ${event.error}\n`);
385
+ }
386
+ } catch {
387
+ process.stdout.write(` ${line}\n`);
388
+ }
389
+ }
390
+ });
391
+ child.stderr.on("data", (chunk) => {
392
+ const txt = chunk.toString();
393
+ logStream.write(txt);
394
+ process.stderr.write(txt);
395
+ });
396
+ child.on("exit", async (code) => {
397
+ logStream.end();
398
+ await updateRunStatus(runDir, code === 0 ? "completed" : "failed");
399
+ if (code === 0) {
400
+ process.stdout.write(`\n✓ Training complete. Model saved to ${path.join(runDir, "checkpoint-final")}\n`);
401
+ process.stdout.write(`Next: infernet publish ${path.join(runDir, "checkpoint-final")} --hf <org>/<name> --ollama <user>/<name>\n`);
402
+ }
403
+ resolve(code === 0 ? 0 : 1);
404
+ });
405
+ child.on("error", (err) => {
406
+ process.stderr.write(`spawn error: ${err?.message ?? err}\n`);
407
+ resolve(1);
408
+ });
409
+ });
410
+ }
411
+
412
+ /**
413
+ * Federated LoRA across all owner-controlled nodes that meet the
414
+ * resources.min_vram_gb threshold. Splits the dataset into N shards,
415
+ * queues a `train_shard` command per node, awaits completion, then
416
+ * runs FedAvg over the resulting adapters.
417
+ *
418
+ * EXPERIMENTAL — assumes the operator has a way to host shard files
419
+ * reachable by the daemons (S3, HF dataset, ngrok, etc) and that nodes
420
+ * have a working Unsloth Python install.
421
+ */
422
+ async function runP2PFederated({ trainConfig, runDir, runId }) {
423
+ const cfg = await loadConfig().catch(() => ({}));
424
+ const controlPlaneUrl = cfg?.controlPlane?.url ?? process.env.NEXT_PUBLIC_APP_URL ?? "https://infernetprotocol.com";
425
+ const sessionToken = cfg?.session?.token ?? process.env.INFERNET_SESSION_TOKEN;
426
+ if (!sessionToken) {
427
+ process.stderr.write(`error: P2P training needs a logged-in session — run \`infernet login\` first\n`);
428
+ return 1;
429
+ }
430
+
431
+ const minVramGb = trainConfig.resources?.min_vram_gb ?? 12;
432
+ process.stdout.write(`P2P federated LoRA — discovering nodes (≥ ${minVramGb} GB VRAM)…\n`);
433
+ const nodes = await discoverNodes({ controlPlaneUrl, sessionToken, minVramGb });
434
+ if (nodes.length === 0) {
435
+ process.stderr.write(`No online nodes meet min_vram_gb=${minVramGb}.\n`);
436
+ return 1;
437
+ }
438
+ process.stdout.write(` found ${nodes.length} eligible node(s):\n`);
439
+ for (const n of nodes) {
440
+ process.stdout.write(` ${n.name ?? n.public_key.slice(0, 16)} ${n.specs?.gpus?.[0]?.vram_tier ?? "?"}\n`);
441
+ }
442
+
443
+ // Split dataset
444
+ const datasetPath = trainConfig.input?.dataset;
445
+ if (!datasetPath) {
446
+ process.stderr.write(`error: input.dataset is required\n`);
447
+ return 1;
448
+ }
449
+ const shardsDir = path.join(runDir, "shards");
450
+ process.stdout.write(`\nSplitting ${datasetPath} into ${nodes.length} shard(s)…\n`);
451
+ const shards = await splitJsonl({ inputPath: datasetPath, outDir: shardsDir, numShards: nodes.length });
452
+
453
+ if (!process.env.INFERNET_SHARD_BASE_URL) {
454
+ process.stdout.write(
455
+ `\n⚠ P2P training is in MVP — set INFERNET_SHARD_BASE_URL to a public URL\n` +
456
+ ` the daemons can fetch from. Each shard's file is at:\n` +
457
+ shards.map((s) => ` ${s.path} (${s.lines} examples)`).join("\n") + "\n" +
458
+ ` Example: \`aws s3 sync ${shardsDir} s3://your-bucket/runs/${runId}/\`\n` +
459
+ ` then: \`export INFERNET_SHARD_BASE_URL=https://your-bucket.s3.amazonaws.com/runs/${runId}\`\n` +
460
+ ` and re-run \`infernet train run\`.\n`
461
+ );
462
+ return 1;
463
+ }
464
+ const shardBase = process.env.INFERNET_SHARD_BASE_URL.replace(/\/$/, "");
465
+
466
+ // Queue train_shard commands
467
+ process.stdout.write(`\nQueueing train_shard commands…\n`);
468
+ const inFlight = [];
469
+ for (let i = 0; i < nodes.length && i < shards.length; i += 1) {
470
+ const node = nodes[i];
471
+ const shard = shards[i];
472
+ const cmd = await queueTrainShard({
473
+ controlPlaneUrl, sessionToken, pubkey: node.public_key,
474
+ args: {
475
+ run_id: runId,
476
+ base_model: trainConfig.base_model,
477
+ shard_url: `${shardBase}/${path.basename(shard.path)}`,
478
+ training: trainConfig.training,
479
+ lora: trainConfig.lora,
480
+ upload_url: process.env.INFERNET_ADAPTER_UPLOAD_URL ?? null
481
+ }
482
+ });
483
+ process.stdout.write(` → node ${node.name ?? node.public_key.slice(0, 16)} command ${cmd.id}\n`);
484
+ inFlight.push({ node, shard, commandId: cmd.id });
485
+ }
486
+
487
+ // Await each
488
+ process.stdout.write(`\nWaiting for shards to finish (this can take hours)…\n`);
489
+ const adapterDirs = [];
490
+ for (const w of inFlight) {
491
+ try {
492
+ const res = await awaitCommand({
493
+ controlPlaneUrl, sessionToken, pubkey: w.node.public_key, commandId: w.commandId
494
+ });
495
+ if (res.status === "completed" && res.result?.adapter_path) {
496
+ process.stdout.write(` ✓ ${w.node.name ?? w.node.public_key.slice(0, 16)}: ${res.result.adapter_path}\n`);
497
+ adapterDirs.push(res.result.adapter_path);
498
+ } else {
499
+ process.stderr.write(` ✗ ${w.node.name ?? w.node.public_key.slice(0, 16)}: ${res.error ?? res.status}\n`);
500
+ }
501
+ } catch (err) {
502
+ process.stderr.write(` ✗ ${w.node.name ?? "?"}: ${err.message}\n`);
503
+ }
504
+ }
505
+
506
+ if (adapterDirs.length === 0) {
507
+ process.stderr.write(`\nNo successful shards — aborting aggregation.\n`);
508
+ return 1;
509
+ }
510
+
511
+ process.stdout.write(`\nRunning FedAvg over ${adapterDirs.length} adapter(s)…\n`);
512
+ await fedAvg({ adapterDirs, outDir: runDir });
513
+ await updateRunStatus(runDir, "completed");
514
+ process.stdout.write(`\n✓ Federated training complete. Final model: ${path.join(runDir, "checkpoint-final")}\n`);
515
+ return 0;
516
+ }
517
+
337
518
  async function runMicrogptLocal({ trainConfig, runDir, runId }) {
338
519
  const steps = parseInt(trainConfig.training?.epochs ?? 3, 10) * 100;
339
520
  const lr = parseFloat(trainConfig.training?.learning_rate ?? 3e-4);
@@ -524,6 +705,47 @@ async function cmdLogs(args) {
524
705
  return 0;
525
706
  }
526
707
 
708
+ async function cmdData(args) {
709
+ const query = args.get("query") ?? args.get("q");
710
+ if (!query) {
711
+ process.stderr.write("error: --query <text> is required\n");
712
+ process.stderr.write(`example: infernet train data --query "svelte 5 framework docs"\n`);
713
+ return 2;
714
+ }
715
+ const apiKey = await resolveValueSerpKey();
716
+ if (!apiKey) {
717
+ process.stderr.write(
718
+ "error: VALUESERP_API_KEY is not set.\n" +
719
+ "Get a key at https://valueserp.com → set in env or in\n" +
720
+ "~/.config/infernet/config.json under integrations.valueserp.api_key\n"
721
+ );
722
+ return 1;
723
+ }
724
+ const out = args.get("out") ?? args.get("output") ?? "./data/train.jsonl";
725
+ const num = Number.parseInt(args.get("num") ?? "20", 10) || 20;
726
+ const domains = args.get("domains")?.split(",").map((s) => s.trim()).filter(Boolean) ?? null;
727
+ const minChars = Number.parseInt(args.get("min-chars") ?? "200", 10) || 200;
728
+ const maxChars = Number.parseInt(args.get("max-chars") ?? "4000", 10) || 4000;
729
+
730
+ process.stdout.write(`Searching ValueSerp for: "${query}"\n`);
731
+ if (domains) process.stdout.write(` domain whitelist: ${domains.join(", ")}\n`);
732
+
733
+ const result = await buildTrainingDataset({
734
+ query, apiKey, outPath: out, num, domains, minChars, maxChars,
735
+ onProgress: ({ stage, index, total, url }) => {
736
+ if (stage === "fetch") {
737
+ process.stdout.write(` [${index}/${total}] ${url}\n`);
738
+ }
739
+ }
740
+ });
741
+
742
+ process.stdout.write(`\n✓ Wrote ${result.examples} examples from ${result.urls} URLs to ${result.outPath}\n`);
743
+ process.stdout.write(`\nNext steps:\n`);
744
+ process.stdout.write(` infernet train init --output ./run # scaffold infernet.train.yml\n`);
745
+ process.stdout.write(` infernet train run --local --output ./run # train against ${out}\n`);
746
+ return 0;
747
+ }
748
+
527
749
  export default async function train(args) {
528
750
  if (args.has("help") || args.has("h")) {
529
751
  process.stdout.write(HELP);
@@ -535,6 +757,9 @@ export default async function train(args) {
535
757
 
536
758
  try {
537
759
  switch (sub) {
760
+ case "data":
761
+ case "crawl":
762
+ return await cmdData(args);
538
763
  case "init":
539
764
  return await cmdInit(args);
540
765
  case "run":
package/index.js CHANGED
@@ -44,6 +44,7 @@ import debug from './commands/debug.js';
44
44
  import deploy from './commands/deploy.js';
45
45
  import consoleCmd from './commands/console.js';
46
46
  import train from './commands/train.js';
47
+ import publish from './commands/publish.js';
47
48
  import uncensored from './commands/uncensored.js';
48
49
 
49
50
  function parseArgs(argv) {
@@ -106,11 +107,11 @@ const COMMANDS = {
106
107
  // aliases
107
108
  uninstall: remove,
108
109
  start, status, stop, restart, stats, logs,
109
- payout, payments, gpu, firewall, chat, setup, model, train, uncensored, tui, doctor, service, pubkey, debug, deploy, console: consoleCmd, help
110
+ payout, payments, gpu, firewall, chat, setup, model, train, publish, uncensored, tui, doctor, service, pubkey, debug, deploy, console: consoleCmd, help
110
111
  };
111
112
 
112
113
  // Commands that can run without a loaded config.
113
- const NO_CONFIG = new Set(['init', 'login', 'help', 'stats', 'logs', 'stop', 'restart', 'gpu', 'firewall', 'chat', 'setup', 'model', 'train', 'uncensored', 'tui', 'doctor', 'service', 'pubkey', 'debug', 'deploy', 'console', 'upgrade', 'update', 'remove', 'uninstall']);
114
+ const NO_CONFIG = new Set(['init', 'login', 'help', 'stats', 'logs', 'stop', 'restart', 'gpu', 'firewall', 'chat', 'setup', 'model', 'train', 'publish', 'uncensored', 'tui', 'doctor', 'service', 'pubkey', 'debug', 'deploy', 'console', 'upgrade', 'update', 'remove', 'uninstall']);
114
115
  // Commands that need a config but not a control-plane client (none today
115
116
  // — kept as a future escape hatch).
116
117
  const NO_CLIENT = new Set();
@@ -0,0 +1,183 @@
1
+ /**
2
+ * Search-driven training-data collector.
3
+ *
4
+ * Pipeline:
5
+ * 1. Hit valueserp.com /search with a query. Returns ranked URLs.
6
+ * 2. For each URL: fetch HTML, strip nav/script/style, extract main text.
7
+ * 3. For each page: chunk into Q/A pairs (heading-based) and emit JSONL.
8
+ *
9
+ * The output JSONL uses the chatml `messages` shape, ready to feed into
10
+ * `infernet train run` or any HF/Unsloth/Axolotl pipeline.
11
+ *
12
+ * VALUESERP_API_KEY must be set in env or `~/.config/infernet/config.json`
13
+ * under integrations.valueserp.api_key.
14
+ */
15
+
16
+ import fs from "node:fs/promises";
17
+ import path from "node:path";
18
+ import { loadConfig } from "./config.js";
19
+
20
+ export async function resolveValueSerpKey() {
21
+ if (process.env.VALUESERP_API_KEY) return process.env.VALUESERP_API_KEY;
22
+ const cfg = await loadConfig().catch(() => null);
23
+ return cfg?.integrations?.valueserp?.api_key
24
+ ?? cfg?.valueserp?.api_key
25
+ ?? null;
26
+ }
27
+
28
+ /**
29
+ * Search the web via ValueSerp and return the ranked list of result URLs.
30
+ * @param {object} opts
31
+ * @param {string} opts.query
32
+ * @param {string} opts.apiKey
33
+ * @param {number} [opts.num=20]
34
+ * @param {string} [opts.location]
35
+ */
36
+ export async function valueSerpSearch({ query, apiKey, num = 20, location } = {}) {
37
+ if (!query) throw new Error("valueserp: query required");
38
+ if (!apiKey) throw new Error("valueserp: apiKey required (set VALUESERP_API_KEY)");
39
+
40
+ const params = new URLSearchParams({
41
+ api_key: apiKey,
42
+ q: query,
43
+ num: String(num),
44
+ output: "json"
45
+ });
46
+ if (location) params.set("location", location);
47
+
48
+ const res = await fetch(`https://api.valueserp.com/search?${params}`);
49
+ if (!res.ok) {
50
+ const txt = await res.text().catch(() => res.statusText);
51
+ throw new Error(`ValueSerp ${res.status}: ${txt.slice(0, 200)}`);
52
+ }
53
+ const json = await res.json();
54
+ return (json?.organic_results ?? []).map((r) => ({
55
+ rank: r.position,
56
+ title: r.title,
57
+ url: r.link,
58
+ snippet: r.snippet,
59
+ domain: r.domain
60
+ }));
61
+ }
62
+
63
+ /** Cheap HTML → readable text. Strips scripts/styles/nav, collapses whitespace. */
64
+ export function extractTextFromHtml(html) {
65
+ if (!html) return "";
66
+ // Drop script + style content entirely.
67
+ let s = html
68
+ .replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, "")
69
+ .replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, "")
70
+ .replace(/<noscript\b[^>]*>[\s\S]*?<\/noscript>/gi, "")
71
+ .replace(/<head\b[^>]*>[\s\S]*?<\/head>/gi, "");
72
+ // Drop common non-content elements.
73
+ s = s.replace(/<(nav|footer|aside|header|form|iframe)\b[^>]*>[\s\S]*?<\/\1>/gi, "");
74
+ // Convert headings/paragraphs/list items to text with line breaks.
75
+ s = s.replace(/<\/?(h[1-6]|p|li|br|div|tr)[^>]*>/gi, "\n");
76
+ // Strip remaining tags.
77
+ s = s.replace(/<[^>]+>/g, " ");
78
+ // Decode common entities.
79
+ s = s
80
+ .replace(/&nbsp;/g, " ")
81
+ .replace(/&amp;/g, "&")
82
+ .replace(/&lt;/g, "<")
83
+ .replace(/&gt;/g, ">")
84
+ .replace(/&quot;/g, '"')
85
+ .replace(/&#39;/g, "'")
86
+ .replace(/&#x27;/g, "'");
87
+ // Collapse whitespace.
88
+ s = s.replace(/[ \t]+/g, " ").replace(/\n[ \t]*/g, "\n").replace(/\n{3,}/g, "\n\n");
89
+ return s.trim();
90
+ }
91
+
92
+ /** Fetch a URL and extract its main text content. Returns "" on failure. */
93
+ export async function fetchAndExtract(url, { timeoutMs = 15_000 } = {}) {
94
+ try {
95
+ const ctrl = new AbortController();
96
+ const t = setTimeout(() => ctrl.abort(), timeoutMs);
97
+ const res = await fetch(url, {
98
+ signal: ctrl.signal,
99
+ redirect: "follow",
100
+ headers: {
101
+ "User-Agent": "Mozilla/5.0 (compatible; InfernetTrainCrawler/0.1; +https://infernetprotocol.com)"
102
+ }
103
+ });
104
+ clearTimeout(t);
105
+ if (!res.ok) return "";
106
+ const ct = res.headers.get("content-type") ?? "";
107
+ if (!ct.includes("text/html") && !ct.includes("text/plain")) return "";
108
+ const html = await res.text();
109
+ return extractTextFromHtml(html);
110
+ } catch {
111
+ return "";
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Convert a long body of text into chatml-style JSONL training examples.
117
+ * Heuristic: split on blank lines into paragraphs; treat the first
118
+ * paragraph as a "question" prompt and following paragraphs as the
119
+ * model's answer. Skips paragraphs shorter than minChars or longer than
120
+ * maxChars (caps very long pages).
121
+ */
122
+ export function paragraphsToChatml({ url, title, text, query, minChars = 200, maxChars = 4000 }) {
123
+ const paras = text.split(/\n{2,}/).map((p) => p.trim()).filter(Boolean);
124
+ const examples = [];
125
+ for (let i = 0; i < paras.length; i += 1) {
126
+ const para = paras[i];
127
+ if (para.length < minChars) continue;
128
+ const truncated = para.length > maxChars ? para.slice(0, maxChars) + "…" : para;
129
+ examples.push({
130
+ messages: [
131
+ { role: "system", content: `You are an expert assistant trained on ${title ?? url}.` },
132
+ { role: "user", content: `${query} — section ${i + 1}` },
133
+ { role: "assistant", content: truncated }
134
+ ],
135
+ meta: { source_url: url, source_title: title, query, paragraph_index: i }
136
+ });
137
+ }
138
+ return examples;
139
+ }
140
+
141
+ /**
142
+ * Top-level helper: search + crawl + chunk → write training JSONL.
143
+ * Used by the `infernet train data` CLI surface.
144
+ *
145
+ * @returns {{ urls: number, examples: number, outPath: string }}
146
+ */
147
+ export async function buildTrainingDataset({
148
+ query,
149
+ apiKey,
150
+ outPath,
151
+ num = 20,
152
+ minChars = 200,
153
+ maxChars = 4000,
154
+ domains = null, // optional whitelist (e.g. ["svelte.dev", "github.com"])
155
+ onProgress = () => {}
156
+ }) {
157
+ const results = await valueSerpSearch({ query, apiKey, num });
158
+ const filtered = domains
159
+ ? results.filter((r) => domains.some((d) => r.domain?.includes(d)))
160
+ : results;
161
+
162
+ await fs.mkdir(path.dirname(outPath), { recursive: true });
163
+ const out = await fs.open(outPath, "w");
164
+
165
+ let totalExamples = 0;
166
+ let urlsUsed = 0;
167
+ for (let i = 0; i < filtered.length; i += 1) {
168
+ const r = filtered[i];
169
+ onProgress({ stage: "fetch", index: i + 1, total: filtered.length, url: r.url });
170
+ const text = await fetchAndExtract(r.url);
171
+ if (!text || text.length < minChars) continue;
172
+ urlsUsed += 1;
173
+ const examples = paragraphsToChatml({
174
+ url: r.url, title: r.title, text, query, minChars, maxChars
175
+ });
176
+ for (const ex of examples) {
177
+ await out.write(JSON.stringify(ex) + "\n");
178
+ totalExamples += 1;
179
+ }
180
+ }
181
+ await out.close();
182
+ return { urls: urlsUsed, examples: totalExamples, outPath };
183
+ }
@@ -0,0 +1,192 @@
1
+ /**
2
+ * P2P training coordinator.
3
+ *
4
+ * Federated-LoRA flow:
5
+ * 1. Discover candidate nodes — operator-owned providers whose
6
+ * reported VRAM tier meets the run's resources.min_vram_gb.
7
+ * 2. Split JSONL dataset into N shards (one per node).
8
+ * 3. Upload each shard to a public URL (S3/HF/whatever) — operator
9
+ * provides --shard-base-url that returns N {shard_url, upload_url}
10
+ * pairs OR we use a transient HF dataset.
11
+ * 4. For each node: queue a `train_shard` command via the existing
12
+ * node_commands API. The daemon picks it up, pulls its shard,
13
+ * runs the same Unsloth script, posts the resulting adapter
14
+ * back via the upload URL.
15
+ * 5. Poll command status; collect adapter URLs as nodes complete.
16
+ * 6. Run FedAvg on the collected adapters → emit final adapter +
17
+ * checkpoint.
18
+ *
19
+ * Status: MVP. Step 3 (shard hosting) currently writes shards to a local
20
+ * directory and expects the operator to expose them somewhere reachable
21
+ * by the daemons (e.g. via a tunnel / S3 bucket). Step 6 invokes a
22
+ * Python aggregator script.
23
+ */
24
+
25
+ import fs from "node:fs/promises";
26
+ import fsSync from "node:fs";
27
+ import path from "node:path";
28
+ import { spawn } from "node:child_process";
29
+
30
+ /**
31
+ * Split a JSONL file into N shards of roughly equal line count.
32
+ * Writes shards to <outDir>/shard-<i>.jsonl.
33
+ */
34
+ export async function splitJsonl({ inputPath, outDir, numShards }) {
35
+ if (numShards <= 0) throw new Error("numShards must be > 0");
36
+ const raw = await fs.readFile(inputPath, "utf8");
37
+ const lines = raw.split("\n").filter(Boolean);
38
+ if (lines.length === 0) throw new Error(`${inputPath} is empty`);
39
+
40
+ await fs.mkdir(outDir, { recursive: true });
41
+ const shards = [];
42
+ const perShard = Math.ceil(lines.length / numShards);
43
+ for (let i = 0; i < numShards; i += 1) {
44
+ const start = i * perShard;
45
+ const end = Math.min(start + perShard, lines.length);
46
+ if (start >= end) break;
47
+ const shardPath = path.join(outDir, `shard-${i}.jsonl`);
48
+ await fs.writeFile(shardPath, lines.slice(start, end).join("\n") + "\n");
49
+ shards.push({ index: i, path: shardPath, lines: end - start });
50
+ }
51
+ return shards;
52
+ }
53
+
54
+ /**
55
+ * Discover candidate nodes for a training run by hitting the
56
+ * operator-side `/api/v1/user/nodes` route. Returns nodes filtered by
57
+ * status="online" + vram tier meeting minVramGb (very rough — sanitized
58
+ * telemetry only carries vram tier strings).
59
+ */
60
+ export async function discoverNodes({ controlPlaneUrl, sessionToken, minVramGb = 8 }) {
61
+ const res = await fetch(`${controlPlaneUrl}/api/v1/user/nodes`, {
62
+ headers: { Authorization: `Bearer ${sessionToken}` }
63
+ });
64
+ if (!res.ok) throw new Error(`discover nodes: HTTP ${res.status}`);
65
+ const body = await res.json();
66
+ const nodes = body?.data ?? [];
67
+ return nodes.filter((n) => {
68
+ if (n.status !== "online") return false;
69
+ const vramTier = (n.specs?.gpus ?? []).map((g) => g.vram_tier).find(Boolean) ?? "unknown";
70
+ return tierMeets(vramTier, minVramGb);
71
+ });
72
+ }
73
+
74
+ function tierMeets(tier, minVramGb) {
75
+ const TIER_GB = { "<8gb": 6, "8-16gb": 12, "16-24gb": 20, "24-48gb": 36, ">=48gb": 64 };
76
+ return (TIER_GB[tier] ?? 0) >= minVramGb;
77
+ }
78
+
79
+ /**
80
+ * Queue a train_shard command for a single node via the user-facing
81
+ * node_commands route.
82
+ */
83
+ export async function queueTrainShard({ controlPlaneUrl, sessionToken, pubkey, args }) {
84
+ const res = await fetch(
85
+ `${controlPlaneUrl}/api/v1/user/nodes/${encodeURIComponent(pubkey)}/commands`,
86
+ {
87
+ method: "POST",
88
+ headers: {
89
+ Authorization: `Bearer ${sessionToken}`,
90
+ "Content-Type": "application/json"
91
+ },
92
+ body: JSON.stringify({ command: "train_shard", args })
93
+ }
94
+ );
95
+ const body = await res.json().catch(() => ({}));
96
+ if (!res.ok) throw new Error(`queue train_shard: ${body?.error ?? res.statusText}`);
97
+ return body.data;
98
+ }
99
+
100
+ /** Poll a command id until it leaves pending/running. */
101
+ export async function awaitCommand({ controlPlaneUrl, sessionToken, pubkey, commandId, timeoutMs = 30 * 60 * 1000 }) {
102
+ const deadline = Date.now() + timeoutMs;
103
+ while (Date.now() < deadline) {
104
+ const res = await fetch(
105
+ `${controlPlaneUrl}/api/v1/user/nodes/${encodeURIComponent(pubkey)}/commands?limit=20`,
106
+ { headers: { Authorization: `Bearer ${sessionToken}` } }
107
+ );
108
+ if (res.ok) {
109
+ const body = await res.json();
110
+ const cmd = (body?.data ?? []).find((c) => c.id === commandId);
111
+ if (cmd && cmd.status !== "pending" && cmd.status !== "running") return cmd;
112
+ }
113
+ await new Promise((r) => setTimeout(r, 5000));
114
+ }
115
+ throw new Error(`command ${commandId} did not finish within ${Math.round(timeoutMs / 1000)}s`);
116
+ }
117
+
118
+ /**
119
+ * Run the FedAvg aggregator over a list of adapter directories.
120
+ * Emits <outDir>/checkpoint-final/ with the averaged adapter weights.
121
+ *
122
+ * Requires Python with `peft` + `safetensors` installed.
123
+ */
124
+ export async function fedAvg({ adapterDirs, outDir }) {
125
+ if (adapterDirs.length === 0) throw new Error("no adapters to average");
126
+
127
+ const scriptPath = path.join(outDir, "_fedavg.py");
128
+ await fs.writeFile(scriptPath, FEDAVG_SCRIPT);
129
+
130
+ return new Promise((resolve, reject) => {
131
+ const args = [scriptPath, "--out", path.join(outDir, "checkpoint-final"), ...adapterDirs];
132
+ const child = spawn("python3", args, { stdio: "inherit" });
133
+ child.on("exit", (code) => code === 0 ? resolve() : reject(new Error(`fedavg exited ${code}`)));
134
+ child.on("error", reject);
135
+ });
136
+ }
137
+
138
+ const FEDAVG_SCRIPT = `#!/usr/bin/env python3
139
+ """Average LoRA adapters across N peers (Federated Averaging).
140
+
141
+ Loads the adapter_model.safetensors from each input directory, averages
142
+ matching tensors, writes the result to --out alongside one of the input
143
+ adapter_config.json files (they should match across peers).
144
+ """
145
+ import argparse
146
+ import json
147
+ import shutil
148
+ from pathlib import Path
149
+
150
+ import torch
151
+ from safetensors.torch import load_file, save_file
152
+
153
+
154
+ def main():
155
+ p = argparse.ArgumentParser()
156
+ p.add_argument("--out", required=True)
157
+ p.add_argument("inputs", nargs="+")
158
+ args = p.parse_args()
159
+
160
+ out = Path(args.out)
161
+ out.mkdir(parents=True, exist_ok=True)
162
+
163
+ inputs = [Path(d) for d in args.inputs]
164
+ weight_files = [d / "adapter_model.safetensors" for d in inputs]
165
+ for wf in weight_files:
166
+ if not wf.exists():
167
+ raise SystemExit(f"missing adapter_model.safetensors in {wf.parent}")
168
+
169
+ print(f"averaging {len(weight_files)} adapters → {out}")
170
+ state = {}
171
+ for wf in weight_files:
172
+ partial = load_file(str(wf))
173
+ for k, v in partial.items():
174
+ state.setdefault(k, []).append(v.float())
175
+
176
+ averaged = {k: torch.stack(vs).mean(dim=0) for k, vs in state.items()}
177
+ save_file(averaged, str(out / "adapter_model.safetensors"))
178
+
179
+ # Copy adapter_config.json + tokenizer files from the first input
180
+ for fname in ("adapter_config.json", "tokenizer.json", "tokenizer_config.json", "special_tokens_map.json"):
181
+ src = inputs[0] / fname
182
+ if src.exists():
183
+ shutil.copy(str(src), str(out / fname))
184
+
185
+ summary = {"averaged_from": [str(p) for p in inputs], "tensors": len(averaged)}
186
+ (out / "fedavg_summary.json").write_text(json.dumps(summary, indent=2))
187
+ print(json.dumps(summary, indent=2))
188
+
189
+
190
+ if __name__ == "__main__":
191
+ main()
192
+ `;
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Generate a Python QLoRA training script using Unsloth.
3
+ *
4
+ * Unsloth is the de-facto standard for fast 4-bit LoRA fine-tuning on
5
+ * consumer GPUs (https://github.com/unslothai/unsloth). We generate a
6
+ * self-contained script per run, parameterized by the YAML config; the
7
+ * script is what `infernet train run` and the daemon's train_shard
8
+ * handler both invoke.
9
+ *
10
+ * The output script:
11
+ * - loads the base model in 4-bit
12
+ * - applies LoRA to attention projections
13
+ * - reads ChatML-format JSONL from --data
14
+ * - trains for the configured epochs
15
+ * - saves checkpoint-final/ as a HF-shape directory (adapter + tokenizer)
16
+ * - writes per-step metrics.jsonl alongside
17
+ */
18
+
19
+ export function buildUnslothScript({ trainConfig }) {
20
+ const baseModel = trainConfig.base_model ?? "unsloth/Qwen2.5-7B-Instruct";
21
+ const epochs = trainConfig.training?.epochs ?? 3;
22
+ const lr = trainConfig.training?.learning_rate ?? 2e-4;
23
+ const batchSize = trainConfig.training?.batch_size ?? 4;
24
+ const gradAccum = trainConfig.training?.gradient_accumulation_steps ?? 4;
25
+ const maxSeqLen = trainConfig.training?.max_seq_len ?? 4096;
26
+ const seed = trainConfig.training?.seed ?? 42;
27
+ const loraR = trainConfig.lora?.rank ?? 16;
28
+ const loraAlpha = trainConfig.lora?.alpha ?? 32;
29
+ const loraDropout = trainConfig.lora?.dropout ?? 0.05;
30
+ const targetModules = JSON.stringify(trainConfig.lora?.target_modules ?? ["q_proj", "k_proj", "v_proj", "o_proj"]);
31
+
32
+ return `#!/usr/bin/env python3
33
+ """Unsloth QLoRA fine-tune — generated by \`infernet train run\`."""
34
+
35
+ import argparse
36
+ import json
37
+ import os
38
+ import sys
39
+ import time
40
+ from pathlib import Path
41
+
42
+
43
+ def main():
44
+ p = argparse.ArgumentParser()
45
+ p.add_argument("--data", required=True, help="JSONL dataset (chatml messages)")
46
+ p.add_argument("--output", required=True, help="Output dir for checkpoints + metrics")
47
+ p.add_argument("--base-model", default=${JSON.stringify(baseModel)})
48
+ args = p.parse_args()
49
+
50
+ output = Path(args.output)
51
+ output.mkdir(parents=True, exist_ok=True)
52
+ metrics_path = output / "metrics.jsonl"
53
+
54
+ # Imports kept here so a missing dep fails loudly with a clear hint
55
+ # instead of crashing at module-import time before logging anything.
56
+ try:
57
+ from unsloth import FastLanguageModel
58
+ from unsloth.chat_templates import standardize_sharegpt, get_chat_template
59
+ from datasets import load_dataset
60
+ from transformers import TrainingArguments
61
+ from trl import SFTTrainer
62
+ import torch
63
+ except ImportError as e:
64
+ print(json.dumps({"error": f"missing dependency: {e}. Install with: pip install unsloth datasets trl"}), flush=True)
65
+ sys.exit(2)
66
+
67
+ print(json.dumps({"event": "loading_model", "base_model": args.base_model}), flush=True)
68
+ model, tokenizer = FastLanguageModel.from_pretrained(
69
+ model_name=args.base_model,
70
+ max_seq_length=${maxSeqLen},
71
+ dtype=None, # auto-detect bf16/fp16
72
+ load_in_4bit=True,
73
+ )
74
+
75
+ model = FastLanguageModel.get_peft_model(
76
+ model,
77
+ r=${loraR},
78
+ lora_alpha=${loraAlpha},
79
+ lora_dropout=${loraDropout},
80
+ target_modules=${targetModules},
81
+ bias="none",
82
+ use_gradient_checkpointing="unsloth",
83
+ random_state=${seed},
84
+ )
85
+
86
+ # ChatML template (works for Qwen, Llama 3 chat, most modern fine-tunes)
87
+ tokenizer = get_chat_template(tokenizer, chat_template="chatml")
88
+
89
+ print(json.dumps({"event": "loading_dataset", "path": args.data}), flush=True)
90
+ dataset = load_dataset("json", data_files=args.data, split="train")
91
+
92
+ def format_examples(examples):
93
+ texts = []
94
+ for msgs in examples["messages"]:
95
+ text = tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=False)
96
+ texts.append(text)
97
+ return {"text": texts}
98
+
99
+ dataset = dataset.map(format_examples, batched=True)
100
+ print(json.dumps({"event": "dataset_ready", "rows": len(dataset)}), flush=True)
101
+
102
+ training_args = TrainingArguments(
103
+ output_dir=str(output),
104
+ per_device_train_batch_size=${batchSize},
105
+ gradient_accumulation_steps=${gradAccum},
106
+ num_train_epochs=${epochs},
107
+ learning_rate=${lr},
108
+ warmup_steps=10,
109
+ logging_steps=5,
110
+ save_steps=200,
111
+ save_total_limit=2,
112
+ seed=${seed},
113
+ bf16=torch.cuda.is_bf16_supported(),
114
+ fp16=not torch.cuda.is_bf16_supported(),
115
+ optim="adamw_8bit",
116
+ report_to="none",
117
+ )
118
+
119
+ trainer = SFTTrainer(
120
+ model=model,
121
+ tokenizer=tokenizer,
122
+ train_dataset=dataset,
123
+ dataset_text_field="text",
124
+ max_seq_length=${maxSeqLen},
125
+ args=training_args,
126
+ packing=False,
127
+ )
128
+
129
+ # Stream logs as JSONL into metrics.jsonl
130
+ metrics_f = open(metrics_path, "a")
131
+ last_step = -1
132
+ t0 = time.time()
133
+
134
+ class JsonLogger:
135
+ def on_log(self, args, state, control, logs=None, **kw):
136
+ nonlocal last_step
137
+ if not logs or state.global_step == last_step:
138
+ return
139
+ last_step = state.global_step
140
+ row = {
141
+ "step": state.global_step,
142
+ "loss": logs.get("loss"),
143
+ "lr": logs.get("learning_rate"),
144
+ "epoch": logs.get("epoch"),
145
+ "elapsed_s": round(time.time() - t0, 2),
146
+ }
147
+ metrics_f.write(json.dumps(row) + "\\n")
148
+ metrics_f.flush()
149
+ print(json.dumps({"event": "step", **row}), flush=True)
150
+
151
+ trainer.add_callback(JsonLogger())
152
+
153
+ print(json.dumps({"event": "training_start", "epochs": ${epochs}}), flush=True)
154
+ result = trainer.train()
155
+ print(json.dumps({"event": "training_done", "loss": float(result.training_loss)}), flush=True)
156
+
157
+ final_dir = output / "checkpoint-final"
158
+ model.save_pretrained(str(final_dir))
159
+ tokenizer.save_pretrained(str(final_dir))
160
+ print(json.dumps({"event": "saved", "path": str(final_dir)}), flush=True)
161
+ metrics_f.close()
162
+
163
+
164
+ if __name__ == "__main__":
165
+ main()
166
+ `;
167
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infernetprotocol/cli",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
4
4
  "description": "GPU-node CLI / daemon for the Infernet Protocol — register a GPU server with a Supabase control plane and start earning.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -30,11 +30,11 @@
30
30
  "ora": "^9.4.0",
31
31
  "react": "^17.0.2",
32
32
  "react-blessed": "^0.7.2",
33
- "@infernetprotocol/auth": "0.1.32",
34
- "@infernetprotocol/deploy-providers": "0.1.32",
35
- "@infernetprotocol/config": "0.1.32",
36
- "@infernetprotocol/engine": "0.1.32",
37
- "@infernetprotocol/gpu": "0.1.32"
33
+ "@infernetprotocol/auth": "0.1.34",
34
+ "@infernetprotocol/deploy-providers": "0.1.34",
35
+ "@infernetprotocol/config": "0.1.34",
36
+ "@infernetprotocol/engine": "0.1.34",
37
+ "@infernetprotocol/gpu": "0.1.34"
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",