Concepts / Cross Entropy in Language Models

Cross Entropy in Language Models

Bits per token is not directly comparable across language models that use different tokenization methods, because each token can represent a different amount of text.

  • Programming

Two Models With the Same Score

Suppose two language models both report a cross entropy of 6 bits. It is tempting to conclude that they are equally good at the same job. That conclusion rests on an assumption that often fails: that each model's token means the same amount of text. A model that treats each word as a token and a model that treats each character as a token can both report 6 bits per token, yet those 6 bits cover very different amounts of text.

This article follows the path a score takes as it is rewritten into a fairer form. You will trace what one token covers, convert bits per token into bits per character, convert again into bits per byte, and finally read that number as a statement about compression.

Tracing What One Token Covers

Cross entropy is measured in bits per token. The bit count tells you how much the model spends to predict each token, but it says nothing about how many characters that token stands for. Two tokenizers can split the same sentence into very different numbers of pieces, so the same per-token cost hides different amounts of text.

Model A tokenabout 6 characters, 6 bitsModel B tokenabout 1 character, 6 bits
How many characters does one token cover under Model A versus Model B, when both spend 6 bits per token? This is why their per-token scores cannot be compared directly.

Here the two models carry identical scores of 6 bits per token, but Model A's token covers about 6 characters while Model B's covers about 1. Model B's 6 bits are spread across far less text, so each character costs it far more. Reading the two numbers side by side as if they measured the same thing would be a category error.

What do you think happens?

Model B spends 6 bits per token and each of its tokens averages 1 character. What is its bits per character?

Reveal answer
Answer:

6 bits per character, because $\frac{6}{1} = 6$.

Dividing the bits per token by the average characters per token gives the per-character figure. With 1 character per token, the per-character figure equals the per-token figure, which is why Model B's cost per character is so much higher than Model A's.

From Bits per Token to Bits per Character

Bits-per-character (BPC) rescales the per-token figure into a per-character figure. Take the number of bits the model spends on each token and divide it by the average number of characters that a token contains. Because every model can be described in terms of characters, BPC gives a common basis for comparing models that split text in different ways.

Bits per token6 bitsDivide by avgcharacters per token2 charactersBits per character3 bitsDivide by fraction ofa byte per character7/8 of a byte under ASCIIBits per byte3.43
What sequence of steps turns bits per token and average characters per token into bits per character, and then into bits per byte?
Worked example

Converting a per-token score into BPC and BPB

A model spends 6 bits per token, and each token averages 2 characters. The text is encoded in ASCII, where each character uses 7 bits. Find the BPC and the BPB.

  1. 1
    Compute BPC

    Divide bits per token by average characters per token: $\frac{6}{2} = 3$ bits per character.

  2. 2
    Find the byte fraction

    Under ASCII each character is 7 bits, which is $\frac{7}{8}$ of a byte, because a byte has 8 bits.

  3. 3
    Compute BPB

    Scale BPC by the byte fraction: $\frac{3}{\frac{7}{8}} = 3.43$, rounded to two decimal places.

Result

BPC is 3 bits per character and BPB is 3.43 bits per byte.

Why BPC Is Not Enough

BPC still has a weakness, and it comes from character encoding. The number of bits used to store a character depends on the encoding scheme. Under ASCII, each character is encoded using 7 bits. Under UTF-8, a character can take anywhere from 8 to 32 bits. Two models could report identical BPC values while the underlying text occupies different amounts of storage, which makes BPC an unreliable yardstick when the encodings differ.

Reference unit:characterper characterReference unit: byteper byte of training dataDepends on encodingASCII 7 bits, UTF-8 8 to 32bitsAccounts for encodingstandardized metric
How do the two metrics differ in their reference unit and in whether they account for the encoding scheme?

Bits-per-byte (BPB) fixes this by anchoring the measure to the byte, the unit of the original training data rather than the character. BPB is the number of bits a language model needs to represent one byte of that data. Because the reference unit is the stored byte, the measure accounts for the encoding scheme, which is why the source calls BPB a more standardized metric than BPC.

