@infernetprotocol/cli 0.1.33 → 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.
- package/commands/start.js +72 -0
- package/commands/train.js +184 -9
- package/lib/training/p2p-coordinator.js +192 -0
- package/lib/training/unsloth-script.js +167 -0
- package/package.json +6 -6
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,9 +17,14 @@ 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";
|
|
22
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";
|
|
23
28
|
|
|
24
29
|
const HELP = `infernet train — fine-tune or train models on the P2P GPU network
|
|
25
30
|
|
|
@@ -324,22 +329,192 @@ async function cmdRun(args) {
|
|
|
324
329
|
|
|
325
330
|
const useLocal = forceLocal || workloadClass === "C1";
|
|
326
331
|
|
|
332
|
+
// P2P (federated LoRA) — workload_class C2/C3 or explicit --p2p flag.
|
|
327
333
|
if (!useLocal) {
|
|
328
|
-
|
|
329
|
-
process.stdout.write(`(P2P training submission not yet implemented — use --local for now)\n`);
|
|
330
|
-
await updateRunStatus(runDir, "pending_p2p");
|
|
331
|
-
return 0;
|
|
334
|
+
return runP2PFederated({ trainConfig, runDir, runId });
|
|
332
335
|
}
|
|
333
336
|
|
|
334
|
-
// Local
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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 });
|
|
338
341
|
}
|
|
339
|
-
|
|
340
342
|
return runMicrogptLocal({ trainConfig, runDir, runId });
|
|
341
343
|
}
|
|
342
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
|
+
|
|
343
518
|
async function runMicrogptLocal({ trainConfig, runDir, runId }) {
|
|
344
519
|
const steps = parseInt(trainConfig.training?.epochs ?? 3, 10) * 100;
|
|
345
520
|
const lr = parseFloat(trainConfig.training?.learning_rate ?? 3e-4);
|
|
@@ -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.
|
|
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.
|
|
34
|
-
"@infernetprotocol/deploy-providers": "0.1.
|
|
35
|
-
"@infernetprotocol/config": "0.1.
|
|
36
|
-
"@infernetprotocol/
|
|
37
|
-
"@infernetprotocol/
|
|
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",
|