Files
Kaname/src/lib/hardware.ts
T
Pascal LinxweilerandClaude Opus 5.5 ee6c623e2a
Release / release (push) Successful in 3m25s
Release 0.11.0: benchmark metadata and system info
Every run records GPUs (PCIe link under load and maximum, ReBAR, P2P),
runtime image/tag/digest and the engine version the server reports,
model, quant, KV cache, context and concurrency, plus a whitelisted
system summary and a description. Shown on the run page, compare and
sweep views; passed to BetterBench as notes and to the launch80 preview
as sections. Settings -> Hardware for manual values.

Also: scripts/intel-probe.sh (read-only diagnostics for Intel hosts),
and --quick is no longer passed together with --passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:04 +02:00

211 lines
9.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Host hardware as Kaname reports it: PCIe links, ReBAR, the system summary, and the manual
// overrides from Settings → Hardware. Pure; shared by the web app, the worker and tests.
import type { HardwareGpu, HardwareOverrides, HostHardware, PcieLink, SystemSummary } from "@/lib/db/schema";
import { formatBytes } from "@/lib/format";
const GT_PER_GEN: [number, number][] = [
[2.5, 1],
[5, 2],
[8, 3],
[16, 4],
[32, 5],
[64, 6],
];
/** sysfs "16.0 GT/s PCIe" → 4; unknown speeds → null. */
export function linkGen(speed: string | null | undefined): number | null {
const gt = Number(speed?.match(/([\d.]+)\s*GT\/s/)?.[1]);
return GT_PER_GEN.find(([s]) => Math.abs(s - gt) < 0.01)?.[1] ?? null;
}
export function formatLink(l: PcieLink | null | undefined): string | null {
if (!l || (l.gen == null && l.width == null)) return null;
return [l.gen != null ? `Gen${l.gen}` : null, l.width != null ? `x${l.width}` : null].filter(Boolean).join(" ");
}
/** "Gen4 x8 of Gen5 x16", or just "Gen5 x16" when the link runs at its maximum. */
export function formatLinkPair(current: PcieLink | null | undefined, max: PcieLink | null | undefined): string | null {
const c = formatLink(current);
const m = formatLink(max);
if (!c) return m ? `? of ${m}` : null;
return !m || c === m ? c : `${c} of ${m}`;
}
/** "Gen4 x8", "gen 4 x8", "x16" → a link; null when it isn't one. */
export function parseLinkText(text: string): PcieLink | null {
const t = text.trim();
const m = t.match(/^(?:gen\s*([1-6]))?\s*(?:x\s*(\d{1,2}))?$/i);
if (!t || !m || (!m[1] && !m[2])) return null;
return { gen: m[1] ? Number(m[1]) : null, width: m[2] ? Number(m[2]) : null };
}
/** ReBAR is on when the largest BAR maps (nearly) all of the VRAM; without it BARs are 256 MiB. */
export function rebarFrom(barMb: number | null, vramMb: number | null): boolean | null {
if (barMb == null) return null;
if (vramMb) return barMb >= vramMb * 0.9;
return barMb >= 2048 ? true : null;
}
export const onOff = (v: boolean | null | undefined) => (v == null ? "unknown" : v ? "on" : "off");
export type EffectiveHardware = HostHardware & {
gpuPassthrough: boolean | null;
gpuDriver: string | null;
/** Which values came from Settings → Hardware (e.g. "gpu 0000:03:00.0 link", "storage"). */
overridden: string[];
};
/** Detected hardware with the overrides applied. */
export function applyOverrides(hw: HostHardware | null | undefined, o: HardwareOverrides = {}): EffectiveHardware | null {
if (!hw) return null;
const overridden: string[] = [];
const gpus: HardwareGpu[] = hw.gpus.map((g) => {
const x = o.gpus?.[g.pciAddress];
if (!x) return g;
const cur = x.linkCurrent ? parseLinkText(x.linkCurrent) : null;
const max = x.linkMax ? parseLinkText(x.linkMax) : null;
const set = <K extends keyof HardwareGpu>(key: K, v: HardwareGpu[K] | null | undefined, label: string) => {
if (v == null || v === "") return;
out[key] = v;
overridden.push(label);
};
const out = { ...g };
set("name", x.name, "gpu name");
set("vramMb", x.vramMb, "vram");
set("linkCurrent", cur, "pcie link");
set("linkMax", max, "pcie max link");
set("rebar", x.rebar, "rebar");
if (x.rebar == null && x.vramMb) out.rebar = rebarFrom(g.barMb, x.vramMb);
return out;
});
const passthroughDetected = hw.virtualization.vm ? hw.gpus.length > 0 : hw.virtualization.vm === false ? false : null;
if (o.p2p) overridden.push("p2p");
if (o.storage) overridden.push("storage");
if (o.hypervisor) overridden.push("virtualization");
if (o.gpuPassthrough != null) overridden.push("gpu passthrough");
if (o.gpuDriver) overridden.push("gpu driver");
return {
...hw,
gpus,
p2p: o.p2p ?? hw.p2p,
storage: o.storage ? { model: o.storage, kind: hw.storage?.kind ?? null, sizeBytes: hw.storage?.sizeBytes ?? null } : hw.storage,
virtualization: o.hypervisor ? { vm: true, hypervisor: o.hypervisor } : hw.virtualization,
gpuPassthrough: o.gpuPassthrough ?? passthroughDetected,
gpuDriver: o.gpuDriver ?? null,
overridden: [...new Set(overridden)],
};
}
/** Driver labels, e.g. "nvidia 580.82.07", "xe (kernel 6.17.1)". */
export function driverLabels(gpus: HardwareGpu[]): string[] {
return [...new Set(gpus.filter((g) => g.driver).map((g) => (g.driverVersion ? `${g.driver} ${g.driverVersion}` : g.driver!)))];
}
/**
* The publishable system summary. Only whitelisted facts: CPU, RAM, disk model, OS, kernel, drivers,
* virtualization, Kaname version. Never the hostname, addresses, users or paths.
*/
export function systemSummary(hw: EffectiveHardware | null, os: string | null, kaname: string): SystemSummary {
return {
cpu: hw?.cpu ?? { model: null, cores: null, threads: null },
memoryBytes: hw?.memoryBytes ?? null,
storage: hw?.storage ?? null,
os,
kernel: hw?.kernel ?? null,
gpuDrivers: hw?.gpuDriver ? [hw.gpuDriver] : driverLabels(hw?.gpus ?? []),
virtualization: { vm: hw?.virtualization.vm ?? null, hypervisor: hw?.virtualization.hypervisor ?? null, gpuPassthrough: hw?.gpuPassthrough ?? null },
kaname,
};
}
const STORAGE_KIND: Record<string, string> = { nvme: "NVMe", ssd: "SSD", hdd: "HDD", virtual: "virtual disk" };
/** One line per fact, for notes, the run page and launch80. */
export function systemSummaryLines(s: SystemSummary): string[] {
const cpu = s.cpu.model
? `${s.cpu.model}${s.cpu.cores || s.cpu.threads ? ` (${[s.cpu.cores && `${s.cpu.cores} cores`, s.cpu.threads && `${s.cpu.threads} threads`].filter(Boolean).join(", ")})` : ""}`
: null;
const disk = s.storage ? [s.storage.model, s.storage.kind && STORAGE_KIND[s.storage.kind], s.storage.sizeBytes && formatBytes(s.storage.sizeBytes)].filter(Boolean).join(", ") : null;
const v = s.virtualization;
const virt = v.vm == null ? null : v.vm ? `VM${v.hypervisor ? ` (${v.hypervisor})` : ""}${v.gpuPassthrough ? " with GPU passthrough" : ""}` : "bare metal";
return [
cpu && `CPU: ${cpu}`,
s.memoryBytes && `RAM: ${formatBytes(s.memoryBytes)}`,
disk && `Models disk: ${disk}`,
(s.os || s.kernel) && `OS: ${[s.os, s.kernel && `kernel ${s.kernel}`].filter(Boolean).join(", ")}`,
s.gpuDrivers.length > 0 && `GPU driver: ${s.gpuDrivers.join(", ")}`,
virt && `Virtualization: ${virt}`,
`Kaname ${s.kaname}`,
].filter((x): x is string => !!x);
}
export const systemSummaryText = (s: SystemSummary) => systemSummaryLines(s).join(" · ");
export const HARDWARE_OVERRIDES_KEY = "hardware.overrides";
const TEXT_MAX = 120;
export class HardwareOverrideError extends Error {}
const text = (v: unknown, what: string): string | undefined => {
if (v == null || v === "") return undefined;
if (typeof v !== "string" || v.trim().length > TEXT_MAX || /[\r\n\0]/.test(v)) throw new HardwareOverrideError(`${what}: at most ${TEXT_MAX} characters on one line.`);
return v.trim() || undefined;
};
const bool = (v: unknown, what: string): boolean | undefined => {
if (v == null || v === "") return undefined;
if (typeof v !== "boolean") throw new HardwareOverrideError(`${what} must be on or off.`);
return v;
};
const linkText = (v: unknown, what: string): string | undefined => {
const t = text(v, what);
if (t && !parseLinkText(t)) throw new HardwareOverrideError(`${what}: "${t}" isn't a PCIe link (e.g. "Gen4 x8").`);
return t;
};
/** Validate overrides from the settings form; throws HardwareOverrideError. Empty fields are dropped. */
export function validateOverrides(input: unknown): HardwareOverrides {
const o = (input ?? {}) as Record<string, unknown>;
const out: HardwareOverrides = {};
const gpus: NonNullable<HardwareOverrides["gpus"]> = {};
for (const [addr, raw] of Object.entries((o.gpus ?? {}) as Record<string, Record<string, unknown>>)) {
if (!/^[0-9a-f]{4,}:[0-9a-f]{2}:[0-9a-f]{2}\.[0-7]$/i.test(addr)) throw new HardwareOverrideError(`"${addr}" isn't a PCI address.`);
const vram = raw?.vramMb;
if (vram != null && vram !== "" && (typeof vram !== "number" || !Number.isInteger(vram) || vram < 256 || vram > 4_194_304)) {
throw new HardwareOverrideError("VRAM must be a whole number of MiB (256 – 4194304).");
}
const g = {
name: text(raw?.name, "GPU name"),
vramMb: typeof vram === "number" ? vram : undefined,
linkCurrent: linkText(raw?.linkCurrent, "Current link"),
linkMax: linkText(raw?.linkMax, "Maximum link"),
rebar: bool(raw?.rebar, "ReBAR"),
};
const set = Object.fromEntries(Object.entries(g).filter(([, v]) => v !== undefined));
if (Object.keys(set).length) gpus[addr.toLowerCase()] = set;
}
if (Object.keys(gpus).length) out.gpus = gpus;
if (o.p2p != null && o.p2p !== "") {
if (o.p2p !== "on" && o.p2p !== "off" && o.p2p !== "partial") throw new HardwareOverrideError("P2P must be on, off or partial.");
out.p2p = o.p2p;
}
const storage = text(o.storage, "Models disk");
const hypervisor = text(o.hypervisor, "Virtualization");
const gpuDriver = text(o.gpuDriver, "GPU driver");
const passthrough = bool(o.gpuPassthrough, "GPU passthrough");
if (storage) out.storage = storage;
if (hypervisor) out.hypervisor = hypervisor;
if (gpuDriver) out.gpuDriver = gpuDriver;
if (passthrough != null) out.gpuPassthrough = passthrough;
return out;
}
/** Stored overrides; anything invalid is ignored rather than breaking a run. */
export function parseOverrides(stored: unknown): HardwareOverrides {
try {
return validateOverrides(stored);
} catch {
return {};
}
}