Perplexity and Bits-Per-Byte in Large Language Models: Mathematical Foundations, Tokenizer Dependency, and Compression Benchmarks

Perplexity and Bits-Per-Byte in Large Language Models: Mathematical Foundations, Tokenizer Dependency, and Compression Benchmarks Evaluating autoregressive large language models requires metrics that quantify predictive confidence across text sequences. The standard objective during pre-training is the minimization of empirical risk under a cross-entropy loss function. Exponentiating this loss yields Perplexity (PPL), a foundational metric dating back to statistical speech recognition and n-gra

9 min
Perplexity and Bits-Per-Byte in Large Language Models: Mathematical Foundations, Tokenizer Dependency, and Compression Benchmarks

Perplexity and Bits-Per-Byte in Large Language Models: Mathematical Foundations, Tokenizer Dependency, and Compression Benchmarks

Evaluating autoregressive large language models requires metrics that quantify predictive confidence across text sequences. The standard objective during pre-training is the minimization of empirical risk under a cross-entropy loss function. Exponentiating this loss yields Perplexity (PPL), a foundational metric dating back to statistical speech recognition and n-gram language models (Jelinek et al., 1977).

Despite its ubiquity, raw token-level perplexity is frequently misunderstood and misapplied in comparative evaluations. Because token-level perplexity depends directly on the sequence segmentation produced by a model's tokenizer, raw perplexity scores across models with different vocabulary sizes (such as Llama 2's 32,000 tokens versus Llama 3's 128,000 tokens or Gemma's 256,000 tokens) are mathematically incomparable.

To establish an invariant benchmark across tokenizers and modalities, evaluation pipelines rely on Bits-Per-Byte (BPB) and Bits-Per-Character (BPC). Grounded in Shannon's source coding theorem, these metrics demonstrate that autoregressive language modeling is mathematically isomorphic to optimal lossless data compression (Delétang et al., 2024).

Perplexity and Data Compression Overview

1. Mathematical Derivation: From Likelihood to Perplexity

An autoregressive language model computes the joint probability of a sequence of TT discrete tokens X=(x1,x2,,xT)X = (x_1, x_2, \dots, x_T) via the chain rule of probability:

P(X)=t=1TP(xtx<t)P(X) = \prod_{t=1}^T P(x_t \mid x_{<t})

where x<t=(x1,,xt1)x_{<t} = (x_1, \dots, x_{t-1}) represents the preceding context tokens.

Negative Log-Likelihood and Cross-Entropy Loss

During training, the model parameters θ\theta are optimized to minimize the negative log-likelihood (NLL) of the training corpus under base ee (measured in nats) or base 2 (measured in bits). Over a sequence of length TT, the mean cross-entropy loss LCE\mathcal{L}_{\text{CE}} is defined as:

LCE(X)=1Tt=1TlnPθ(xtx<t)\mathcal{L}_{\text{CE}}(X) = -\frac{1}{T} \sum_{t=1}^T \ln P_\theta(x_t \mid x_{<t})

When evaluated with base-2 logarithms, the loss reflects the average information content per token:

LCE, bits(X)=1Tt=1Tlog2Pθ(xtx<t)=LCE(X)ln(2)\mathcal{L}_{\text{CE, bits}}(X) = -\frac{1}{T} \sum_{t=1}^T \log_2 P_\theta(x_t \mid x_{<t}) = \frac{\mathcal{L}_{\text{CE}}(X)}{\ln(2)}

Derivation of Perplexity

Perplexity is formally defined as the exponential of the cross-entropy loss:

PPL(X)=exp(LCE(X))=exp(1Tt=1TlnPθ(xtx<t))\text{PPL}(X) = \exp\left(\mathcal{L}_{\text{CE}}(X)\right) = \exp\left(-\frac{1}{T} \sum_{t=1}^T \ln P_\theta(x_t \mid x_{<t})\right)

Using algebraic properties of exponentials and logarithms, this can be rewritten as the geometric mean of the inverse conditional probabilities:

