Attention Basics: How a Transformer Reads Context
Understand Q/K/V, scaled dot-product attention and multi-head attention through intuition, a worked numeric example and a minimal code sample.
Wiki chapters and blog posts about Transformer.
Understand Q/K/V, scaled dot-product attention and multi-head attention through intuition, a worked numeric example and a minimal code sample.
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.
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.
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.
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.
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.
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.
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.
A large language model (LLM) is a neural network, usually a Transformer, trained on huge amounts of text to predict the next token; pre-training, post-training and inference decoding turn it into an assistant that can chat, write code and reason.
Attention Residuals (AttnRes) is Kimi's March 2026 fix for residual connections: it swaps the fixed weight-1 sum over earlier layers for softmax attention over their outputs. This post reviews the paper, recomputes its numbers and tests the idea on a small model.
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.
Differential Transformer computes two softmax attention maps and subtracts them to cancel shared noise. The paper reports improvements in language modeling, long-context retrieval and hallucination metrics.
BitNet b1.58 trains Transformer weights in {-1,0,1}, about 1.58 bits each, with 8-bit activations. It approaches LLaMA-quality results at 3B scale while targeting weight bandwidth and multiplication cost.
RetNet replaces softmax attention with multi-scale retention, supporting parallel training, recurrent decoding and chunkwise processing with fixed-size inference state.
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.
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.
The Switch Transformer simplifies mixture-of-experts routing to top-1, balances load with an expert capacity and an auxiliary loss, and pre-trains up to 7x faster than T5 at equal FLOPs per token. This page recomputes the paper's numbers and simulates capacity overflow.
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.
T5 casts translation, classification and summarization as text in, text out, trains one encoder-decoder with one loss, and compares architectures, objectives, datasets and scale. Denoising beats language modeling, corrupting spans is cheaper, and large clean C4 beats repeated small data.
BERT pre-trains a deep bidirectional Transformer encoder by restoring masked words, then adds one output layer per task and fine-tunes everything, setting records on 11 NLP tasks. It rechecks the paper's tables, parameter counts and training budget, and probes bert-base-uncased three ways.
The original Transformer paper: an encoder-decoder built only from multi-head self-attention and feed-forward layers, 28.4 BLEU on WMT 2014 English-German. We recompute its parameter counts, costs and BLEU claims, then train a tiny Transformer to copy digits.