6.1 Exponents, logarithms, and units of information
Every language model in AI Engineering reports its quality as a loss, and the first one, the byte bigram, reports nll: the mean negative log-likelihood in nats per predicted byte, nats because numpy’s log is the natural logarithm. That number means something only next to another model scored on the same units of text. Once tokenizers enter the picture, the text is split into multi-byte tokens, and a model over BPE tokens reports nats per token. A vocabulary of longer pieces makes every per-token number larger, even for a better model. When an n-gram model, a neural model, and an LSTM trained on different token streams are compared, and when the model zoo ranks every architecture in one table, they all have to be converted to a common unit first.
Concepts from other sections (8)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Negative log-likelihood (NLL): , the surprise of the true token under the model, in nats. Read more: 17.2 Perplexity, bits per byte, NLL accumulator
- n-gram model: a language model that conditions each token only on the previous tokens. Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney in AI Engineering
- Log-likelihood: , the log probability of the observed counts under a distribution . Read more: 13.3 MLE, Laplace, absolute discounting
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives
- Perplexity: , the geometric mean of ; the effective number of choices per token. Read more: 17.2 Perplexity, bits per byte, NLL accumulator
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- Log-mel: Whisper’s features, of the mel spectrogram with a floor, an 8-unit clip, and the map . Read more: 18.6 Mel scale and filterbanks
- Bytes per token (): total UTF-8 bytes of the val texts over their tokens; converts loss per token into bits per byte. Read more: 5.7 Tokenize and pack to llm.c .bin in Data Engineering
That unit is bits per byte, and this section builds it from first principles: exponents, logarithms, change of base, and the idea that the base of a logarithm is a unit of information. Along the way it fixes the vocabulary the rest of this book uses: exponent, logarithm, base, nat, bit. The only prerequisite is high-school algebra; for the worked example it helps to have read the byte bigram, and the code assumes the numpy basics in Python and numpy from Programming and Algorithms.
The finished code is course/solutions/python/tinyllm/num/units.py, which provides LN2, nats_to_bits, bits_to_nats, log_base, and bits_per_byte; its interface is fixed by the contract course/contracts/py/tinyllm/num/units.pyi.
Learning objectives
- Derive the meaning of zero, negative, and fractional exponents from the law , and the laws of logarithms from the laws of exponents.
- Derive the change-of-base formula and say which bases it rules out.
- Convert between nats and bits, and explain why one bit is nats.
- Compute the bits per byte of a model by hand, and explain why it is the one number that compares models with different tokenizers.
- Implement
log_baseandbits_per_bytewith the validation that keeps NaN and silent infinities out of later sums.
Exponents
Section titled “Exponents”The symbols used throughout are collected here first:
| Symbol | Meaning | Type / shape |
|---|---|---|
| a base: a real number with and | float | |
| whole numbers (integers) | int | |
| raised to the power | float | |
| Euler’s number, | math.e | |
| the natural exponential function | ||
| the logarithm of in base : the with | float | |
the natural logarithm, (numpy’s np.log) | float | |
| a probability, | float | |
| the information content (surprisal) of an outcome of probability : | float | |
| the total negative log-likelihood of a text, in nats | float | |
| the length of the text in UTF-8 bytes | int | |
| bits per byte, | float |
For a whole number , is multiplied by itself times: . Counting factors gives the three laws of exponents:
The first law forces the meaning of every other exponent. Since , we must have . Since , we must have . And since , , the positive number whose -th power is . This is why the base must be positive: is not a real number. Fractions follow: . Filling in the gaps between the fractions continuously gives for every real , a function that is always positive, increasing when and decreasing when .
One base is special. is the base whose exponential grows, at every , at a rate equal to its own value; Derivatives and finite differences proves this from the derivative. is the exponential the rest of the book uses: in softmax, in the sigmoid, in every probability a model outputs.
Logarithms undo exponentials
Section titled “Logarithms undo exponentials”Because is strictly increasing (or strictly decreasing) and takes every positive value exactly once, it has an inverse, the logarithm:
So , , and in every base. Taking of both sides turns each law of exponents into a law of logarithms:
The first of these is the reason models are trained on log-probabilities. The probability of a text is a product of one probability per token, thousands of numbers below 1 multiplied together, and that product underflows to 0 in floating point after a few hundred tokens. Its logarithm is a sum, which stays representable, and it is what the byte bigram already computes.
As approaches 0, falls without bound when , so . An outcome a model calls impossible () is infinitely surprising. That is a value, not an error, and the log_base function built below returns it as one.
Change of base
Section titled “Change of base”You rarely have a logarithm in the base you need, but you never need more than one. Write and take the natural log of both sides; the power law gives . Divide:
Every logarithm is the natural log divided by a constant. For example, , which checks out because . Two consequences follow at once. when , so base 1 is not a base: the formula divides by zero. And , because .
In floating point the division rounds, so change of base is accurate to about one unit in the last place, not exact: math.log(2**29) / math.log(2) is 29.000000000000004. When you need an exact power of two, for instance when counting the bits of a block size, use integer arithmetic or np.log2. Results in this section are compared with float64 tolerances, never with equality.
Units of information
Section titled “Units of information”Logarithms become a measuring instrument once you give them a meaning. An outcome with probability carries units of information, also called its surprisal: a certain outcome () carries none, and rarer outcomes carry more. The base names the unit, as the two standard cases show:
| Base | Unit | One fair coin flip () | One uniform byte () |
|---|---|---|---|
| 2 | bit | 1 bit | 8 bits |
| nat | nats | nats |
By change of base, . So to turn nats into bits, divide by , and multiply by to go back. One nat is bits.
A language model assigns each next token a probability, and its total negative log-likelihood on a text is the sum of the surprisals, nats. Dividing by the number of tokens gives nats per token, which depends on the tokenizer. Dividing by the number of bytes does not: the same text has the same bytes however it is cut. So the book compares models by
A model that gives all 256 byte values probability scores exactly ; anything trained must do better, and English text under a good model scores around 1. Perplexity, bits per byte, and the NLL accumulator adds perplexity, , and explains why bits per byte and cross-entropy are the same idea.
Working it by hand
Section titled “Working it by hand”Now price the byte bigram’s own example in bits per byte. That section fits an add-one bigram to the text “abbacab” (ids 0, 1, 1, 0, 2, 0, 1 over the alphabet a, b, c) and gets these next-symbol probabilities:
| context | P(a) | P(b) | P(c) |
|---|---|---|---|
| a | 1/6 | 3/6 | 2/6 |
| b | 2/5 | 2/5 | 1/5 |
| c | 2/4 | 1/4 | 1/4 |
The first symbol is never predicted, so the model makes 6 predictions, one per remaining byte. Each costs nats, or bits:
| step | pair | nats, | bits, | |
|---|---|---|---|---|
| 1 | a to b | 1/2 | 0.693147 | 1.000000 |
| 2 | b to b | 2/5 | 0.916291 | 1.321928 |
| 3 | b to a | 2/5 | 0.916291 | 1.321928 |
| 4 | a to c | 1/3 | 1.098612 | 1.584963 |
| 5 | c to a | 1/2 | 0.693147 | 1.000000 |
| 6 | a to b | 1/2 | 0.693147 | 1.000000 |
| total | 7.228819 |
Then . The bits column gives the same answer directly, . Per byte in nats it is , which is exactly the nll the bigram’s hand example computes, and again. Against the 8 bits per byte of knowing nothing, the bigram has learned something about this (tiny) text.
The change-of-base examples are just as quick: as derived above, , and because .
Designing the interface
Section titled “Designing the interface”The module needs a constant, two scalar conversions, a general logarithm, and the bits-per-byte rate:
LN2 = math.log(2.0)def nats_to_bits(x: float) -> float: ... # x / ln 2def bits_to_nats(x: float) -> float: ... # x * ln 2def log_base(x: ArrayLike, b: float) -> NDArray: ... # ln x / ln b, float64, x's shapedef bits_per_byte(nll_nats_sum: float, n_bytes: int) -> float: ...log_base returns at (for ) without a warning. It raises ValueError for a base that is not positive, finite, and different from 1, and for negative or NaN inputs. bits_per_byte raises ValueError unless n_bytes is a positive integer and the sum is a non-negative number. The full rules are in the contract.
Building it step by step
Section titled “Building it step by step”The design follows from five observations, each from the theory above.
- One constant carries every conversion. By change of base, , so a quantity in nats becomes bits by dividing by . Compute once, at full float64 precision, and name it.
- Every logarithm is a natural log divided by a constant. So
log_baseneeds onlynp.logand one division by , and it inherits numpy’s float64 accuracy and array handling. - The base must make the division meaningful. is 0 at , undefined for , and infinite for ; each would return a silent or NaN. So the base is validated before any computation.
- Zero is a legitimate input; negatives and NaN are not. is the correct surprisal of an impossible event, so it is returned without a warning. A negative probability or a NaN is a bug upstream that would poison every sum after it, so it raises.
- Bits per byte is total nats over total bytes, in bits. is one multiplication in the denominator, with the byte count validated as a positive integer, because an empty text has no rate.
The constant and the scalar conversions
Section titled “The constant and the scalar conversions”import math
import numpy as npfrom numpy.typing import ArrayLike, NDArray
LN2 = math.log(2.0) # nats in one bit
def nats_to_bits(x: float) -> float: # One bit is ln 2 nats, so a quantity in nats is x / ln 2 bits. return float(x) / LN2
def bits_to_nats(x: float) -> float: return float(x) * LN2math.log(2.0) is the correctly rounded float64 value of ; a typed literal such as 0.693 would cost 13 digits. float(x) makes the result a plain Python float even when a numpy scalar is passed. Dividing, not multiplying, in nats_to_bits is the whole point: a nat is larger than a bit, so a count in bits must be the larger number.
The general logarithm
Section titled “The general logarithm”def log_base(x: ArrayLike, b: float) -> NDArray: b = float(b) # Reject every base where ln b is 0, undefined, or infinite. Written as # "not (good)", so NaN (for which every comparison is False) is rejected too. if not (b > 0.0 and b != 1.0 and math.isfinite(b)): raise ValueError(f"the base must be positive, finite, and not 1, got {b}") # float64 always: float32 would underflow 1e-300 to 0 and keep 7 digits. a = np.asarray(x, dtype=np.float64) if np.isnan(a).any() or (a < 0).any(): raise ValueError( "log_base needs x >= 0 everywhere (the log of a negative number is not real)" ) # Change of base: log_b x = ln x / ln b. log(0) = -inf is the right answer # (an impossible event is infinitely surprising), so silence numpy's warning. with np.errstate(divide="ignore"): return np.log(a) / math.log(b)The order of the checks matters. The base is checked first, so a bad base is reported even when x is also bad. np.asarray(..., dtype=np.float64) converts lists, scalars, and integer arrays alike and keeps the shape; a 0-d input gives a numpy scalar out, so scalars stay scalars. The errstate block suppresses only the divide-by-zero warning that np.log(0) raises, and only inside this function; inputs that would produce NaN never reach it because they were already rejected. For , , so , which is exactly the right boundary behavior with no special case needed.
Bits per byte
Section titled “Bits per byte”def bits_per_byte(nll_nats_sum: float, n_bytes: int) -> float: # bool is a subclass of int in Python: True would pass as 1 byte. # int(n) != n rejects 2.5; n < 1 rejects empty and negative texts. if isinstance(n_bytes, bool) or int(n_bytes) != n_bytes or n_bytes < 1: raise ValueError(f"n_bytes must be a positive integer, got {n_bytes!r}") nll = float(nll_nats_sum) # A sum of surprisals is >= 0; NaN means something upstream broke. if math.isnan(nll) or nll < 0: raise ValueError(f"a summed negative log-likelihood is >= 0, got {nll}") # Total nats -> total bits, then shared by every byte of the text. return nll / (int(n_bytes) * LN2)The function takes the sum of the negative log-likelihood, not the mean, on purpose. The caller knows its token count, this function knows only bytes, and a sum is the one quantity that does not depend on how the text was tokenized. On the hand example’s numbers, bits_per_byte(5.010635, 6) is . An infinite sum, from a model that assigned some byte probability 0, passes validation and returns , consistent with log_base.
The full solution is units.py.
Why this design
Section titled “Why this design”The scalar functions are ; log_base is in the array size, one log and one division per element plus two validation passes. It uses change of base rather than np.log2 and np.log10: those are slightly more accurate for their own bases, but log_base must support any base (Whisper’s base 10, base at the boundaries) with one code path, and its accuracy is within a unit in the last place, which is all the tests ask. It raises rather than returning NaN, because NaN propagates silently through sums and comparisons while a ValueError names the problem where it happened. But it returns for rather than raising, because a zero probability is a real outcome of a badly smoothed model and evaluation must report it, not crash. And bits_per_byte takes a sum and a byte count rather than a mean, because a mean per token would bake the tokenizer into the input.
What can go wrong
Section titled “What can go wrong”A correct implementation must pass the following checks, each tied to something that depends on it:
| Case | Kind of check | What must hold | Why it matters downstream |
|---|---|---|---|
| the hand example | unit | “abbacab” costs 1.204803 bits per byte | everyone agrees on the definition |
| a uniform byte model is 8 bits | unit | nats over bytes is 8 bits per byte | the baseline every model in the zoo must beat |
| one bit is nats | unit | nats is one bit, 1 nat is 1.4427 bits, and back | the factor every later conversion uses |
| nats to bits at full precision | golden | six values against mpmath at 60 digits | full float64 precision, not an approximation |
| the conversions round-trip | property | bits_to_nats(nats_to_bits(x)) == x from to | the two conversions are inverses |
| change of base by hand | unit | , , | the change-of-base derivation |
log_base at full precision | golden | 66 pairs, from to , bases 0.5 to 256 | float64 all the way through |
| the log laws | property | product, power, and reciprocal-base laws on random inputs | the identities later chapters rewrite losses with |
| shape and dtype | boundary | arrays keep their shape, results are float64, scalars stay scalars | tokenizer metrics and the NLL accumulator pass whole arrays |
| log of zero | boundary | , , no warning | a zero-probability token is a value, not a crash |
| bad bases | boundary | bases 1, 0, , , NaN raise ValueError | base 1 silently divides by zero |
| negative or NaN | boundary | negative or NaN raises ValueError | a NaN surprisal poisons every later sum |
| bits per byte at full precision | golden | four totals against mpmath | the conversion later chapters compare models with |
| bits per byte is per byte | property | doubling text and cost keeps bpb; doubling cost alone doubles it | the byte count really divides |
| bad bits-per-byte input | boundary | zero, negative, or fractional byte counts and negative or NaN totals raise ValueError | an empty text has no bits per byte |
Behind those checks are six mistakes that are easy to make. The most common is getting the conversion backwards.
Warning: Multiplying by to get bits, or dividing to get nats, puts every number off by a factor of : a 1.2 bpb model reports 0.58.
nats_to_bitsdivides byLN2,bits_to_natsmultiplies, andbits_per_bytedivides byn_bytes * LN2.
A close cousin is inverting change of base () or mixing logarithms (). Both give plausible numbers that are wrong by a constant factor or a reciprocal; the solution writes np.log(a) / math.log(b), natural logs on both sides with on top. Accepting base 1 is the third: , so the division returns or NaN without an error, which is why the base is validated first. The fourth goes the other way, treating as an error, so that evaluating a model that assigns probability 0 to some byte crashes instead of reporting ; np.errstate(divide="ignore") returns the infinity without a warning. The fifth is dividing by tokens, or not dividing at all: a BPE model then looks worse than a worse byte model, or bits per byte grows with text length, and the fix is that the input is a total in nats and the divisor is the byte count. The last is computing in float32, where underflows to 0, its log becomes , and you keep 7 digits instead of 16; np.asarray(x, dtype=np.float64) prevents it.
Where this leads
Section titled “Where this leads”The worked example came from the byte bigram in AI Engineering, and from here these functions are called throughout the books. In this book, Perplexity, bits per byte, and the NLL accumulator reports bits per byte through bits_per_byte, and the mel scale and filterbanks take base-10 logs with log_base for decibels and Whisper’s log-mel spectrogram. The next three sections of this chapter, on trigonometry and Euler’s formula, sequences and frequency ladders, and polynomials and Horner’s rule, use exponentials and logarithms in their derivations. In AI Engineering, tokenizer metrics report bytes per token and the bits per byte of each tokenizer’s model, the model zoo ranks every architecture by bits per byte, and it is the headline number of the TinyStories capstone.
Further reading
Section titled “Further reading”- OpenStax, Precalculus 2e (free), chapter 6, on exponential and logarithmic functions.
- MacKay, Information Theory, Inference, and Learning Algorithms (free), section 2.4, on the information content of an outcome.
- EleutherAI’s lm-evaluation-harness, whose perplexity tasks report
word_perplexity,byte_perplexity, andbits_per_bytefor every model on the same text; seebits_per_byteinlm_eval/api/metrics.py. - Gao et al., The Pile (2020), which reports bits per byte so that models with different tokenizers land on one scale; see its evaluation of GPT-2 and GPT-3 on the Pile components.
- The numpy reference, “Exponents and logarithms”:
log2(exact for powers of two),log10,log1p(accurate near 1, used in Sequences, geometric series, and frequency ladders), andlogaddexp(stable sums of exponentials, developed in Stable numerics).
Key terms
Section titled “Key terms”- Exponent: the power in ; zero, negative, and fractional exponents are defined by requiring .
- Logarithm: the inverse of the exponential, exactly when .
- Change of base: the identity , which makes every logarithm a natural log divided by a constant.
- Surprisal (information content): , the information carried by an outcome of probability .
- Nat: the unit of information for base ; one nat is bits.
- Bit: the unit of information for base 2; one bit is nats.
- Bits per byte: a text’s total negative log-likelihood in nats divided by ; independent of the tokenizer.
Summary
Section titled “Summary”- A logarithm undoes an exponential and turns products into sums, which is why every loss adds log-probabilities instead of multiplying probabilities.
- Change of base is one division, ; inverting it, or mixing with , gives numbers that look plausible and are wrong.
- The base of the log is the unit of information: base gives nats, base 2 gives bits, and one bit is nats.
- Bits per byte divides a text’s total cost in nats by . It does not depend on tokenization, so it compares a byte bigram with a BPE transformer.
- A model that knows nothing about bytes pays exactly 8 bits per byte; the “abbacab” bigram pays 1.204803.
Review questions
Section titled “Review questions”- Why does
nats_to_bitsdivide by rather than multiply?Answer
One bit is $\ln 2 \approx 0.693$ nats, so a nat is the larger unit and the same quantity is a larger number in bits. $x$ nats $= x / \ln 2$ bits. Multiplying would shrink every result by a further factor of $(\ln 2)^2$. - How does the base check reject
b = float("nan"), and why is it written as “not (all conditions for a good base)”?Answer
Every comparison with NaN is False, sob > 0.0is False and the wholeandis False;notmakes it raise. Written the other way round, as a list of bad cases (b <= 0 or b == 1), NaN would make every test False and slip through unless a separate check caught it. Stating the valid set fails safe. - What does
log_base(0, 0.5)return, and why does the code need no special case for it?Answer
$+\infty$.np.log(0)is $-\infty$ andmath.log(0.5)is negative, so the quotient is $+\infty$: the sign falls out of the change-of-base formula. - Why does
bits_per_bytetake a total in nats and a byte count instead of a mean per token?Answer
The total is tokenizer-independent and so is the byte count; their ratio is what makes a byte bigram and a BPE transformer comparable. A per-token mean would need the token count to undo, and mixing it up is exactly the mistake of dividing by tokens. - Why compute in float64 even when the input is a float32 array?
Answer
float32 underflows below about $10^{-38}$ (so $10^{-300}$ becomes 0 and its log $-\infty$) and keeps about 7 significant digits. The golden cases span $10^{-300}$ to $10^{300}$ at full float64 precision. - Starting from , show that and .
Answer
With $m = 0$, $b^0 b^n = b^n$, so dividing by $b^n \ne 0$ gives $b^0 = 1$. With $m = -n$, $b^{-n} b^n = b^0 = 1$, so $b^{-n} = 1/b^n$.