Interactive Diagnostic Essay · Topic 16

The Transformer: Attention Is All You Need

A complete first-principles architectural breakdown: from subword Byte-Pair Encoding and 2D semantic token spaces, to sinusoidal positional encoding, two-layer feed-forward expansion, residual highways, 6-layer dual towers, and final softmax temperature sampling.

Read Time: 16 min•Vaswani et al. (NeurIPS 2017)•AIS Research Cohort
01 / Tokenization & Vocabulary

Byte-Pair Encoding (BPE) & Subword Representation

Transformers cannot ingest raw characters or arbitrary-length string words directly. Word-level vocabularies suffer from out-of-vocabulary (OOV) tokens, while character-level tokenization inflates sequence length by 10× (and self-attention scales quadratically O(N2)O(N^2)). Byte-Pair Encoding (BPE) merges frequent character pairs into morphemic subwords:

SIMPLIFIED SUBWORD TOKENIZER
0 TOKENS EXTRACTED

Simplified demo: words longer than 7 characters are split after the 5th character and IDs are hashed into a 37,000-entry range. A real BPE tokenizer learns its merges from corpus statistics (the original paper used a shared ~37k-token BPE vocabulary for English–German).

02 / Vector Geometry

Tokens on the 2D Semantic Plane & Cosine Angular Geometry

Each token ID is projected into a dense continuous embedding space dmodel=512d_{model} = 512. High-dimensional embeddings organize semantic meaning geometrically: tokens with similar contextual roles cluster together. On the 2D semantic plane below, observe semantic cluster hulls, select token pairs, and inspect vector alignment cos⁡(θ)=u⋅v∥u∥∥v∥\cos(\theta) = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\| \|\mathbf{v}\|}:

2D SEMANTIC PLANE SIMULATOR · R512→R2\mathbb{R}^{512} \to \mathbb{R}^2
INTERACTIVE 2D VECTOR SPACE · EMBEDDING GEOMETRY
2D Semantic Plane Coordinate System+X (Syntactic)+Y (Semantic)(0,0)ML ARCHITECTUREANALOGY / ROYALTYGRAMMAR / SEQUENCEθ (1.00)attentionallyouneedtransformerkingqueenmanwomanneuralsequencemodel
VECTOR A:
VECTOR B:
COSINE SIMILARITY: 0.999u⋅v∥u∥∥v∥\frac{u \cdot v}{\|u\| \|v\|}
03 / Harmonic Geometry

Sinusoidal Positional Encoding & Frequency Harmonics

Because self-attention operates across all positions in parallel with zero recurrence, it has no built-in notion of order: it is permutation-equivariant, so shuffling the input words simply shuffles the outputs the same way. Vaswani et al. solved this by adding sine and cosine waves of different frequencies directly into the embedding vectors:

PE(pos,2i)=sin⁡(pos100002i/dmodel),PE(pos,2i+1)=cos⁡(pos100002i/dmodel)PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d_{model}}}\right), \quad PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{model}}}\right)
HARMONIC SINE (CHARCOAL) & COSINE (TERRACOTTA) SPECTRUM
POSITIONS:20 POS
Dimension pairs 2i = 0, 32, 64, 128 (darker = lower dimension = higher frequency)Wavelengths grow from 2π (2i = 0) to ≈10,000·2π (2i = 510)
04 / Attention Mechanics

Scaled Dot-Product & Multi-Head Projections

Queries (QQ), Keys (KK), and Values (VV) are computed via learned projections. Scaling by 1/dk1/\sqrt{d_k} prevents softmax gradients from vanishing for large vector dimensions:

Scaled Dot-Product Attention

