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.
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 ). Byte-Pair Encoding (BPE) merges frequent character pairs into morphemic subwords:
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).
Tokens on the 2D Semantic Plane & Cosine Angular Geometry
Each token ID is projected into a dense continuous embedding space . 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 :
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:
Scaled Dot-Product & Multi-Head Projections
Queries (), Keys (), and Values () are computed via learned projections. Scaling by prevents softmax gradients from vanishing for large vector dimensions:
Scaled Dot-Product Attention
Scrollable →Multi-Head Projections ()
Scrollable →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 up to via with non-linear activation (ReLU or GELU), and contracts back down to via :
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.
Residual Connections & Layer Normalization Gradient Highways
Without residual skip connections, gradients vanish exponentially in deep architectures. By formulating each sub-layer as , the gradient derivative contains an unbroken identity term :
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:
Encoder Multi-Head Attention
All tokens in the source sequence attend to all other tokens bidirectionally. Every token integrates global context from across the entire prompt simultaneously.
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 time. The KV Cache eliminates redundant projections by storing past Key and Value matrices in GPU VRAM, turning each decoding step into an operation:
Click any cell in the 5×5 attention matrix to inspect how Query token takes the dot-product with Key token , scales by (), and produces the final softmax attention weight:
Token "network" directs 26.4% of its attention context towards "neural".
Final Linear Projection, Softmax Probabilities & Temperature
The top decoder output vector is projected by a final linear weight matrix into raw vocabulary logits . A calibrated softmax with Temperature () converts logits into normalized probability distribution :
