Perplexity to bits per byte: comparing models that use different tokenizers (llama.cpp, lm-eval)
Bits per byte = ln(perplexity) × tokens scored ÷ (ln 2 × UTF-8 bytes those tokens cover). Perplexity and “loss” are per token, so they change when the tokenizer changes; bits per byte does not. On the same 807 kB of Python source, three toy models scored perplexity 33.6, 108.6 and 897.4 with llama-perplexity, a 27× spread, yet their bits per byte were 2.35, 1.97 and 2.23: the model with the lowest perplexity was the worst of the three. The catch with llama.cpp is that llama-perplexity prints neither count you need. It scores only the second half of each chunk and drops the tail of the file, 49.2% to 49.9% of the text in the runs below. The calculator on this page does the arithmetic, and ppl2bpb.py works out the exact counts for a llama.cpp run. A figure such as OpenAI's “log-perplexity below 0.8” cannot be compared with yours either way: it is per token, on a private dataset, with the log base and the tokenizer not stated.
Convert a perplexity or a loss to bits per byte
Enter the number you have and how much text it covers. Everything is computed in your browser.
Formulas: nats per token = ln(PPL) = bits per token × ln 2 = log10 value × ln 10. Bits per byte = nats per token × tokens ÷ (ln 2 × bytes). Byte perplexity = 2bits per byte. The llama-perplexity mode counts the scored tokens exactly (chunks = floor(tokens ÷ c), each scoring c − 1 − floor(c ÷ 2) tokens) and assumes the scored half has the same bytes per token as the whole file; that assumption was within 0.62% of the exact figure in the nine full runs below. The bytes-per-token mode depends on the text: the presets are for Python source and are wrong for prose.
How do I convert perplexity to bits per byte?
Take the natural log of the perplexity, multiply by the number of tokens that were scored, and divide by ln 2 and by the UTF-8 bytes those tokens cover. The first step undoes the exponential (perplexity is e to the average loss per token), the second gives the total information in the text, and the last expresses it per byte, a unit no tokenizer can change.
nats per token = ln(PPL) # the "loss"; also called log-perplexity
bits per token = ln(PPL) / ln 2
total bits = bits per token * tokens scored
bits per byte = total bits / bytes covered by the scored tokens
byte perplexity = 2 ** bits per byte # = exp(total nats / bytes)
Worked example from the runs below (Toy B, -c 512): PPL 108.5936 is 4.68761 nats or 6.76280 bits per token. Over 116,790 scored tokens that is about 789,827 bits, and those tokens cover 401,209 bytes, so the model needs 1.9686 bits per byte. Bits per character is the same sum divided by characters; for ASCII-heavy text such as code the two are nearly equal, and for CJK text they differ by about a factor of three.
Why can't I compare perplexity between models with different tokenizers?
Because the average is taken over tokens, and each tokenizer cuts the same text into a different number of tokens. A tokenizer with short tokens gives the model many easy predictions and a low perplexity; one with long tokens gives fewer, harder predictions and a high perplexity, for the same total information.
The evaluation text here is 807,325 bytes of Python (20 files of CPython 3.14.0's Lib/). The GPT-2 vocabulary splits it into 372,143 tokens (2.17 bytes each, largely because it spends one token on every space of indentation), the Llama 3 vocabulary into 183,439 (4.40 bytes each):
| Tokenizer | Tokens for the text | Bytes per token | Tokens if llama-tokenize runs with its defaults (difference) |
|---|---|---|---|
| GPT-2 BPE (llama.cpp ggml-vocab-gpt-2) | 372,143 | 2.169 | 371,816 (-327) |
| Llama 2 SentencePiece (ggml-vocab-llama-spm) | 234,551 | 3.442 | 234,281 (-270) |
| Llama 3 BPE (ggml-vocab-llama-bpe) | 183,439 | 4.401 | 183,209 (-230) |
| OpenAI cl100k_base (tiktoken 0.14.0) | 183,476 | 4.400 | n/a |
| OpenAI o200k_base (tiktoken 0.14.0) | 184,937 | 4.365 | n/a |
Three toy language models, one per llama.cpp vocabulary, were trained for about four minutes each on the other 133 files and measured with llama-perplexity at its default context of 512:
| Model | Bytes per scored token | Perplexity | Rank by perplexity | Nats per token | Bits per token | Bits per byte | Rank by bits per byte |
|---|---|---|---|---|---|---|---|
| Toy A (GPT-2 BPE vocabulary) gpt2, 2 layers, width 128, vocabulary 50,257 | 2.156 | 33.6001 | 1 | 3.5145 | 5.0704 | 2.3518 | 3 |
| Toy B (Llama 2 SentencePiece vocabulary) llama, 2 layers, width 128, vocabulary 32,000 | 3.435 | 108.5936 | 2 | 4.6876 | 6.7628 | 1.9686 | 1 |
| Toy C (Llama 3 BPE vocabulary) llama, 2 layers, width 64, vocabulary 128,256 | 4.404 | 897.3537 | 3 | 6.7995 | 9.8095 | 2.2275 | 2 |
Ranked by perplexity, Toy A looks about three times better than Toy B and 27 times better than Toy C. Ranked by bits per byte it is the worst of the three. Nothing about the models changed between the two columns, only the unit. The toys are small on purpose (they are here to check the arithmetic, and at this context size all three compress worse than gzip), so read the table as a demonstration of the unit, not as a result about these tokenizers.
Context size moves perplexity too: Toy A scores 31.9 at -c 128, 33.6 at 512 and 35.7 at 1024 on the same file. (These toys were trained on 128-token windows, so they get worse with longer chunks; real models usually get better.) Compare runs only when the text and -c are the same.
Which tokens does llama-perplexity actually score?
Only the second half of every chunk: with the default -c 512, 255 tokens out of each 512, and nothing from the tail of the file that does not fill a chunk. So the perplexity it prints describes roughly half of your text, and the byte count to divide by is the bytes of those tokens, not the file size.
From tools/perplexity/perplexity.cpp at b12925:
- The whole file is tokenized once, with BOS in front if the vocabulary adds one. The tool needs at least 2 × c tokens.
- Chunks = floor(tokens ÷ c). Each chunk is evaluated on its own with an empty cache; its first token is replaced by BOS when the vocabulary adds BOS.
- Scored tokens are positions c/2 + 1 to c − 1 of each chunk: c − 1 − floor(c/2) per chunk. The first half is context only.
-fdrops one trailing newline, and the tool turns escape processing off (params.escape = false).llama-tokenizeprocesses escapes and parses special tokens by default, so on text with\nor\twritten out (any source code) it reports a different count: 230 to 327 fewer tokens on this text. Usellama-tokenize --no-escape --no-parse-special.--ppl-output-type 1prints one line per chunk with the running perplexity, the running mean in nats per token and its standard error (last line of an 8-chunk test run:158.9393 5.068523 0.119408), which saves taking the log yourself.
To check that reading, each run was recomputed in PyTorch from the original checkpoint in float64, scoring exactly those positions. All nine perplexities agree with llama.cpp's (F32 GGUF) to within 0.0003%:
| Model | -c | Chunks | Tokens scored | Bytes covered | Share of the text | PPL, llama-perplexity | PPL, PyTorch | Bits per byte (exact) | Whole-file shortcut (error) |
|---|---|---|---|---|---|---|---|---|---|
| Toy A | 128 | 2,907 | 183,141 | 397,193 | 49.2% | 31.9419 | 31.9419 | 2.3042 | 2.3036 (-0.03%) |
| Toy A | 512 | 726 | 185,130 | 399,135 | 49.4% | 33.6001 | 33.6001 | 2.3518 | 2.3372 (-0.62%) |
| Toy A | 1024 | 363 | 185,493 | 403,000 | 49.9% | 35.7197 | 35.7196 | 2.3744 | 2.3779 (+0.15%) |
| Toy B | 128 | 1,832 | 115,416 | 398,005 | 49.3% | 85.5358 | 85.5356 | 1.8613 | 1.8648 (+0.19%) |
| Toy B | 512 | 458 | 116,790 | 401,209 | 49.7% | 108.5936 | 108.5936 | 1.9686 | 1.9648 (-0.19%) |
| Toy B | 1024 | 229 | 117,019 | 402,887 | 49.9% | 138.2609 | 138.2613 | 2.0655 | 2.0660 (+0.03%) |
| Toy C | 128 | 1,433 | 90,279 | 398,428 | 49.4% | 891.0020 | 891.0012 | 2.2204 | 2.2266 (+0.28%) |
| Toy C | 512 | 358 | 91,290 | 402,017 | 49.8% | 897.3537 | 897.3524 | 2.2275 | 2.2289 (+0.06%) |
| Toy C | 1024 | 179 | 91,469 | 402,361 | 49.8% | 929.3054 | 929.3042 | 2.2415 | 2.2404 (-0.05%) |
The last column is what you get by dividing by the whole file instead: ln(PPL) × all tokens ÷ (ln 2 × file bytes). When the run covers the whole file the scored half is a fair sample and the shortcut was off by at most 0.62% here. It breaks when the run is partial: with --chunks 40 at -c 256 the scored tokens were 2.0% of the text, and the shortcut gave 2.180 against an exact 2.321 bits per byte (-6.1%).
ppl2bpb.py: the exact conversion for a llama.cpp run
ppl2bpb.py (Python 3.8+, standard library only) tokenizes the file with llama-tokenize the way llama-perplexity does, replays the chunking, adds up the bytes of the scored tokens and prints every unit.
# run llama-perplexity and convert (arguments after -- go to llama-perplexity)
python ppl2bpb.py --bin-dir llama.cpp/build/bin -m model.gguf -f text.txt -c 512 -- -ngl 99
# convert a perplexity you already measured with the same -f and -c
python ppl2bpb.py --bin-dir llama.cpp/build/bin -m model.gguf -f text.txt -c 512 --ppl 7.4213
# arithmetic only
python ppl2bpb.py --ppl 7.4213 --tokens 91290 --bytes 372100
It stops if llama-perplexity reports a different number of chunks than it expects, which is how a build with different chunking would show up. Runs with --ppl-stride use another window and are not supported.
Can I compare my model with OpenAI's log-perplexity of 0.8?
No. The numbers in OpenAI's “Perplexity and Intelligence” post (2026-11-22) are per-token losses on a dataset nobody else has, and the post does not say which log base, tokenizer or context length they use. Converting your own result to bits per byte fixes the tokenizer problem but not the other two.
| What you would need | What the post says | Why it matters |
|---|---|---|
| The evaluation text | Held-out data “sourced from our internal codebase”, a fixed snapshot; not published | Loss depends on the text more than on anything else. In the Pile paper the same GPT-3 davinci scores 0.7177 bits per byte on the whole Pile and 0.5635 on its GitHub part. |
| The log base | “the log of perplexity, also called cross-entropy loss”; no base given | Training code normally reports the natural log, but base 2 would make every value 31% smaller in nats and base 10 would make it 2.3 times larger. |
| The tokenizer | Not stated | A per-token value turns into bits per byte only through bytes per token. OpenAI's public tokenizers differ little on Python source (4.400 bytes per token for cl100k_base, 4.365 for o200k_base here), but the one behind the chart is unknown. |
| Context length and checkpoint | No context length given; the post speaks of comparing “base models” | Context length changes the loss (see the -c comparison above), and a chat-tuned checkpoint is not the base model. |
To see what the choice of base alone does, here are the post's values turned into bits per byte under each reading, assuming o200k_base and text that tokenizes like the Python source used on this page (4.365 bytes per token). This is an illustration of the units, not an estimate of OpenAI's models:
| Value in the post | As printed | Bits per byte if it is in nats | if it is in bits | if it is base 10 |
|---|---|---|---|---|
| GPT-3.5 | 0.99 | 0.327 | 0.227 | 0.753 |
| GPT-4 | 0.91 | 0.301 | 0.208 | 0.692 |
| “losses below 0.8” | 0.80 | 0.264 | 0.183 | 0.609 |
| the model that “started producing groundbreaking results” | 0.76 | 0.251 | 0.174 | 0.578 |
What you can do with your own model is measure it and a reference model on the same public text and report bits per byte for both.
What is a good bits-per-byte number?
It depends on the text, so the useful anchors are other things measured on the same text. A general-purpose compressor is the cheapest one: a model that cannot beat xz or bzip2 on your text has not learned much about it.
| On the 807 kB of Python source used here | Bits per byte | How |
|---|---|---|
gzip -9 (Python standard library) | 1.854 | measured here, whole text |
bzip2 -9 (Python standard library) | 1.544 | measured here, whole text |
zstd -19 (zstandard package) | 1.591 | measured here, whole text |
xz -9e (Python standard library) | 1.563 | measured here, whole text |
Toy B, -c 512 | 1.969 | measured here, the scored half of the text |
Toy C, -c 512 | 2.228 | measured here, the scored half of the text |
Toy A, -c 512 | 2.352 | measured here, the scored half of the text |
At -c 512 the toys need 1.97 to 2.35 bits per byte, more than gzip's 1.85 on the whole text; they have two layers and a few minutes of training. For real models, the Pile paper (2021) reports bits per byte for GPT-2 and GPT-3 on its test set (Table 2, one tenth of the test data):
| Text | GPT-2 small | GPT-2 medium | GPT-2 large | GPT-2 xl | GPT-3 ada | GPT-3 babbage | GPT-3 curie | GPT-3 davinci |
|---|---|---|---|---|---|---|---|---|
| The Pile (all components) | 1.2253 | 1.0928 | 1.0828 | 1.0468 | 0.9631 | 0.8718 | 0.7980 | 0.7177 |
| Github | 1.7912 | 1.3180 | 1.7909 | 1.6486 | 0.8761 | 0.7335 | 0.6415 | 0.5635 |
Those rows are on the Pile's text, not on the Python files used here, so they are not comparable with the table above.
How do I get bits per byte from lm-eval or llama-server?
lm-eval already prints it for rolling-likelihood tasks, and llama-server cannot give you the per-token likelihood of a prompt through /v1/completions, so for GGUF models use llama-perplexity and convert.
- lm-eval: tasks with
output_type: loglikelihood_rolling(for examplewikitext) reportword_perplexity,byte_perplexityandbits_per_byte. The aggregation is −Σ log-likelihood ÷ Σ bytes ÷ ln 2, with the bytes of each document counted from its UTF-8 text (lm_eval/api/metrics.py, read at commitd6de816, version 0.4.14.dev0). - llama-server (b12925): a
/v1/completionsrequest with"echo": trueis rejected with “Only no echo is supported” (tools/server/server-common.cpp), so the usual echo-plus-logprobs trick does not work there. - Anything else that gives you per-token log-probabilities: sum the negative log-likelihoods of the tokens you scored, and divide by the bytes of exactly those tokens (not of the prompt including context you did not score).
An alternative that stays in token units is to rescale every model to one reference tokenizer's token count, as the “weighted perplexity benchmark” write-up does; it is the same total log-likelihood divided by a different constant.
How was this measured?
Everything ran on 2 CPU cores on 2026-12-02 with llama.cpp b12925 (commit 58bd04a05) built from source.
- Text: CPython v3.14.0
Lib/*.py, sorted; every 8th file (20 files, 807,325 bytes after the trailing newline is dropped) is the evaluation text, the other 133 files are training text. - Models: three 2-layer causal language models (one GPT-2-style, two Llama-style) using the vocabularies shipped in llama.cpp's
models/ggml-vocab-*.gguf, trained from scratch for 255 seconds each, converted withconvert_hf_to_gguf.py --outtype f32. - Runs:
llama-perplexity -f test.txt -c {128,512,1024}for each model (9 runs) plus one--chunks 40run and one--ppl-output-type 1run. Each of the first ten was recomputed in PyTorch (float64) from the token ids thatllama-tokenizeproduced. - Tokenizer counts: llama.cpp vocabularies with
llama-tokenize; cl100k_base and o200k_base with tiktoken 0.14.0, using the encoding files bundled in the litellm 1.112.0 wheel (tiktoken verifies their hashes). - Compressors: Python's
gzip,bz2,lzmaand thezstandardpackage on the whole evaluation text.
The scripts that produce every number, and the test that runs this page's calculator against them, are in the site repository.
Limits
- No real model was measured: the test machine has no GPU and could not download model weights. The token accounting does not depend on the weights, but the bits-per-byte values of the toys say nothing about real models.
- The chunking rules were read and tested at b12925 only.
ppl2bpb.pychecks the chunk count against the tool's own log, which would catch a change in chunking but not a change in which positions are scored. --ppl-stride,--kl-divergenceand the multiple-choice modes are not covered.- The bytes-per-token presets come from one kind of text (Python source). Prose, other programming languages and non-English text give different values.
- The OpenAI section relies on the post as published on 2026-11-22; if OpenAI later states the base or tokenizer, the table of open questions is out of date.
FAQ
- How do I convert perplexity to bits per byte?
- Bits per byte = ln(perplexity) x tokens scored / (ln 2 x UTF-8 bytes those tokens cover). ln(perplexity) is the average loss in nats per token; multiplying by the token count gives the total, and dividing by ln 2 and by the bytes gives bits per byte. Example from this page: perplexity 108.5936 over 116,790 tokens covering 401,209 bytes is 1.9686 bits per byte.
- Can I compare perplexity between models that use different tokenizers?
- No. Perplexity is per token, and tokenizers cut the same text into different numbers of tokens. On the same Python source, three toy models here scored perplexity 33.6, 108.6 and 897.4, but 2.35, 1.97 and 2.23 bits per byte: the model with the lowest perplexity had the worst bits per byte. Compare bits per byte (or total log-likelihood) on the same text instead.
- Can I compare my llama.cpp perplexity with OpenAI's log-perplexity of 0.8?
- No. OpenAI's numbers are a per-token loss on held-out data from its internal codebase, which is not public, and the post states neither the log base nor the tokenizer. Even after converting your own result to bits per byte you would be measuring a different text.
- Does llama-perplexity score every token in the file?
- No. In llama.cpp b12925 it cuts the tokens into chunks of -c tokens (default 512), scores only the second half of each chunk (255 of 512 tokens) and drops the tail that does not fill a chunk. In the nine full runs here the scored tokens covered 49.2% to 49.9% of the bytes of the text.
- Why does llama-tokenize report a different token count than llama-perplexity uses?
- llama-tokenize processes escape sequences such as \n in the text and parses special tokens by default; llama-perplexity does neither, and its -f drops one trailing newline. Run llama-tokenize with --no-escape --no-parse-special to get the same tokens. On the Python source used here the default gave 230 to 327 fewer tokens.
- Does lm-eval report bits per byte?
- Yes, for tasks with output_type loglikelihood_rolling such as wikitext. It prints word_perplexity, byte_perplexity and bits_per_byte, where bits_per_byte is minus the summed log-likelihood divided by the summed UTF-8 bytes and by ln 2, so no conversion is needed.
Related
- How many tokens per character Japanese text costs in 18 tokenizers
- How far vLLM and llama.cpp logprobs may differ from Transformers
- Checking a GGUF's token ids against the Hugging Face tokenizer
Sources
- llama.cpp b12925: tools/perplexity/perplexity.cpp, its README (“perplexity is not directly comparable between models, especially if they use different tokenizers”), tools/tokenize/tokenize.cpp, tools/server/server-common.cpp
- lm-evaluation-harness: lm_eval/api/metrics.py and tasks/wikitext/preprocess_wikitext.py
- OpenAI, Perplexity and Intelligence (2026-11-22), and its Hacker News discussion, where the tokenizer question and bits per byte come up
- Gao et al., The Pile: An 800GB Dataset of Diverse Text for Language Modeling (2021), Table 2
- The weighted perplexity benchmark (LessWrong, 2025): perplexity normalized to one reference tokenizer, measured with llama.cpp
- CPython v3.14.0 Lib/ (the text), tiktoken, litellm 1.112.0 (bundled encoding files)