The transformer is a learned probability model
A joint probability over sequences is impossible to store. The chain rule turns it into one function you can learn.
Helloo,
Transformer is the model architecture used by Large Language Models. It was introduced by the Google Brain team in 2017 in the famous research paper — “Attention is all you need”.
The latest trillion parameter models trained by frontier labs, that feel like magic, out of a science fiction book, are scaled up versions of this architecture. There have been a few tweaks to the architecture over the years but the core is still the same.
In this blog we’ll go over high level Math behind the transformer architecture. Let’s Go!
Transformers are from the class of autoregressive models. Which means you generate tokens one by one. Each new token depends on the previous tokens.
The first step is, to convert raw input text into tokens. This step is called Tokenization. It is the process of breaking down Raw Text into chunks.
There are multiple ways to build the Tokenizer algorithm. It could be as simple as tokenizing on characters, tokenizing on whitespaces (words), but currently the most widely used technique is BPE Tokenizer. Check out Andrej Karpathy’s video on Tokenization for a deep dive. We are moving forward, as this step is comparatively trivial and we have a lot to cover.
The token is the smallest unit a model sees. Tokens have a fixed vocabulary size . Current state of the art models (GPT-5, Opus) have a vocab size of 100K-200K tokens.
When we say training a model, it means learning the underlying probability distribution of the sequence of tokens. This sequence of tokens can be anything — code, essays, images, music…
And then inference simply becomes sampling from this learned probability distribution given a sequence of input tokens.
The question is how do we make the model learn this probability distribution?
If is the real world probability distribution of human generated text. We have to learn a model that approximates human text generation. We’ll call this
If we have 3 human generated sentences —
- the cat sat on the mat
- the cat sat zx oj squid
- Paris is the capital of France
In real world the probability of seeing a sentence like 2 is near zero, no human will write like that. So will give it near 0 prob. and we expect to do the same.
can be defined as
where is the joint probability over the sequence of input tokens of length . What we’re saying is — I want to know the probability of seeing a particular sequence . On each position I have options, so for the sequence of length the number of options to choose from becomes
The naive way to learn this joint probability is:
- Build a giant lookup table: one row for every possible sequence of length ,
which is rows. Where we store the occurrence of each sequence, count how
often each appears and normalize to get probabilities. This approach is not
feasible.
- Even with a moderate size of vocab and just sequence length. Since we can have 50000 choices at each position the size of the table becomes which is larger than the number of atoms in the universe. And the sequence length is nothing compared to real world sequences.
- Second, if this model sees a sequence it has not seen before it will assign it a score of zero. This table will be incredibly sparse.
We tackle this problem by using the chain rule.
The chain rule states that the joint probability of a sequence of events can be expressed as the product of conditional probabilities of each event, given the events that came before it.
Mathematically, it can be expressed as:
This is amazing. The joint probability , which was impossible to compute, now breaks into smaller problems that all look exactly the same — given the tokens so far, what is the next token?
Each of these smaller problems is a function. It takes the previous sequence as input, and outputs a probability distribution over the next token. That distribution is always a vector of size , one probability for every token in the vocabulary, no matter how long the input sequence is. And we don’t memorize a lookup anymore — we learn the weights of this one function, and the same weights serve every position and every sequence length.
Let’s call this function , where are the learned weights.
And we can stack this up. gives us a distribution over the next token, we pick a token from it, append it to the sequence, and call again on the longer sequence to get the token after that. Keep going till we reach the end of the sequence. Multiply the ’s along the way and we get back the probability of the whole sequence.
The transformer is this function — given an input sequence, it predicts the probability distribution of the next token. Training a transformer means learning the weights .
One small note on notation before we go further. To keep every step uniform we add a special start token at the front of the sequence. This way even the very first token is a conditional — is really — and every step looks the same.
Let’s see how this unfolds.
So after converting, raw text —> tokens
Each token is represented by a numerical id in a fixed vocabulary. Each token is depicted by a vector of raw numbers called embedding. The embedding encodes the semantic meaning of the token in a numerical format. You can imagine it being like a dictionary of token_id and embedding vector —
We’ll denote embedding vectors by
The tokens are converted into embeddings, which we get by just a lookup in the embedding matrix. The embedding matrix is learned at training time and fixed during inference.
Here is what each symbol in the diagram means —
— the token at position . The input sequence is
— the embedding vector of . This is what actually goes into the transformer, the transformer never sees the raw token id.
— the output at position . Before we can say what’s in it, we need a name for the tokens in the vocabulary. The vocabulary is a fixed list of tokens, we’ll call them . Now, is a vector of raw scores, one for each vocabulary token. The -th entry is a score for how likely is to be the next token , given everything that came before it, . Higher score means more likely. These raw scores are called logits.
There is one vector at every position. sits above and scores what comes after . sits above and scores what comes after . And so on. That’s why in the diagram the ’s start at 0 and the ’s start at 1 — is always the prediction for , computed from everything before .
We view the whole mapping from ’s to ’s as a black box in this section and call it a Transformer.
We call the transformer a function with parameters . Given the input tokens it produces the logit vector .
only depends upon
Logits are not probabilities yet — they can be negative, and they don’t sum to 1. To turn them into a probability distribution we do a softmax over them.
It looks something like this —
is the transformer, is the distribution we set out to learn. The only gap between them is the softmax — gives us the raw scores, gives us probabilities.
So the output vector has one entry per vocabulary token, where is the probability of seeing as the next token when given the input sequence and so on and so forth.
Autoregressive Inference§
Given an autoregressive transformer we sample tokens from it sequentially, and the next token generated is used as an input for the next generation phase.
Each generated token is used as the input to the model when generating the following tokens. This is the high level math of a transformer. Keeping it as a black box.
In Part 2, we will look under the hood of a transformer and see how we get the logit vector and how each step works.