@infernetprotocol/cli 0.1.34 → 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();
@@ -977,6 +1044,24 @@ async function runDaemon(args, ctx) {
977
1044
  }, commandPollMs);
978
1045
  if (typeof commandTimer.unref === 'function') commandTimer.unref();
979
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
+
980
1065
  // Self-update: check npm registry every 5 minutes. If a newer version is
981
1066
  // available, pull the installer, re-generate any missing keys + re-register
982
1067
  // specs (idempotent), then re-exec so the new code is running immediately.
package/commands/train.js CHANGED
@@ -34,8 +34,14 @@ Usage:
34
34
  [--num <n>] (default: 20 results)
35
35
  [--domains <a,b,c>] (whitelist filter)
36
36
  infernet train init [--output <dir>] Scaffold infernet.train.yml
37
- infernet train run [--config <file>] Run training (local or P2P)
37
+ infernet train run [--config <file>] Run training (local | P2P | market)
38
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
39
45
  [--output <dir>] Where to write run artifacts
40
46
  infernet train status [<run-id>] Show run status
41
47
  infernet train list List recent runs
@@ -329,6 +335,13 @@ async function cmdRun(args) {
329
335
 
330
336
  const useLocal = forceLocal || workloadClass === "C1";
331
337
 
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 });
343
+ }
344
+
332
345
  // P2P (federated LoRA) — workload_class C2/C3 or explicit --p2p flag.
333
346
  if (!useLocal) {
334
347
  return runP2PFederated({ trainConfig, runDir, runId });
@@ -342,6 +355,80 @@ async function cmdRun(args) {
342
355
  return runMicrogptLocal({ trainConfig, runDir, runId });
343
356
  }
344
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
+
345
432
  async function runUnslothLocal({ trainConfig, runDir, runId }) {
346
433
  const dataset = trainConfig.input?.dataset;
347
434
  if (!dataset) {
@@ -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
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infernetprotocol/cli",
3
- "version": "0.1.34",
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.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"
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",