import { useRef, useState } from "preact/hooks"; import { config, generating, cacheState, resolution, kindBActive, instrumentation } from "../state"; import { streamGenerate } from "../api"; import { HINTS } from "../lib/labels"; import { HelpHint } from "./HelpHint"; import { attnLabels } from "./AttentionGrid"; export function RunPanel() { const [prompt, setPrompt] = useState("Once upon a time"); const [maxTokens, setMaxTokens] = useState(600); const [temperature, setTemperature] = useState(0); const [output, setOutput] = useState(""); const [status, setStatus] = useState(null); const [statusIsErr, setStatusIsErr] = useState(false); const [hoverRun, setHoverRun] = useState(false); const aborterRef = useRef(null); const res = resolution.value; const kbActive = kindBActive.value; const runnable = res.state !== "red"; function stop() { if (aborterRef.current) { aborterRef.current.abort(); } } async function run() { setOutput(prompt); setStatus("streaming..."); setStatusIsErr(false); generating.value = true; instrumentation.value = null; cacheState.value = { positions: 0, status: "streaming", prompt_tokens: 0 }; attnLabels.value = Array.from(prompt); const t0 = performance.now(); let tokens = 0; let acc = prompt; let tStart = t0; const aborter = new AbortController(); aborterRef.current = aborter; try { for await (const ev of streamGenerate({ config: config.value, prompt, max_tokens: maxTokens, temperature, instrument: true, }, aborter.signal)) { if (ev.event === "start") { cacheState.value = { positions: ev.prompt_tokens, status: "streaming", prompt_tokens: ev.prompt_tokens, }; tStart = performance.now(); } else if (ev.event === "token") { acc += ev.text; tokens += 1; setOutput(acc); attnLabels.value = [...attnLabels.value, ev.text]; const prev = cacheState.value; cacheState.value = { ...prev, positions: prev.positions + 1 }; if (tokens % 4 === 0 || tokens < 4) { const dt = (performance.now() - tStart) / 1000; const tps = dt > 0 ? tokens / dt : 0; setStatus(`streaming · ${tokens} tok · ${tps.toFixed(1)} tok/s`); } } else if (ev.event === "done") { const dt = (performance.now() - tStart) / 1000; const tps = dt > 0 ? tokens / dt : 0; const stop = ev.stop_reason === "eos" ? "stopped at story end" : "hit max_tokens cap"; setStatus(`${tokens} tok · ${tps.toFixed(1)} tok/s · ${dt.toFixed(2)}s · ${stop} · val_loss=${ev.val_loss.toFixed(3)}`); cacheState.value = { ...cacheState.value, status: "done" }; } else if (ev.event === "instr") { const { event: _e, ...payload } = ev as any; instrumentation.value = payload; } else if (ev.event === "error") { setStatus("error: " + ev.message); setStatusIsErr(true); } } } catch (e) { const err = e as Error; if (err.name === "AbortError" || aborter.signal.aborted) { const dt = (performance.now() - tStart) / 1000; const tps = dt > 0 ? tokens / dt : 0; setStatus(`stopped · ${tokens} tok · ${tps.toFixed(1)} tok/s · ${dt.toFixed(2)}s`); cacheState.value = { ...cacheState.value, status: "done" }; } else { setStatus("error: " + err.message); setStatusIsErr(true); } } finally { generating.value = false; aborterRef.current = null; setHoverRun(false); } } return (
{res.state === "green" && ( 0 ? " Kind-B interventions are turned on at inference, so " + kbActive.map(s => s.replace("enable_", "")).join(", ") + " is disabled in the forward pass." : ""}`}> hash {res.resolvedEntry!.hash} · val={res.resolvedEntry!.val_loss.toFixed(3)} {kbActive.length > 0 && · Kind-B: {kbActive.map(s => s.replace("enable_", "")).join(", ")}} )} {res.state === "amber" && ( approx · {res.distance} axis{res.distance === 1 ? "" : "es"} differ ({res.differingFields.join(", ")}) {kbActive.length > 0 && · Kind-B: {kbActive.map(s => s.replace("enable_", "")).join(", ")}} )} {res.state === "red" && ( no checkpoint within 2 axes )}