// The fleet: what is on each card, what every model is doing, and how that moved over the window. import { useMemo } from "react" import { Chart, Legend, type Line } from "@/components/chart" import { Empty, Kpi, Panel, PartBar, PartLegend, StateBadge, StateDot, STATE_OF_CODE, stateOf, type Part } from "@/components/bits" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip" import type { Fleet, FleetModel } from "@/lib/api" import { bytesParts, fBytes, fClock, fDur, fin, fNum } from "@/lib/format" import { fleet as fleetScope, useScope, type Series } from "@/lib/store" import { hrefOf, useRange } from "@/lib/ui" const hue = (i: number) => `var(--series-${(i % 8) + 1})` // What a model holds on one accelerator: the server's figure for it in any state, so a model that // is loading or partly parked is counted as itself; from a server that sends none, its claim // while it is loaded. const onCard = (m: FleetModel, card: number) => m.vram_bytes_by_card ? (m.vram_bytes_by_card[String(card)] ?? 0) : m.state === "resident" && m.cards?.includes(card) ? (m.vram_bytes_per_card ?? 0) : 0 const held = (m: FleetModel) => m.vram_bytes_by_card ? Object.values(m.vram_bytes_by_card).reduce((a, b) => a + b, 0) : m.state === "resident" ? (m.vram_bytes_per_card ?? 0) * (m.cards?.length ?? 0) : 0 export function Overview() { const scope = useScope(fleetScope) const range = useRange() const f = scope.live if (!f) return {scope.error ?? "Waiting for data…"} const hueOf = new Map(f.models.map((m, i) => [m.id, hue(i)])) const resident = f.models.filter((m) => m.state === "resident") const parked = f.models.filter((m) => m.state === "parked") const cold = f.models.filter((m) => m.state === "cold") const failed = f.models.filter((m) => m.state === "failed") const onCards = f.models.reduce((a, m) => a + held(m), 0) const vram = f.cards.reduce((a, c) => a + (c.vram_bytes ?? 0), 0) // All the process pins in host memory: the models' pools, tables and cache tiers as well as // the copies of parked ones. A server that does not say counts the copies alone. const copies = f.models.reduce((a, m) => a + (m.host_bytes ?? 0), 0) const hostBytes = f.host?.held_bytes ?? copies const flight = f.models.reduce((a, m) => a + (m.in_flight ?? 0), 0) const waiting = f.models.reduce((a, m) => a + (m.waiting ?? 0), 0) const returns = f.models.reduce((a, m) => a + (m.restores ?? 0), 0) const power = f.cards.reduce((a, c) => a + (fin(c.power_w) ? c.power_w : 0), 0) const [cv, cu] = bytesParts(onCards) const [hv, hu] = bytesParts(hostBytes) return (
{failed.map((m) => ( {m.id} did not start {m.error ?? "no reason was given"} ))}
m.id).join(", ") : "none"} /> 0 ? ` · ${fBytes(copies)} parked copies` : "")} /> 0 ? fNum(power) : "–"} unit={power > 0 ? "W" : undefined} detail={`across ${f.cards.length} accelerator${f.cards.length === 1 ? "" : "s"}`} />
{f.cards.map((c) => )}
Model State Priority Accelerators VRAM Host Active Loads Parks Last load {f.models.map((m) => ( { location.hash = hrefOf(m.id) }}> e.stopPropagation()}> {m.id}
{m.error ?? m.file}
{fNum(m.priority)}{m.pinned ? " · pinned" : ""} {m.cards?.length ? m.cards.join(", ") : "–"} {held(m) > 0 ? fBytes(held(m)) : "–"} {(m.host_bytes ?? 0) + (m.host_pinned_bytes ?? 0) > 0 ? fBytes((m.host_bytes ?? 0) + (m.host_pinned_bytes ?? 0)) : "–"} {m.in_flight ? fNum(m.in_flight) : "–"}{m.waiting ? ` +${m.waiting}` : ""} {fNum(m.loads)} {fNum(m.parks)} {fin(m.load_seconds as number) ? fDur(m.load_seconds as number) : "–"}
))}
) } function CardPanel({ fleet: f, card, hueOf, data, version, range }: { fleet: Fleet; card: number; hueOf: Map; data: Series; version: number; range: number }) { const c = f.cards.find((x) => x.card === card)! const parts: Part[] = f.models.map((m) => ({ key: m.id, label: m.id, value: onCard(m, card), color: hueOf.get(m.id)!, text: fBytes(onCard(m, card)) })) const used = (c.vram_bytes ?? 0) - (c.free_bytes ?? 0) const named = parts.reduce((a, p) => a + p.value, 0) // What no model's ledger names: the open hardware queues, by the server's count, and the // rest, which is buffers the loaded models' kernels allocate themselves and the driver's own. const queues = Math.max(Math.min(c.queue_bytes ?? 0, used - named), 0) const rest = used - named - queues if (queues > 0) parts.push({ key: "~queues", label: `Queues (${c.queues ?? 0} open)`, value: queues, color: "var(--idle)", text: fBytes(queues) }) if (rest > 64 << 20) parts.push({ key: "~other", label: "Model buffers and driver", value: rest, color: "var(--muted-foreground)", text: fBytes(rest) }) const lines: Line[] = f.models.map((m, i) => ({ key: `c${card}:${m.id}`, label: m.id, color: i + 1 })) return ( Accelerator {card}{c.name}} action={{fBytes(c.free_bytes)} free of {fBytes(c.vram_bytes)}}>
) } /** A figure with its last five minutes drawn beside it. */ function Gauge({ label, value, data, k, max }: { label: string; value: string; data: Series; k: string; max?: number }) { const col = data.col(k) ?? [] const lo = data.firstAfter(data.end - 300) const pts = col.slice(lo) const vals = pts.filter((v): v is number => v !== null) const top = max ?? Math.max(1, ...vals) * 1.05 const base = max !== undefined ? 0 : Math.min(...vals, top) * 0.9 const n = pts.length let d = "" let open = false pts.forEach((v, i) => { if (v === null) { open = false; return } d += (open ? "L" : "M") + ((100 * i) / Math.max(1, n - 1)).toFixed(2) + " " + (27 - (24 * (Math.min(v, top) - base)) / Math.max(1e-9, top - base)).toFixed(2) open = true }) return (
{label}
{value}
) } /** One row a model: its state as coloured runs along the window. */ function StateStrip({ models, data, version, range }: { models: FleetModel[]; data: Series; version: number; range: number }) { const rows = useMemo(() => { const end = data.end, start = end - range, lo = data.firstAfter(start) return models.map((m) => { const col = data.col("s:" + m.id) ?? [] const runs: { a: number; b: number; s: string }[] = [] for (let i = lo; i < data.t.length; ++i) { const v = col[i] if (v == null) continue const s = STATE_OF_CODE[v] ?? "cold" const a = i === lo ? Math.max(start, data.t[i] - 1) : data.t[i - 1], b = data.t[i] const last = runs[runs.length - 1] if (last && last.s === s && a - last.b < 2.5) last.b = b else runs.push({ a, b, s }) } return { id: m.id, runs } }) // eslint-disable-next-line react-hooks/exhaustive-deps }, [models.map((m) => m.id).join("|"), data, version, range]) const end = data.end, start = end - range const ticks = [0, 0.25, 0.5, 0.75, 1] const seen = new Set(rows.flatMap((r) => r.runs.map((x) => x.s))) return (
{rows.map((r) => (
{r.id}
{r.runs.map((x, i) => ( } /> {stateOf(x.s).label} · {fClock(x.a, true)} to {fClock(x.b, true)} · {fDur(x.b - x.a)} ))}
))}
{ticks.map((p) => ( ))}
{["resident", "loading", "waiting", "parked", "cold", "failed"].filter((s) => seen.has(s)).map((s) => ( {stateOf(s).label} ))}
) } export { Legend }