import { useEffect, useState } from "react"; import { Dialog, SegmentedControl } from "@elvis/ui"; import { python } from "./python"; import type { Inspection, Translation } from "./types"; // Okabe–Ito Dark Blue and Dark Orange: a colour-blind-safe pair for positive and negative const POSITIVE = "#0072B2"; const NEGATIVE = "#D55E00"; type Props = { translation: Translation; layer: number; // Head to show first; give the dialog a new key per cell so it starts there head: number; // The clicked cell: output token `row`, source token `col`; null when closed cell: { row: number; col: number } | null; onClose: () => void; }; // The steps behind one row of a head's attention: q · k, scale, softmax, weighted sum of v export default function InspectDialog({ translation, layer, head: initialHead, cell, onClose }: Props) { const [head, setHead] = useState(initialHead); const [result, setResult] = useState<{ key: string; data: Inspection } | null>(null); const [error, setError] = useState(null); const row = cell?.row ?? 0; const key = `${layer}/${head}/${row}`; useEffect(() => { if (!cell) { return; } let current = true; python.call("handle_inspect", { layer, head, row: cell.row }).then((r) => { if (current) { setError(r.ok ? null : r.error.message); if (r.ok) { setResult({ key: `${layer}/${head}/${cell.row}`, data: r.data }); } } }); return () => { current = false; }; }, [layer, head, cell]); const target = translation.target[row]; const data = result?.key === key ? result.data : null; const headCount = translation.cross[layer].length; return ( {cell && (
({ value: String(h), label: `Head ${h + 1}` }))} value={String(head)} onChange={(v) => setHead(Number(v))} />

While predicting {target}, this head compares its query q with each English token's{" "} key k. The dot product, divided by √{data?.query.length ?? "d"}, is the score; softmax turns the scores into weights that sum to 1; the head's output is the weighted sum of the tokens' values v.

{error &&

{error}

} {data && }
)}
); } function Steps({ data, translation, col }: { data: Inspection; translation: Translation; col: number }) { const dh = data.query.length; const keyMax = maxAbs([data.query, ...data.keys]); const valueMax = maxAbs([...data.values, data.output]); const expSum = data.scores.reduce((a, s) => a + Math.exp(s), 0); const token = translation.source[col].token; return ( <> {translation.source.map((s, j) => ( ))}
Token Key k q · k / √{dh} Weight Value v
Query q
{s.known ? s.token : `${s.token}?`} {data.scores[j].toFixed(2)} {data.weights[j].toFixed(3)}
Output Σ weight × v

q · k{token} = {(data.scores[col] * Math.sqrt(dh)).toFixed(2)}; ÷ √{dh} = {data.scores[col].toFixed(2)}
softmax: e{data.scores[col].toFixed(2)} / Σ escore = {Math.exp(data.scores[col]).toFixed(2)} /{" "} {expSum.toFixed(2)} = {data.weights[col].toFixed(3)}

Each square is one of the {dh} numbers in a vector: ■ positive,{" "} ■ negative, stronger for larger values; hover for the value. The{" "} {translation.cross[0].length} heads' outputs are then joined and multiplied by the output matrix WO.

); } function VectorStrip({ values, max }: { values: number[]; max: number }) { return (
{values.map((v, d) => (
= 0 ? POSITIVE : NEGATIVE} ${((Math.abs(v) / max) * 100).toFixed(0)}%, var(--color-surface))`, }} /> ))}
); } function maxAbs(vectors: number[][]): number { return Math.max(...vectors.flat().map(Math.abs)) || 1; }