PPL(X)=(t=1T1Pθ(xtx<t))1T=(1Pθ(x1,,xT))1T\text{PPL}(X) = \left( \prod_{t=1}^T \frac{1}{P_\theta(x_t \mid x_{<t})} \right)^{\frac{1}{T}} = \left( \frac{1}{P_\theta(x_1, \dots, x_T)} \right)^{\frac{1}{T}}

Information-Theoretic Interpretation

Perplexity represents the effective branching factor of the model. A perplexity of KK indicates that, on average across the evaluation sequence, the model's uncertainty at each step is equivalent to selecting uniformly among KK equally probable candidate tokens from its vocabulary.

  • Theoretical Minimum: PPL=1.0\text{PPL} = 1.0. The model assigns probability P(xtx<t)=1.0P(x_t \mid x_{<t}) = 1.0 to every ground-truth token with zero uncertainty.
  • Uniform Random Guessing: PPL=V\text{PPL} = |V|, where V|V| is the total vocabulary size. If a model assigns equal probability 1/V1/|V| to every token, ln(1/V)=lnV\ln(1/|V|) = -\ln|V|, yielding exp(lnV)=V\exp(\ln|V|) = |V|.

2. The Tokenizer Dependency Problem

A widespread methodological error in LLM benchmarking is the direct comparison of token-level perplexity between models with different tokenizers.

The Mechanism of Tokenizer Distortion

Tokenizers partition raw text into subword units using algorithms like Byte-Pair Encoding (BPE) or WordPiece. The vocabulary size V|V| determines the average sequence compression ratio:

  • Models with large vocabularies (e.g., Gemma with V=256,000|V| = 256,000) merge common multi-word phrases and syllables into single token IDs.
  • Models with smaller vocabularies (e.g., Llama 2 with V=32,000|V| = 32,000) split the identical text into substantially more subword tokens.

Consider an evaluation sentence SS containing NbytesN_{\text{bytes}} bytes:

  • Tokenizer A (V=32,000|V| = 32,000) segments SS into TA=20T_A = 20 tokens.
  • Tokenizer B (V=128,000|V| = 128,000) segments SS into TB=10T_B = 10 tokens.

Even if both models assign identical cumulative sequence probability P(S)P(S) to the total text, their per-token losses diverge:

LA=120lnP(S)    PPLA=exp(120lnP(S))=P(S)120\mathcal{L}_A = -\frac{1}{20} \ln P(S) \implies \text{PPL}_A = \exp\left(-\frac{1}{20} \ln P(S)\right) = P(S)^{-\frac{1}{20}}

LB=110lnP(S)    PPLB=exp(110lnP(S))=P(S)110=(PPLA)2\mathcal{L}_B = -\frac{1}{10} \ln P(S) \implies \text{PPL}_B = \exp\left(-\frac{1}{10} \ln P(S)\right) = P(S)^{-\frac{1}{10}} = (\text{PPL}_A)^2

If P(S)=106P(S) = 10^{-6}:

  • PPLA=(106)0.052.0\text{PPL}_A = (10^{-6})^{-0.05} \approx 2.0
  • PPLB=(106)0.104.0\text{PPL}_B = (10^{-6})^{-0.10} \approx 4.0

Model B exhibits a raw perplexity twice as high as Model A despite assigning the exact same likelihood to the underlying text string. Because Model B predicts larger chunks of text per step from a wider choice set, its per-step branching factor is higher, even though its overall modeling capability is equivalent or superior.

Tokenizer Normalization Comparison

3. Bits-Per-Byte (BPB): The Invariant Standard

To eliminate tokenizer artifacts, language modeling loss must be normalized by an invariant physical unit: the total count of uncompressed UTF-8 bytes (NbytesN_{\text{bytes}}) or characters (NcharsN_{\text{chars}}) in the evaluated text.

Mathematical Definition of BPB

Let a text corpus consist of NbytesN_{\text{bytes}} raw bytes, which a tokenizer encodes into TT tokens. The total cross-entropy loss in bits across the sequence is:

Total Loss (bits)=t=1Tlog2Pθ(xtx<t)\text{Total Loss (bits)} = \sum_{t=1}^T -\log_2 P_\theta(x_t \mid x_{<t})

Dividing by the total byte count yields Bits-Per-Byte (BPB):

BPB=t=1Tlog2Pθ(xtx<t)Nbytes=LCE, natsln(2)×TNbytes\text{BPB} = \frac{\sum_{t=1}^T -\log_2 P_\theta(x_t \mid x_{<t})}{N_{\text{bytes}}} = \frac{\mathcal{L}_{\text{CE, nats}}}{\ln(2)} \times \frac{T}{N_{\text{bytes}}}

Similarly, Bits-Per-Character (BPC) is defined over character length NcharsN_{\text{chars}}:

BPC=LCE, natsln(2)×TNchars\text{BPC} = \frac{\mathcal{L}_{\text{CE, nats}}}{\ln(2)} \times \frac{T}{N_{\text{chars}}}

Cross-Model Comparison Across Tokenizer Families

Evaluating different vocabulary scales across standard English corpora (such as WikiText-103 or C4) demonstrates why BPB provides an objective comparison:

  • GPT-2 (124M): Vocabulary 50,257 | ~3.70 bytes/token | Typical Token PPL ~16.5 | Computed BPB: ~1.09 BPB
  • Llama 2 (7B): Vocabulary 32,000 | ~3.65 bytes/token | Typical Token PPL ~5.5 | Computed BPB: ~0.67 BPB
  • Llama 3 (8B): Vocabulary 128,256 | ~4.25 bytes/token | Typical Token PPL ~7.8 | Computed BPB: ~0.59 BPB
  • Gemma 2 (9B): Vocabulary 256,000 | ~4.60 bytes/token | Typical Token PPL ~10.2 | Computed BPB: ~0.57 BPB
  • Qwen 2.5 (7B): Vocabulary 151,936 | ~4.30 bytes/token | Typical Token PPL ~8.1 | Computed BPB: ~0.58 BPB

While Llama 3 (8B) has a higher raw per-token perplexity (7.8) than Llama 2 (7B) (5.5), its BPB is significantly lower (0.59 vs 0.67 BPB), correctly identifying Llama 3 as the superior predictive model.


4. Language Modeling as Lossless Compression

The relationship between negative log-likelihood and information theory is formal: minimizing cross-entropy is identical to minimizing compressed bitstream length under arithmetic coding (Shannon, 1948).

The Shannon-Huffman Equivalence

According to Shannon's source coding theorem, the optimal code length L(x)L(x) for an event xx occurring with probability P(x)P(x) is:

L(x)=log2P(x)bitsL(x) = -\log_2 P(x) \quad \text{bits}

Using Arithmetic Coding (Witten et al., 1987), a sequence of tokens x1,,xTx_1, \dots, x_T can be compressed into a single floating-point interval whose length in bits approaches the theoretical entropy limit:

Compressed Size (bits)t=1Tlog2Pθ(xtx<t)+2\text{Compressed Size (bits)} \approx \sum_{t=1}^T -\log_2 P_\theta(x_t \mid x_{<t}) + 2

An autoregressive language model paired with an arithmetic coder functions as an exact, lossless compressor and decompressor:

  1. Compression: The sender initializes the model context, feeds each token, queries the next-token probability distribution Pθ(xtx<t)P_\theta(x_t \mid x_{<t}), and emits arithmetic coding bits.
  2. Decompression: The receiver initializes the identical model weights, generates the exact same probability distribution, and uses the arithmetic coder to decode the deterministic token sequence.

DeepMind's Compression Benchmark

In a seminal 2024 study, Language Modeling Is Compression (Delétang et al., 2024), researchers at Google DeepMind evaluated pre-trained foundation models as general-purpose lossless data compressors across text, audio, and image datasets.

The findings demonstrated that foundation models trained on web corpora acquire generalizable compression capabilities across modalities:

  • ImageNet Patches: Chinchilla 70B compressed raw image patches to 43.4% of their uncompressed byte size, outperforming domain-specific image compression standards such as PNG (58.5%).
  • LibriSpeech Audio: Chinchilla 70B compressed raw audio waveforms to 16.4% of their uncompressed byte size, surpassing the specialized FLAC audio compressor (30.3%).