python
Output (expected)
3.0 3.43
Notes on the code
  • bpc_from_bits_per_token implements the division by average characters per token
  • bpb_from_bpc divides by the byte fraction, which is 7/8 under ASCII
  • The printed output matches the worked example: 3 bits per character and 3.43 bits per byte

Reading BPB as Compression

The reason these metrics matter is their link to cross entropy. The source states that cross entropy tells us how efficient a language model will be at compressing text. Cross entropy is measured in bits, and BPB expresses that same cost against the bytes of the original text. A lower BPB therefore means fewer bits are spent on each original byte, so the model can represent the text more compactly.

Before

Original text: 1 byte = 8 bits

Each original byte uses 8 bits of storage.

After

Model representation: 1 byte = 3.43 bits (BPB)

The model represents that byte using only 3.43 bits, less than half of 8.

Tracing the Conversion With a Second Tokenizer

Worked example

Putting both models on the same footing

Model A and Model B both spend 6 bits per token. Model A averages 6 characters per token and Model B averages 1 character per token. Both are evaluated on ASCII text. Compare their BPC and BPB.

  1. 1
    Model A BPC

    $\frac{6}{6} = 1$ bit per character.

  2. 2
    Model B BPC

    $\frac{6}{1} = 6$ bits per character.

  3. 3
    Model A BPB

    $\frac{1}{\frac{7}{8}} \approx 1.14$ bits per byte.

  4. 4
    Model B BPB

    $\frac{6}{\frac{7}{8}} \approx 6.86$ bits per byte.

Result

The same 6-bit score becomes 1.14 bits per byte for Model A and 6.86 bits per byte for Model B. Once the scores are normalized, the difference between the models is visible, and Model A is the more compact representation of the text.

Mistakes Beginners Make With These Metrics

The metrics in this article are easy to apply mechanically, and that is where most errors come from. The three mistakes below are the ones that most often lead to wrong conclusions.

  • Common mistake

    Comparing bits per token directly across models with different tokenizers

    What it looks like

    Declaring two models equally good because both report 6 bits per token.

    Why it fails

    Each token can represent a different amount of text, so the same bit count covers different amounts of content.

    Do this instead

    Convert each score to bits per character by dividing by average characters per token, then compare.

  • Common mistake

    Using BPC as the final cross-model yardstick when encodings differ

    What it looks like

    Treating two BPC values as equal even though one text uses 7-bit ASCII characters and the other uses UTF-8 characters of up to 32 bits.

    Why it fails

    The number of bits used to store a character depends on the encoding scheme, so BPC does not fix the storage basis.

    Do this instead

    Convert BPC to BPB, which is anchored to the byte of the original training data.

  • Common mistake

    Dividing by the byte fraction in the wrong direction

    What it looks like

    Multiplying 3 by $\frac{7}{8}$ to get the BPB, which gives 2.63 instead of 3.43.

    Why it fails

    Each character occupies $\frac{7}{8}$ of a byte, so the BPB is the BPC divided by that fraction, not multiplied by it.

    Do this instead
    Compute $\frac{3}{\frac{7}{8}} = 3.43$.

Checking Your Conversion

MEDIUM

A model spends 4 bits per token, and its tokens average 2 characters each. The text is in ASCII. Compute the BPC, then the BPB, and state whether this model compresses the original text to less than half its size.

Hints
  • First divide 4 bits per token by 2 characters per token.
  • Under ASCII each character is 7 bits, so it is $\frac{7}{8}$ of a byte. Divide your BPC by that fraction.
  • Compare your BPB with 4, which is half of the 8 bits in one original byte.

Key Takeaways

  • Bits per token is not comparable across models with different tokenizers, because each token can cover a different amount of text.
  • Bits-per-character (BPC) divides bits per token by the average number of characters per token, giving a shared per-character basis.
  • BPC depends on the character encoding scheme, so bits-per-byte (BPB) is the more standardized metric; BPB equals BPC divided by the fraction of a byte each character occupies.
  • A lower BPB means fewer bits are spent per original byte, which connects cross entropy directly to how compactly a model can represent text.

Related concepts

More concepts starting with C