"use client"; import { Grid3x3Icon, PlusIcon, XIcon } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { NativeSelect } from "@/components/ui/native-select"; import type { BenchmarkPhase, DeploymentOptions, RuntimeAccelerator, RuntimeEngine } from "@/lib/db/schema"; import { quantGroup } from "@/lib/selection"; import { expandSweep, MAX_SWEEP_VARIANTS, splitValues, SweepError, sweepKeysFor } from "@/lib/sweep"; export type SweepBase = { id: string; name: string; engine: RuntimeEngine; accelerator: RuntimeAccelerator; repoId: string; options: DeploymentOptions; cpus: number | null; memoryMb: number | null; ggufFiles: string[]; }; const PHASES: { id: BenchmarkPhase; label: string }[] = [ { id: "decode", label: "Generation" }, { id: "prefill", label: "Prompt processing" }, { id: "concurrency", label: "Parallel requests" }, ]; /** The base's current value of a sweep key, shown as a hint. */ function currentValue(base: SweepBase, key: string): string { const o = base.options; const v: Record = { contextSize: o.contextSize, modelFile: o.modelFile, kvCache: o.cacheTypeK ?? "f16", threads: o.threads ?? "auto", parallel: o.parallel ?? "auto", batchSize: o.batchSize ?? "default", ubatchSize: o.ubatchSize ?? "default", flashAttn: o.flashAttn ?? "auto", cacheRamMb: o.cacheRamMb ?? "8192 (default)", gpuLayers: o.gpuLayers ?? "auto", dtype: o.dtype ?? "runtime default", maxNumSeqs: o.maxNumSeqs ?? "default", kvCacheGb: o.kvCacheGb ?? "runtime default", gpuMemoryUtilization: o.gpuMemoryUtilization ?? "0.9", cpus: base.cpus ?? "no limit", }; return String(v[key] ?? ""); } type Param = { key: string; text: string; files: string[] }; export function SweepForm({ bases, initialBaseId }: { bases: SweepBase[]; initialBaseId?: string }) { const router = useRouter(); const [baseId, setBaseId] = useState(initialBaseId && bases.some((b) => b.id === initialBaseId) ? initialBaseId : (bases[0]?.id ?? "")); const base = bases.find((b) => b.id === baseId) ?? null; const keys = base ? sweepKeysFor(base.engine, base.accelerator) : []; const [params, setParams] = useState([{ key: "contextSize", text: "", files: [] }]); const [label, setLabel] = useState(""); const [mode, setMode] = useState("quick"); const [passes, setPasses] = useState("10"); const [phases, setPhases] = useState(["decode", "prefill", "concurrency"]); const [busy, setBusy] = useState(false); const values = (p: Param) => (p.key === "modelFile" ? p.files : splitValues(p.text)); let preview: { variants: ReturnType | null; error: string | null } = { variants: null, error: null }; if (base) { try { preview = { variants: expandSweep(params.map((p) => ({ key: p.key, values: values(p) })), base.engine, base.accelerator, { modelFiles: base.ggufFiles }), error: null }; } catch (err) { preview = { variants: null, error: err instanceof SweepError ? err.message : String(err) }; } } const setParam = (i: number, patch: Partial) => setParams((ps) => ps.map((p, j) => (j === i ? { ...p, ...patch } : p))); async function submit() { if (!base || !preview.variants) return; setBusy(true); try { const res = await fetch("/api/sweeps", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ baseDeploymentId: base.id, label: label.trim() || null, params: params.map((p) => ({ key: p.key, values: values(p) })), quick: mode === "quick", passes: mode === "custom" ? Number(passes) || 10 : null, phases: phases.length === PHASES.length ? [] : phases, }), }); const json = await res.json().catch(() => ({})); if (!res.ok) return toast.error(json.error?.message ?? `Could not start (${res.status})`); router.push(`/benchmarks/sweeps/${json.id}`); } finally { setBusy(false); } } if (bases.length === 0) return

Create a deployment first; a sweep varies its settings.

; return (
Base deployment Every variant starts from its settings. It doesn't need to be running. setBaseId(e.target.value)} aria-label="Base deployment"> {bases.map((b) => ( ))} What to vary One or two settings; every combination becomes a variant (up to {MAX_SWEEP_VARIANTS}). {base && params.map((p, i) => { const def = keys.find((k) => k.key === p.key); return (
setParam(i, { key: e.target.value, text: "", files: [] })} aria-label={`Setting ${i + 1}`}> {keys .filter((k) => k.key === p.key || !params.some((q) => q.key === k.key)) .map((k) => ( ))} {params.length > 1 && ( )}
{p.key === "modelFile" ? (
{base.ggufFiles.map((f) => ( ))} {base.ggufFiles.length < 2 &&

Only one GGUF file is downloaded; download more quants to compare them.

}
) : ( setParam(i, { text: e.target.value })} placeholder={def?.hint} aria-label="Values" className="font-mono text-sm" /> )}

Base value: {currentValue(base, p.key)}

); })} {params.length < 2 && base && ( )}
Benchmark per variant
setMode(e.target.value)} aria-label="Run length"> {mode === "custom" && setPasses(e.target.value.replace(/\D/g, ""))} inputMode="numeric" className="w-16" aria-label="Passes" />}
{PHASES.map((p) => ( ))}
setLabel(e.target.value)} maxLength={80} placeholder="e.g. context vs KV cache (optional)" />
{preview.variants ? `${preview.variants.length} variants` : "Variants"} {preview.error ? (

{preview.error}

) : (
    {preview.variants?.map((v) => (
  1. {v.index + 1} {v.label}
  2. ))}
)}

Variants run one after another, each in its own temporary deployment{base?.memoryMb ? " (memory limit: the base's, raised where a variant needs more)" : ""}. {mode !== "quick" && " Full runs on a CPU can take hours per variant."}

); }