real-attention -- finding GPT-2's heads (real weights)

Inside a language model, each word looks at the other words and works out which of them matter to it. GPT-2 does that 144 separate times on every pass, and this page runs a real GPT-2 inside your browser so you can go hunting through all of them on a sentence you type. Some do a job you can recognise: one looks only at the word immediately before, and one spots a phrase that appeared earlier and points at whatever followed it last time. Those two are heads researchers have named; a third that parks on the very first word is this page's own measurement on this page's own sentence, not a named result. The real weights are about 550 MB and download only when you click — until then you get a labelled stand-in.

Interactive page: the real counterpart to the synthetic attention pages (attention-patterns, scaled-dot-attention). It runs a real GPT-2 (124M) in your browser and lets you hunt its attention heads on any sentence you type. Anchor: A2 attention. One of the real-model grounding pages: it runs a real trained model instead of synthetic weights.

Why a hand-written forward pass

transformers.js cannot emit attention weights — the ONNX export drops the attention probabilities. The default OnnxConfig common-outputs set has no attentions, so a stock export — including the Xenova/* models transformers.js consumes — has none, and asking for them throws at InferenceSession init with "Graph output (attentions) does not exist in the graph". (Optimum #325 is the reference, but note it was closed in 2022 with a custom-OnnxConfig workaround, not a change to the default export — this page used to cite it as a live blocker.) So this page does NOT use transformers.js for inference. Instead gpt2.js:

  1. fetches the raw GPT-2 safetensors weights (HF, CORS-enabled) and parses them in vanilla JS,
  2. runs a minimal GPT-2 forward pass (token+pos embed → 12 blocks of LayerNorm + Conv1D QKV + causal attention + Conv1D MLP with gelu_new), capturing A[layer][head] = softmax(QKᵀ/√d + causal mask) at every layer.

transformers.js is still used — but only for the small tokenizer (real GPT-2 BPE), not the model.

Verified against PyTorch

"Real" has to mean real, so the JS forward is checked against PyTorch GPT-2 (output_attentions, eager) by gpt2.test.mjs over a committed fixture (gpt2-groundtruth.fixture.json):

Run it (548 MB weights not committed): GPT2_WEIGHTS=~/.cache/huggingface/hub/models--gpt2/snapshots/*/model.safetensors node real-attention/gpt2.test.mjs

WebGPU compute backend (optional)

The same forward also runs on the GPU: gpt2-webgpu.js expresses each step (Conv1D matmul, LayerNorm, causal attention, gelu_new) as a WGSL compute shader, keeps activations resident on-device across the 12 layers, and reads back only the attention matrices. The embedding gather stays on the CPU so the 154 MB wte never has to go to the GPU. Toggle “WebGPU compute” in the panel; the page picks WebGPU automatically when the browser exposes it.

It is self-verifying. On the first GPU run the page also runs the CPU forward and reports max|Δ| over every attention weight in the banner; on any mismatch (a shader bug, a flaky adapter) it falls back to the CPU path automatically. Both forward timings are shown so the GPU speedup is visible. gpt2-webgpu.test.mjs runs that GPU-vs-CPU check on a tiny random model under any WebGPU runtime (e.g. Deno) and skips cleanly where navigator.gpu is absent.

Verification: the WGSL is author-verified — gpt2-webgpu.test.mjs run under Deno (WebGPU via radv on an AMD Radeon 610M) reproduces the CPU forward within worst attn |Δ| = 5.96e-7. Plus the in-page self-check on a real browser, with the PyTorch-pinned CPU forward (gpt2.test.mjs) as the ground-truth and the automatic fallback. (Headless chromium here doesn't expose navigator.gpu, a host with a broken headless Intel GPU wedges Deno's Vulkan adapter init — use a clean AMD/radv host for the Deno run.)

What it shows

Type a sentence (the default the cat sat on the mat. the cat ran repeats "the cat" so an induction head fires). Two linked views:

Render tier

T2 (Canvas2D heatmap + head-map; the "compute" tier is the GPT-2 forward in JS).

Wiring

layout.mount + controls.text('sentence') + layer/head steppers + a "jump to induction head" button + a "WebGPU compute" toggle + "load real GPT-2". The forward runs on the CPU (gpt2.js) or the GPU (gpt2-webgpu.js); it is async degrades to a labelled idealized synthetic stand-in offline (the three head shapes, hand-built on the default sentence) — never blank, headless-verifiable. A 25 s timeout guards the tokenizer import. Two challenges (?ch=N): load the real model, and find an induction head (score ≥ 0.30). Headless hooks: ?ids= (inject exact token ids, skip the tokenizer — the real-forward verification path), ?weights= (override the safetensors URL, e.g. a local file://), ?layer=/?head=, ?real=0 (synthetic only), ?hover=x,y. Source: page.js, gpt2.js.