type Props = { // Layers in each of the encoder and decoder (the model has the same number in both) layers: number; heads: number; // Selected decoder layer, and head (null for the mean of heads) layer: number; head: number | null; // Name of the output language, e.g. "French" language: string; onSelect: (layer: number, head: number | null) => void; }; const W = 340; const ENC_X = 8; const ENC_W = 84; const BUS_X = 106; const DEC_X = 120; const DEC_W = 212; const LAYER_X = DEC_X + 8; const LAYER_W = DEC_W - 16; const SUB_X = LAYER_X + 8; const SUB_W = LAYER_W - 16; const SUB_CX = SUB_X + SUB_W / 2; const ENC_CX = ENC_X + ENC_W / 2; const BLOCK_H = 26; const CHIP_H = 20; const CHIP_GAP = 4; const CROSS_H = BLOCK_H + 2 * CHIP_H + CHIP_GAP + 6; const TITLE_H = 22; const ARROW_GAP = 14; const LAYER_GAP = 18; const ENC_H = 34; // The translator's Transformer, bottom to top: encoder layers on the left, // decoder layers on the right, each reading the encoded English in its cross-attention export default function ModelDiagram({ layers, heads, layer, head, language, onSelect }: Props) { const decTop = 40; // Decoder layers from the top one down let y = decTop + TITLE_H; const dec: { l: number; top: number; crossTop: number; selfTop: number; bottom: number }[] = []; for (let l = layers - 1; l >= 0; l--) { const crossTop = y + TITLE_H; const selfTop = crossTop + CROSS_H + ARROW_GAP; const bottom = selfTop + BLOCK_H + 8; dec.push({ l, top: y, crossTop, selfTop, bottom }); y = bottom + LAYER_GAP; } const decBottom = dec[dec.length - 1].bottom + 8; const inputY = decBottom + 34; // Room for the encoder's input label on two lines const H = inputY + 21; // Encoder layers from the bottom one up, sharing the decoder's bottom edge const enc = Array.from({ length: layers }, (_, l) => decBottom - 8 - ENC_H - l * (ENC_H + LAYER_GAP)); const encTop = enc[layers - 1] - TITLE_H; // The encoded English leaves the top encoder layer's right side const memoryY = enc[layers - 1] + ENC_H / 2; const crossYs = dec.map((d) => d.crossTop + BLOCK_H / 2); const busTop = Math.min(memoryY, ...crossYs); const busBottom = Math.max(memoryY, ...crossYs); return ( {/* Encoder */} {enc.map((top, l) => ( ))} {/* The encoded English, read by every cross-attention */} {/* Decoder */} {dec.map((d, i) => ( {`Layer ${d.l + 1}`} onSelect(d.l, h)} /> {/* Into this layer, from the layer below or the output so far */} ))} {/* Drawn after the decoder layers, so their boxes don't cover them */} {crossYs.map((cy, i) => ( ))} ); } function HeadChips({ top, layer, heads, head, onSelect }: { top: number; layer: number; heads: number; head: number | null | undefined; onSelect: (head: number | null) => void }) { const x = SUB_X + 6; const w = SUB_W - 12; const chipW = (w - (heads - 1) * CHIP_GAP) / heads; const rowY = top + CHIP_H + CHIP_GAP; return ( <> onSelect(null)} /> {Array.from({ length: heads }, (_, h) => ( onSelect(h)} /> ))} ); } // An SVG button: click, Enter or Space function Chip({ x, y, w, label, ariaLabel, selected, onPress }: { x: number; y: number; w: number; label: string; ariaLabel: string; selected: boolean; onPress: () => void }) { return ( { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); onPress(); } }} className="cursor-pointer outline-none [&:hover>rect]:fill-paper [&:focus-visible>rect]:stroke-ink [&:focus-visible>rect]:[stroke-dasharray:3_2]" > {label} ); } function Block({ y, h = BLOCK_H, label, selected = false }: { y: number; h?: number; label: string; selected?: boolean }) { return ( <> ); } function Group({ x, y, w, h, title }: { x: number; y: number; w: number; h: number; title: string }) { return ( {title} ); } function Label({ x, y, muted = false, children }: { x: number; y: number; muted?: boolean; children: string }) { return ( {children} ); } function Arrow({ d }: { d: string }) { return ; }