@infernetprotocol/cli 0.1.33 → 0.1.35

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 CHANGED
@@ -505,6 +505,73 @@ async function runDaemon(args, ctx) {
505
505
  }
506
506
  }
507
507
 
508
+ /**
509
+ * IPIP-0030: poll the open training market, claim a shard if any
510
+ * fit our hardware, run it via runTrainShard, report back. Single
511
+ * shard per poll cycle — keeps a node from monopolizing a job.
512
+ */
513
+ let trainingBusy = false;
514
+ async function pollTrainingMarket() {
515
+ if (node.role !== 'provider') return;
516
+ if (trainingBusy) return; // one at a time
517
+
518
+ let listing;
519
+ try {
520
+ listing = await client.listAvailableTrainingShards(3);
521
+ } catch (err) {
522
+ process.stderr.write(`training market poll error: ${err?.message ?? err}\n`);
523
+ return;
524
+ }
525
+ const shards = listing?.shards ?? [];
526
+ if (shards.length === 0) return;
527
+
528
+ // Race-claim the first one. If another node beats us, just exit;
529
+ // we'll retry on the next poll cycle.
530
+ let claimed = null;
531
+ for (const shard of shards) {
532
+ try {
533
+ const res = await client.claimTrainingShard(shard.shard_id);
534
+ claimed = res;
535
+ break;
536
+ } catch (err) {
537
+ if (err?.status === 409) continue; // someone else got it
538
+ process.stderr.write(`training claim error: ${err?.message ?? err}\n`);
539
+ }
540
+ }
541
+ if (!claimed) return;
542
+
543
+ trainingBusy = true;
544
+ const shardId = claimed.shard_id;
545
+ process.stdout.write(
546
+ `[training] claimed shard ${shardId} of job ${claimed.job_id} ` +
547
+ `(base ${claimed.base_model}, idx ${claimed.shard_index})\n`
548
+ );
549
+ const t0 = Date.now();
550
+ try {
551
+ // Reuse the same daemon-side handler used by node_commands.
552
+ const result = await runTrainShard({
553
+ shard_url: claimed.shard_url,
554
+ upload_url: claimed.upload_url,
555
+ base_model: claimed.base_model,
556
+ ...(claimed.config ?? {})
557
+ }, shardId);
558
+ await client.reportTrainingShard(shardId, {
559
+ status: 'completed',
560
+ adapter_url: result?.uploaded ? claimed.upload_url : null,
561
+ metrics: { duration_ms: Date.now() - t0 }
562
+ });
563
+ process.stdout.write(`[training] reported completed shard ${shardId}\n`);
564
+ } catch (err) {
565
+ const msg = err?.message ?? String(err);
566
+ process.stderr.write(`[training] shard ${shardId} failed: ${msg}\n`);
567
+ try {
568
+ await client.reportTrainingShard(shardId, { status: 'failed', error: msg });
569
+ } catch { /* swallow */ }
570
+ } finally {
571
+ trainingBusy = false;
572
+ }
573
+ }
574
+
508
575
  async function runNodeCommand(cmd) {
509
576
  const { id, command, args } = cmd;
510
577
  const t0 = new Date().toISOString();
@@ -520,6 +587,8 @@ async function runDaemon(args, ctx) {
520
587
  result = await ollamaPullWithProgress(modelName, id);
521
588
  } else if (command === 'model_remove') {
522
589
  result = await ollamaSpawn(['rm', String(args?.model)]);
590
+ } else if (command === 'train_shard') {
591
+ result = await runTrainShard(args, id);
523
592
  } else {
524
593
  throw new Error(`unknown command verb: ${command}`);
525
594
  }
@@ -637,6 +706,76 @@ async function runDaemon(args, ctx) {
637
706
  return { stdout: 'pulled via /api/pull', code: 0 };
638
707
  }
639
708
 
709
+ /**
710
+ * Daemon-side handler for the `train_shard` command verb (federated
711
+ * LoRA). Pulls a JSONL shard from a public URL, runs the same Unsloth
712
+ * script the operator's local mode uses, and returns a path to the
713
+ * resulting adapter so the coordinator can FedAvg it.
714
+ *
715
+ * EXPERIMENTAL: assumes Python + Unsloth + datasets + trl are
716
+ * installed on the node. Future: bundle a thin Python venv at setup
717
+ * time so this works out of the box. Adapter upload is currently
718
+ * "report local path" — for cross-machine training the operator needs
719
+ * to provide an args.upload_url that we POST the safetensors to.
720
+ */
721
+ async function runTrainShard(args, _commandId) {
722
+ const fs = await import('node:fs/promises');
723
+ const path = await import('node:path');
724
+ const { buildUnslothScript } = await import('../lib/training/unsloth-script.js');
725
+
726
+ if (!args?.shard_url) throw new Error('train_shard: args.shard_url required');
727
+ if (!args?.base_model) throw new Error('train_shard: args.base_model required');
728
+
729
+ const runId = args.run_id ?? `shard-${Date.now()}`;
730
+ const workDir = path.join(process.env.HOME ?? '/tmp', '.infernet', 'training', runId);
731
+ await fs.mkdir(workDir, { recursive: true });
732
+
733
+ // Download the shard
734
+ const shardPath = path.join(workDir, 'shard.jsonl');
735
+ const res = await fetch(args.shard_url);
736
+ if (!res.ok) throw new Error(`fetch shard: HTTP ${res.status}`);
737
+ const text = await res.text();
738
+ await fs.writeFile(shardPath, text);
739
+
740
+ // Build + run the Unsloth script
741
+ const trainConfig = {
742
+ base_model: args.base_model,
743
+ training: args.training ?? {},
744
+ lora: args.lora ?? {},
745
+ input: { dataset: shardPath }
746
+ };
747
+ const scriptPath = path.join(workDir, 'unsloth_runner.py');
748
+ await fs.writeFile(scriptPath, buildUnslothScript({ trainConfig }));
749
+
750
+ await new Promise((resolve, reject) => {
751
+ const child = spawn('python3', [scriptPath, '--data', shardPath, '--output', workDir], {
752
+ stdio: 'inherit'
753
+ });
754
+ child.on('exit', (code) => code === 0 ? resolve() : reject(new Error(`unsloth_runner exited ${code}`)));
755
+ child.on('error', reject);
756
+ });
757
+
758
+ const adapterDir = path.join(workDir, 'checkpoint-final');
759
+
760
+ // Optional: upload adapter back to the operator-provided URL
761
+ if (args.upload_url) {
762
+ const adapterFile = path.join(adapterDir, 'adapter_model.safetensors');
763
+ const buf = await fs.readFile(adapterFile);
764
+ const up = await fetch(args.upload_url, {
765
+ method: 'PUT',
766
+ body: buf,
767
+ headers: { 'Content-Type': 'application/octet-stream' }
768
+ });
769
+ if (!up.ok) throw new Error(`upload adapter: HTTP ${up.status}`);
770
+ }
771
+
772
+ return {
773
+ adapter_path: adapterDir,
774
+ uploaded: !!args.upload_url,
775
+ shard_url: args.shard_url
776
+ };
777
+ }
778
+
640
779
  function snapshot() {
641
780
  return {
642
781
  pid: process.pid,
@@ -905,6 +1044,24 @@ async function runDaemon(args, ctx) {
905
1044
  }, commandPollMs);
906
1045
  if (typeof commandTimer.unref === 'function') commandTimer.unref();
907
1046
 
1047
+ // Open-market training (IPIP-0030). Daemons that opted in via
1048
+ // `infernet config set engine.acceptTraining true` (or env var
1049
+ // INFERNET_ACCEPT_TRAINING=1) poll for available shards and run them
1050
+ // for pay. Every ~60s — training shards are rare + slow, so polling
1051
+ // hot wastes RPS.
1052
+ const acceptTraining = process.env.INFERNET_ACCEPT_TRAINING === '1'
1053
+ || config?.engine?.acceptTraining === true;
1054
+ if (acceptTraining) {
1055
+ process.stdout.write('[training] open-market shard claiming enabled\n');
1056
+ const trainingPollMs = 60_000;
1057
+ const trainingTimer = setInterval(() => {
1058
+ pollTrainingMarket().catch((err) =>
1059
+ process.stderr.write(`training poll threw: ${err?.message ?? err}\n`)
1060
+ );
1061
+ }, trainingPollMs);
1062
+ if (typeof trainingTimer.unref === 'function') trainingTimer.unref();
1063
+ }
1064
+
908
1065
  // Self-update: check npm registry every 5 minutes. If a newer version is
909
1066
  // available, pull the installer, re-generate any missing keys + re-register
910
1067
  // specs (idempotent), then re-exec so the new code is running immediately.
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
 
@@ -29,8 +34,14 @@ Usage:
29
34
  [--num <n>] (default: 20 results)
30
35
  [--domains <a,b,c>] (whitelist filter)
31
36
  infernet train init [--output <dir>] Scaffold infernet.train.yml
32
- infernet train run [--config <file>] Run training (local or P2P)
37
+ infernet train run [--config <file>] Run training (local | P2P | market)
33
38
  [--local] Force local execution
39
+ [--open-market] Post to the open network market — any
40
+ opted-in operator can claim shards
41
+ for pay (--budget <usd>, --max-nodes)
42
+ [--shard-base-url <url>] Public prefix where daemons fetch shards
43
+ [--budget <usd>] Total budget; per-shard = budget / max-nodes
44
+ [--max-nodes <n>] Number of shards to split into
34
45
  [--output <dir>] Where to write run artifacts
35
46
  infernet train status [<run-id>] Show run status
36
47
  infernet train list List recent runs
@@ -324,22 +335,273 @@ async function cmdRun(args) {
324
335
 
325
336
  const useLocal = forceLocal || workloadClass === "C1";
326
337
 
327
- if (!useLocal) {
328
- process.stdout.write(`Submitting to P2P network (workload class ${workloadClass})...\n`);
329
- process.stdout.write(`(P2P training submission not yet implemented — use --local for now)\n`);
330
- await updateRunStatus(runDir, "pending_p2p");
331
- return 0;
338
+ // Open-market: distribute across the entire network of opted-in
339
+ // operators (--open-market). The submitter pays per shard via the
340
+ // existing payment rails.
341
+ if (args.has("open-market")) {
342
+ return runOpenMarket({ trainConfig, runDir, runId, args });
332
343
  }
333
344
 
334
- // Local execution
335
- if (runtime !== "microgpt") {
336
- process.stdout.write(`Note: only microgpt runtime is supported in local mode (v0.1).\n`);
337
- process.stdout.write(`Falling back to microgpt.\n`);
345
+ // P2P (federated LoRA) — workload_class C2/C3 or explicit --p2p flag.
346
+ if (!useLocal) {
347
+ return runP2PFederated({ trainConfig, runDir, runId });
338
348
  }
339
349
 
350
+ // Local — pick a real runtime (Unsloth) for QLoRA fine-tuning of a
351
+ // base HF model. microgpt remains for the from-scratch demo path.
352
+ if (runtime === "unsloth" || runtime === "transformers" || runtime === "trl") {
353
+ return runUnslothLocal({ trainConfig, runDir, runId });
354
+ }
340
355
  return runMicrogptLocal({ trainConfig, runDir, runId });
341
356
  }
342
357
 
358
+ /**
359
+ * IPIP-0030: post a training job to the open market. Any operator on
360
+ * the network running with --accept-training claims shards and reports
361
+ * back; we poll until all shards complete, then FedAvg the adapters.
362
+ */
363
+ async function runOpenMarket({ trainConfig, runDir, runId, args }) {
364
+ const cfg = await loadConfig().catch(() => ({}));
365
+ const controlPlaneUrl = cfg?.controlPlane?.url ?? process.env.NEXT_PUBLIC_APP_URL ?? "https://infernetprotocol.com";
366
+ const sessionToken = cfg?.session?.token ?? process.env.INFERNET_SESSION_TOKEN;
367
+ if (!sessionToken) {
368
+ process.stderr.write(`error: open-market training needs a logged-in session — run \`infernet login\` first\n`);
369
+ return 1;
370
+ }
371
+ const datasetBaseUrl = args.get("shard-base-url") ?? process.env.INFERNET_SHARD_BASE_URL;
372
+ const uploadBaseUrl = args.get("upload-base-url") ?? process.env.INFERNET_ADAPTER_UPLOAD_URL;
373
+ if (!datasetBaseUrl) {
374
+ process.stderr.write(
375
+ `error: --shard-base-url or INFERNET_SHARD_BASE_URL is required.\n` +
376
+ `Host shards somewhere reachable by daemons (S3 / HF / ngrok / etc).\n`
377
+ );
378
+ return 1;
379
+ }
380
+ const numShards = Number.parseInt(args.get("max-nodes") ?? args.get("num-shards") ?? "5", 10);
381
+ const budget = Number.parseFloat(args.get("budget") ?? "0");
382
+ const pricePerShard = numShards > 0 ? budget / numShards : 0;
383
+
384
+ const datasetPath = trainConfig.input?.dataset;
385
+ if (!datasetPath) {
386
+ process.stderr.write(`error: input.dataset is required\n`);
387
+ return 1;
388
+ }
389
+
390
+ // Split the local JSONL into N shards in runDir/shards/
391
+ const { splitJsonl } = await import("../lib/training/p2p-coordinator.js");
392
+ const shardsDir = path.join(runDir, "shards");
393
+ process.stdout.write(`Splitting ${datasetPath} into ${numShards} shard(s)…\n`);
394
+ const shards = await splitJsonl({ inputPath: datasetPath, outDir: shardsDir, numShards });
395
+ process.stdout.write(` shards written to ${shardsDir} (operator must upload to ${datasetBaseUrl})\n`);
396
+
397
+ // Submit the job
398
+ process.stdout.write(`\nPosting job to the open market…\n`);
399
+ const res = await fetch(`${controlPlaneUrl}/api/v1/training/jobs`, {
400
+ method: "POST",
401
+ headers: {
402
+ Authorization: `Bearer ${sessionToken}`,
403
+ "Content-Type": "application/json"
404
+ },
405
+ body: JSON.stringify({
406
+ name: trainConfig.name ?? runId,
407
+ base_model: trainConfig.base_model,
408
+ config: trainConfig,
409
+ dataset_base_url: datasetBaseUrl,
410
+ upload_base_url: uploadBaseUrl,
411
+ num_shards: shards.length,
412
+ min_vram_gb: trainConfig.resources?.min_vram_gb ?? 16,
413
+ price_per_shard_usd: pricePerShard
414
+ })
415
+ });
416
+ if (!res.ok) {
417
+ const body = await res.text();
418
+ process.stderr.write(`submit failed: HTTP ${res.status} ${body.slice(0, 300)}\n`);
419
+ return 1;
420
+ }
421
+ const { data } = await res.json();
422
+ process.stdout.write(`✓ job ${data.job.id} posted (${data.shards.length} shards · $${pricePerShard.toFixed(4)}/shard · $${budget.toFixed(2)} budget)\n`);
423
+ process.stdout.write(`\nMonitor at: ${controlPlaneUrl}/training/${data.job.id}\n`);
424
+ process.stdout.write(`Poll status: curl ${controlPlaneUrl}/api/v1/training/jobs/${data.job.id}\n\n`);
425
+ process.stdout.write(`Tip: opted-in operators see your shards next poll cycle (~60s). When all\n`);
426
+ process.stdout.write(` report back, FedAvg the adapter URLs from the job status.\n`);
427
+
428
+ await fs.writeFile(path.join(runDir, "open-market-job.json"), JSON.stringify(data, null, 2));
429
+ return 0;
430
+ }
431
+
432
+ async function runUnslothLocal({ trainConfig, runDir, runId }) {
433
+ const dataset = trainConfig.input?.dataset;
434
+ if (!dataset) {
435
+ process.stderr.write(`error: input.dataset is required for unsloth runtime\n`);
436
+ return 1;
437
+ }
438
+ if (!fsSync.existsSync(dataset)) {
439
+ process.stderr.write(`error: dataset not found: ${dataset}\n`);
440
+ return 1;
441
+ }
442
+
443
+ const scriptPath = path.join(runDir, "unsloth_runner.py");
444
+ await fs.writeFile(scriptPath, buildUnslothScript({ trainConfig }));
445
+
446
+ const python = await findPython();
447
+ if (!python) {
448
+ process.stderr.write(`error: python3 not found\n`);
449
+ process.stderr.write(`install with: sudo apt install python3 python3-pip && pip install unsloth datasets trl\n`);
450
+ return 1;
451
+ }
452
+
453
+ process.stdout.write(`Starting Unsloth QLoRA training (runtime=unsloth, base=${trainConfig.base_model})...\n\n`);
454
+ const logStream = createWriteStream(path.join(runDir, "run.log"), { flags: "a" });
455
+
456
+ return new Promise((resolve) => {
457
+ const child = spawn(python, [scriptPath, "--data", dataset, "--output", runDir], {
458
+ stdio: ["ignore", "pipe", "pipe"]
459
+ });
460
+ child.stdout.on("data", (chunk) => {
461
+ const lines = chunk.toString().split("\n").filter(Boolean);
462
+ for (const line of lines) {
463
+ logStream.write(line + "\n");
464
+ try {
465
+ const event = JSON.parse(line);
466
+ if (event.event === "step") {
467
+ process.stdout.write(` step ${event.step} loss=${event.loss?.toFixed?.(4) ?? "—"} lr=${event.lr?.toExponential?.(2) ?? "—"}\n`);
468
+ } else if (event.event) {
469
+ process.stdout.write(` ${event.event}${event.path ? ` → ${event.path}` : ""}${event.rows ? ` (${event.rows} rows)` : ""}\n`);
470
+ } else if (event.error) {
471
+ process.stderr.write(` ✗ ${event.error}\n`);
472
+ }
473
+ } catch {
474
+ process.stdout.write(` ${line}\n`);
475
+ }
476
+ }
477
+ });
478
+ child.stderr.on("data", (chunk) => {
479
+ const txt = chunk.toString();
480
+ logStream.write(txt);
481
+ process.stderr.write(txt);
482
+ });
483
+ child.on("exit", async (code) => {
484
+ logStream.end();
485
+ await updateRunStatus(runDir, code === 0 ? "completed" : "failed");
486
+ if (code === 0) {
487
+ process.stdout.write(`\n✓ Training complete. Model saved to ${path.join(runDir, "checkpoint-final")}\n`);
488
+ process.stdout.write(`Next: infernet publish ${path.join(runDir, "checkpoint-final")} --hf <org>/<name> --ollama <user>/<name>\n`);
489
+ }
490
+ resolve(code === 0 ? 0 : 1);
491
+ });
492
+ child.on("error", (err) => {
493
+ process.stderr.write(`spawn error: ${err?.message ?? err}\n`);
494
+ resolve(1);
495
+ });
496
+ });
497
+ }
498
+
499
+ /**
500
+ * Federated LoRA across all owner-controlled nodes that meet the
501
+ * resources.min_vram_gb threshold. Splits the dataset into N shards,
502
+ * queues a `train_shard` command per node, awaits completion, then
503
+ * runs FedAvg over the resulting adapters.
504
+ *
505
+ * EXPERIMENTAL — assumes the operator has a way to host shard files
506
+ * reachable by the daemons (S3, HF dataset, ngrok, etc) and that nodes
507
+ * have a working Unsloth Python install.
508
+ */
509
+ async function runP2PFederated({ trainConfig, runDir, runId }) {
510
+ const cfg = await loadConfig().catch(() => ({}));
511
+ const controlPlaneUrl = cfg?.controlPlane?.url ?? process.env.NEXT_PUBLIC_APP_URL ?? "https://infernetprotocol.com";
512
+ const sessionToken = cfg?.session?.token ?? process.env.INFERNET_SESSION_TOKEN;
513
+ if (!sessionToken) {
514
+ process.stderr.write(`error: P2P training needs a logged-in session — run \`infernet login\` first\n`);
515
+ return 1;
516
+ }
517
+
518
+ const minVramGb = trainConfig.resources?.min_vram_gb ?? 12;
519
+ process.stdout.write(`P2P federated LoRA — discovering nodes (≥ ${minVramGb} GB VRAM)…\n`);
520
+ const nodes = await discoverNodes({ controlPlaneUrl, sessionToken, minVramGb });
521
+ if (nodes.length === 0) {
522
+ process.stderr.write(`No online nodes meet min_vram_gb=${minVramGb}.\n`);
523
+ return 1;
524
+ }
525
+ process.stdout.write(` found ${nodes.length} eligible node(s):\n`);
526
+ for (const n of nodes) {
527
+ process.stdout.write(` ${n.name ?? n.public_key.slice(0, 16)} ${n.specs?.gpus?.[0]?.vram_tier ?? "?"}\n`);
528
+ }
529
+
530
+ // Split dataset
531
+ const datasetPath = trainConfig.input?.dataset;
532
+ if (!datasetPath) {
533
+ process.stderr.write(`error: input.dataset is required\n`);
534
+ return 1;
535
+ }
536
+ const shardsDir = path.join(runDir, "shards");
537
+ process.stdout.write(`\nSplitting ${datasetPath} into ${nodes.length} shard(s)…\n`);
538
+ const shards = await splitJsonl({ inputPath: datasetPath, outDir: shardsDir, numShards: nodes.length });
539
+
540
+ if (!process.env.INFERNET_SHARD_BASE_URL) {
541
+ process.stdout.write(
542
+ `\n⚠ P2P training is in MVP — set INFERNET_SHARD_BASE_URL to a public URL\n` +
543
+ ` the daemons can fetch from. Each shard's file is at:\n` +
544
+ shards.map((s) => ` ${s.path} (${s.lines} examples)`).join("\n") + "\n" +
545
+ ` Example: \`aws s3 sync ${shardsDir} s3://your-bucket/runs/${runId}/\`\n` +
546
+ ` then: \`export INFERNET_SHARD_BASE_URL=https://your-bucket.s3.amazonaws.com/runs/${runId}\`\n` +
547
+ ` and re-run \`infernet train run\`.\n`
548
+ );
549
+ return 1;
550
+ }
551
+ const shardBase = process.env.INFERNET_SHARD_BASE_URL.replace(/\/$/, "");
552
+
553
+ // Queue train_shard commands
554
+ process.stdout.write(`\nQueueing train_shard commands…\n`);
555
+ const inFlight = [];
556
+ for (let i = 0; i < nodes.length && i < shards.length; i += 1) {
557
+ const node = nodes[i];
558
+ const shard = shards[i];
559
+ const cmd = await queueTrainShard({
560
+ controlPlaneUrl, sessionToken, pubkey: node.public_key,
561
+ args: {
562
+ run_id: runId,
563
+ base_model: trainConfig.base_model,
564
+ shard_url: `${shardBase}/${path.basename(shard.path)}`,
565
+ training: trainConfig.training,
566
+ lora: trainConfig.lora,
567
+ upload_url: process.env.INFERNET_ADAPTER_UPLOAD_URL ?? null
568
+ }
569
+ });
570
+ process.stdout.write(` → node ${node.name ?? node.public_key.slice(0, 16)} command ${cmd.id}\n`);
571
+ inFlight.push({ node, shard, commandId: cmd.id });
572
+ }
573
+
574
+ // Await each
575
+ process.stdout.write(`\nWaiting for shards to finish (this can take hours)…\n`);
576
+ const adapterDirs = [];
577
+ for (const w of inFlight) {
578
+ try {
579
+ const res = await awaitCommand({
580
+ controlPlaneUrl, sessionToken, pubkey: w.node.public_key, commandId: w.commandId
581
+ });
582
+ if (res.status === "completed" && res.result?.adapter_path) {
583
+ process.stdout.write(` ✓ ${w.node.name ?? w.node.public_key.slice(0, 16)}: ${res.result.adapter_path}\n`);
584
+ adapterDirs.push(res.result.adapter_path);
585
+ } else {
586
+ process.stderr.write(` ✗ ${w.node.name ?? w.node.public_key.slice(0, 16)}: ${res.error ?? res.status}\n`);
587
+ }
588
+ } catch (err) {
589
+ process.stderr.write(` ✗ ${w.node.name ?? "?"}: ${err.message}\n`);
590
+ }
591
+ }
592
+
593
+ if (adapterDirs.length === 0) {
594
+ process.stderr.write(`\nNo successful shards — aborting aggregation.\n`);
595
+ return 1;
596
+ }
597
+
598
+ process.stdout.write(`\nRunning FedAvg over ${adapterDirs.length} adapter(s)…\n`);
599
+ await fedAvg({ adapterDirs, outDir: runDir });
600
+ await updateRunStatus(runDir, "completed");
601
+ process.stdout.write(`\n✓ Federated training complete. Final model: ${path.join(runDir, "checkpoint-final")}\n`);
602
+ return 0;
603
+ }
604
+
343
605
  async function runMicrogptLocal({ trainConfig, runDir, runId }) {
344
606
  const steps = parseInt(trainConfig.training?.epochs ?? 3, 10) * 100;
345
607
  const lr = parseFloat(trainConfig.training?.learning_rate ?? 3e-4);
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Petals engine adapter — DISTRIBUTED inference across the volunteer
3
+ * swarm (IPIP-0031, follow-up PR).
4
+ *
5
+ * Petals (https://github.com/bigscience-workshop/petals) splits a
6
+ * Llama/Mixtral-style transformer by layer across DHT-discovered nodes.
7
+ * Each node holds a few transformer blocks; activations flow through.
8
+ * Designed for residential bandwidth — exactly the Infernet model.
9
+ *
10
+ * STATUS: scaffolding only. The flow:
11
+ *
12
+ * serve() — operator runs `infernet inference serve --backend petals
13
+ * --model meta-llama/Llama-3.1-70B`. We:
14
+ * 1. spawn `python -m petals.cli.run_server <model>`
15
+ * 2. register the operator as a Petals server in our
16
+ * own registry: row in providers.specs.petals_models
17
+ * 3. heartbeat that the server is alive
18
+ *
19
+ * client() — control plane's chat router, when input_spec.distributed
20
+ * is true, runs a Petals CLIENT (Python subprocess or
21
+ * a thin TS wrapper) that fans out across the swarm.
22
+ * Tokens stream back to the SSE handler exactly like
23
+ * Ollama's stream.
24
+ *
25
+ * The SSE handler in apps/web/app/api/chat/stream/[jobId]/route.js
26
+ * already has the typed events plumbing — when distributed===true,
27
+ * it would call into routePetalsRequest below instead of subscribing
28
+ * to job_events.
29
+ *
30
+ * To-do for the follow-up PR:
31
+ * - bundle a thin Python venv with Petals at infernet setup time
32
+ * - add `infernet inference serve --backend petals` as a top-level command
33
+ * - register petals_models on heartbeat so the dashboard can show
34
+ * "this model is served by 8 nodes via Petals · ~12 tok/s" badges
35
+ * - wire the SSE handler's distributed branch through routePetalsRequest
36
+ * - per-token receipts to operators contributing layers (CPR / IPIP-0007)
37
+ */
38
+
39
+ import { spawn } from "node:child_process";
40
+
41
+ const DEFAULT_DHT_PREFIX = "/petals/v3-public";
42
+
43
+ /**
44
+ * Start a Petals server contributing this node's VRAM to the swarm.
45
+ * Returns a child handle the daemon can monitor + restart.
46
+ */
47
+ export function startPetalsServer({ model, numBlocks, port = 31330, prefix = DEFAULT_DHT_PREFIX }) {
48
+ const args = ["-m", "petals.cli.run_server", model];
49
+ if (numBlocks) args.push("--num_blocks", String(numBlocks));
50
+ if (port) args.push("--port", String(port));
51
+ if (prefix) args.push("--initial_peers", prefix);
52
+ const child = spawn("python3", args, { stdio: ["ignore", "pipe", "pipe"] });
53
+ return child;
54
+ }
55
+
56
+ /**
57
+ * Run a single inference through a Petals client and stream tokens.
58
+ * Returns an async iterable of { type: 'token' | 'done' | 'error', data }.
59
+ *
60
+ * Currently a stub — wires up the subprocess but doesn't translate the
61
+ * Python output into typed events. Follow-up PR: pip install
62
+ * `petals` in our control-plane container and call from a Python
63
+ * worker, OR use a JS-native bridge once one exists.
64
+ */
65
+ export async function* routePetalsRequest({ model, messages, maxTokens = 256, temperature = 0.7 }) {
66
+ yield {
67
+ type: "error",
68
+ data: {
69
+ message:
70
+ `Distributed inference via Petals is in scaffolding (IPIP-0031). ` +
71
+ `For now, this request needs to fall back to a single-node provider. ` +
72
+ `Track progress at https://github.com/infernetprotocol/infernet-protocol/issues`
73
+ }
74
+ };
75
+ }
@@ -131,6 +131,26 @@ export function createNodeClient({ url, publicKey, privateKey, role, timeoutMs =
131
131
  );
132
132
  },
133
133
 
134
+ // ---- Open-market training (IPIP-0030) ----
135
+
136
+ listAvailableTrainingShards(limit = 5) {
137
+ return signedFetch('/api/v1/training/shards/available', { limit });
138
+ },
139
+
140
+ claimTrainingShard(shardId) {
141
+ return signedFetch(
142
+ `/api/v1/training/shards/${encodeURIComponent(shardId)}/claim`,
143
+ {}
144
+ );
145
+ },
146
+
147
+ reportTrainingShard(shardId, payload) {
148
+ return signedFetch(
149
+ `/api/v1/training/shards/${encodeURIComponent(shardId)}/report`,
150
+ payload
151
+ );
152
+ },
153
+
134
154
  listPayouts() {
135
155
  return signedFetch('/api/v1/node/payouts/list', {});
136
156
  },
@@ -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.33",
3
+ "version": "0.1.35",
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.33",
34
- "@infernetprotocol/deploy-providers": "0.1.33",
35
- "@infernetprotocol/config": "0.1.33",
36
- "@infernetprotocol/gpu": "0.1.33",
37
- "@infernetprotocol/engine": "0.1.33"
33
+ "@infernetprotocol/auth": "0.1.35",
34
+ "@infernetprotocol/engine": "0.1.35",
35
+ "@infernetprotocol/deploy-providers": "0.1.35",
36
+ "@infernetprotocol/config": "0.1.35",
37
+ "@infernetprotocol/gpu": "0.1.35"
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",