@infernetprotocol/gpu 0.1.22

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/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2025 Infernet Protocol
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@infernetprotocol/gpu",
3
+ "version": "0.1.22",
4
+ "description": "Local hardware detection for the Infernet Protocol — GPUs (NVIDIA, AMD, Apple Silicon) via native tooling, plus CPUs / RAM / arch via node:os. Pure-JS for the CPU side, vendor-CLI shell-out for GPUs.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./src/index.js",
8
+ "exports": {
9
+ ".": "./src/index.js",
10
+ "./detect": "./src/detect.js",
11
+ "./cpu": "./src/cpu.js"
12
+ },
13
+ "files": [
14
+ "src/**/*.js",
15
+ "README.md"
16
+ ],
17
+ "engines": {
18
+ "node": ">=18"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/profullstack/infernet-protocol.git",
23
+ "directory": "packages/gpu"
24
+ },
25
+ "homepage": "https://github.com/profullstack/infernet-protocol#readme"
26
+ }
package/src/cpu.js ADDED
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Local CPU + system inventory.
3
+ *
4
+ * Sister of detect.js — that one shells out for GPU info, this one
5
+ * uses `node:os` for CPU + memory + platform. Pure-JS, no shell-out,
6
+ * no privileges required.
7
+ *
8
+ * Keeps the privacy-minimization story intact: detection happens
9
+ * client-side, the operator decides what to surface in a heartbeat /
10
+ * register payload (the server-side sanitizer in apps/web drops
11
+ * anything outside `{ vendor, vram_tier, model }` per GPU and zero
12
+ * CPU info, per IPIP-0001).
13
+ */
14
+
15
+ import os from "node:os";
16
+
17
+ /**
18
+ * Return one entry per logical CPU group, collapsed by model.
19
+ *
20
+ * Most boxes have a single homogeneous CPU package, so the typical
21
+ * result is a single-element array. Heterogeneous machines (Apple
22
+ * Silicon with P+E cores; some servers with mixed sockets) get one
23
+ * entry per distinct model string.
24
+ */
25
+ export function detectCpus() {
26
+ const cpus = os.cpus();
27
+ if (!Array.isArray(cpus) || cpus.length === 0) {
28
+ return [{
29
+ vendor: null,
30
+ model: null,
31
+ arch: process.arch,
32
+ speed_mhz: null,
33
+ cores_total: 0,
34
+ cores_per_group: 0
35
+ }];
36
+ }
37
+
38
+ const groups = new Map();
39
+ for (const c of cpus) {
40
+ const key = `${c.model ?? "unknown"}|${c.speed ?? 0}`;
41
+ if (!groups.has(key)) {
42
+ groups.set(key, {
43
+ vendor: vendorOfModel(c.model),
44
+ model: c.model ?? "unknown",
45
+ arch: process.arch,
46
+ speed_mhz: c.speed ?? null,
47
+ cores_total: 0,
48
+ cores_per_group: 0
49
+ });
50
+ }
51
+ const g = groups.get(key);
52
+ g.cores_total += 1;
53
+ g.cores_per_group += 1;
54
+ }
55
+
56
+ return [...groups.values()];
57
+ }
58
+
59
+ /**
60
+ * Snapshot of host-level details that pair with the CPU list.
61
+ * Useful for `infernet gpu` / `infernet doctor` output but never
62
+ * sent in a heartbeat without operator opt-in.
63
+ */
64
+ export function detectHost() {
65
+ return {
66
+ platform: process.platform,
67
+ arch: process.arch,
68
+ node_version: process.versions.node,
69
+ hostname: os.hostname(),
70
+ cpu_count: os.cpus().length,
71
+ total_ram_mb: Math.round(os.totalmem() / (1024 * 1024)),
72
+ free_ram_mb: Math.round(os.freemem() / (1024 * 1024)),
73
+ load_avg: os.loadavg()
74
+ };
75
+ }
76
+
77
+ function vendorOfModel(model) {
78
+ if (!model || typeof model !== "string") return null;
79
+ const m = model.toLowerCase();
80
+ if (m.includes("intel")) return "intel";
81
+ if (m.includes("amd") || m.includes("ryzen") || m.includes("epyc") || m.includes("threadripper")) return "amd";
82
+ if (m.includes("apple") || /\bm[1-9]\b/.test(m)) return "apple";
83
+ if (m.includes("graviton") || m.includes("aws")) return "aws";
84
+ if (m.includes("ampere")) return "ampere";
85
+ return null;
86
+ }
87
+
88
+ /**
89
+ * One-line summary per CPU group, suitable for CLI output.
90
+ */
91
+ export function formatCpuLine(cpu) {
92
+ const ghz = cpu.speed_mhz ? `${(cpu.speed_mhz / 1000).toFixed(2)} GHz` : "?";
93
+ const vendor = cpu.vendor ? `[${cpu.vendor}]` : "";
94
+ return `${vendor} ${cpu.model} — ${cpu.cores_total} cores @ ${ghz}`.trim();
95
+ }
96
+
97
+ export const __testables__ = { vendorOfModel };
package/src/detect.js ADDED
@@ -0,0 +1,327 @@
1
+ /**
2
+ * GPU detection for Infernet Protocol.
3
+ *
4
+ * Returns a normalized array of GPU descriptors collected from whichever
5
+ * vendor tooling is present on the host. Runs are best-effort: any vendor
6
+ * whose detection tool is missing or fails is silently skipped.
7
+ *
8
+ * Normalized shape:
9
+ * {
10
+ * vendor: 'nvidia' | 'amd' | 'apple' | 'intel',
11
+ * index: number,
12
+ * model: string,
13
+ * vram_mb: number | null,
14
+ * vram_used_mb: number | null,
15
+ * utilization: number | null, // 0..100
16
+ * temperature_c: number | null,
17
+ * power_w: number | null,
18
+ * driver: string | null,
19
+ * cuda: string | null, // for NVIDIA only
20
+ * uuid: string | null
21
+ * }
22
+ *
23
+ * The CLI / daemon calls `detectGpus()` and stores the result in the
24
+ * `providers.specs.gpus` jsonb column, plus surfaces live utilization in
25
+ * the daemon IPC `stats` snapshot.
26
+ */
27
+
28
+ import { execFile } from 'node:child_process';
29
+ import { promisify } from 'node:util';
30
+
31
+ const pExecFile = promisify(execFile);
32
+
33
+ const EXEC_TIMEOUT_MS = 4000;
34
+
35
+ /**
36
+ * Module-level diagnostics from the most recent `detectGpus()` call —
37
+ * per-vendor "why we found nothing" reasons. Cleared on each detect
38
+ * run. Read via `lastDetectionDiagnostics()` from the CLI to surface
39
+ * actionable hints when no GPUs are detected.
40
+ */
41
+ let lastDiagnostics = {};
42
+
43
+ export function lastDetectionDiagnostics() {
44
+ return { ...lastDiagnostics };
45
+ }
46
+
47
+ async function tryExec(bin, args) {
48
+ try {
49
+ const { stdout } = await pExecFile(bin, args, { timeout: EXEC_TIMEOUT_MS });
50
+ return { stdout, error: null };
51
+ } catch (err) {
52
+ // ENOENT = binary not on PATH. Other codes = it ran but failed
53
+ // (driver issue, permission, timeout). Both are useful signals.
54
+ const code = err?.code ?? null;
55
+ const reason =
56
+ code === 'ENOENT' ? 'not installed (binary missing)' :
57
+ code === 'ETIMEDOUT' ? `timed out after ${EXEC_TIMEOUT_MS}ms` :
58
+ err?.stderr ? String(err.stderr).trim().split('\n')[0].slice(0, 200) :
59
+ err?.message ? String(err.message).slice(0, 200) :
60
+ 'unknown error';
61
+ return { stdout: null, error: { code, reason } };
62
+ }
63
+ }
64
+
65
+ // ---------------------------------------------------------------------------
66
+ // NVIDIA — nvidia-smi
67
+ // ---------------------------------------------------------------------------
68
+ async function detectNvidia() {
69
+ const query = [
70
+ 'index', 'name', 'memory.total', 'memory.used', 'utilization.gpu',
71
+ 'temperature.gpu', 'power.draw', 'driver_version', 'uuid'
72
+ ].join(',');
73
+ const result = await tryExec('nvidia-smi', [
74
+ `--query-gpu=${query}`,
75
+ '--format=csv,noheader,nounits'
76
+ ]);
77
+ if (!result.stdout) {
78
+ lastDiagnostics.nvidia = result.error
79
+ ? `nvidia-smi: ${result.error.reason}`
80
+ : 'nvidia-smi: empty output';
81
+ return [];
82
+ }
83
+ const out = result.stdout;
84
+
85
+ const cudaResult = await tryExec('nvidia-smi', ['--query', '--display=COMPUTE']);
86
+ let cuda = null;
87
+ if (cudaResult.stdout) {
88
+ const m = cudaResult.stdout.match(/CUDA Version\s*:\s*([\d.]+)/i);
89
+ if (m) cuda = m[1];
90
+ }
91
+
92
+ const gpus = [];
93
+ for (const raw of out.split('\n')) {
94
+ const line = raw.trim();
95
+ if (!line) continue;
96
+ const parts = line.split(',').map((s) => s.trim());
97
+ if (parts.length < 9) continue;
98
+ const [
99
+ index, name, memTotal, memUsed, util,
100
+ temp, power, driver, uuid
101
+ ] = parts;
102
+ gpus.push({
103
+ vendor: 'nvidia',
104
+ index: Number.parseInt(index, 10),
105
+ model: name,
106
+ vram_mb: toNum(memTotal),
107
+ vram_used_mb: toNum(memUsed),
108
+ utilization: toNum(util),
109
+ temperature_c: toNum(temp),
110
+ power_w: toNum(power),
111
+ driver: driver || null,
112
+ cuda,
113
+ uuid: uuid || null
114
+ });
115
+ }
116
+ return gpus;
117
+ }
118
+
119
+ // ---------------------------------------------------------------------------
120
+ // AMD — rocm-smi
121
+ // ---------------------------------------------------------------------------
122
+ async function detectAmd() {
123
+ const result = await tryExec('rocm-smi', ['--showproductname', '--showmeminfo', 'vram',
124
+ '--showuse', '--showtemp', '--showpower', '--showdriverversion', '--json']);
125
+ if (!result.stdout) {
126
+ lastDiagnostics.amd = result.error
127
+ ? `rocm-smi: ${result.error.reason}`
128
+ : 'rocm-smi: empty output';
129
+ return [];
130
+ }
131
+ let parsed;
132
+ try { parsed = JSON.parse(result.stdout); }
133
+ catch (err) {
134
+ lastDiagnostics.amd = `rocm-smi: parse error (${err?.message ?? err})`;
135
+ return [];
136
+ }
137
+
138
+ const gpus = [];
139
+ let i = 0;
140
+ for (const key of Object.keys(parsed)) {
141
+ if (!key.startsWith('card')) continue;
142
+ const card = parsed[key] ?? {};
143
+ const vramTotal = parseRocmBytes(card['VRAM Total Memory (B)']);
144
+ const vramUsed = parseRocmBytes(card['VRAM Total Used Memory (B)']);
145
+ gpus.push({
146
+ vendor: 'amd',
147
+ index: i,
148
+ model: card['Card series'] || card['Card model'] || 'AMD GPU',
149
+ vram_mb: vramTotal != null ? Math.round(vramTotal / (1024 * 1024)) : null,
150
+ vram_used_mb: vramUsed != null ? Math.round(vramUsed / (1024 * 1024)) : null,
151
+ utilization: toNum(card['GPU use (%)']),
152
+ temperature_c: toNum(card['Temperature (Sensor edge) (C)']),
153
+ power_w: toNum(card['Average Graphics Package Power (W)']),
154
+ driver: card['Driver version'] ?? null,
155
+ cuda: null,
156
+ uuid: card['Unique ID'] ?? null
157
+ });
158
+ i += 1;
159
+ }
160
+ return gpus;
161
+ }
162
+
163
+ function parseRocmBytes(v) {
164
+ if (v == null) return null;
165
+ const n = Number.parseInt(String(v).replace(/[^\d]/g, ''), 10);
166
+ return Number.isFinite(n) ? n : null;
167
+ }
168
+
169
+ // ---------------------------------------------------------------------------
170
+ // Apple Silicon / macOS — system_profiler
171
+ // ---------------------------------------------------------------------------
172
+ async function detectApple() {
173
+ if (process.platform !== 'darwin') {
174
+ lastDiagnostics.apple = `skipped (platform=${process.platform}, system_profiler is darwin-only)`;
175
+ return [];
176
+ }
177
+ const result = await tryExec('system_profiler', ['SPDisplaysDataType', '-json']);
178
+ if (!result.stdout) {
179
+ lastDiagnostics.apple = result.error
180
+ ? `system_profiler: ${result.error.reason}`
181
+ : 'system_profiler: empty output';
182
+ return [];
183
+ }
184
+ let parsed;
185
+ try { parsed = JSON.parse(result.stdout); }
186
+ catch (err) {
187
+ lastDiagnostics.apple = `system_profiler: parse error (${err?.message ?? err})`;
188
+ return [];
189
+ }
190
+ const items = parsed.SPDisplaysDataType ?? [];
191
+ const gpus = [];
192
+ items.forEach((item, i) => {
193
+ // Apple Silicon has unified memory — report total system RAM as a
194
+ // proxy when the card-level VRAM isn't reported.
195
+ const vramStr = item.spdisplays_vram ?? item._spdisplays_vram ?? null;
196
+ gpus.push({
197
+ vendor: 'apple',
198
+ index: i,
199
+ model: item.sppci_model ?? item._name ?? 'Apple GPU',
200
+ vram_mb: parseMacVram(vramStr),
201
+ vram_used_mb: null,
202
+ utilization: null,
203
+ temperature_c: null,
204
+ power_w: null,
205
+ driver: null,
206
+ cuda: null,
207
+ uuid: null
208
+ });
209
+ });
210
+ return gpus;
211
+ }
212
+
213
+ function parseMacVram(s) {
214
+ if (!s) return null;
215
+ const m = String(s).match(/([\d.]+)\s*(GB|MB)/i);
216
+ if (!m) return null;
217
+ const n = Number.parseFloat(m[1]);
218
+ if (!Number.isFinite(n)) return null;
219
+ return m[2].toUpperCase() === 'GB' ? Math.round(n * 1024) : Math.round(n);
220
+ }
221
+
222
+ // ---------------------------------------------------------------------------
223
+ // Top-level
224
+ // ---------------------------------------------------------------------------
225
+ /**
226
+ * Last-resort PCI fallback for Linux. If nvidia-smi / rocm-smi /
227
+ * system_profiler all came back empty but a discrete GPU is physically
228
+ * installed, `lspci` will still see it. We can't query VRAM / utilization
229
+ * via lspci, so the resulting descriptor is bare — but it's strictly
230
+ * better than reporting "CPU-only" on a box with a 4090 in it.
231
+ *
232
+ * Triggers ONLY when no other vendor returned anything, and only on Linux.
233
+ */
234
+ async function detectLspciFallback() {
235
+ if (process.platform !== 'linux') return [];
236
+ const result = await tryExec('lspci', ['-mm', '-d', '::0300']); // class 0x0300 = VGA
237
+ if (!result.stdout) {
238
+ // Try the broader `-nn` form so `3d controllers` (class 0302) also surface.
239
+ const fb = await tryExec('lspci', ['-nn']);
240
+ if (!fb.stdout) {
241
+ lastDiagnostics.lspci = result.error
242
+ ? `lspci: ${result.error.reason}`
243
+ : 'lspci: no VGA-class devices reported';
244
+ return [];
245
+ }
246
+ const lines = fb.stdout.split('\n').filter((l) => /VGA|3D controller|Display controller/i.test(l));
247
+ if (lines.length === 0) {
248
+ lastDiagnostics.lspci = 'lspci ran but no VGA/3D/Display devices found';
249
+ return [];
250
+ }
251
+ return lines.map((line, i) => parseLspciLine(line, i));
252
+ }
253
+ const lines = result.stdout.split('\n').filter((l) => l.trim().length > 0);
254
+ if (lines.length === 0) {
255
+ lastDiagnostics.lspci = 'lspci ran but reported no VGA devices';
256
+ return [];
257
+ }
258
+ return lines.map((line, i) => parseLspciLine(line, i));
259
+ }
260
+
261
+ function parseLspciLine(line, index) {
262
+ // -mm output: BDF "Vendor" "Device" "Subsys" ...
263
+ // -nn output: BDF Class: Vendor Device [vendor:dev]
264
+ let vendor = null;
265
+ let model = line.trim();
266
+ const lower = line.toLowerCase();
267
+ if (lower.includes('nvidia')) vendor = 'nvidia';
268
+ else if (lower.includes('amd') || lower.includes('ati ')) vendor = 'amd';
269
+ else if (lower.includes('intel')) vendor = 'intel';
270
+ // Best-effort model extraction: take whatever's between the first
271
+ // and second double-quote (works for -mm format).
272
+ const m = line.match(/"([^"]+)"\s+"([^"]+)"/);
273
+ if (m) model = `${m[1]} ${m[2]}`.trim();
274
+ return {
275
+ vendor: vendor ?? 'unknown',
276
+ index,
277
+ model: model.slice(0, 96),
278
+ vram_mb: null,
279
+ vram_used_mb: null,
280
+ utilization: null,
281
+ temperature_c: null,
282
+ power_w: null,
283
+ driver: null,
284
+ cuda: null,
285
+ uuid: null,
286
+ source: 'lspci' // marker so callers can surface "install vendor tooling for full info"
287
+ };
288
+ }
289
+
290
+ /**
291
+ * Collect GPU descriptors from all detected vendors. Always returns an
292
+ * array (empty if no GPU is physically present). Each call resets and
293
+ * populates module-level diagnostics — read via lastDetectionDiagnostics()
294
+ * to surface "we tried nvidia-smi but it errored with X" hints.
295
+ */
296
+ export async function detectGpus() {
297
+ lastDiagnostics = {};
298
+ const [nvidia, amd, apple] = await Promise.all([
299
+ detectNvidia(), detectAmd(), detectApple()
300
+ ]);
301
+ const found = [...nvidia, ...amd, ...apple];
302
+ if (found.length > 0) return found;
303
+ // Vendor tooling reported nothing — last-resort lspci fallback so a
304
+ // box with a discrete GPU but missing drivers/tooling at least
305
+ // surfaces the hardware presence.
306
+ return detectLspciFallback();
307
+ }
308
+
309
+ /**
310
+ * Summarize detected GPUs into one line per card.
311
+ */
312
+ export function formatGpuLine(gpu) {
313
+ const vram = gpu.vram_mb ? `${(gpu.vram_mb / 1024).toFixed(1)} GB VRAM` : 'VRAM unknown';
314
+ const extras = [];
315
+ if (gpu.utilization != null) extras.push(`${gpu.utilization}% util`);
316
+ if (gpu.temperature_c != null) extras.push(`${gpu.temperature_c}°C`);
317
+ if (gpu.power_w != null) extras.push(`${gpu.power_w}W`);
318
+ const suffix = extras.length ? ` (${extras.join(', ')})` : '';
319
+ const cuda = gpu.cuda ? ` cuda=${gpu.cuda}` : '';
320
+ return `[${gpu.vendor}:${gpu.index}] ${gpu.model} — ${vram}${cuda}${suffix}`;
321
+ }
322
+
323
+ function toNum(v) {
324
+ if (v == null) return null;
325
+ const n = Number.parseFloat(String(v).trim().replace(/[^\d.\-]/g, ''));
326
+ return Number.isFinite(n) ? n : null;
327
+ }
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @infernetprotocol/gpu — local hardware detection (GPU + CPU).
3
+ */
4
+ export * from "./detect.js";
5
+ export * from "./cpu.js";
6
+ export * from "./interconnect.js";
@@ -0,0 +1,395 @@
1
+ /**
2
+ * Interconnect detection — NVLink between GPUs, InfiniBand fabric.
3
+ *
4
+ * Both matter for distributed work:
5
+ * - NVLink lets Ollama / vLLM / DeepSpeed do tensor-parallel inference
6
+ * across multiple GPUs on the same host without going through PCIe.
7
+ * - InfiniBand (or RoCE) is what NCCL/RCCL uses to do all-reduce
8
+ * across hosts during distributed training. Without IB, multi-node
9
+ * training falls back to TCP and slows down by 10-100×.
10
+ *
11
+ * Returned shape (always defined, even when nothing is detected):
12
+ *
13
+ * {
14
+ * nvlink: {
15
+ * available: boolean,
16
+ * topology: 'all-to-all' | 'mesh' | 'pair' | 'none' | 'unknown',
17
+ * links: [{ from: number, to: number, kind: 'NV1'..'NV12' }],
18
+ * raw_topo?: string // nvidia-smi topo -m output for debugging
19
+ * },
20
+ * infiniband: {
21
+ * available: boolean,
22
+ * devices: [{ name, board_id, port, state, rate, link_layer }]
23
+ * },
24
+ * rdma_capable: boolean
25
+ * }
26
+ *
27
+ * Detection is best-effort — any command that's missing or fails is
28
+ * silently skipped. Same contract as detectGpus().
29
+ */
30
+
31
+ import { execFile } from 'node:child_process';
32
+ import { promisify } from 'node:util';
33
+ import { readdir, readFile } from 'node:fs/promises';
34
+ import { join } from 'node:path';
35
+
36
+ const pExecFile = promisify(execFile);
37
+ const EXEC_TIMEOUT_MS = 4000;
38
+
39
+ async function tryExec(bin, args) {
40
+ try {
41
+ const { stdout } = await pExecFile(bin, args, { timeout: EXEC_TIMEOUT_MS });
42
+ return stdout;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ // ---------------------------------------------------------------------------
49
+ // NVLink — `nvidia-smi topo -m`
50
+ //
51
+ // Output looks like:
52
+ // GPU0 GPU1 GPU2 GPU3 CPU Affinity
53
+ // GPU0 X NV2 NV2 NV2 0-15
54
+ // GPU1 NV2 X NV2 NV2 0-15
55
+ // ...
56
+ // where NV<n> means "n NVLink hops" and SYS / PHB / NODE mean PCIe.
57
+ // ---------------------------------------------------------------------------
58
+ async function detectNvlink() {
59
+ const empty = { available: false, topology: 'none', links: [] };
60
+
61
+ const out = await tryExec('nvidia-smi', ['topo', '-m']);
62
+ if (!out) return empty;
63
+
64
+ const links = parseNvidiaTopo(out);
65
+ if (links.length === 0) {
66
+ return { ...empty, raw_topo: out.length < 4096 ? out : undefined };
67
+ }
68
+
69
+ // Topology classification:
70
+ // all-to-all: every distinct GPU pair has at least one NVLink
71
+ // mesh: most pairs but not all
72
+ // pair: only certain pairs (typical 2-GPU NVLink bridge)
73
+ const gpuIds = new Set();
74
+ for (const l of links) {
75
+ gpuIds.add(l.from);
76
+ gpuIds.add(l.to);
77
+ }
78
+ const n = gpuIds.size;
79
+ const expectedAllToAll = n >= 2 ? (n * (n - 1)) / 2 : 0;
80
+ let topology = 'unknown';
81
+ if (n === 2) topology = 'pair';
82
+ else if (n > 2 && links.length >= expectedAllToAll) topology = 'all-to-all';
83
+ else if (n > 2) topology = 'mesh';
84
+
85
+ return { available: true, topology, links };
86
+ }
87
+
88
+ /**
89
+ * Parse `nvidia-smi topo -m` output, returning a deduped list of
90
+ * NVLink edges between GPU indices.
91
+ */
92
+ export function parseNvidiaTopo(text) {
93
+ const lines = text.split('\n').map((s) => s.trim()).filter(Boolean);
94
+ // First header line names the columns. Normalize.
95
+ const header = lines.shift();
96
+ if (!header) return [];
97
+ const cols = header.split(/\s+/).filter((c) => /^GPU\d+$/i.test(c));
98
+ if (cols.length === 0) return [];
99
+
100
+ const links = [];
101
+ const seen = new Set();
102
+ for (const line of lines) {
103
+ const m = line.match(/^GPU(\d+)\s+(.*)$/i);
104
+ if (!m) continue;
105
+ const rowIdx = Number.parseInt(m[1], 10);
106
+ const cells = m[2].split(/\s+/);
107
+ for (let i = 0; i < cols.length && i < cells.length; i++) {
108
+ const colName = cols[i];
109
+ const colIdx = Number.parseInt(colName.replace(/^GPU/i, ''), 10);
110
+ if (rowIdx === colIdx) continue;
111
+ const cell = cells[i];
112
+ // NV<n> entries indicate NVLink connections; X is self; SYS,
113
+ // NODE, PHB, PIX, etc. are PCIe paths.
114
+ if (!/^NV\d+$/i.test(cell)) continue;
115
+ const a = Math.min(rowIdx, colIdx);
116
+ const b = Math.max(rowIdx, colIdx);
117
+ const key = `${a}-${b}`;
118
+ if (seen.has(key)) continue;
119
+ seen.add(key);
120
+ links.push({ from: a, to: b, kind: cell.toUpperCase() });
121
+ }
122
+ }
123
+ return links;
124
+ }
125
+
126
+ // ---------------------------------------------------------------------------
127
+ // AMD xGMI / Infinity Fabric — `rocm-smi --showtopo` shows GPU↔GPU links.
128
+ //
129
+ // Output looks roughly like:
130
+ // ============================ ROCm System Management Interface ============================
131
+ // ================================ Weight between two GPUs ================================
132
+ // GPU0 GPU1 GPU2 GPU3
133
+ // GPU0 0 15 15 30
134
+ // GPU1 15 0 30 15
135
+ // ...
136
+ // where lower weight = closer / higher-bandwidth (xGMI). Anything > 30
137
+ // is typically PCIe.
138
+ // ---------------------------------------------------------------------------
139
+ async function detectXgmi() {
140
+ const empty = { available: false, topology: 'none', links: [] };
141
+
142
+ const out = await tryExec('rocm-smi', ['--showtopo']);
143
+ if (!out) return empty;
144
+
145
+ const links = parseRocmTopo(out);
146
+ if (links.length === 0) {
147
+ return { ...empty, raw_topo: out.length < 4096 ? out : undefined };
148
+ }
149
+
150
+ const gpuIds = new Set();
151
+ for (const l of links) {
152
+ gpuIds.add(l.from);
153
+ gpuIds.add(l.to);
154
+ }
155
+ const n = gpuIds.size;
156
+ const expectedAllToAll = n >= 2 ? (n * (n - 1)) / 2 : 0;
157
+ let topology = 'unknown';
158
+ if (n === 2) topology = 'pair';
159
+ else if (n > 2 && links.length >= expectedAllToAll) topology = 'all-to-all';
160
+ else if (n > 2) topology = 'mesh';
161
+
162
+ return { available: true, topology, links };
163
+ }
164
+
165
+ /**
166
+ * Parse rocm-smi --showtopo weight matrix → deduped xGMI edge list.
167
+ * Treats "weight ≤ XGMI_WEIGHT_THRESHOLD" as an xGMI link; higher
168
+ * weights mean PCIe / cross-socket and are ignored.
169
+ */
170
+ const XGMI_WEIGHT_THRESHOLD = 30;
171
+ export function parseRocmTopo(text, threshold = XGMI_WEIGHT_THRESHOLD) {
172
+ const lines = text.split('\n').map((s) => s.trim()).filter(Boolean);
173
+
174
+ // Find a line that starts with a GPU column header (e.g. "GPU0 GPU1 …")
175
+ let headerIdx = -1;
176
+ for (let i = 0; i < lines.length; i++) {
177
+ if (/^GPU\d+(\s+GPU\d+)+/.test(lines[i])) {
178
+ headerIdx = i;
179
+ break;
180
+ }
181
+ }
182
+ if (headerIdx < 0) return [];
183
+ const cols = lines[headerIdx].split(/\s+/).filter((c) => /^GPU\d+$/i.test(c));
184
+
185
+ const links = [];
186
+ const seen = new Set();
187
+ for (const line of lines.slice(headerIdx + 1)) {
188
+ const m = line.match(/^GPU(\d+)\s+(.*)$/i);
189
+ if (!m) continue;
190
+ const rowIdx = Number.parseInt(m[1], 10);
191
+ const cells = m[2].split(/\s+/);
192
+ for (let i = 0; i < cols.length && i < cells.length; i++) {
193
+ const colIdx = Number.parseInt(cols[i].replace(/^GPU/i, ''), 10);
194
+ if (rowIdx === colIdx) continue;
195
+ const weight = Number.parseInt(cells[i], 10);
196
+ if (!Number.isFinite(weight) || weight <= 0 || weight > threshold) continue;
197
+ const a = Math.min(rowIdx, colIdx);
198
+ const b = Math.max(rowIdx, colIdx);
199
+ const key = `${a}-${b}`;
200
+ if (seen.has(key)) continue;
201
+ seen.add(key);
202
+ links.push({ from: a, to: b, weight });
203
+ }
204
+ }
205
+ return links;
206
+ }
207
+
208
+ // ---------------------------------------------------------------------------
209
+ // AWS EFA — Elastic Fabric Adapter, AWS's NIC for HPC/distributed training.
210
+ // Cheapest cross-distro detection: `lspci -d 1d0f:` (vendor 1d0f = Amazon)
211
+ // and grep for an EFA device. EFA devices also expose themselves under
212
+ // /sys/class/infiniband/ on properly-configured instances.
213
+ // ---------------------------------------------------------------------------
214
+ async function detectEfa() {
215
+ const empty = { available: false, devices: [] };
216
+ if (process.platform !== 'linux') return empty;
217
+
218
+ const out = await tryExec('lspci', ['-d', '1d0f:', '-mm']);
219
+ if (!out) return empty;
220
+
221
+ const devices = [];
222
+ for (const line of out.split('\n')) {
223
+ if (!/EFA|Elastic Fabric/i.test(line)) continue;
224
+ // -mm output is whitespace-tolerant CSV with quoted fields. We
225
+ // only need a coarse name + the PCI BDF.
226
+ const bdfMatch = line.match(/^([0-9a-f:.]+)\s/i);
227
+ const bdf = bdfMatch ? bdfMatch[1] : null;
228
+ devices.push({ bdf, raw: line.trim() });
229
+ }
230
+ return { available: devices.length > 0, devices };
231
+ }
232
+
233
+ // ---------------------------------------------------------------------------
234
+ // InfiniBand — Linux exposes everything under /sys/class/infiniband/
235
+ // ---------------------------------------------------------------------------
236
+ const IB_SYSFS = '/sys/class/infiniband';
237
+
238
+ async function detectInfiniband() {
239
+ const empty = { available: false, devices: [] };
240
+ if (process.platform !== 'linux') return empty;
241
+
242
+ let entries;
243
+ try {
244
+ entries = await readdir(IB_SYSFS);
245
+ } catch {
246
+ return empty;
247
+ }
248
+ if (!entries.length) return empty;
249
+
250
+ const devices = [];
251
+ for (const name of entries) {
252
+ const dir = join(IB_SYSFS, name);
253
+ const board_id = (await readSafe(join(dir, 'board_id'))) ?? null;
254
+
255
+ let portEntries = [];
256
+ try {
257
+ portEntries = await readdir(join(dir, 'ports'));
258
+ } catch {
259
+ // No ports subdir → skip but still record the device.
260
+ }
261
+
262
+ if (!portEntries.length) {
263
+ devices.push({ name, board_id, port: null, state: null, rate: null, link_layer: null });
264
+ continue;
265
+ }
266
+
267
+ for (const port of portEntries) {
268
+ const portDir = join(dir, 'ports', port);
269
+ const stateRaw = (await readSafe(join(portDir, 'state'))) ?? '';
270
+ const rate = (await readSafe(join(portDir, 'rate'))) ?? null;
271
+ const linkLayer = (await readSafe(join(portDir, 'link_layer'))) ?? null;
272
+ // state file looks like "4: ACTIVE" or "1: DOWN"
273
+ const stateMatch = stateRaw.match(/:\s*(\w+)/);
274
+ const state = stateMatch ? stateMatch[1].toLowerCase() : stateRaw || null;
275
+
276
+ devices.push({
277
+ name,
278
+ board_id,
279
+ port: Number.parseInt(port, 10),
280
+ state,
281
+ rate,
282
+ link_layer: linkLayer
283
+ });
284
+ }
285
+ }
286
+
287
+ const anyActive = devices.some((d) => d.state === 'active');
288
+ return { available: anyActive, devices };
289
+ }
290
+
291
+ async function readSafe(path) {
292
+ try {
293
+ const raw = await readFile(path, 'utf8');
294
+ return raw.trim();
295
+ } catch {
296
+ return null;
297
+ }
298
+ }
299
+
300
+ // ---------------------------------------------------------------------------
301
+ // Top-level
302
+ // ---------------------------------------------------------------------------
303
+ export async function detectInterconnects() {
304
+ const [nvlink, xgmi, infiniband, efa] = await Promise.all([
305
+ detectNvlink(),
306
+ detectXgmi(),
307
+ detectInfiniband(),
308
+ detectEfa()
309
+ ]);
310
+ // RDMA is "we have something resembling an active IB / RoCE port,
311
+ // or an EFA adapter present". RoCE shows up in /sys/class/infiniband
312
+ // with link_layer=Ethernet, so InfiniBand detection covers it.
313
+ const rdma_capable =
314
+ (infiniband.available && infiniband.devices.some((d) => d.state === 'active')) ||
315
+ efa.available;
316
+ return { nvlink, xgmi, infiniband, efa, rdma_capable };
317
+ }
318
+
319
+ /**
320
+ * Build a process-env object that turns on detected interconnects for
321
+ * common ML toolkits when spawning an engine / training worker.
322
+ *
323
+ * - NVLink: Ollama, vLLM, llama.cpp pick this up automatically as
324
+ * long as CUDA isn't restricted to a single device. We surface a
325
+ * marker so the daemon can decide whether to enable
326
+ * tensor-parallel modes.
327
+ * - InfiniBand: NCCL (the all-reduce library every distributed-
328
+ * training framework uses) falls back to TCP unless NCCL_IB_DISABLE
329
+ * is explicitly off and an HCA is named. We emit NCCL_IB_DISABLE=0
330
+ * and NCCL_IB_HCA pointing at the active devices.
331
+ *
332
+ * Caller merges this object into the spawn env. Empty object when
333
+ * nothing actionable is detected.
334
+ */
335
+ export function interconnectEnv({ nvlink, xgmi, infiniband, efa, rdma_capable }) {
336
+ const env = {};
337
+ if (nvlink?.available) {
338
+ env.INFERNET_NVLINK = '1';
339
+ env.INFERNET_NVLINK_TOPOLOGY = nvlink.topology ?? 'unknown';
340
+ }
341
+ if (xgmi?.available) {
342
+ // RCCL (AMD's NCCL) honors the same NCCL_* envs.
343
+ env.INFERNET_XGMI = '1';
344
+ env.INFERNET_XGMI_TOPOLOGY = xgmi.topology ?? 'unknown';
345
+ }
346
+ if (infiniband?.available) {
347
+ const activeHcas = infiniband.devices
348
+ .filter((d) => d.state === 'active' && d.name)
349
+ .map((d) => (d.port != null ? `${d.name}:${d.port}` : d.name));
350
+ if (activeHcas.length) {
351
+ env.NCCL_IB_DISABLE = '0';
352
+ env.NCCL_IB_HCA = activeHcas.join(',');
353
+ }
354
+ }
355
+ if (efa?.available) {
356
+ // libfabric / AWS OFI NCCL plugin: prefer EFA provider.
357
+ env.FI_PROVIDER = 'efa';
358
+ env.NCCL_PROTO = 'simple';
359
+ env.INFERNET_EFA = '1';
360
+ }
361
+ if (rdma_capable) {
362
+ env.INFERNET_RDMA = '1';
363
+ }
364
+ return env;
365
+ }
366
+
367
+ /**
368
+ * Human one-liner for CLI output.
369
+ */
370
+ export function formatInterconnectSummary({ nvlink, xgmi, infiniband, efa, rdma_capable }) {
371
+ const parts = [];
372
+ if (nvlink?.available) {
373
+ const n = nvlink.links.length;
374
+ parts.push(`NVLink ${nvlink.topology} (${n} link${n === 1 ? '' : 's'})`);
375
+ } else {
376
+ parts.push('NVLink: none');
377
+ }
378
+ if (xgmi?.available) {
379
+ const n = xgmi.links.length;
380
+ parts.push(`xGMI ${xgmi.topology} (${n} link${n === 1 ? '' : 's'})`);
381
+ }
382
+ if (infiniband?.available) {
383
+ const active = infiniband.devices.filter((d) => d.state === 'active');
384
+ parts.push(`InfiniBand ${active.length} active port${active.length === 1 ? '' : 's'}`);
385
+ } else if (infiniband?.devices?.length > 0) {
386
+ parts.push(`InfiniBand present (no active ports)`);
387
+ } else {
388
+ parts.push('InfiniBand: none');
389
+ }
390
+ if (efa?.available) {
391
+ parts.push(`EFA ${efa.devices.length} adapter${efa.devices.length === 1 ? '' : 's'}`);
392
+ }
393
+ if (rdma_capable) parts.push('RDMA-capable');
394
+ return parts.join(' · ');
395
+ }