This empirical result validates Marcus Hutter's theoretical framework and the Hutter Prize (Hutter, 2006): compression performance serves as an objective, non-gameable benchmark for algorithmic intelligence.


5. Computational Methodology: Strided Sliding Window Evaluation

Computing perplexity over long documents presents subtle edge-case challenges related to context truncation and receptive fields.

The Truncation Pitfall (Non-Overlapping Chunks)

If a document exceeds the model's maximum sequence length LmaxL_{\text{max}}, the naive approach splits text into independent segments of length LmaxL_{\text{max}}:

S1=(x1,,xLmax),S2=(xLmax+1,,x2Lmax)S_1 = (x_1, \dots, x_{L_{\text{max}}}), \quad S_2 = (x_{L_{\text{max}}+1}, \dots, x_{2L_{\text{max}}})

In segment S2S_2, the model must predict token xLmax+1x_{L_{\text{max}}+1} with zero preceding context, even though the full document contains preceding text. This introduces severe cold-start penalties and artificially degrades the reported perplexity.

Strided Sliding Window Formulation

The standard evaluation protocol uses a strided sliding window (Hugging Face Transformers Documentation). The model context window slides forward by a fixed stride S<LmaxS < L_{\text{max}}. At each step:

  1. The model receives a full context window of length LmaxL_{\text{max}}.
  2. Loss is computed only on the newly introduced tail tokens of length SS.
  3. Preceding context tokens are masked in the loss computation using target masking (e.g., setting label targets to -100).
Step 1: [Context: x_1 ... x_{L-S}] [Target: x_{L-S+1} ... x_L]     -> Loss computed on Target
Step 2: [Context: x_{S+1} ... x_L] [Target: x_{L+1} ... x_{L+S}]   -> Loss computed on Target

Reference Implementation

The following Python script implements strided evaluation using PyTorch and Hugging Face Transformers, calculating both token-level perplexity and normalized Bits-Per-Byte:

import torch
import math
from transformers import AutoModelForCausalLM, AutoTokenizer

def evaluate_perplexity_and_bpb(
    model_name: str,
    text: str,
    max_length: int = 4096,
    stride: int = 512,
    device: str = "cuda"
) -> dict:
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        torch_dtype=torch.float16,
        device_map=device
    )
    model.eval()

    # Encode input text to token IDs and calculate raw UTF-8 byte count
    encodings = tokenizer(text, return_tensors="pt")
    input_ids = encodings.input_ids.to(device)
    seq_len = input_ids.size(1)
    
    raw_bytes = len(text.encode("utf-8"))
    if raw_bytes == 0:
        raise ValueError("Evaluation text cannot be empty.")

    nlls = []
    total_eval_tokens = 0

    with torch.no_grad():
        for i in range(0, seq_len, stride):
            begin_loc = max(i + stride - max_length, 0)
            end_loc = min(i + stride, seq_len)
            trg_len = end_loc - i  # Number of newly evaluated target tokens

            chunk_input_ids = input_ids[:, begin_loc:end_loc]
            target_ids = chunk_input_ids.clone()
            
            # Mask context tokens so loss is only evaluated on target tokens
            target_ids[:, :-trg_len] = -100

            outputs = model(chunk_input_ids, labels=target_ids)
            
            # outputs.loss is the mean negative log-likelihood over unmasked tokens
            neg_log_likelihood = outputs.loss.item()
            
            # Total NLL for this target segment in nats
            nlls.append(neg_log_likelihood * trg_len)
            total_eval_tokens += trg_len

            if end_loc == seq_len:
                break

    total_nll_nats = sum(nlls)
    mean_nll_nats = total_nll_nats / total_eval_tokens
    
    # Standard token-level perplexity
    token_ppl = math.exp(mean_nll_nats)
    
    # Bits-per-token
    bits_per_token = mean_nll_nats / math.log(2)
    
    # Invariant Bits-per-Byte (BPB)
    total_loss_bits = total_nll_nats / math.log(2)
    bpb = total_loss_bits / raw_bytes

    return {
        "model": model_name,
        "total_tokens": total_eval_tokens,
        "total_bytes": raw_bytes,
        "token_perplexity": round(token_ppl, 4),
        "bits_per_token": round(bits_per_token, 4),
        "bits_per_byte": round(bpb, 4),
    }

