KL Divergence
How to measure the difference between two probability distributions, and why it sits at the heart of modern LLM alignment.
When you fine-tune a large language model, how do you measure whether its output distribution is actually moving closer to the desired distribution? This question is a central to modern alignment techniques like RLHF [1, 2].
Large language models like GPTs output a probability distribution over tokens at each step. They assign a likelihood for each possible next word and then append the most likely next word to the output. During training the model outputs a predicted distribution of the most likely next words. For ensuring correct output, the distribution needs to be compared to the true distribution . This is exactly what KL-divergence does. KL-divergence takes the original distribution and compares it to a new generated distribution and outputs a value that tells how much the two distributions differ. The closer the value is to zero, the more similar the distributions are.
Theory
The Kullback-Leibler divergence from Q to P is defined as:
where and are the probabilities assigned by distributions and to outcome , and the sum is taken over all outcomes in the support of .
KL divergence can be interpreted intuitively using the concept of entropy form information theory.
The entropy of is
This is the average number of bits needed to encode samples from P using an optimal code for [3].
If instead if a code optimized for was used to encode samples that came form , the average code length is the cross-entropy:
Now combining entropy and cross entropy together, KL divergence is the extra cost you pay for using a code for to encode samples from :
Key properties
Asymmetric:
Always non-negative:
Equal to zero only when the two distributions are identical.
So intuitively, the KL-divergence asks how supriced you would be about the current distribution of the model given you know what the training data looks like. The more suprised you would be, the further the model is from the truth and thus the KL-divergence would be bigger.
Code demo
Now imagine a small vocabulary of five tokens: ["cat", "sat", "on", "the", "mat"]. We have an imaginary LLM that has a predicted distribution for the vocabulary and we also know the true distribution from the training data for the vocabulary.
=
# True distribution (training data)
=
# Model's predicted distribution
=
By plotting and the mismatch is immediately visible.
Show code
=
= 0.35
, =
Directly applying the discrete formula :
return
=
D_KL(P || Q) = 0.1874 nats
The KL-divergence is non-zero so there is a difference between the true distribution and the current distribution of our model. The difference between the true distribution and the predicted distribution is obvious.
The order of the distributions is not trivial when computing KL-divergence. This can be shown shown numerically:
=
D_KL(P || Q) = 0.1874 nats D_KL(Q || P) = 0.2147 nats Symmetric? False
Summary
KL-divergence can be used answering the question: how different are two probability distributions? The distribution is used, for example, LLM alignment to tell how far away the models predicted vocabulary distribution is from the training data distribution. KL-divergence has a mathematical foundation in information theory.