Spaces:
Running

attention / frontends /react /src /ModelDiagram.tsx
nhamid289's picture
Deploy elvis-code attention-architecture d17e79c
b869e0a verified
Raw History Blame Contribute Delete
7.98 kB
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 (
<svg viewBox={`0 0 ${W} ${H}`} className="w-full" role="group" aria-label="The translator's encoder and decoder layers">
<defs>
<marker id="model-arrow" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="6" markerHeight="6" orient="auto">
<path d="M 0 0 L 8 4 L 0 8 z" className="fill-ink-muted" />
</marker>
</defs>
{/* Encoder */}
<Group x={ENC_X} y={encTop} w={ENC_W} h={decBottom - encTop} title="Encoder" />
{enc.map((top, l) => (
<g key={l}>
<rect x={ENC_X + 8} y={top} width={ENC_W - 16} height={ENC_H} rx={4} strokeWidth={1.5} className="fill-surface stroke-grid" />
<Label x={ENC_CX} y={top + ENC_H / 2 + 4}>{`Layer ${l + 1}`}</Label>
<Arrow d={`M ${ENC_CX} ${l === 0 ? inputY - 14 : top + ENC_H + LAYER_GAP} V ${top + ENC_H}`} />
</g>
))}
<Label x={ENC_CX} y={inputY} muted>Full English</Label>
<Label x={ENC_CX} y={inputY + 13} muted>sentence</Label>
{/* The encoded English, read by every cross-attention */}
<line x1={ENC_X + ENC_W - 8} y1={memoryY} x2={BUS_X} y2={memoryY} strokeWidth={1.5} className="stroke-ink-muted" />
<line x1={BUS_X} y1={busTop} x2={BUS_X} y2={busBottom} strokeWidth={1.5} className="stroke-ink-muted" />
{/* Decoder */}
<Group x={DEC_X} y={decTop} w={DEC_W} h={decBottom - decTop} title="Decoder" />
{dec.map((d, i) => (
<g key={d.l}>
<rect x={LAYER_X} y={d.top} width={LAYER_W} height={d.bottom - d.top} rx={4} strokeWidth={1.5} className="fill-paper stroke-grid" />
<text x={LAYER_X + 8} y={d.top + 15} className="fill-ink-muted text-[11px]">{`Layer ${d.l + 1}`}</text>
<Block y={d.selfTop} label="Self-attention" />
<Arrow d={`M ${SUB_CX} ${d.selfTop} V ${d.crossTop + CROSS_H}`} />
<Block y={d.crossTop} h={CROSS_H} label="Cross-attention" selected={d.l === layer} />
<HeadChips
top={d.crossTop + BLOCK_H}
layer={d.l}
heads={heads}
// undefined: no chip selected, as another layer is
head={d.l === layer ? head : undefined}
onSelect={(h) => onSelect(d.l, h)}
/>
{/* Into this layer, from the layer below or the output so far */}
<Arrow d={`M ${SUB_CX} ${i === dec.length - 1 ? inputY - 14 : dec[i + 1].crossTop} V ${d.selfTop + BLOCK_H}`} />
</g>
))}
{/* Drawn after the decoder layers, so their boxes don't cover them */}
{crossYs.map((cy, i) => (
<Arrow key={i} d={`M ${BUS_X} ${cy} H ${SUB_X}`} />
))}
<Arrow d={`M ${SUB_CX} ${dec[0].crossTop} V 18`} />
<Label x={SUB_CX} y={12} muted>{`Next ${language} token`}</Label>
<Label x={SUB_CX} y={inputY} muted>{`${language} output so far`}</Label>
</svg>
);
}
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 (
<>
<Chip x={x} y={top} w={w} label="Mean of heads" ariaLabel={`Layer ${layer + 1}, mean of heads`} selected={head === null} onPress={() => onSelect(null)} />
{Array.from({ length: heads }, (_, h) => (
<Chip key={h} x={x + h * (chipW + CHIP_GAP)} y={rowY} w={chipW} label={`Head ${h + 1}`} ariaLabel={`Layer ${layer + 1}, head ${h + 1}`} selected={head === h} onPress={() => 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 (
<g
role="button"
tabIndex={0}
aria-label={ariaLabel}
aria-pressed={selected}
onClick={onPress}
onKeyDown={(e) => {
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]"
>
<rect x={x} y={y} width={w} height={CHIP_H} rx={3} strokeWidth={1.5} className={`fill-surface ${selected ? "stroke-ink" : "stroke-grid"}`} />
<text x={x + w / 2} y={y + CHIP_H / 2 + 3.5} textAnchor="middle" className="fill-ink text-[10px] font-medium">{label}</text>
</g>
);
}
function Block({ y, h = BLOCK_H, label, selected = false }: { y: number; h?: number; label: string; selected?: boolean }) {
return (
<>
<rect x={SUB_X} y={y} width={SUB_W} height={h} rx={4} strokeWidth={1.5} className={`fill-surface ${selected ? "stroke-ink" : "stroke-grid"}`} />
<Label x={SUB_CX} y={y + BLOCK_H / 2 + 4}>{label}</Label>
</>
);
}
function Group({ x, y, w, h, title }: { x: number; y: number; w: number; h: number; title: string }) {
return (
<g>
<rect x={x} y={y} width={w} height={h} rx={6} strokeWidth={1} strokeDasharray="4 3" fill="none" className="stroke-ink-muted" />
<text x={x + 8} y={y + 15} className="fill-ink text-[11px] font-semibold">{title}</text>
</g>
);
}
function Label({ x, y, muted = false, children }: { x: number; y: number; muted?: boolean; children: string }) {
return (
<text x={x} y={y} textAnchor="middle" className={`text-[11px] ${muted ? "fill-ink-muted" : "fill-ink"}`}>
{children}
</text>
);
}
function Arrow({ d }: { d: string }) {
return <path d={d} fill="none" strokeWidth={1.5} markerEnd="url(#model-arrow)" className="stroke-ink-muted" />;
}