import { useCallback, useEffect, useState } from "react"; import { AppLayout, Button, Dialog, InputField, InputSection, SegmentedControl, StepControls, useStepper, } from "@elvis/ui"; import AttentionHeatmap from "./AttentionHeatmap"; import Candidates from "./Candidates"; import InspectDialog from "./InspectDialog"; import { LANGUAGES } from "./languages"; import ModelDiagram from "./ModelDiagram"; import TokenLines from "./TokenLines"; import useTranslation from "./useTranslation"; const MEAN = "mean"; // The Q/K/V popup behind a click on a heatmap square; off for now (2026-11-13) const INSPECT = false; const STEP_MS = 500; export default function App() { const app = useTranslation(); // Index into cross; -1 is the last layer const [layer, setLayer] = useState(-1); const [head, setHead] = useState(MEAN); const [architectureOpen, setArchitectureOpen] = useState(false); // The heatmap cell being inspected const [inspected, setInspected] = useState<{ row: number; col: number; } | null>(null); // Step k shows the first k output tokens const length = app.translation?.target.length ?? 0; const fetchStep = useCallback( async (k: number) => (k <= length ? k : null), [length], ); const steps = useStepper(fetchStep, { intervalMs: STEP_MS }); const { stepper } = steps; useEffect(() => { if (app.translation) { stepper.restart({ total: app.translation.target.length, intervalMs: STEP_MS, }); } }, [app.translation, stepper]); // Capped for the render where a shorter translation arrives, before the restart const shown = Math.min(steps.iteration, length); const language = LANGUAGES.find((l) => l.value === app.language)!.label; const cross = app.translation?.cross; const layerIndex = cross ? (layer < 0 ? cross.length - 1 : layer) : 0; const heads = cross?.[layerIndex]; const weights = heads && (head === MEAN ? heads[0].map((row, i) => row.map((_, j) => mean(heads.map((h) => h[i][j]))), ) : heads[Number(head)]); return ( ({ value, label }))} value={app.language} onChange={app.setLanguage} /> {cross && (

A small transformer with an attention mechanism:

  • Two encoder layers read the English input sentence.
  • Two decoder layers write the {language} output sentence one token at a time.
  • Each decoder layer has four cross-attention heads, which attend to parts of the English input sentence.

Showing layer {layerIndex + 1},{" "} {head === MEAN ? "mean of heads" : `head ${Number(head) + 1}`}.