6. Practical Limitations and Benchmark Decoupling

While perplexity and BPB are rigorous mathematical measures of density estimation, several practical nuances limit their direct correlation with downstream task performance:

1. Memorization vs. Generalization

A model can achieve exceptionally low perplexity on a test set by overfitting to stylistic markers, boilerplate, or leaked evaluation text without acquiring robust reasoning abilities. Deduplication algorithms (Lee et al., 2022) are necessary during pre-training to prevent memorization artifacts from artificially skewing evaluation.

2. Post-Training Alignment Trade-Offs

Supervised Fine-Tuning (SFT) and preference optimization (RLHF, DPO, GRPO) typically cause a modest increase in base validation perplexity (often 5% to 15%). Alignment constrains the generative output distribution to human-preferred responses, slightly reducing the model's likelihood on the broader distribution of all valid human text while dramatically boosting instruction-following performance on benchmarks like MMLU, GSM8K, and HumanEval.

3. Length Extrapolation Failures

When evaluating long contexts (e.g., 32K to 1M tokens), models without proper positional extrapolation mechanisms (such as YaRN, dynamic NTK scaling, or attention sinks) exhibit catastrophic perplexity explosion after exceeding their training context limit (Xiao et al., 2023). Tracking perplexity as a function of sequence position remains the primary tool for diagnosing attention collapse and context degradation.


7. Summary of Evaluation Metrics

When designing evaluation pipelines for foundation models and fine-tuned checkpoints:

  1. Use Token Perplexity strictly for tracking training loss curves and comparing checkpoints of the exact same model family and tokenizer.
  2. Use Bits-Per-Byte (BPB) when comparing models across different architectures, vocabulary sizes, or tokenizer configurations.
  3. Use Strided Sliding Windows (SLmax/2S \le L_{\text{max}} / 2) to eliminate cold-start truncation artifacts on long documents.
  4. Complement BPB with Downstream Evals (exact match, code pass@k, reasoning benchmarks) to measure functional capability alongside raw compression efficiency.

Sources

Written by

More to read

  • Embedding Model Migration in Production: Dual-Indexing, Vector Adapters, and Zero-Downtime Re-Indexing Architectures

    Upgrading embedding models in production retrieval-augmented generation (RAG) and semantic search platforms is often mischaracterized as a routine model bump. When engineering teams replace a legacy embedding model such as OpenAI text-embedding-ada-002 or BAAI bge-large with a modern successor like text-embedding-3-large or Cohere embed-v3, the underlying mathematical coordinate space changes entirely. Because dense vectors generated by distinct neural networks live on incompatible manifolds, v

    1 min
  • Apple Cuts 200 Jobs Across Siri and Vision Pro Teams in AI Realignment

    Apple has eliminated approximately 200 roles across its Siri voice assistant organization and the Vision Products Group, according to reporting from Bloomberg and AppleInsider. The personnel reductions reflect an internal reallocation of engineering resources as Apple shifts from legacy voice parsing architectures to foundation model pipelines and redirects hardware focus toward lightweight AI-enabled wearable devices. The workforce reductions impact roughly 100 employees in the Vision Products

    1 min
  • US Public Opposition to Local AI Data Centers Surges to 75%, Heatmap Survey Finds

    Public opposition to local data center construction in the United States has accelerated sharply over the past twelve months, according to a survey conducted by Heatmap News and Embold Research. The nationwide survey found that 75 percent of American adults now oppose the development of a data center facility near their community, with 61 percent reporting they are strongly opposed. The shift marks a substantial reversal from August 2025, when public sentiment was evenly divided at 43 percent i

    1 min