Skip to main content

Overview

llama-perplexity is a tool for measuring the perplexity and other quality metrics of language models over text corpora. It’s primarily used to evaluate quantization quality loss and compare model performance.

What is Perplexity?

Perplexity measures how well a model predicts the next token:
  • Lower values = better prediction
  • Indicates model “surprise” at seeing the actual next token
  • Used to compare quantized models against FP16 baseline
  • Not directly comparable between different models or tokenizers
Perplexity is a technical metric for judging quantization quality, not end-user model quality. Finetunes may have higher perplexity but better human-rated outputs.

Quick Start

Basic Usage

Measure Perplexity

Outputs:
  • Progressive perplexity per chunk
  • Final mean perplexity ± uncertainty
  • Uncertainty calculated via Gaussian distribution assumption

Command-Line Options

string
Path to the GGUF model file.
string
Text file containing the test corpus.
integer
Context size for evaluation.
integer
Batch size for processing.
integer
Number of layers to offload to GPU.

Advanced Analysis: KL Divergence

Why KL Divergence?

Kullback-Leibler (KL) divergence measures how similar two probability distributions are:
  • KL = 0: Distributions are identical
  • Higher values: More difference between models
  • Used to compare quantized model to FP16 reference

Two-Step Process

1

Record FP16 baseline

First, record logits from the FP16 model:
The .kld file will be very large:
  • LLaMA 2: ~11 GiB
  • LLaMA 3: ~37 GiB
    (for Wikitext-2 test set)
2

Compare quantized model

Then compare the quantized model against the baseline:

KL Divergence Output

With --kl-divergence, you get comprehensive statistics:

Understanding Metrics

Perplexity Ratio

  • Ratio of quantized to FP16 perplexity
  • Closer to 1.0 = less quality loss
  • Values > 1.0 indicate degradation

Mean Δp (Change in Token Probability)

  • Average change in correct token probability
  • Positive: Model improved (rare)
  • Negative: Model degraded
  • Close to 0%: Minimal impact

RMS Δp (Root Mean Square Change)

Think of this as “noise level” from quantization:
  • Lower is better
  • Indicates overall distribution shift
  • Related to Gaussian noise assumption

Same Top p

  • Percentage of time both models agree on the most likely token
  • Higher is better
  • Practical indicator of consistency

Percentile Analysis

The tool also reports change in token probability at various percentiles:
  • Symmetric distribution: Quantization adds random noise
  • Asymmetric (more negative): Actual quality degradation
  • Helps distinguish noise from systematic errors

Benchmarking Quantizations

Standard Test Setup

Llama.cpp contributors use this standard:
  1. Dataset: Wikitext-2 test set
  2. Baseline: FP16 model
  3. Method: KL divergence comparison

Example: Compare Q4 Quantizations

Interpreting Results

Lower KLD and higher “Same top p” indicate better quantization:
These are example numbers. Actual results vary by model architecture and content.

Importance Matrices

Some quantizations support importance matrices for better quality:
Importance matrices can significantly improve quality for some quantization types, especially at lower bit counts.

Example: LLaMA 3 8B Results

From the official llama.cpp benchmarks:

Practical Guidelines

1

Choose baseline

Use FP16 or BF16 as your reference model.
2

Record logits

Create the baseline .kld file once:
3

Test quantizations

Compare each quantized version:
4

Compare results

Look at:
  • KL divergence: Overall distribution similarity
  • Same top p: Practical consistency
  • Mean Δp: Average quality change
  • Percentiles: Noise vs degradation

Limitations & Notes

Important Limitations
  • Perplexity is not comparable between different models
  • Different tokenizers produce different perplexity values
  • Finetunes often have higher perplexity but better quality
  • Results are implementation-specific (llama.cpp vs other frameworks)
  • Use the same test set for all comparisons

Performance Considerations

Perplexity calculation can be slow:

See Also