Every LLM you use runs on one core equation: (\text{softmax}(QK^T / \sqrt{d_k})V).
ChatGPT, Claude, Gemini, and Llama all share the same underlying transformer architecture. Strip away the chat interface, and you’re left with a pipeline of straightforward linear algebra repeated dozens of times.
Here is how a prompt becomes a prediction, step by step:
- Tokens to Vectors (Embeddings) • Text is broken into subword tokens, and each token ID pulls a corresponding row from a learned embedding matrix. • Raw text like "The model learns" is converted into dense numerical vectors the network can process.
- Positional Encoding • Self-attention processes all tokens simultaneously, meaning it has no built-in sense of word order. • Position signals—originally sine and cosine waves, and now commonly rotary embeddings (RoPE)—are injected into the vectors so "dog bites man" differs from "man bites dog." • Input = Token Embedding + Position Signal
- Self-Attention • Each token vector is projected into three roles: a Query (Q) (what I'm looking for), a Key (K) (what I contain), and a Value (V) (what I pass along). • Score: Multiply (QK^{T}) to measure how relevant every token is to every other token. • Scale & Normalize: Divide by (\sqrt{d_{k}}) to prevent gradients from exploding, then apply softmax to turn scores into attention weights. • Mix: Multiply by V to blend token values into context-aware representations.
- Multi-Head Attention • Instead of running attention once, the model splits the vectors across multiple parallel "heads." • Each head learns to track different relationships—one might follow grammar, while another tracks coreferences or tone. • Their outputs are concatenated and projected back to the model's standard dimension.
- Feed-Forward Network (FFN) • Each token passes independently through a small two-layer neural network with a non-linear activation (like GELU or SwiGLU). • The division of labor: Attention routes information between tokens; the FFN processes and transforms information within each token.
- The Transformer Block • Attention and the FFN are wrapped together with residual connections (skip connections that preserve gradient flow) and layer normalization (to keep activations stable). • Stack this identical block N times—often 32, 80, or over 100 layers deep—to build increasingly abstract representations.
- Next-Token Prediction • The final vector for the last token is projected across the model's entire vocabulary (often 50,000+ tokens) to produce raw scores (logits). • A final softmax converts those logits into a probability distribution—and the model samples the next token. The Takeaway Transformers don’t rely on one exotic mathematical trick. Their capability comes from simple primitives—matrix lookups, dot products, softmax, and small MLPs—stacked deep and scaled across massive datasets.
Top comments (0)