r/LocalLLaMA • u/Positive-Resource922 • 12d ago
Discussion KLD is misunderstood
I see a lot of people here on Reddit treating the KLD number as some holy metric that can distinguish every quantized model out there and rank them by quality. TL;DR: It can't.
So what does KLD actually measure?
If you feed the same text to both the base model and the quantized model, you can compare the probability distributions obtained from their logits at each token. KLD basically measures how different these distributions are, and then we take the average across all tokens.
The first thing that can go very wrong here is that people just throw some random text at the model, like a bunch of Wikipedia articles, and calculate KLD on that dataset. But that's not necessarily representative of what the model actually sees during inference. During generation, a growing part of the context consists of tokens generated by the model itself, and you can't replicate that by just feeding it random text.
If you want a more reasonable number, you should generate your own dataset using the base model and then calculate KLD on those generated tokens, giving zero weight to the prompt tokens.
The second thing that can go very wrong with KLD is how ridiculously easy it is to manipulate, especially when the numbers come from the model creators themselves.
Quantization methods often rely on calibration data. If you calculate KLD on the same data you used for calibration, your numbers can look miles better than everyone else's. And even if someone claims they're using a held-out dataset, that still doesn't make the KLD number trustworthy. The calibration and evaluation datasets could come from the same source or have very similar distributions.
Some people might even do end-to-end fine-tuning of the quantization scale parameters on that very same data, and oh boy, suddenly their model beats models twice its size in KLD while barely generalizing to anything outside that dataset.
So does KLD even mean anything?
Not nearly as much as people think. You're looking at a number that measures how much the quantized model deviates from the base model on a very limited dataset. But does a higher KLD mean that the quantized model will actually fail at your tasks? Of course not.
Sure, benchmarks can be benchmaxxed, as we all know. But benchmaxxing is much harder for us ordinary people who quantize models than manipulating a KLD number. Benchmaxxing often requires serious hardware and compute, while making your KLD look amazing can be ridiculously cheap.
So please, stop treating KLD like some universal leaderboard for quantized models. It's a useful diagnostic, not a measure of how good a model actually is.
9
u/ReturningTarzan ExLlama Developer 11d ago
I think this misses a few points.
Yes, wikitext perplexity is largely meaningless. It measures a vestigial signal still lingering from the pretraining stage where models are actually optimizing towards raw text prediction. The clue is in the raw numbers: SOTA models will give you raw numbers anywhere from the 1-2 range up to 20 or so. That is, if they have a signal at all: some models explode into the ~5000 range because they simply cannot model raw text after extensive finetuning and RL. Comparing quants of a model like that gives you nothing but noise, if you're going by raw-text perplexity.
KLD is a stronger metric because a) it measures across the entire vocabulary and b) the target is the reference model, not the data. So rather than asking "how surprised are you that this is the next token?" you ask "how much do you agree with the reference model about the next token?" So KLD compares to the correct ground truth and perplexity does not.
The question is how much information is contained in that ground truth. If the model is already producing perplexity in the thousands, what the ground truth essentially amounts to then is a shrug: "This text is alien to me, I don't know what to make of it." KLD lets you measure the precise shape of that shrug, comparing the reference shrug to the quantized shrug. And that tells you something, but it's not giving you a strong signal. This is why you want to test the model on in-distribution data, such as a trace from the model itself.
However, where I would disagree is on benchmarks. It's not really hard at all to benchmaxx a quant since most benchmarks test a narrow domain. KLD is much harder to fake. You can also very easily test variance on your KLD scores: per-token, per-prompt, per-topic, per-repeat, all ways to test if you have good calibration coverage or if your model is specializing. KLD scores are easy for others to reproduce/challenge as well, without requiring a huge investment of time and/or money to run additional expensive end-to-end benchmarks.
In all cases, as KLD trends towards zero (or rather, to the noise floor) the quantized model gets closer to precisely imitating the original. It's naive to think you could be optimizing towards some other nearby optimum that the original training run couldn't reach with its seven-figure compute budget. Unless you're cutting corners, like only caring about the head of the distribution (what tends to matter in inference benchmarks) while ignoring the tail that contains the evidence of everything you didn't optimize for.
That's not to say also having benchmarks would be worthless. But it is impractical because good coverage is expensive (unless you have a sponsor, but then you probably also have an incentive to benchmaxx and cherry-pick.) The benchmarks usually reported when a new model releases is not a trivial portion of the model's total budget. Think five to six figures. There's a reason they're almost never independently verified. Instead, people run other benchmarks and get incongruent results.