// What a benchmark run records about its setup (RunMeta, SystemSummary) and the BetterBench notes // made from it. Kaname can't see the machine behind an endpoint, so both come from the endpoint's // hardware notes plus what the server reports. Pure; shared by the worker, the web app and tests. import type { EndpointEngine, EndpointHardware, EndpointSnapshot, RunMeta, SystemSummary } from "@/lib/db/schema"; import { formatLink, formatLinkPair, onOff, parseLinkText, systemSummaryText } from "@/lib/hardware"; /** "ghcr.io/org/img:tag@sha256:…" → "tag" (null: no tag, i.e. "latest" or digest only). */ export function imageTag(ref: string): string | null { const noDigest = ref.split("@")[0]; const last = noDigest.slice(noDigest.lastIndexOf("/") + 1); const i = last.indexOf(":"); return i >= 0 ? last.slice(i + 1) : null; } const REPO_QUANT = /(?:^|[-_.])(nvfp4|mxfp4|fp8(?:[-_]dynamic)?|int8|int4|w8a8|w4a16|awq|gptq|bnb[-_]?4bit|bf16|fp16)(?=$|[-_.])/i; const NAME_QUANT = /[-_.:]((?:UD-)?(?:I?Q\d(?:_[A-Z0-9]+)*|BF16|F16|F32|MXFP4(?:_MOE)?|TQ\d_\d))$/i; /** The quant named in a model name: a GGUF quant tag ("Q4_K_M", "IQ3_XXS") or FP8, AWQ, … */ export function quantFromName(name: string): string | null { const base = name.trim().replace(/\.gguf$/i, "").replace(/-\d{5}-of-\d{5}$/, ""); const gguf = base.match(NAME_QUANT); if (gguf) return gguf[1]; const m = base.split(/[/:]/).pop()?.match(REPO_QUANT); return m ? m[1].toUpperCase().replace("_", "-") : null; } export const ENGINE_LABEL: Record = { "llama.cpp": "llama.cpp", vllm: "vLLM", sglang: "SGLang", ollama: "Ollama", other: "OpenAI-compatible server" }; /** The run's setup from the endpoint snapshot: the hardware notes win over what the server reported. */ export function endpointRunMeta(s: EndpointSnapshot): RunMeta { const h = s.hardware; const count = h.gpuName?.trim() ? Math.max(1, h.gpuCount ?? 1) : 0; const link = h.pcie ? parseLinkText(h.pcie) : null; const engine = s.engine ?? "other"; return { v: 1, gpus: Array.from({ length: count }, () => ({ name: h.gpuName!.trim(), vramMb: h.vramGb ? Math.round(h.vramGb * 1024) : null, linkCurrent: link, linkMax: link, rebar: h.rebar ?? null, })), // Typed in by the user, not sampled at idle. linkUnderLoad: true, p2p: count < 2 ? "n/a" : (h.p2p ?? null), rebar: count ? (h.rebar ?? null) : null, runtime: { image: h.image?.trim() ?? "", tag: h.image?.trim() ? imageTag(h.image.trim()) : null, digest: null, engine, engineName: h.engineName?.trim() || ENGINE_LABEL[engine], engineVersion: h.engineVersion?.trim() || s.engineVersion, }, model: { repoId: s.model, file: null, quant: h.quant?.trim() || quantFromName(s.model), kvCache: h.kvCache?.trim() || null }, contextSize: h.contextSize ?? s.contextSize, concurrency: h.concurrency ?? s.slots ?? null, overridden: [], }; } /** The publishable system summary from the notes: CPU, RAM, OS, driver, virtualization, Kaname. */ export function endpointSystem(h: EndpointHardware, kaname: string): SystemSummary { const virt = h.virtualization?.trim() || null; const bare = !!virt && /bare[\s-]*metal/i.test(virt); return { cpu: { model: h.cpu?.trim() || null, cores: null, threads: null }, // GB as entered (formatBytes shows decimal units too). memoryBytes: h.memoryGb ? Math.round(h.memoryGb * 1e9) : null, storage: null, os: h.os?.trim() || null, kernel: null, gpuDrivers: h.gpuDriver?.trim() ? [h.gpuDriver.trim()] : [], virtualization: { vm: virt ? !bare : null, hypervisor: virt && !bare ? virt : null, gpuPassthrough: null }, kaname, }; } /** "2× NVIDIA RTX 4090 (24 GiB)", "1× Intel Arc Pro B65 (32 GiB) + 1× …"; null without GPUs. */ export function gpuLabel(m: RunMeta): string | null { if (m.gpus.length === 0) return null; const counts = new Map(); for (const g of m.gpus) { const name = `${g.name ?? "unknown GPU"}${g.vramMb ? ` (${Math.round(g.vramMb / 1024)} GiB)` : ""}`; counts.set(name, (counts.get(name) ?? 0) + 1); } return [...counts].map(([name, n]) => `${n}× ${name}`).join(" + "); } /** The PCIe links of the GPUs used, deduplicated; "(idle)" when not measured under load. */ export function pcieLabel(m: RunMeta): string | null { const links = [...new Set(m.gpus.map((g) => formatLinkPair(g.linkCurrent, g.linkMax) ?? "unknown"))]; if (!links.length) return null; const idle = !m.linkUnderLoad && m.gpus.some((g) => g.linkCurrent); return `${links.join(", ")}${idle ? " (at idle)" : ""}`; } /** Maximum links only ("Gen5 x16"): known before the run, unlike the link under load. */ export function pcieMaxLabel(m: RunMeta): string | null { const links = [...new Set(m.gpus.map((g) => formatLink(g.linkMax) ?? "unknown"))]; return links.length ? links.join(", ") : null; } export const engineLabel = (m: RunMeta) => [m.runtime.engineName, m.runtime.engineVersion].filter(Boolean).join(" "); /** "Qwen3-8B-GGUF Q4_K_M with q8_0 KV cache" */ export function modelLabel(m: RunMeta): string { const name = m.model.repoId.split("/").pop() ?? m.model.repoId; return `${name}${m.model.quant ? ` ${m.model.quant}` : ""}${m.model.kvCache ? ` with ${m.model.kvCache} KV cache` : ""}`; } /** Rows for the run page, compare views and sweeps (label → value, "unknown" where not known). */ export function metaRows(m: RunMeta): { key: string; label: string; value: string }[] { return [ { key: "gpus", label: "GPUs", value: gpuLabel(m) ?? "none (CPU)" }, ...(m.gpus.length ? [{ key: "pcie", label: "PCIe", value: pcieLabel(m) ?? "unknown" }] : []), ...(m.gpus.length ? [{ key: "p2p", label: "P2P", value: m.p2p ?? "unknown" }] : []), ...(m.gpus.length ? [{ key: "rebar", label: "ReBAR", value: onOff(m.rebar) }] : []), { key: "engine", label: "Engine", value: engineLabel(m) || "unknown" }, ...(m.runtime.image ? [{ key: "image", label: "Image", value: m.runtime.image }] : []), ...(m.runtime.digest ? [{ key: "digest", label: "Digest", value: m.runtime.digest }] : []), { key: "model", label: "Model", value: modelLabel(m) }, { key: "ctx", label: "Context", value: m.contextSize != null ? m.contextSize.toLocaleString("en-US") : "unknown" }, { key: "concurrency", label: "Concurrency", value: m.concurrency != null ? String(m.concurrency) : "server default" }, ]; } /** * BetterBench --note values (recorded in results.json, shown in its report and published to * launch80). `engine` is the key launch80 uses to name the stack. */ export function metaNotes(m: RunMeta, system: SystemSummary | null, description: string | null): Record { const notes: Record = { engine: engineLabel(m) || m.runtime.engine, ...(m.runtime.image ? { image: m.runtime.image } : {}), ...(m.runtime.digest ? { digest: m.runtime.digest } : {}), ...(m.model.quant ? { quant: m.model.quant } : {}), ...(m.model.kvCache ? { kv_cache: m.model.kvCache } : {}), ...(m.contextSize != null ? { ctx: String(m.contextSize) } : {}), ...(m.concurrency != null ? { concurrency: String(m.concurrency) } : {}), gpus: gpuLabel(m) ?? "none", }; if (m.gpus.length) { notes.pcie_max = pcieMaxLabel(m) ?? "unknown"; notes.p2p = m.p2p ?? "unknown"; notes.rebar = onOff(m.rebar); } if (system) notes.system = systemSummaryText(system); if (description?.trim()) notes.description = description.trim().replace(/\s+/g, " ").slice(0, 2000); return notes; }