Scrollable →
Attention(Q,K,V)=softmax(QKTdk)V\mathrm{Attention}(Q, K, V) = \mathrm{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

Multi-Head Projections (h=8h=8)

Scrollable →
MultiHead(Q,K,V)=Concat(head1,…,headh)WO\mathrm{MultiHead}(Q, K, V) = \mathrm{Concat}(\mathrm{head}_1, \dots, \mathrm{head}_h)W^O
INTERACTIVE COREFERENCE ATTENTION RESOLUTION
The:
2.0%
animal:
76.0%
didn't:
1.0%
cross:
3.0%
the:
1.0%
street:
4.0%
because:
2.0%
it:
5.0%
was:
2.0%
too:
1.0%
tired:
3.0%
05 / Deep Layers

Two-Layer Position-Wise Feed-Forward Networks as Associative Memories

While self-attention connects different tokens across sequence space, the Feed-Forward Network (FFN) processes each token independently. It projects the embedding dimension 4× from dmodel=512d_{model} = 512 up to dff=2048d_{ff} = 2048 via W1W_1 with non-linear activation (ReLU or GELU), and contracts back down to dmodel=512d_{model} = 512 via W2W_2:

FFN(x)=max⁡(0,xW1+b1)W2+b2=ReLU(xW1+b1)W2+b2\mathrm{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2 = \mathrm{ReLU}(xW_1 + b_1)W_2 + b_2
TWO-LAYER NEURAL ARCHITECTURE · W₁ EXPANSION & W₂ PROJECTION
2/3 OF MODEL WEIGHTS LIVE IN FFN
ACTIVATION:
INPUT MAGNITUDE (x):1.4x
Two-Layer Feed Forward Neural Network ArchitectureINPUT VECTOR x (d=512)EXPANDED HIDDEN LAYER (d_ff = 2048) · 4×PROJECTED OUTPUT (d=512)W₁ ∈ ℝ^(512 × 2048) + b₁W₂ ∈ ℝ^(2048 × 512) + b₂x1x2x3x4x50000011100010y1y2y3y4y5
DIMENSIONAL PROJECTION512 → 2048 → 512 (4×)
ACTIVE NEURONS (FIRING)460 / 2048 Neurons Active
RELU SPARSITY77.5% output exactly 0

Toy layer: a scalar input x feeds 2,048 hidden neurons with fixed random weights and slightly negative biases. ReLU zeroes every negative pre-activation exactly; GELU lets small negative values through, so it never produces exact zeros. The 13 drawn nodes are the first 13 of the 2,048.

06 / Stability & Backprop

Residual Connections & Layer Normalization Gradient Highways

Without residual skip connections, gradients vanish exponentially in deep architectures. By formulating each sub-layer as x+Sublayer(x)x + \mathrm{Sublayer}(x), the gradient derivative contains an unbroken identity term ∂L∂x=∂L∂y⋅(1+∂Sublayer∂x)\frac{\partial L}{\partial x} = \frac{\partial L}{\partial y} \cdot \left(1 + \frac{\partial \mathrm{Sublayer}}{\partial x}\right):

Output=LayerNorm(x+Sublayer(x))=[(x+Sublayer(x))−μσ2+ϵ]⊙γ+β\mathrm{Output} = \mathrm{LayerNorm}\left(x + \mathrm{Sublayer}(x)\right) = \left[\frac{(x + \mathrm{Sublayer}(x)) - \mu}{\sqrt{\sigma^2 + \epsilon}}\right] \odot \gamma + \beta
SIGNAL RETENTION THROUGH 6 DEEP LAYERS
LAYER 1:
100%
LAYER 2:
99.2%
LAYER 3:
98.4%
LAYER 4:
97.6%
LAYER 5:
96.8%
LAYER 6:
96%
✓ Gradient Highway Active: the identity skip path keeps the signal close to full strength across all 6 layers.
07 / Canonical Paper Architecture

The Transformer: Vaswani et al. (2017) Dual-Tower Blueprint

The authentic architecture from “Attention Is All You Need” (Vaswani et al., 2017) consists of an Encoder stack that creates rich contextual embeddings and an Autoregressive Decoder stack that consumes target prefix tokens and cross-attends into the encoder states:

Vaswani et al. (2017) Canonical Transformer ArchitectureOutput ProbabilitiesSoftmaxLinear ProjectionENCODER (Nx = 6)Add & NormFeed ForwardAdd & NormMulti-HeadAttention+Pos. EncodingInput EmbeddingInputsK, VDECODER (Nx = 6)Add & NormFeed ForwardAdd & NormMulti-HeadCross AttentionAdd & NormMasked Multi-HeadAttention+Pos. EncodingOutput EmbeddingOutputs (shifted right)
INTERACTIVE LAYER INSPECTOR
encoder TOWERX_enc ∈ ℝ^{N × 512} (h=8, d_k=64)

Encoder Multi-Head Attention

MultiHead(Q,K,V)=Concat(head1,…,headh)WO\mathrm{MultiHead}(Q, K, V) = \mathrm{Concat}(\mathrm{head}_1, \dots, \mathrm{head}_h)W^O

All tokens in the source sequence attend to all other tokens bidirectionally. Every token integrates global context from across the entire prompt simultaneously.

Hyperparameters (Base Vaswani Model)
• Layers: N = 6
• d_model: 512
• Heads: h = 8
• d_k = d_v: 64
• d_ff: 2048
• Vocab: 37,000
08 / Generation Dynamics & Memory

Attention Matrix, Causal Masking & Incremental KV Cache

In autoregressive generation, each token attends only to prior positions. Naive inference re-computes all previous representations at each step in O(N2)O(N^2) time. The KV Cache eliminates redundant projections by storing past Key and Value matrices in GPU VRAM, turning each decoding step into an O(N)O(N) operation:

AUTOREGRESSIVE GENERATION SEQUENCESTEP 3 OF 6
Context Window (Input to Decoder)
<BOS>Attentionis? → Next Token
Top Softmax Predictions
“all”97%
“a”2%
“the”1%
ATTENTION MATRIX, CAUSAL MASK & KV CACHE STUDIO

Click any cell (i,j)(i, j) in the 5×5 attention matrix to inspect how Query token qiq_i takes the dot-product with Key token kjk_j, scales by 1/dk1/\sqrt{d_k} (1/64=1/81/\sqrt{64} = 1/8), and produces the final softmax attention weight:

Q \ K
The
neural
network
learns
fast
The
neural
network
learns
fast
CELL (2, 1) BREAKDOWNACTIVE ATTENTION
Query Token (i):“network” (row 2)
Key Token (j):“neural” (col 1)
Raw Dot Product qi⋅kjq_i \cdot k_j:28.0
Scale Factor dk=64\sqrt{d_k} = \sqrt{64}:8.0
Scaled Score Sij/dkS_{ij} / \sqrt{d_k}:3.50
Softmax Weight AijA_{ij}:26.37%

Token "network" directs 26.4% of its attention context towards "neural".

09 / Output Head

Final Linear Projection, Softmax Probabilities & Temperature

The top decoder output vector hfinal∈Rdmodelh_{final} \in \mathbb{R}^{d_{model}} is projected by a final linear weight matrix Wv∈Rdmodel×∣V∣W_v \in \mathbb{R}^{d_{model} \times |V|} into raw vocabulary logits z∈R∣V∣z \in \mathbb{R}^{|V|}. A calibrated softmax with Temperature (TT) converts logits into normalized probability distribution P(wi)P(w_i):

P(wi)=exp⁡(zi/T)∑j=1∣V∣exp⁡(zj/T)P(w_i) = \frac{\exp\left(z_i / T\right)}{\sum_{j=1}^{|V|} \exp\left(z_j / T\right)}
INTERACTIVE SOFTMAX TEMPERATURE & TOP-K STUDIO
TEMPERATURE (T):0.80
0.1 (Greedy / Deterministic)1.0 (Standard)2.0 (High Entropy / Creative)
CANDIDATE TOKEN (LOGIT z_i)SOFTMAX PROBABILITY P(w_i)
“transduction”(z=6.8)
75.6%
“modeling”(z=5.4)
13.1%
“generation”(z=4.9)
7.0%
“translation”(z=4.2)
2.9%
“synthesis”(z=3.1)
0.7%
“learning”(z=2.4)
0.3%
“prediction”(z=1.8)
0.1%
“recognition”(z=0.9)
0.0%
💡 Notice: Lowering temperature to T=0.2 collapses entropy onto the highest logit (“transduction”), while raising to T=1.8 flattens the distribution across all candidates.

Primary Reference: Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention Is All You Need. Advances in Neural Information Processing Systems (NeurIPS 2017).

AI Society — Bennett University Research Curriculum Archive.