Transformer

Wiki chapters and blog posts about Transformer.

Wiki

From RNNs to Attention: Why the Transformer Appeared

An RNN squeezes history into one fixed-size state and computes it step by step, so gradients pass through a long chain of multiplications. Attention lets every position read every other position directly, in parallel. Experiments measure vanishing gradients, gating and the copy bottleneck.

Beginner 31 min read

Mixture of Experts (MoE): Routing, Load Balancing and Sparse Activation

A mixture-of-experts layer swaps the feed-forward network for many experts and routes each token to only k of them, so total parameters grow while per-token compute stays small. This page computes total and active parameters for Mixtral and DeepSeek-V3 and lets you try load balancing.

Intermediate 31 min read

Modern LLM Architecture: From GPT-2 to Llama, Qwen and DeepSeek

Almost every mainstream open LLM is a refined decoder-only Transformer: Pre-Norm with RMSNorm, a SwiGLU feed-forward network, RoPE positions and GQA attention, and some add mixture of experts (MoE) to grow parameters without growing compute in proportion.

Intermediate 15 min read

Positional Encoding Explained: Absolute, Relative and RoPE

Self-attention cannot see token order, so positional encodings tell the model where a token is or how far apart two tokens are: sinusoidal and learned encodings are added to the input, relative biases and ALiBi act on attention scores, and RoPE rotates queries and keys.

Intermediate 26 min read

Transformer Block Explained: Attention, FFN, Residuals and LayerNorm

A Transformer block is multi-head attention plus a feed-forward network (FFN), each wrapped in a residual connection and LayerNorm. This page gives the equations, Pre-Norm versus Post-Norm, a parameter count (about 12d²) and runnable PyTorch code.

Intermediate 25 min read

Mechanistic Interpretability: Superposition, Sparse Autoencoders and Circuits

Mechanistic interpretability reverse-engineers a trained network into a readable algorithm. Three CPU experiments cover superposition and sparse autoencoders for finding features, the logit lens and activation patching for locating causes, and why the choice of ablation changes the answer.

Advanced 46 min read

LLM Cheat Sheet: Estimating Parameters, Compute, Memory and Decoding Speed

One page for the estimates you reuse most: counting parameters, training compute (about 6ND), memory for weights, training state and the KV cache, the bandwidth bound on decoding speed, and the LoRA and pass@k formulas, each with a standard-library script checked against published numbers.

Intermediate 24 min read

Blog posts

mHC: Keeping a Wider Residual Stream Stable With Doubly Stochastic Mixing

mHC (Manifold-Constrained Hyper-Connections) is DeepSeek's fix for hyper-connections: Sinkhorn-Knopp turns the residual mixing matrix into a doubly stochastic one, keeping multi-layer gain bounded, for a reported 6.7% extra training time. This post reviews it and checks the math in code.

Paper notes arXiv 2025

Decision Transformer: Reinforcement Learning as Conditional Sequence Modeling

Decision Transformer (Chen et al., 2021) recasts offline RL as conditional sequence modeling: a causal GPT trained on return, state and action tokens, prompted with a target return. We recompute its tables, rerun its shortest-path toy, and show where return conditioning fails.

Paper notes NeurIPS 2021 2021

RoFormer: Writing Position into the Attention Dot Product with Rotations

RoFormer's rotary position embedding (RoPE) rotates queries and keys by position, so their dot product depends only on relative position, with no extra parameters. It later became standard in open LLMs such as LLaMA. We check its formulas, its mixed small-scale results and its properties in code.

Paper notes arXiv (later Neurocomputing 2024) 2021

ViT: An Image Is Worth 16x16 Words, Fed Straight to a Transformer

ViT drops convolutions: it cuts an image into fixed-size patches, projects each into a token, adds position embeddings and feeds a standard Transformer encoder. It trails ResNets on small data but, pre-trained on JFT-300M, reaches 88.55% on ImageNet with about a quarter of BiT-L's compute.

Paper notes ICLR 2021 2020

Topics