Tensorum

LLM Foundations

How large language models actually work, from tokens to next-token sampling.

AI EngineeringBeginner6 hrs4 key ideas · 5 topics
0%
your progress

Prompt

  1. Tokenize
  2. Embed
  3. Attend
  4. ×12 blocks
  5. Predict

01Tokenize

GPT-2 splits text with byte-pair encoding into pieces from a 50,257-token vocabulary. Common words are one token; rare words break into parts. Click a token to follow it below.

5 tokens24 characters4.8 chars / token

02Embed

Each token id looks up a 768-number vector; a position signal is added so the model knows word order. Showing 12 of 768 dimensions for ␣is.

Token embedding
+ Position
= Input vector

03Self-attention

Tokens look at the words that matter for meaning, like "is" looking at "capital" and "France".

What ␣is pays attention to
The1.5% capital37% of1.5% France56% is4.1%
5² = 25 token pairs per head× 12 heads × 12 layers = 3,600 scoresDouble the context → 4× the attention work

04Transformer block × 12

LayerNormMulti-head attention · 12 heads+ residualLayerNormMLP · 768 → 3,072 → 768+ residual

Attention moves information between tokens; the MLP transforms each token on its own. GPT-2 small stacks 12 of these blocks, then turns the last token's vector into one score (logit) per vocabulary entry.

05Predict & sample

pi = exp(zi/T) / Σ exp(zj/T). Low T sharpens toward the top choice; high T flattens. Top-k and top-p then drop the tail and renormalise. Top 10 of 50,257 candidates shown.

␣Paris19.194%
␣the16.46.3%
␣a15.2cut
␣located14.6cut
␣also14.1cut
␣not13.8cut
␣home13.5cut
␣known13.1cut
␣now12.9cut
␣one12.6cut

The capital of France is ▍

Token ids come from the real GPT-2 tokenizer. Attention heads and next-token scores are simplified, illustrative models of GPT-2 small, so the page works instantly with no model download. Inspired by Transformer Explainer ↗, which runs the full model in your browser.