Interactive Mechanism Deep Dive

How Transformers Work: Scaled Dot-Product & Multi-Head Attention

Unlike recurrent networks (RNNs) that process words one-by-one, transformers ingest entire sequences in parallel. Through Query-Key dot products and softmax normalizations, every token computes a custom contextual representation by attending directly to every other token.

Query Token: "it" [pos: 7]
Click any token to inspect its Query vectors and incoming attention weights: 10 tokens
Self-Attention Matrix A = softmax(QKT / √dk) Row: Query → Col: Key
Attention Distribution for "it" ∑ = 1.000

How much information this token extracts from each key token in the sequence for the current head:

The Core Architectural Innovations of Transformers

1. Parallel Processing vs. RNNs

Recurrent networks (RNNs/LSTMs) step sequentially through time steps t = 1, 2, ..., T, which prevents hardware parallelization and leads to vanishing gradients across long horizons. Transformers process all tokens simultaneously using matrix multiplications on GPUs/TPUs.

2. Scaled Dot-Product Attention

Why divide by √dk? For large dimensions, dot products grow large in magnitude, pushing the softmax function into regions with tiny gradients (vanishing gradient problem). Dividing by √dk preserves unit variance and smooth optimization.

3. Multi-Head Attention

A single attention head can only focus on one type of relationship at a time. Multi-head attention projects the sequence into h distinct subspaces (e.g. 8, 16, or 64 heads), allowing the network to simultaneously track syntactic dependencies, pronoun coreference, and lexical nuances.

How does pronoun coreference resolution work in "it"?

In the classic Winograd schema: "The animal didn't cross the street because it was too tired", the pronoun "it" refers to "animal". If you change the sentence to "because it was too wide", "it" refers to "street". Through training, the Query projection for "it" combined with the adjective ("tired" or "wide") produces a key matching the semantic attributes of animate creatures or spatial barriers respectively.

Why is the Residual Connection (Skip Connection) essential?

Modern transformers (GPT-4, Claude, LLaMA) stack between 32 and 128 transformer layers. The formula Output = LayerNorm(x + SubLayer(x)) ensures the residual stream acts as a high-bandwidth information highway. Attention heads write updates directly into this stream without overwriting earlier representations, enabling gradient backpropagation through hundreds of layers without degradation.

Enjoy this tool? Build your own with Super