Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Formula Sheet

MATH 3219: every formula in six parts, every symbol said out loud, every worked instance computed

This is the course formula sheet. It collects every named formula in the book, in the order the chapters introduce them, and it is meant to sit open beside you while you work.

Nothing on this page needs calculus. Nothing on this page assumes you remember high-school algebra either. If a symbol is unfamiliar, it has a row in a table telling you how to say it and what it means.

Each entry has six parts, always in this order

  1. In words. What the formula does, with no symbols at all.

  2. The formula, on its own line.

  3. The symbols. A table with three columns: the symbol, how to say it out loud, and what it means. Every symbol gets a row, including the operators, including the ones you are sure you already know.

  4. Out loud. The whole formula read as one English sentence.

  5. Worked, step by step. A small numeric instance with every multiplication and every division written out, so you can reproduce it on a phone calculator.

  6. Sanity check. How to tell whether you got it right, and what going wrong looks like.

Each entry also names the chapter it comes from.


How to read a formula

A formula is not a sentence to be memorised. It is a set of instructions. You can read any formula in this book by asking three questions, in this order.

Question 1: what goes in? Look for the letters on the right-hand side of the equals sign. Those are the numbers you have to supply. In the softmax formula below, the things going in are the scores, written z1,z2,z3z_1, z_2, z_3 and so on. If you cannot say where a number comes from, you cannot use the formula yet, and that is the first thing to fix.

Question 2: what comes out? Look at the left-hand side of the equals sign. That is the single thing the formula produces. In softmax it is pip_i, one probability. Ask yourself two follow-up questions about it. Is the answer one number or a list of numbers? And what range can the answer live in? For softmax the answer is one number between 0 and 1. Knowing the range is what lets you catch a mistake later.

Question 3: what do the operators do? An operator is a symbol that tells you to perform an action: add, multiply, divide, take a square root, add up a whole list. Work from the inside out, the way you would peel an onion. In softmax the innermost action is “raise ee to a power”, then “add those results up”, then “divide”. Three actions, in that order.

Here is the habit in one worked pass, using softmax on three scores.

QuestionAnswer for softmax
What goes in?a list of scores, one per word. Here: 2, 1, 0.
What comes out?one probability per word, each between 0 and 1, all adding to 1.
What do the operators do?raise ee to each score; add the results; divide each result by that total.

Now do the three actions. Raise ee to each score: 7.389056, 2.718282, 1.000000. Add them: 11.107338. Divide each by that total: 0.665241, 0.244728, 0.090031. Check the range you predicted in Question 2: all three sit between 0 and 1, and they add to 1. Done.

That is the whole method, and it works on every formula on this page.


1. Notation used on this page

These are the letters that appear more than once. Each entry below repeats the ones it uses, so you never have to scroll back up.

SymbolHow to say it out loudWhat it means
==“equals”the thing on the left is the same number as the thing on the right
ziz_i“z sub i”logit: the raw score a model gives token ii before any conversion to probability. Can be negative. First used in Ch. 4.
VV“vee”vocabulary size: how many distinct tokens the model can choose from. V=151,936V = 151{,}936 for the model used throughout this book. Ch. 2.
pip_i“p sub i”probability the model assigns to token ii. Always between 0 and 1. Ch. 4.
TT“tee”temperature: a positive number you divide the logits by before converting them. Ch. 5.
HH“aitch”entropy, measured in bits: one number for how spread out a probability distribution is. Ch. 5.
NN“en”number of parameters in a model, or in part of one. N=494,032,768N = 494{,}032{,}768 for Qwen2.5-0.5B-Instruct. Ch. 3.
wiw_i“w sub i”one weight, that is, one of those parameters, stored as a number. Ch. 3.
bb“bee”bits used to store one weight. Ch. 6.
ss“ess”quantization scale: the size of one step on the grid of values a quantized weight is allowed to take. Ch. 7.
BB“capital bee”block size: how many consecutive weights share one scale. Ch. 7.
u,v,q,d\mathbf{u}, \mathbf{v}, \mathbf{q}, \mathbf{d}“bold u”, “bold v”, “bold q”, “bold d”vectors, written as lists of coordinates such as (3,4)(3, 4). Bold type marks a list of numbers rather than one number. Ch. 8.
DD“capital dee”number of coordinates in a vector. D=2D = 2 when you can draw it; D=384D = 384 for the sentence embeddings in this book. Ch. 8.
nn“en”number of questions on a test. Ch. 11.
xx“ex”number of those questions answered correctly. Ch. 11.
p^\hat{p}“p hat”sample proportion, here the measured accuracy x/nx/n. The hat marks a number you measured rather than a number you know. Ch. 11.
dˉ\bar{d}“d bar”the bar marks an average. dˉ\bar{d} is the average of the dd values. Ch. 13.
SESE“ess ee”standard error: how much an estimate would move if you drew a different sample. Ch. 12.
zz^\star“z star”critical value, a fixed multiplier set by the confidence level. z=1.96z^\star = 1.96 for 95%, and that is the only value this book uses. Ch. 12.

2. Counting what is in the model

2.1 Share of the parameters held by one component (USE)

In words. A model is built out of named parts. This works out what fraction of the whole model one of those parts accounts for, by dividing the size of the part by the size of the whole thing.

The formula.

share=NcomponentNtotal\text{share} = \frac{N_{\text{component}}}{N_{\text{total}}}

The symbols.

SymbolHow to say it out loudWhat it means
share\text{share}“share”the answer: a number between 0 and 1, which you turn into a percentage by multiplying by 100
==“equals”everything on the left is the same number as everything on the right
NcomponentN_{\text{component}}“en sub component”how many parameters are in one named part of the model, such as the vocabulary table, or all the attention layers together
NtotalN_{\text{total}}“en sub total”how many parameters are in the whole model
the fraction bar“divided by”divide the number on top by the number underneath
the subscript, as in NtotalN_{\text{total}}“sub total”a label attached to a letter. It is not multiplication and it is not a power. It tells you which NN this is.

Out loud. “The share held by a component is the number of parameters in that component, divided by the number of parameters in the whole model.”

Worked, step by step. The vocabulary table of Qwen2.5-0.5B-Instruct is a grid with one row for each of the 151,936 tokens and 896 columns.

Step 1, count the parameters in the component. That is rows times columns.

151,936×896151{,}936 \times 896

Break the multiplication into two easy pieces if you are working on paper:

151,936×900=136,742,400151{,}936 \times 900 = 136{,}742{,}400

151,936×4=607,744151{,}936 \times 4 = 607{,}744

136,742,400607,744=136,134,656136{,}742{,}400 - 607{,}744 = 136{,}134{,}656

Step 2, write down the total for the whole model: Ntotal=494,032,768N_{\text{total}} = 494{,}032{,}768.

Step 3, divide.

136,134,656÷494,032,768=0.275558136{,}134{,}656 \div 494{,}032{,}768 = 0.275558

Step 4, turn it into a percentage by multiplying by 100.

0.275558×100=27.55580.275558 \times 100 = 27.5558, which is 27.56% to two decimal places.

Sanity check. A share must land between 0 and 1. If you get a number bigger than 1 you divided the wrong way round; swap the top and the bottom. If you work out the share for every component and add them up, the total has to be 1, or 100%. For this model the vocabulary table is 27.56%, the MLP blocks are 63.51%, and all of attention is 8.92%. Attention is the famous part and it is under a tenth of the model.

Chapter. Chapter 3, What is a parameter?

2.2 Size of a stored matrix (USE)

In words. A weight matrix is a rectangle of numbers. To count how many numbers are in it, multiply how many rows it has by how many columns it has.

The formula.

Nmatrix=(rows)×(columns)N_{\text{matrix}} = (\text{rows}) \times (\text{columns})

The symbols.

SymbolHow to say it out loudWhat it means
NmatrixN_{\text{matrix}}“en sub matrix”the answer: how many numbers the rectangle holds
==“equals”the two sides are the same number
rows\text{rows}“rows”how many horizontal lines of numbers the rectangle has
×\times“times”multiply the thing on the left by the thing on the right
columns\text{columns}“columns”how many vertical lines of numbers the rectangle has
the round brackets“bracket”they group a quantity together so you can see it counts as one thing

Out loud. “The number of parameters in a matrix is its number of rows multiplied by its number of columns.”

Worked, step by step. The first attention query matrix in Qwen2.5-0.5B-Instruct has 896 rows and 896 columns.

Step 1, write down the two numbers: 896 and 896.

Step 2, multiply. On paper, split it:

896×900=806,400896 \times 900 = 806{,}400

896×4=3,584896 \times 4 = 3{,}584

806,4003,584=802,816806{,}400 - 3{,}584 = 802{,}816

So that one matrix holds 802,816 numbers.

Sanity check. The answer must be a whole number, and it must be at least as large as the bigger of the two sides. If your answer is smaller than 896, you added instead of multiplying. The measured statistics of this particular matrix drive all of Section 4: its standard deviation is 0.066741, its largest magnitude is 1.2266, and half of its weights are smaller in magnitude than 0.02698.

Chapter. Chapter 3


3. The probability of the next word

3.1 Softmax (USE)

In words. Softmax takes a list of scores, where a bigger score means the model likes that word more, and turns them into percentages that add up to 100%.

The formula.

pi=ezij=1Vezjp_i = \frac{e^{z_i}}{\displaystyle\sum_{j=1}^{V} e^{z_j}}

The symbols.

SymbolHow to say it out loudWhat it means
pip_i“p sub i”the answer: the probability of word number ii, a number between 0 and 1
==“equals”the two sides are the same number
ii“eye”a counter. i=1i = 1 means the first word, i=2i = 2 means the second word.
ziz_i“z sub i”the score the model gave to word number ii. Called a logit. It can be negative.
ee“e”a fixed number, 2.7182822.718282\ldots, in the same way that π\pi is a fixed number
ezie^{z_i}“e to the z sub i”raise ee to the power ziz_i. On a calculator this is the key marked exp or exe^x.
the raised position, as in ezie^{z_i}“to the power of”a number written up and to the right of another is an exponent, meaning a power
\sum“sum”, or “sigma”add up everything that follows, once for each value of the counter
j=1j=1 underneath the \sum“j equals one”start the counter at 1
VV above the \sum“vee”stop the counter at VV, the vocabulary size. For our model V=151,936V = 151{,}936.
jj“jay”a second counter, used inside the sum so it does not clash with ii
the fraction bar“divided by”divide the top by the bottom

Out loud. “The probability of a word is ee raised to that word’s score, divided by the sum of ee raised to every word’s score.”

Worked, step by step, with three scores: 2, 1, and 0. These three numbers are made up, and chosen so the arithmetic is short enough to check on paper.

Step 1, raise ee to each score.

e2=7.389056e^{2} = 7.389056

e1=2.718282e^{1} = 2.718282

e0=1.000000e^{0} = 1.000000

Step 2, add those three results together. This is what the \sum sign asked for.

7.389056+2.718282=10.1073387.389056 + 2.718282 = 10.107338

10.107338+1.000000=11.10733810.107338 + 1.000000 = 11.107338

Step 3, divide each result from Step 1 by that total.

p1=7.389056÷11.107338=0.665241p_1 = 7.389056 \div 11.107338 = 0.665241

p2=2.718282÷11.107338=0.244728p_2 = 2.718282 \div 11.107338 = 0.244728

p3=1.000000÷11.107338=0.090031p_3 = 1.000000 \div 11.107338 = 0.090031

stepword 1word 2word 3total
score ziz_i210
ezie^{z_i}7.3890562.7182821.00000011.107338
pi=ezi÷11.107338p_i = e^{z_i} \div 11.1073380.6652410.2447280.0900311.000000

Sanity check. Add your three probabilities. They must come to 1.

0.665241+0.244728=0.9099690.665241 + 0.244728 = 0.909969

0.909969+0.090031=1.0000000.909969 + 0.090031 = 1.000000

If they do not add to 1, you divided by the wrong total. Two more things must always be true. No probability can be negative, because ee raised to any power is positive. And the word with the largest score must end up with the largest probability, so the order 2, 1, 0 has to survive as 0.665, 0.245, 0.090. The real run does this same arithmetic 151,936 times.

Chapter. Chapter 4, The probability of the next word

3.2 The ratio form (READ)

In words. If you want to know how many times more likely one word is than another, you do not need the whole vocabulary. You need only the difference between their two scores.

The formula.

pipj=ezizj\frac{p_i}{p_j} = e^{\,z_i - z_j}

The symbols.

SymbolHow to say it out loudWhat it means
pip_i“p sub i”the probability of word ii
pjp_j“p sub j”the probability of some other word, word jj
the fraction bar“divided by”divide the top by the bottom. The answer says how many times bigger pip_i is than pjp_j.
==“equals”the two sides are the same number
ee“e”the fixed number 2.7182822.718282\ldots
zizjz_i - z_j“z sub i minus z sub j”the gap between the two scores
-“minus”subtract the number on the right from the number on the left
the raised position“to the power of”raise ee to whatever is written up there, gap and all

Out loud. “The ratio of two probabilities is ee raised to the difference between those two words’ scores.”

Worked, step by step, on the made-up scores from 3.1. Word 1 scored 2 and word 2 scored 1.

Step 1, subtract the scores. 21=12 - 1 = 1.

Step 2, raise ee to that. e1=2.718282e^{1} = 2.718282.

Step 3, check it against the probabilities you already computed in 3.1.

0.665241÷0.244728=2.7182870.665241 \div 0.244728 = 2.718287

The two answers agree to five decimal places. The last digit differs because the probabilities in 3.1 were rounded to six decimal places before being divided. That is rounding, not error.

Worked, step by step, on the real model run. On the prompt The capital of France is the top two logits were 17.21729 for the token ' Paris' and 16.31964 for the token ' ______'.

Step 1, subtract. 17.2172916.31964=0.8976517.21729 - 16.31964 = 0.89765.

Step 2, raise ee to that. e0.89765=2.45383e^{0.89765} = 2.45383.

Step 3, check against the recorded probabilities, 30.219%30.219\% and 12.315%12.315\%.

0.30219÷0.12315=2.45380.30219 \div 0.12315 = 2.4538

They agree to four decimal places.

Sanity check. If the score on top is the bigger one, your answer must be bigger than 1. If the two scores are equal, the difference is 0, and e0=1e^{0} = 1, so the ratio is exactly 1 and the two words are equally likely. An answer below 1 means you put the lower-scoring word on top. This formula also explains something useful: the enormous denominator in 3.1 cancels out, so you can compare two words without knowing anything about the other 151,934.

Chapter. Chapter 4

3.3 Softmax with temperature (USE)

In words. Temperature is a dial. Before you turn the scores into probabilities, you divide every score by the same number. A small number spreads the scores apart, which makes the leading word even more likely. A large number squashes the scores together, which shares the probability out more evenly.

The formula.

pi(T)=ezi/Tj=1Vezj/Tp_i(T) = \frac{e^{\,z_i/T}}{\displaystyle\sum_{j=1}^{V} e^{\,z_j/T}}

The symbols.

SymbolHow to say it out loudWhat it means
pi(T)p_i(T)“p sub i of tee”the answer: the probability of word ii when the temperature dial is set to TT
the round brackets in pi(T)p_i(T)“of”they say the answer depends on TT. They do not mean multiply.
TT“tee”the temperature, a positive number you choose. T=1T = 1 is the model’s own setting.
ziz_i“z sub i”the score for word ii
zi/Tz_i/T“z sub i over tee”divide the score by the temperature. This happens before the exponential.
the slash //“divided by”divide the number on the left by the number on the right
ee“e”the fixed number 2.7182822.718282\ldots
the raised position“to the power of”raise ee to whatever is written up there
j=1V\sum_{j=1}^{V}“the sum from j equals one to vee”add up what follows, once for every word in the vocabulary
the fraction bar“divided by”divide the top by the bottom
==“equals”the two sides are the same number

Out loud. “The probability of a word at temperature TT is ee raised to that word’s score divided by TT, divided by the sum of ee raised to every word’s score divided by TT.”

Worked, step by step, at T=0.5T = 0.5, on the same made-up scores 2, 1, 0.

Step 1, divide every score by T=0.5T = 0.5. Dividing by 0.5 is the same as multiplying by 2.

2÷0.5=42 \div 0.5 = 4

1÷0.5=21 \div 0.5 = 2

0÷0.5=00 \div 0.5 = 0

Step 2, raise ee to each of those.

e4=54.598150e^{4} = 54.598150

e2=7.389056e^{2} = 7.389056

e0=1.000000e^{0} = 1.000000

Step 3, add them up.

54.598150+7.389056=61.98720654.598150 + 7.389056 = 61.987206

61.987206+1.000000=62.98720661.987206 + 1.000000 = 62.987206

Step 4, divide each by that total.

p1=54.598150÷62.987206=0.866813p_1 = 54.598150 \div 62.987206 = 0.866813

p2=7.389056÷62.987206=0.117310p_2 = 7.389056 \div 62.987206 = 0.117310

p3=1.000000÷62.987206=0.015876p_3 = 1.000000 \div 62.987206 = 0.015876

Worked again, at T=2.0T = 2.0.

Step 1, divide every score by 2: 1, 0.5, 0.

Step 2, raise ee to each: e1=2.718282e^{1} = 2.718282, e0.5=1.648721e^{0.5} = 1.648721, e0=1.000000e^{0} = 1.000000.

Step 3, add them: 2.718282+1.648721=4.3670032.718282 + 1.648721 = 4.367003, then 4.367003+1.000000=5.3670034.367003 + 1.000000 = 5.367003.

Step 4, divide each by 5.367003: 0.506480, 0.307196, 0.186324.

Put the three settings side by side.

word 1word 2word 3
pip_i at T=0.5T = 0.50.8668130.1173100.015876
pip_i at T=1.0T = 1.00.6652410.2447280.090031
pip_i at T=2.0T = 2.00.5064800.3071960.186324

Sanity check. Each row must add to 1. Check the top row: 0.866813+0.117310=0.9841230.866813 + 0.117310 = 0.984123, then 0.984123+0.015876=0.9999990.984123 + 0.015876 = 0.999999, which is 1 apart from rounding in the sixth decimal place. Then check the ordering. Word 1 is first in every row and word 3 is last in every row. If your ordering changed, you made an arithmetic mistake, because temperature cannot reorder words. Section 3.4 shows why.

Chapter. Chapter 5, Temperature, and what it does not do

3.4 Temperature as a power, the equivalent form (READ)

In words. You can get the temperature-adjusted probabilities from the ordinary probabilities alone, without ever seeing the scores. Raise each probability to the power one-over-the-temperature, then divide everything by the new total so it adds to 1 again.

The formula.

pi(T)=(pi(1))1/Tj=1V(pj(1))1/Tp_i(T) = \frac{\big(p_i(1)\big)^{1/T}}{\displaystyle\sum_{j=1}^{V} \big(p_j(1)\big)^{1/T}}

The symbols.

SymbolHow to say it out loudWhat it means
pi(T)p_i(T)“p sub i of tee”the answer: the probability of word ii at temperature TT
pi(1)p_i(1)“p sub i of one”the probability of word ii at T=1T = 1, that is, plain softmax from 3.1
1/T1/T“one over tee”the reciprocal of the temperature. At T=0.5T = 0.5 this is 2; at T=2T = 2 it is 0.5.
the big round brackets“bracket”they group the whole probability together, so the power applies to all of it
the raised 1/T1/T“to the power one over tee”raise the bracketed number to that power. A power of 2 means square it; a power of 0.5 means take its square root.
j=1V\sum_{j=1}^{V}“the sum from j equals one to vee”add up the same quantity for every word in the vocabulary
the fraction bar“divided by”divide the top by the bottom. This is the renormalising step.
==“equals”the two sides are the same number

Out loud. “The probability of a word at temperature TT is that word’s ordinary probability raised to the power one over TT, divided by the sum of every word’s ordinary probability raised to the power one over TT.”

Worked, step by step, at T=2T = 2. Here 1/T=1÷2=0.51/T = 1 \div 2 = 0.5, and raising to the power 0.5 is taking a square root. Start from the T=1T = 1 probabilities computed in 3.1.

Step 1, take the square root of each probability.

0.665241=0.815623\sqrt{0.665241} = 0.815623

0.244728=0.494700\sqrt{0.244728} = 0.494700

0.090031=0.300052\sqrt{0.090031} = 0.300052

Step 2, add the three roots.

0.815623+0.494700=1.3103230.815623 + 0.494700 = 1.310323

1.310323+0.300052=1.6103751.310323 + 0.300052 = 1.610375

Step 3, divide each root by that total.

0.815623÷1.610375=0.5064800.815623 \div 1.610375 = 0.506480

0.494700÷1.610375=0.3071960.494700 \div 1.610375 = 0.307196

0.300052÷1.610375=0.1863240.300052 \div 1.610375 = 0.186324

Compare with 3.3, where dividing the scores by 2 first gave 0.506480, 0.307196, 0.186324. The two routes agree. Carried out at full machine precision they agree to better than one part in a trillion: both give 0.50648039, 0.30719589, 0.18632372.

Why it is the same formula

Exponentials turn division in the exponent into a power: ezi/T=(ezi)1/Te^{\,z_i/T} = \big(e^{z_i}\big)^{1/T}.

Now pi(1)=ezi/Sp_i(1) = e^{z_i}/S where S=jezjS = \sum_j e^{z_j}, so (pi(1))1/T=(ezi)1/T/S1/T\big(p_i(1)\big)^{1/T} = \big(e^{z_i}\big)^{1/T} / S^{1/T}.

The factor S1/TS^{1/T} is the same for every word, so it appears in the numerator and in every term of the denominator, and it cancels. What is left is exactly the formula in 3.3. The constant you divided by does not survive a renormalisation, which is the whole trick.

Worked again, at T=0.5T = 0.5, where 1/T=21/T = 2 and raising to the power 2 means squaring.

0.665241×0.665241=0.442545590.665241 \times 0.665241 = 0.44254559

0.244728×0.244728=0.059891790.244728 \times 0.244728 = 0.05989179

0.090031×0.090031=0.008105580.090031 \times 0.090031 = 0.00810558

0.44254559+0.05989179=0.502437380.44254559 + 0.05989179 = 0.50243738, then 0.50243738+0.00810558=0.510542960.50243738 + 0.00810558 = 0.51054296

0.44254559÷0.51054296=0.8668140.44254559 \div 0.51054296 = 0.866814

0.05989179÷0.51054296=0.1173100.05989179 \div 0.51054296 = 0.117310

0.00810558÷0.51054296=0.0158760.00810558 \div 0.51054296 = 0.015876

Compare that with 3.3, which gave 0.866813, 0.117310, 0.015876. The first answer differs by one in the sixth decimal place, because you started from probabilities that had already been rounded to six decimal places and then squared them. That is rounding carried forward, not an error in the formula.

Check it on the real run. At T=1T = 1 the top token is 2.45383 times as probable as the runner-up, which is what 3.2 worked out. Squaring that gives 2.45383×2.45383=6.02132.45383 \times 2.45383 = 6.0213. The recorded probabilities at T=0.5T = 0.5 are 73.430%73.430\% and 12.195%12.195\%, and 0.73430÷0.12195=6.02130.73430 \div 0.12195 = 6.0213. They match.

Sanity check. Your answers must still add to 1 after Step 3, and the order must not have changed. Here is the reason the order cannot change, and it is worth reading twice. Raising every probability to the same positive power keeps them in the same order, because a bigger number stays bigger when you square it, and a bigger number stays bigger when you take its square root. Dividing them all by the same total keeps them in the same order too. So nothing in this formula can promote one word above another. That is the cleanest proof that temperature cannot reorder tokens.

Chapter. Chapter 5

3.5 Entropy in bits (USE)

In words. Entropy is one number saying how undecided the model is. If the model is certain of one word, entropy is zero. The more candidates share the probability, the bigger entropy gets.

The formula.

H=i=1Vpilog2piH = -\sum_{i=1}^{V} p_i \log_2 p_i

The symbols.

SymbolHow to say it out loudWhat it means
HH“aitch”the answer: the entropy, measured in bits
==“equals”the two sides are the same number
the leading -“minus”, or “negative”flip the sign of everything that follows. It is there because the logarithm of a number below 1 is negative, and entropy is reported as a positive number.
\sum“sum”, or “sigma”add up what follows, once for each value of the counter
i=1i=1 underneath“i equals one”start the counter at the first word
VV above“vee”stop at the last word in the vocabulary
pip_i“p sub i”the probability of word ii
log2\log_2“log base two”the logarithm base 2. It answers the question “2 raised to what power gives this number?” So log28=3\log_2 8 = 3, because 23=82^3 = 8.
log2pi\log_2 p_i“log base two of p sub i”apply that question to the probability. Because every pip_i is below 1, every answer is negative.
the space between pip_i and log2pi\log_2 p_i“times”two things written next to each other are multiplied

Out loud. “Entropy is minus the sum, over every word, of that word’s probability multiplied by the logarithm base two of that same probability.”

Worked, step by step, on three made-up probabilities: 0.5, 0.25, 0.25. These were chosen because every logarithm lands on a whole number.

Step 1, take the logarithm base 2 of each probability.

log20.50=1\log_2 0.50 = -1, because 21=0.52^{-1} = 0.5

log20.25=2\log_2 0.25 = -2, because 22=0.252^{-2} = 0.25

log20.25=2\log_2 0.25 = -2

Step 2, multiply each probability by its own logarithm.

0.50×(1)=0.500.50 \times (-1) = -0.50

0.25×(2)=0.500.25 \times (-2) = -0.50

0.25×(2)=0.500.25 \times (-2) = -0.50

Step 3, add those three products.

0.50+(0.50)=1.00-0.50 + (-0.50) = -1.00, then 1.00+(0.50)=1.50-1.00 + (-0.50) = -1.50

Step 4, flip the sign, because of the minus at the front of the formula.

H=1.5H = 1.5 bits

Worked again, on the three probabilities from 3.1. These do not land on whole numbers, so use the log button on a calculator and divide the answer by log2\log 2, or use a log2 button if your calculator has one.

pip_ilog2pi\log_2 p_ipi×log2pip_i \times \log_2 p_ipilog2pi-p_i \log_2 p_i
0.665241-0.588051-0.3911960.391196
0.244728-2.030746-0.4969810.496981
0.090031-3.473441-0.3127160.312716

0.391196+0.496981=0.8881770.391196 + 0.496981 = 0.888177

0.888177+0.312716=1.2008930.888177 + 0.312716 = 1.200893

So H=1.200893H = 1.200893 bits. Repeating that for the other two temperature settings gives the table that makes the temperature lesson quantitative.

TTp1p_1p2p_2p3p_3entropy HH (bits)
0.50.8668130.1173100.0158760.636311
1.00.6652410.2447280.0900311.200893
2.00.5064800.3071960.1863241.471825

Sanity check. Entropy can never be negative. If yours is, you left off the minus sign at the front. Entropy also has a ceiling: it can never exceed log2\log_2 of the number of choices. With three choices the ceiling is log23=1.584963\log_2 3 = 1.584963 bits, and the largest number in the table above is 1.471825, which sits under it. Two values worth committing to memory: a fair coin gives exactly 1 bit, and a certain outcome gives exactly 0 bits.

What the real run gives. On the same prompt, entropy over the full 151,936-token vocabulary was 0.237 bits at T=0.25T = 0.25, 4.450 bits at T=1.0T = 1.0, and 13.367 bits at T=2.0T = 2.0. The ceiling for this vocabulary is log2151,936=17.2131\log_2 151{,}936 = 17.2131 bits, which only a perfectly flat distribution would reach.

Chapter. Chapter 5

3.6 How many tokens hold 90% of the probability (USE)

In words. Sort the words from most likely to least likely, then start adding their probabilities from the top. Count how many words you need before the running total reaches nine tenths. That count is an honest answer to “how many words is the model really considering?”

The formula.

k0.9=min{k : p(1)+p(2)++p(k)  0.9}k_{0.9} = \min\Big\{\,k \ : \ p_{(1)} + p_{(2)} + \cdots + p_{(k)} \ \ge\ 0.9 \,\Big\}

The symbols.

SymbolHow to say it out loudWhat it means
k0.9k_{0.9}“k sub nought point nine”the answer: a whole number, how many words it takes to reach 90%
==“equals”the two sides are the same number
min\min“min”, short for “minimum”the smallest value that works
the curly brackets“the set of”they hold a description of a collection of numbers
the colon“such that”it separates which numbers we are talking about from the condition they must meet
kk“kay”a counter: how many words you have added so far
p(1)p_{(1)}“p bracket one”the largest probability. The brackets around the number mean the list has been sorted, largest first.
p(2)p_{(2)}“p bracket two”the second largest probability, and so on down the list
++“plus”add
\cdots“and so on”keep adding in the same pattern
\ge“is greater than or equal to”the thing on the left is at least as big as the thing on the right
0.9“nought point nine”nine tenths, which is 90%

Out loud.kk sub nought point nine is the smallest number of words you can take from the top of the sorted list whose probabilities add to at least nine tenths.”

Worked, step by step, on the made-up probabilities at T=1T = 1: 0.665241, 0.244728, 0.090031. They are already sorted from largest to smallest.

Step 1, take the top one. Running total =0.665241= 0.665241. Is 0.6652410.90.665241 \ge 0.9? No.

Step 2, add the next one. 0.665241+0.244728=0.9099690.665241 + 0.244728 = 0.909969. Is 0.9099690.90.909969 \ge 0.9? Yes.

Step 3, stop, and report how many words you used. k0.9=2k_{0.9} = 2.

Worked again, on the same words at T=2T = 2: 0.506480, 0.307196, 0.186324.

Step 1, running total =0.506480= 0.506480. Below 0.9.

Step 2, 0.506480+0.307196=0.8136760.506480 + 0.307196 = 0.813676. Still below 0.9.

Step 3, 0.813676+0.186324=1.0000000.813676 + 0.186324 = 1.000000. At or above 0.9, so k0.9=3k_{0.9} = 3.

Raising the temperature from 1 to 2 took the count from 2 to 3. That is the same movement the entropy showed in 3.5, in a unit anyone can picture.

Sanity check. k0.9k_{0.9} must be a whole number, at least 1, and no larger than the number of words. It can never go down when you raise the temperature. If your running total passes 1, you are adding probabilities that were never a valid distribution, so go back and check that they added to 1 in the first place.

What the real run gives. At T=0.25T = 0.25 the top token alone carries 96.799%96.799\%, which already clears 90%90\%, so k0.9=1k_{0.9} = 1. One word holds nine tenths of the probability. At T=2.0T = 2.0 on the same logits it takes 41,274 tokens to reach the same threshold. That pair of numbers is what temperature actually does.

Chapter. Chapter 5


4. Making models small

4.1 Symmetric linear quantization: the scale (USE)

In words. Storing a weight with fewer bits means you can only keep a small number of different values. Line those allowed values up evenly on either side of zero, like marks on a ruler. This works out how far apart the marks have to be so that the largest weight in the group still lands on the ruler.

The formula.

s=maxiwi2b11s = \frac{\displaystyle\max_i \lvert w_i \rvert}{2^{\,b-1} - 1}

The symbols.

SymbolHow to say it out loudWhat it means
ss“ess”the answer: the scale, the gap between two neighbouring marks on the ruler
==“equals”the two sides are the same number
wiw_i“w sub i”weight number ii in the group being quantized together
wi\lvert w_i \rvert“the absolute value of w sub i”, or “mod w sub i”the size of the weight with its minus sign thrown away. 3=3\lvert -3 \rvert = 3 and 3=3\lvert 3 \rvert = 3.
the two upright bars“absolute value”they are the throw-away-the-sign instruction, nothing to do with division
maxi\max_i“the maximum over i”look at every weight in the group and keep the biggest answer
bb“bee”how many bits you are spending on each stored weight
b1b-1“bee minus one”one bit is spent recording whether the weight is positive or negative, so b1b-1 bits are left for the size
2b12^{\,b-1}“two to the bee minus one”2 multiplied by itself b1b-1 times. This is how many different sizes those bits can express.
1-\,1“minus one”subtract one, because one of the levels has to be zero itself
the fraction bar“divided by”divide the top by the bottom

Out loud. “The scale is the largest weight size in the group, divided by the number of whole steps that fit on each side of zero.”

Worked, step by step, at 8 bits. The real weight matrix from 2.2 has maxiwi=1.2265625\max_i \lvert w_i \rvert = 1.2265625.

Step 1, work out b1b - 1. With b=8b = 8 bits, 81=78 - 1 = 7.

Step 2, raise 2 to that power. That means 2 multiplied by itself seven times, so 27=1282^{7} = 128.

Step 3, subtract 1. 1281=127128 - 1 = 127. So 127 whole steps fit on each side of zero.

Step 4, divide. 1.2265625÷127=0.009657971.2265625 \div 127 = 0.00965797.

Worked again, at 4 bits.

Step 1, 41=34 - 1 = 3.

Step 2, 23=2×2×2=82^{3} = 2 \times 2 \times 2 = 8.

Step 3, 81=78 - 1 = 7.

Step 4, 1.2265625÷7=0.175223211.2265625 \div 7 = 0.17522321.

Every bit width, on the same matrix:

bb2b12^{b-1}steps each side, 2b112^{b-1}-1scale ss
81281270.0096580
632310.0395665
4870.1752232
3430.4088542
2211.2265625

Hold on to the 4-bit scale, 0.1752232. It is used in 4.2 and 4.3.

Sanity check. The scale is always positive, and it always gets bigger as bb gets smaller, because fewer bits means fewer marks on the same ruler, so the marks must be further apart. Here is the direct check: divide the largest weight by the scale and you must land exactly on the top step. 1.2265625÷0.00965797=127.01.2265625 \div 0.00965797 = 127.0, which is the number from Step 3. If you get something else, you used the wrong bb.

Chapter. Chapter 7, Quantization: what you lose, what it buys

4.2 Quantize, then dequantize (USE)

In words. Take each weight, work out which mark on the ruler it is nearest to, and store only the number of that mark. Later, when the model runs, multiply the mark number by the gap between marks to get a weight back. The number you get back is close to the original, and the original is gone for good.

The formula.

qi=round ⁣(wis),w^i=qisq_i = \operatorname{round}\!\left(\frac{w_i}{s}\right), \qquad \hat{w}_i = q_i \, s

The symbols.

SymbolHow to say it out loudWhat it means
qiq_i“q sub i”the whole number actually stored in place of weight ii. It sits between (2b11)-(2^{\,b-1}-1) and +(2b11)+(2^{\,b-1}-1).
==“equals”the two sides are the same number
round\operatorname{round}“round”round to the nearest whole number. 2.4 rounds to 2; 2.6 rounds to 3; -1.9 rounds to -2.
wiw_i“w sub i”the original weight
ss“ess”the scale from 4.1
the fraction bar“divided by”divide the top by the bottom
the commano soundit separates the two formulas. This entry has two, done in order.
w^i\hat{w}_i“w hat sub i”the dequantized weight: the value the model actually uses once it has been shrunk
the hat“hat”it marks a number that stands in for another number, rather than being the real thing
the space between qiq_i and ss“times”two things written next to each other are multiplied

Out loud. “The stored number is the weight divided by the scale, rounded to the nearest whole number; the weight you get back is that stored number multiplied by the scale.”

Worked, step by step, at 8 bits. Take the typical weight of that matrix, w=0.02697754w = -0.02697754. It is the median magnitude, so half of the matrix is smaller than this.

Step 1, write down the 8-bit scale from 4.1: s=0.00965797s = 0.00965797.

Step 2, divide the weight by the scale. 0.02697754÷0.00965797=2.79329-0.02697754 \div 0.00965797 = -2.79329

Step 3, round to the nearest whole number. -2.79329 sits between -3 and -2, and it is nearer to -3, so q=3q = -3.

Step 4, multiply back to get the dequantized weight. 3×0.00965797=0.02897391-3 \times 0.00965797 = -0.02897391

Step 5, work out how far off you are. 0.028973910.02697754=0.001996370.02897391 - 0.02697754 = 0.00199637

Worked again, at 4 bits.

Step 1, the 4-bit scale from 4.1: s=0.17522321s = 0.17522321.

Step 2, 0.02697754÷0.17522321=0.15396-0.02697754 \div 0.17522321 = -0.15396.

Step 3, round. -0.15396 is nearer to 0 than to -1, so q=0q = \mathbf{0}.

Step 4, 0×0.17522321=00 \times 0.17522321 = \mathbf{0}.

Step 5, the error is the whole weight: 0.02697754.

bbssw÷sw \div sqqw^=q×s\hat{w} = q \times serror
80.00965797-2.79329-3-0.028973910.00199637
40.17522321-0.153960\mathbf{0}0\mathbf{0}0.02697754

At 8 bits the weight survives with a small error. At 4 bits it rounds to zero and is erased. The lab run records -0.02897392 for the 8-bit case, which matches the hand arithmetic to seven decimal places; the last digit differs because the scale was rounded to eight places above.

Sanity check. qq must be a whole number, and it must sit inside the range (2b11)-(2^{b-1}-1) to +(2b11)+(2^{b-1}-1). At 4 bits that range is -7 to +7, and 0 is inside it. The dequantized weight must have the same sign as the original, or be zero. And the error can never be bigger than half the scale: at 4 bits half the scale is 0.17522321÷2=0.087611610.17522321 \div 2 = 0.08761161, and the error 0.02697754 is under that.

Chapter. Chapter 7

4.3 Quantization error, measured against the spread of the weights (READ)

In words. Work out how far off each weight is on average, then compare that to how spread out the weights were in the first place. Reporting the error as a share of the spread makes “how bad is 4-bit?” a question with an answer that does not depend on the units.

The formula.

relative error=1Ni=1Nw^iwisd(w)\text{relative error} = \frac{\dfrac{1}{N}\displaystyle\sum_{i=1}^{N} \big\lvert \hat{w}_i - w_i \big\rvert}{\operatorname{sd}(w)}

The symbols.

SymbolHow to say it out loudWhat it means
relative error\text{relative error}“relative error”the answer: a number usually written as a percentage
==“equals”the two sides are the same number
NN“en”how many weights are in the group
1N\dfrac{1}{N}“one over en”dividing by NN after adding NN things up is how you take an average
i=1N\sum_{i=1}^{N}“the sum from i equals one to en”add up what follows, once for every weight
w^i\hat{w}_i“w hat sub i”the weight you get back after quantizing, from 4.2
wiw_i“w sub i”the original weight
w^iwi\hat{w}_i - w_i“w hat sub i minus w sub i”the rounding error on that one weight. It can be positive or negative.
the two upright bars“absolute value”throw away the minus sign, so errors in opposite directions do not cancel each other out
sd(w)\operatorname{sd}(w)“the standard deviation of w”one number saying how spread out the original weights were. A big standard deviation means the weights are far apart.
the big fraction bar“divided by”divide the average error on top by the spread underneath

Out loud. “The relative error is the average size of the rounding errors, divided by the standard deviation of the weights themselves.”

Worked, step by step, on four made-up weights. Take 0.10, -0.06, 0.03, -0.02697754, quantized at 4 bits with the whole-matrix scale s=0.17522321s = 0.17522321 from 4.1. The last of the four is the real typical weight from 4.2; the other three are chosen so the arithmetic is short.

Step 1, quantize and dequantize each one, using 4.2.

wiw_iwi÷sw_i \div sqiq_iw^i\hat{w}_iw^iwi\lvert \hat{w}_i - w_i \rvert
0.100000000.570710.175223210.07522321
-0.06000000-0.3424000.06000000
0.030000000.1712000.03000000
-0.02697754-0.1540000.02697754

Step 2, add the four errors.

0.07522321+0.06000000=0.135223210.07522321 + 0.06000000 = 0.13522321

0.13522321+0.03000000=0.165223210.13522321 + 0.03000000 = 0.16522321

0.16522321+0.02697754=0.192200750.16522321 + 0.02697754 = 0.19220075

Step 3, divide by N=4N = 4 to get the average error.

0.19220075÷4=0.048050190.19220075 \div 4 = 0.04805019

Step 4, work out the standard deviation of the four original weights. First their average.

0.10+(0.06)+0.03+(0.02697754)=0.043022460.10 + (-0.06) + 0.03 + (-0.02697754) = 0.04302246

0.04302246÷4=0.010755620.04302246 \div 4 = 0.01075562

Step 5, subtract that average from each weight and square the result.

(0.100000000.01075562)2=(0.08924438)2=0.00796456(0.10000000 - 0.01075562)^2 = (0.08924438)^2 = 0.00796456

(0.060000000.01075562)2=(0.07075562)2=0.00500636(-0.06000000 - 0.01075562)^2 = (-0.07075562)^2 = 0.00500636

(0.030000000.01075562)2=(0.01924438)2=0.00037035(0.03000000 - 0.01075562)^2 = (0.01924438)^2 = 0.00037035

(0.026977540.01075562)2=(0.03773316)2=0.00142379(-0.02697754 - 0.01075562)^2 = (-0.03773316)^2 = 0.00142379

Step 6, add those four squares.

0.00796456+0.00500636=0.012970920.00796456 + 0.00500636 = 0.01297092

0.01297092+0.00037035=0.013341270.01297092 + 0.00037035 = 0.01334127

0.01334127+0.00142379=0.014765060.01334127 + 0.00142379 = 0.01476506

Step 7, divide by N1=3N - 1 = 3, then take the square root.

0.01476506÷3=0.004921690.01476506 \div 3 = 0.00492169

0.00492169=0.07015476\sqrt{0.00492169} = 0.07015476

Step 8, divide the average error by the standard deviation.

0.04805019÷0.07015476=0.6849170.04805019 \div 0.07015476 = 0.684917

0.684917×100=68.5%0.684917 \times 100 = \mathbf{68.5\%}

What the real matrix gives. Those four weights are a made-up sample, not the matrix. Across all 802,816 weights, the measured mean absolute error at 4 bits with one scale for the whole matrix is 0.031098, and the measured standard deviation of the weights is 0.066741.

0.031098÷0.066741=0.46600.031098 \div 0.066741 = 0.4660, which is the 46.6% quoted in Chapter 7.

With one scale per 32 weights the mean error drops to 0.004831, and 0.004831÷0.066741=0.07240.004831 \div 0.066741 = 0.0724, or 7.2%.

bitsone scale per matrixone scale per 32 weights
83.6%0.4%
614.7%1.6%
446.6%7.2%
358.9%16.8%
262.7%44.7%

Sanity check. Relative error can never be negative, because both the top and the bottom are built from things that cannot be negative. It should also fall as you spend more bits: reading either column of that table upwards, the numbers get smaller, which is the direction you would expect. If your relative error rises when you add bits, you used the wrong scale somewhere.

Chapter. Chapter 7

4.4 Blockwise quantization (USE)

In words. Instead of one ruler for the whole matrix, chop the weights into short runs of 32 and give each run its own ruler, sized by the largest weight in that run alone. Then one unusually large weight can only spoil the 31 weights next to it, rather than all 802,816.

The formula.

s(k)=maxiblock kwi2b11,qi=round ⁣(wis(k)),w^i=qis(k)s^{(k)} = \frac{\displaystyle\max_{i \,\in\, \text{block } k} \lvert w_i \rvert}{2^{\,b-1} - 1}, \qquad q_i = \operatorname{round}\!\left(\frac{w_i}{s^{(k)}}\right), \qquad \hat{w}_i = q_i \, s^{(k)}

The symbols.

SymbolHow to say it out loudWhat it means
a block“block”a short run of BB weights that sit next to each other. B=32B = 32 throughout this book.
kk“kay”which block you are in: block 1, block 2, and so on
s(k)s^{(k)}“ess bracket kay”that block’s own scale. The raised (k)(k) is a label, not a power.
the raised bracketed (k)(k)“bracket kay”a label saying which block. It is not an exponent.
maxiblock k\max_{i \in \text{block } k}“the maximum over i in block kay”look only at the weights inside block kk and keep the biggest answer
\in“in”, or “is an element of”says which weights you are allowed to look at
wi\lvert w_i \rvert“absolute value of w sub i”the weight with its minus sign thrown away
2b112^{\,b-1} - 1“two to the bee minus one, minus one”the number of whole steps on each side of zero, exactly as in 4.1
qiq_i“q sub i”the whole number stored for weight ii
w^i\hat{w}_i“w hat sub i”the weight you get back
round\operatorname{round}“round”round to the nearest whole number
the fraction bar“divided by”divide the top by the bottom
the commasno soundthey separate three formulas carried out in order

Out loud. “Each block gets its own scale, which is the largest weight size inside that block divided by the number of steps; then every weight in the block is divided by that scale and rounded, and multiplied back by that same scale to be read.”

Worked, step by step, on a block of four weights at 4 bits. The block is 0.10, -0.06, 0.03, -0.02697754, the same four as in 4.3. Real blocks hold 32 weights; four keeps the arithmetic short.

Step 1, find the largest magnitude inside the block. The four magnitudes are 0.10, 0.06, 0.03 and 0.02697754, so the largest is 0.10.

Step 2, work out the number of steps, exactly as in 4.1. At b=4b = 4 this is 231=81=72^{3} - 1 = 8 - 1 = 7.

Step 3, divide to get this block’s scale. s(k)=0.10÷7=0.01428571s^{(k)} = 0.10 \div 7 = 0.01428571

Step 4, quantize and dequantize each weight with that scale.

wiw_iwi÷s(k)w_i \div s^{(k)}qiq_iw^i=qi×s(k)\hat{w}_i = q_i \times s^{(k)}error
0.100000007.0000070.099999970.00000003
-0.06000000-4.20000-4-0.057142840.00285716
0.030000002.1000020.028571420.00142858
-0.02697754-1.88843-2-0.028571420.00159388

Take the fourth row line by line. 0.02697754÷0.01428571=1.88843-0.02697754 \div 0.01428571 = -1.88843, which is nearer to -2 than to -1, so q=2q = -2. Then 2×0.01428571=0.02857142-2 \times 0.01428571 = -0.02857142, and the error is 0.028571420.02697754=0.001593880.02857142 - 0.02697754 = 0.00159388.

Step 5, redo the relative error from 4.3 on these new numbers.

0.00000003+0.00285716=0.002857190.00000003 + 0.00285716 = 0.00285719

0.00285719+0.00142858=0.004285770.00285719 + 0.00142858 = 0.00428577

0.00428577+0.00159388=0.005879650.00428577 + 0.00159388 = 0.00587965

0.00587965÷4=0.001469910.00587965 \div 4 = 0.00146991

The standard deviation of the four weights has not changed, because the weights have not changed: it is still 0.07015476 from 4.3.

0.00146991÷0.07015476=0.0209520.00146991 \div 0.07015476 = 0.020952, which is 2.1%.

Compare with 4.3, where the same four weights under the whole-matrix scale gave 68.5%. The arithmetic is identical apart from which maximum you divided by. That one change took the error from 68.5% of the spread to 2.1% of it.

Sanity check. Inside a block, the largest weight must come back exactly, or very nearly, so check it first: 0.10 came back as 0.09999997. A block scale can never be larger than the whole-matrix scale, because the largest weight in one block cannot exceed the largest weight overall. So blockwise error can never be worse than whole-matrix error. If yours is, you took the maximum over the wrong set of weights.

Chapter. Chapter 7

4.5 Bytes per weight, including the block scales (USE)

In words. Every block needs its own scale written down somewhere, and that scale takes up room too. This counts the honest storage cost: the bits for the weight itself, plus each weight’s share of its block’s scale, converted from bits into bytes.

The formula.

bytes per weight=b+bscaleB8\text{bytes per weight} = \frac{b + \dfrac{b_{\text{scale}}}{B}}{8}

The symbols.

SymbolHow to say it out loudWhat it means
bytes per weight\text{bytes per weight}“bytes per weight”the answer. It can be less than 1, which is the point of the exercise.
==“equals”the two sides are the same number
bb“bee”bits spent on one stored weight
++“plus”add
bscaleb_{\text{scale}}“bee sub scale”bits spent on one block scale. It is 16 in this book, because the scale is kept in half precision.
BB“capital bee”how many weights share that one scale. It is 32 in this book.
the small fraction bar“divided by”dividing the scale’s bits by the number of weights sharing it gives each weight’s share of the bookkeeping
the big fraction bar“divided by”divide everything above it by the 8 below it
8“eight”there are 8 bits in a byte, so dividing by 8 turns bits into bytes

Out loud. “Bytes per weight is the bits used for a weight, plus the bits used for a block scale shared out across the weights in that block, all divided by eight.”

Worked, step by step, for 4-bit weights with a 16-bit scale per 32 weights.

Step 1, work out the bookkeeping share. 16÷32=0.516 \div 32 = 0.5 bits per weight

Step 2, add it to the bits spent on the weight itself. 4+0.5=4.54 + 0.5 = 4.5 bits per weight

Step 3, turn bits into bytes by dividing by 8. 4.5÷8=0.56254.5 \div 8 = \mathbf{0.5625} bytes per weight

Step 4, compare with the cost if you ignored the scales. 4÷8=0.54 \div 8 = 0.5 bytes per weight, and 0.5625÷0.5=1.1250.5625 \div 0.5 = 1.125, so the bookkeeping costs 12.5% on top. That is the whole overhead.

schemebbbscale÷Bb_{\text{scale}} \div Bbytes per weight
FP323204.0000
FP161602.0000
INT8801.0000
4-bit, 16-bit scale per 3240.50.5625
4-bit, no overhead counted400.5000

Sanity check. Multiply your answer by 8 and you must get back the total bits per weight. For the 4-bit blockwise row, 0.5625×8=4.50.5625 \times 8 = 4.5, which is the number from Step 2. When there are no block scales the middle term is 0 and the formula becomes b÷8b \div 8, so 32-bit storage gives 32÷8=432 \div 8 = 4 bytes, which is what FP32 means.

Chapter. Chapter 6, Bits, precision, and rounding and Chapter 7

4.6 Model size on disk (USE)

In words. Multiply how many parameters the model has by how many bytes each one takes up. That gives you the size of the file you are about to download.

The formula.

size in bytes=N×(bytes per weight)\text{size in bytes} = N \times (\text{bytes per weight})

The symbols.

SymbolHow to say it out loudWhat it means
size in bytes\text{size in bytes}“size in bytes”the answer, in bytes
==“equals”the two sides are the same number
NN“en”the parameter count. N=494,032,768N = 494{,}032{,}768 for Qwen2.5-0.5B-Instruct.
×\times“times”multiply
bytes per weight\text{bytes per weight}“bytes per weight”the answer from 4.5
the round brackets“bracket”they group the quantity so you can see it counts as one number

Throughout this book GB means 109 bytes, which is how model files are usually quoted.

Out loud. “The size in bytes is the number of parameters multiplied by the number of bytes each parameter takes up.”

Worked, step by step, at FP16.

Step 1, write down the parameter count: 494,032,768494{,}032{,}768.

Step 2, write down the bytes per weight from 4.5: FP16 is 16 bits, and 16÷8=216 \div 8 = 2 bytes.

Step 3, multiply. 494,032,768×2=988,065,536494{,}032{,}768 \times 2 = 988{,}065{,}536 bytes

Step 4, turn bytes into gigabytes by dividing by 1,000,000,0001{,}000{,}000{,}000. 988,065,536÷1,000,000,000=0.988988{,}065{,}536 \div 1{,}000{,}000{,}000 = 0.988 GB

That is why a “0.5B model” arrives as a file a little under a gigabyte.

Worked again, at 4 bits with block scales.

Step 1, N=494,032,768N = 494{,}032{,}768.

Step 2, bytes per weight from 4.5: 0.5625.

Step 3, 494,032,768×0.5625=277,893,432494{,}032{,}768 \times 0.5625 = 277{,}893{,}432 bytes.

Step 4, 277,893,432÷1,000,000,000=0.278277{,}893{,}432 \div 1{,}000{,}000{,}000 = 0.278 GB.

schemebytes per weightsize in bytessize
FP324.00001,976,131,0721.976 GB
FP162.0000988,065,5360.988 GB
INT81.0000494,032,7680.494 GB
4-bit, 16-bit scale per 320.5625277,893,4320.278 GB
4-bit, no overhead counted0.5000247,016,3840.247 GB

Sanity check. Halving the bytes per weight must halve the file size. Read the table down from FP32 to FP16 to INT8: 1.976, then 0.988, then 0.494, each one half the last. If your numbers do not halve, check whether you divided by 8 when converting bits to bytes. One more check on the size of the answer: at FP32 this model is about 2 GB, and 2 GB for half a billion numbers at 4 bytes each is the right order of magnitude.

Chapter. Chapter 3, Chapter 6, Chapter 7


5. Meaning as geometry

A vector is an ordered list of numbers, such as (3,4)(3, 4). When there are two numbers in the list you can draw it as an arrow on graph paper: go 3 across, then 4 up. The models in this book use lists of 384 numbers, which nobody can draw, but every formula below works the same way no matter how long the list is.

5.1 Dot product (USE)

In words. Take two lists of numbers of the same length. Multiply the first number of one list by the first number of the other, then the second by the second, and so on. Add up all those products. The answer is one number.

The formula.

uv=i=1Duivi=u1v1+u2v2++uDvD\mathbf{u} \cdot \mathbf{v} = \sum_{i=1}^{D} u_i v_i = u_1 v_1 + u_2 v_2 + \cdots + u_D v_D

The symbols.

SymbolHow to say it out loudWhat it means
u\mathbf{u}, v\mathbf{v}“bold u”, “bold v”two vectors, each a list of DD numbers. Bold type marks a list rather than a single number.
\cdot“dot”the dot product operation itself. It is the name of the whole procedure, not plain multiplication.
==“equals”the two sides are the same number
i=1D\sum_{i=1}^{D}“the sum from i equals one to capital dee”add up what follows, once for each position in the list
ii“eye”which position in the list you are looking at
uiu_i“u sub i”the number in position ii of the list u\mathbf{u}
viv_i“v sub i”the number in position ii of the list v\mathbf{v}
uiviu_i v_i“u sub i times v sub i”two things written next to each other are multiplied
DD“capital dee”how many numbers are in each list. Both lists must have the same DD.
++“plus”add
\cdots“and so on”keep going in the same pattern to the end of the list

Out loud. “The dot product of two vectors is the sum, over every position in the list, of the number in that position of the first vector multiplied by the number in that position of the second.”

Worked, step by step, with a=(3,4)\mathbf{a} = (3, 4) and b=(4,3)\mathbf{b} = (4, 3). Here D=2D = 2.

Step 1, multiply the first numbers. 3×4=123 \times 4 = 12.

Step 2, multiply the second numbers. 4×3=124 \times 3 = 12.

Step 3, add the two products. 12+12=2412 + 12 = \mathbf{24}.

Worked again, with a=(3,4)\mathbf{a} = (3, 4) and c=(4,3)\mathbf{c} = (-4, 3).

Step 1, 3×(4)=123 \times (-4) = -12. A positive times a negative gives a negative.

Step 2, 4×3=124 \times 3 = 12.

Step 3, 12+12=0-12 + 12 = \mathbf{0}.

Worked once more, with a=(3,4)\mathbf{a} = (3, 4) and d=(6,8)\mathbf{d} = (6, 8).

Step 1, 3×6=183 \times 6 = 18.

Step 2, 4×8=324 \times 8 = 32.

Step 3, 18+32=5018 + 32 = \mathbf{50}.

Sanity check. The answer is one number, never a list. If you finished with a list, you forgot to add the products up. A dot product of exactly zero means the two arrows meet at a right angle, which is what happened with a\mathbf{a} and c\mathbf{c}. A large positive answer means the arrows point roughly the same way and are long; a negative answer means they point in opposing directions. And the order does not matter: ab\mathbf{a} \cdot \mathbf{b} and ba\mathbf{b} \cdot \mathbf{a} both give 24.

Chapter. Chapter 8, A sentence is an arrow

5.2 Length of a vector (USE)

In words. Square every number in the list, add the squares up, and take the square root of the total. In two dimensions this is the length of the arrow you would draw, and the formula is Pythagoras.

The formula.

u=uu=i=1Dui2\lVert \mathbf{u} \rVert = \sqrt{\mathbf{u} \cdot \mathbf{u}} = \sqrt{\sum_{i=1}^{D} u_i^{2}}

The symbols.

SymbolHow to say it out loudWhat it means
u\lVert \mathbf{u} \rVert“the norm of u”, or “the length of u”the answer: how long the arrow is. Also called the magnitude.
the double upright bars“norm”, or “length”they are the length instruction. Single bars mean absolute value; double bars mean vector length.
==“equals”the two sides are the same number
x\sqrt{\phantom{x}}“the square root of”the number which, multiplied by itself, gives what is underneath. 25=5\sqrt{25} = 5 because 5×5=255 \times 5 = 25.
the bar across the top of the root signno soundit shows how far the square root reaches. Everything underneath it goes inside.
uu\mathbf{u} \cdot \mathbf{u}“u dot u”the dot product of the vector with itself, from 5.1
i=1D\sum_{i=1}^{D}“the sum from i equals one to capital dee”add up what follows, once for each position in the list
ui2u_i^{2}“u sub i squared”the number in position ii, multiplied by itself
the raised 2“squared”multiply the thing by itself once

Out loud. “The length of a vector is the square root of the sum of the squares of its numbers.”

Worked, step by step, with a=(3,4)\mathbf{a} = (3, 4).

Step 1, square each number. 32=3×3=93^{2} = 3 \times 3 = 9, and 42=4×4=164^{2} = 4 \times 4 = 16.

Step 2, add the squares. 9+16=259 + 16 = 25.

Step 3, take the square root. 25=5\sqrt{25} = \mathbf{5}, because 5×5=255 \times 5 = 25.

Worked again, with b=(4,3)\mathbf{b} = (4, 3). 42=164^{2} = 16, 32=93^{2} = 9, 16+9=2516 + 9 = 25, 25=5\sqrt{25} = \mathbf{5}. Same length as a\mathbf{a}, pointing a different way.

Worked once more, with d=(6,8)\mathbf{d} = (6, 8). 62=366^{2} = 36, 82=648^{2} = 64, 36+64=10036 + 64 = 100, 100=10\sqrt{100} = \mathbf{10}.

Sanity check. A length can never be negative, because squaring removes every minus sign and the square root of a positive number is positive. A length can only be zero if every number in the list is zero. And a length is always at least as big as the largest single number in the list, ignoring signs: for d=(6,8)\mathbf{d} = (6, 8) the length 10 is bigger than 8, as it must be. Notice also that d\mathbf{d} is exactly twice a\mathbf{a}, and its length is exactly twice as big. That fact is the whole point of 5.3.

Chapter. Chapter 8

5.3 Cosine similarity (USE)

In words. The dot product gets bigger when the arrows point the same way, but it also gets bigger when either arrow is longer. Dividing by both lengths removes the effect of length and leaves only direction. That is what you want when you are asking whether two pieces of text mean the same thing, because a longer sentence should not count as a better match.

The formula.

cos(u,v)=uvuv\cos(\mathbf{u}, \mathbf{v}) = \frac{\mathbf{u} \cdot \mathbf{v}} {\lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert}

The symbols.

SymbolHow to say it out loudWhat it means
cos(u,v)\cos(\mathbf{u}, \mathbf{v})“cosine of u and v”the answer: a number between -1 and +1
the round brackets and comma“of ... and ...”they hold the two vectors the answer depends on. They do not mean multiply.
==“equals”the two sides are the same number
uv\mathbf{u} \cdot \mathbf{v}“u dot v”the dot product from 5.1
u\lVert \mathbf{u} \rVert“the length of u”the length from 5.2
v\lVert \mathbf{v} \rVert“the length of v”the length of the other vector
the space between the two lengths“times”multiply the two lengths together
the fraction bar“divided by”divide the dot product on top by the product of the lengths underneath

Out loud. “The cosine similarity of two vectors is their dot product, divided by the length of the first multiplied by the length of the second.”

Worked, step by step, with a=(3,4)\mathbf{a} = (3, 4) and b=(4,3)\mathbf{b} = (4, 3).

Step 1, the dot product, from 5.1. 3×4+4×3=12+12=243 \times 4 + 4 \times 3 = 12 + 12 = 24.

Step 2, the two lengths, from 5.2. a=5\lVert \mathbf{a} \rVert = 5 and b=5\lVert \mathbf{b} \rVert = 5.

Step 3, multiply the lengths. 5×5=255 \times 5 = 25.

Step 4, divide. 24÷25=0.960024 \div 25 = \mathbf{0.9600}.

Worked again, with a=(3,4)\mathbf{a} = (3, 4) and c=(4,3)\mathbf{c} = (-4, 3).

Step 1, dot product: 3×(4)+4×3=12+12=03 \times (-4) + 4 \times 3 = -12 + 12 = 0.

Step 2, lengths: 5 and 5.

Step 3, 5×5=255 \times 5 = 25.

Step 4, 0÷25=0.00000 \div 25 = \mathbf{0.0000}.

Worked once more, with a=(3,4)\mathbf{a} = (3, 4) and d=(6,8)\mathbf{d} = (6, 8). This is the row that explains why the formula divides at all.

Step 1, dot product: 3×6+4×8=18+32=503 \times 6 + 4 \times 8 = 18 + 32 = 50.

Step 2, lengths: a=5\lVert \mathbf{a} \rVert = 5 and d=10\lVert \mathbf{d} \rVert = 10.

Step 3, 5×10=505 \times 10 = 50.

Step 4, 50÷50=1.000050 \div 50 = \mathbf{1.0000}, exactly.

pairdot productlengthsproduct of lengthscosine
a=(3,4)\mathbf{a} = (3,4), b=(4,3)\mathbf{b} = (4,3)245 and 52524÷25=0.960024 \div 25 = \mathbf{0.9600}
a=(3,4)\mathbf{a} = (3,4), c=(4,3)\mathbf{c} = (-4,3)05 and 5250÷25=0.00000 \div 25 = \mathbf{0.0000}
a=(3,4)\mathbf{a} = (3,4), d=(6,8)\mathbf{d} = (6,8)505 and 105050÷50=1.000050 \div 50 = \mathbf{1.0000}

Look at the last row. d\mathbf{d} is exactly 2a2\mathbf{a}: same direction, twice as long. Its dot product with a\mathbf{a} is 50, more than double the 24 in the first row, so a dot product on its own would rank d\mathbf{d} as the better match purely for being longer. Dividing by the lengths returns exactly 1. Length is not meaning, and that is the entire reason for the denominator.

Sanity check. The answer must land between -1 and +1. If it does not, you made an arithmetic slip, most often by forgetting to take a square root in Step 2. A cosine of exactly 1 means the two vectors point in exactly the same direction. A cosine of 0 means a right angle. A negative cosine means they point in opposing directions, and real embeddings do produce negative values: two unrelated sentences in the lab run scored -0.017.

Chapter. Chapter 9, Similarity is geometry

5.4 Angle between two vectors (READ)

In words. A cosine similarity can be turned back into the angle you would measure with a protractor if you drew the two arrows on paper. This is for intuition; the labs work with the cosine directly.

The formula.

θ=arccos(cos(u,v))\theta = \arccos\big(\cos(\mathbf{u}, \mathbf{v})\big)

The symbols.

SymbolHow to say it out loudWhat it means
θ\theta“theta”the answer: the angle. It is a Greek letter, used for angles by long habit.
==“equals”the two sides are the same number
arccos\arccos“arc cosine”, or “inverse cosine”the undo button for cosine. Give it a number between -1 and 1 and it hands back the angle that produces it.
the calculator key cos1\cos^{-1}“cos inverse”the same thing on a calculator. The raised -1 here means “undo”, not “one over”.
the round brackets“of”they hold the number you are feeding to arccos\arccos
cos(u,v)\cos(\mathbf{u}, \mathbf{v})“cosine of u and v”the cosine similarity from 5.3

Out loud. “Theta is the inverse cosine of the cosine similarity of the two vectors.”

Worked, step by step, on the three pairs from 5.3. Put your calculator in degrees mode first, otherwise it will answer in radians and every number will look wrong.

Step 1, for a\mathbf{a} and b\mathbf{b}, the cosine was 0.96. Press cos1\cos^{-1} on 0.96: θ=16.26\theta = 16.26^\circ.

Step 2, for a\mathbf{a} and c\mathbf{c}, the cosine was 0. Press cos1\cos^{-1} on 0: θ=90.00\theta = 90.00^\circ, a right angle.

Step 3, for a\mathbf{a} and d\mathbf{d}, the cosine was 1. Press cos1\cos^{-1} on 1: θ=0.00\theta = 0.00^\circ. No angle at all, because they point the same way.

Sanity check. The answer must land between 00^\circ and 180180^\circ. If your calculator gives something like 0.2838 for the first one, it is in radians; switch to degrees. If it gives an error, you fed it a number outside -1 to 1, which means the cosine in 5.3 was already wrong.

Chapter. Chapter 9

5.5 Retrieval: pick the nearest passage (USE)

In words. Turn the question into a list of numbers. Turn every passage in your collection into a list of numbers the same way. Work out the cosine similarity between the question and each passage, and hand the model whichever passage scores highest.

The formula.

best passage=argmaxk cos(q,dk)\text{best passage} = \arg\max_{k} \ \cos(\mathbf{q}, \mathbf{d}_k)

The symbols.

SymbolHow to say it out loudWhat it means
best passage\text{best passage}“best passage”the answer: which passage to retrieve. It is a passage, not a score.
==“equals”the two sides are the same thing
argmaxk\arg\max_{k}“arg max over kay”“the value of kk that makes what follows as large as possible”. max\max would give you the best score; argmax\arg\max gives you which one scored it.
kk“kay”a counter over the passages: passage 1, passage 2, and so on
q\mathbf{q}“bold q”the embedding of the question: the list of DD numbers a model produces to stand for that text. D=384D = 384 for the model used here.
dk\mathbf{d}_k“bold d sub k”the embedding of passage number kk
cos(q,dk)\cos(\mathbf{q}, \mathbf{d}_k)“cosine of q and d sub k”the cosine similarity from 5.3, between the question and passage kk

Out loud. “The best passage is the one whose embedding has the largest cosine similarity with the question’s embedding.”

Worked, step by step, on made-up two-number embeddings. Suppose the question comes out as q=(3,4)\mathbf{q} = (3, 4), and there are three passages, d1=(4,3)\mathbf{d}_1 = (4, 3), d2=(4,3)\mathbf{d}_2 = (-4, 3) and d3=(6,8)\mathbf{d}_3 = (6, 8).

Step 1, cosine with passage 1. Dot product 3×4+4×3=243 \times 4 + 4 \times 3 = 24; lengths 5 and 5; 24÷25=0.960024 \div 25 = 0.9600.

Step 2, cosine with passage 2. Dot product 3×(4)+4×3=03 \times (-4) + 4 \times 3 = 0; lengths 5 and 5; 0÷25=0.00000 \div 25 = 0.0000.

Step 3, cosine with passage 3. Dot product 3×6+4×8=503 \times 6 + 4 \times 8 = 50; lengths 5 and 10; 50÷50=1.000050 \div 50 = 1.0000.

Step 4, pick the largest. The three scores are 0.9600, 0.0000 and 1.0000, so the largest is the third. Retrieve passage 3.

Worked on the real run. The query Which California county is Bakersfield in? was scored against six sentences. The cosines came back, sorted from largest:

rankcosine
10.9101
20.7191
30.0893
40.0615
50.0556
60.0116

The largest is rank 1, so that passage is retrieved.

Sanity check. Every score has to sit between -1 and 1, because each one is a cosine. The useful check is not the top score on its own but the gap. Here rank 2 scores 0.7191 and rank 3 scores 0.0893, a drop of 0.6298. The two relevant passages separate cleanly from the four irrelevant ones, which is what makes retrieval work. If the top few scores are all close together, the method has not found anything; it has ranked noise.

Chapter. Chapter 10, Retrieval: the open-book exam


6. Judging a model like a statistician

6.1 Sample proportion, which is what accuracy is (USE)

In words. Count how many questions the model got right, divide by how many questions there were, and you have its accuracy.

The formula.

p^=xn\hat{p} = \frac{x}{n}

The symbols.

SymbolHow to say it out loudWhat it means
p^\hat{p}“p hat”the answer: the accuracy, a number between 0 and 1
the hat“hat”it marks a number you measured rather than a number you know for certain. It is the difference between “what this model scored on these 20 questions” and “how good this model is”.
==“equals”the two sides are the same number
xx“ex”how many questions the model answered correctly
nn“en”how many questions were on the test
the fraction bar“divided by”divide the top by the bottom

Out loud. “P hat is the number of questions answered correctly, divided by the number of questions asked.”

Worked, step by step. Qwen2.5-0.5B-Instruct answered 5 of 20 questions correctly under the naive scoring procedure.

Step 1, write down x=5x = 5 and n=20n = 20.

Step 2, divide. 5÷20=0.255 \div 20 = 0.25.

Step 3, turn it into a percentage by multiplying by 100. 0.25×100=25.0%0.25 \times 100 = \mathbf{25.0\%}.

Worked again, for the set piece used in 6.3 and 6.4: x=8x = 8 out of n=10n = 10.

Step 1, x=8x = 8, n=10n = 10.

Step 2, 8÷10=0.88 \div 10 = 0.8.

Step 3, 0.8×100=80.0%0.8 \times 100 = \mathbf{80.0\%}.

Sanity check. Accuracy must land between 0 and 1, which is 0% and 100%. If you get a number above 1 you divided the wrong way round. Here is the part that matters more than the arithmetic: because your questions are a sample of the questions you could have asked, p^\hat{p} is a random variable, not a fixed property of the model. Run a different 20 questions and you get a different number. Chance on a four-option multiple-choice test is 25%, which is exactly what this model scored, and that coincidence is the observation that starts Chapter 13.

Chapter. Chapter 11, Can a model take a test?

6.2 Standard error of a proportion (USE)

In words. An accuracy measured on a handful of questions would come out differently if you had asked a different handful. This puts a size on that wobble, in the same units as the accuracy itself.

The formula.

SE=p^(1p^)nSE = \sqrt{\frac{\hat{p}\,(1 - \hat{p})}{n}}

The symbols.

SymbolHow to say it out loudWhat it means
SESE“ess ee”the answer: the standard error, an estimate of how far p^\hat{p} would typically move if you drew a fresh set of nn questions of the same kind
==“equals”the two sides are the same number
x\sqrt{\phantom{x}}“the square root of”the number which, multiplied by itself, gives what is underneath
p^\hat{p}“p hat”the measured accuracy, from 6.1
1p^1 - \hat{p}“one minus p hat”the share the model got wrong
the round brackets“bracket”they say to work out 1p^1 - \hat{p} first, before multiplying
the space between p^\hat{p} and the bracket“times”multiply
nn“en”how many questions were on the test
the fraction bar“divided by”divide the top by the bottom

Out loud. “The standard error is the square root of the accuracy multiplied by one minus the accuracy, all divided by the number of questions.”

Worked, step by step, with p^=0.25\hat{p} = 0.25 and n=20n = 20.

Step 1, work out 1p^1 - \hat{p}. 10.25=0.751 - 0.25 = 0.75.

Step 2, multiply. 0.25×0.75=0.18750.25 \times 0.75 = 0.1875.

Step 3, divide by nn. 0.1875÷20=0.0093750.1875 \div 20 = 0.009375.

Step 4, take the square root. 0.009375=0.096825\sqrt{0.009375} = \mathbf{0.096825}.

Nearly ten percentage points of wobble on a 20-question test.

Worked again, with p^=0.8\hat{p} = 0.8 and n=10n = 10.

Step 1, 10.8=0.21 - 0.8 = 0.2.

Step 2, 0.8×0.2=0.160.8 \times 0.2 = 0.16.

Step 3, 0.16÷10=0.0160.16 \div 10 = 0.016.

Step 4, 0.016=0.126491\sqrt{0.016} = \mathbf{0.126491}.

Sanity check. The standard error can never be negative, and it can never be larger than 0.5. It is largest when p^=0.5\hat{p} = 0.5, because 0.5×0.5=0.250.5 \times 0.5 = 0.25 is the biggest that top can get, and it shrinks toward zero as the accuracy approaches 0 or 1. Here is the fact worth carrying around: because nn sits under a square root, cutting the standard error in half takes four times as many questions, not twice as many.

Chapter. Chapter 12, Is that score real?

6.3 Wald confidence interval for a proportion (USE)

In words. Take the accuracy you measured and put a range around it, stretching about two standard errors either side. This is the textbook interval, the first one anyone learns, and the first one to fail.

The formula.

p^ ± zp^(1p^)n\hat{p} \ \pm\ z^\star \sqrt{\frac{\hat{p}\,(1 - \hat{p})}{n}}

The symbols.

SymbolHow to say it out loudWhat it means
p^\hat{p}“p hat”the measured accuracy, from 6.1. It sits at the centre of the interval.
±“plus or minus”do the calculation twice: once adding, once subtracting. You get two endpoints, a lower and an upper.
zz^\star“z star”the critical value, a fixed multiplier set by how confident you want to be. z=1.96z^\star = 1.96 for 95%, and that is the only value this book uses.
the square root and what is under it“the standard error”this is exactly the SESE from 6.2
nn“en”how many questions were on the test

Out loud. “The interval runs from the accuracy minus 1.96 standard errors, to the accuracy plus 1.96 standard errors.”

Conditions. The usual rule is at least 10 correct and at least 10 incorrect. Check this before you compute anything.

Worked, step by step, instance 1: p^=0.25\hat{p} = 0.25, n=20n = 20.

Step 1, the standard error, from 6.2: SE=0.096825SE = 0.096825.

Step 2, multiply by zz^\star. 1.96×0.096825=0.1897771.96 \times 0.096825 = 0.189777.

Step 3, subtract for the lower end. 0.250.189777=0.0602230.25 - 0.189777 = 0.060223.

Step 4, add for the upper end. 0.25+0.189777=0.4397770.25 + 0.189777 = 0.439777.

The interval is 0.0602\mathbf{0.0602} to 0.4398\mathbf{0.4398}, that is 6.0%6.0\% to 44.0%44.0\%. The honest statement about that model is not “25%”; it is “somewhere between 6% and 44%”, which is wide enough to be almost useless. That uselessness is the lesson about benchmark sizes. Note also that this instance already breaks its own conditions: there were only 5 correct answers, and the rule asks for at least 10.

Worked, step by step, instance 2, the one to remember: x=8x = 8, n=10n = 10.

Step 1, the accuracy, from 6.1. 8÷10=0.88 \div 10 = 0.8.

Step 2, the standard error, from 6.2. 10.8=0.21 - 0.8 = 0.2; 0.8×0.2=0.160.8 \times 0.2 = 0.16; 0.16÷10=0.0160.16 \div 10 = 0.016; 0.016=0.126491\sqrt{0.016} = 0.126491.

Step 3, multiply by zz^\star. 1.96×0.126491=0.2479221.96 \times 0.126491 = 0.247922.

Step 4, subtract. 0.80.247922=0.5520780.8 - 0.247922 = 0.552078.

Step 5, add. 0.8+0.247922=1.0479220.8 + 0.247922 = \mathbf{1.047922}.

The interval is 0.5521\mathbf{0.5521} to 1.0479\mathbf{1.0479}.

Sanity check. The interval must sit inside 0 and 1, because an accuracy outside that range is meaningless. Instance 2 fails that check, and the failure is the lesson.

Chapter. Chapter 12

6.4 Wilson confidence interval for a proportion (READ)

In words. The same job as the Wald interval, done in a way that cannot produce an impossible answer. It shifts the centre of the interval away from the measured accuracy and toward the middle, by an amount that shrinks as you ask more questions, and it can never run below 0 or above 1.

The formula.

centre=x+z22n+z2,half-width=zn+z2p^(1p^)n+z24\text{centre} = \frac{x + \dfrac{z^{\star 2}}{2}}{n + z^{\star 2}}, \qquad \text{half-width} = \frac{z^\star}{n + z^{\star 2}} \sqrt{\hat{p}(1 - \hat{p})\,n + \frac{z^{\star 2}}{4}}
interval=centre ± half-width\text{interval} = \text{centre} \ \pm\ \text{half-width}

The symbols.

SymbolHow to say it out loudWhat it means
centre\text{centre}“centre”the middle of the interval. It is not p^\hat{p}: it is p^\hat{p} pulled toward 0.5.
half-width\text{half-width}“half width”how far the interval reaches on each side of the centre
xx“ex”how many questions the model got right
nn“en”how many questions there were
p^\hat{p}“p hat”the measured accuracy x/nx/n, from 6.1
zz^\star“z star”the critical value, 1.96 for 95%
z2z^{\star 2}“z star squared”1.96×1.96=3.84161.96 \times 1.96 = 3.8416
the raised 2“squared”multiply the thing by itself
z22\dfrac{z^{\star 2}}{2}“z star squared over two”3.8416÷2=1.92083.8416 \div 2 = 1.9208. Adding this to xx is what pulls the centre toward the middle.
z24\dfrac{z^{\star 2}}{4}“z star squared over four”3.8416÷4=0.96043.8416 \div 4 = 0.9604
++“plus”add
±“plus or minus”do it twice, once subtracting and once adding
x\sqrt{\phantom{x}}“the square root of”the number which, multiplied by itself, gives what is underneath
the fraction bars“divided by”divide the top by the bottom
the comma between the two formulasno soundit separates two quantities you work out before combining them

Out loud. “The centre is the number correct plus half of z star squared, divided by the number of questions plus z star squared. The half-width is z star divided by that same bottom, multiplied by the square root of the accuracy times one minus the accuracy times the number of questions, plus a quarter of z star squared. The interval is the centre plus or minus the half-width.”

Worked, step by step, on the same x=8x = 8, n=10n = 10.

Step 1, square the critical value. 1.96×1.96=3.84161.96 \times 1.96 = 3.8416.

Step 2, build the bottom of both fractions. 10+3.8416=13.841610 + 3.8416 = 13.8416.

Step 3, build the top of the centre. 3.8416÷2=1.92083.8416 \div 2 = 1.9208, then 8+1.9208=9.92088 + 1.9208 = 9.9208.

Step 4, divide to get the centre. 9.9208÷13.8416=0.7167389.9208 \div 13.8416 = 0.716738.

Step 5, build what goes under the square root. p^=0.8\hat{p} = 0.8, so p^(1p^)=0.8×0.2=0.16\hat{p}(1-\hat{p}) = 0.8 \times 0.2 = 0.16; 0.16×10=1.60.16 \times 10 = 1.6; 3.8416÷4=0.96043.8416 \div 4 = 0.9604; 1.6+0.9604=2.56041.6 + 0.9604 = 2.5604.

Step 6, take the square root. 2.5604=1.600125\sqrt{2.5604} = 1.600125.

Step 7, work out the multiplier in front. 1.96÷13.8416=0.1416021.96 \div 13.8416 = 0.141602.

Step 8, multiply to get the half-width. 0.141602×1.600125=0.2265810.141602 \times 1.600125 = 0.226581.

Step 9, subtract and add. 0.7167380.226581=0.4901570.716738 - 0.226581 = \mathbf{0.490157} 0.716738+0.226581=0.9433190.716738 + 0.226581 = \mathbf{0.943319}

The interval is 0.4902\mathbf{0.4902} to 0.9433\mathbf{0.9433}.

Put the two methods side by side. This is the comparison to carry out of Chapter 12.

x=8x = 8, n=10n = 10, 95%lowerupperinside 0 to 1?
Wald (6.3)0.55211.0479no
Wilson (6.4)0.49020.9433yes

The Wilson interval is not the Wald interval with the top trimmed off. It is lower at both ends, because it recognises that a model scoring 8 of 10 is more likely to be a mediocre model that got lucky than an excellent one that got unlucky.

Worked again, on the 20-question run: x=5x = 5, n=20n = 20.

Step 1, z2=3.8416z^{\star 2} = 3.8416.

Step 2, 20+3.8416=23.841620 + 3.8416 = 23.8416.

Step 3, 5+1.9208=6.92085 + 1.9208 = 6.9208.

Step 4, 6.9208÷23.8416=0.2902836.9208 \div 23.8416 = 0.290283.

Step 5, 0.25×0.75=0.18750.25 \times 0.75 = 0.1875; 0.1875×20=3.750.1875 \times 20 = 3.75; 3.75+0.9604=4.71043.75 + 0.9604 = 4.7104.

Step 6, 4.7104=2.170346\sqrt{4.7104} = 2.170346.

Step 7, 1.96÷23.8416=0.0822091.96 \div 23.8416 = 0.082209.

Step 8, 0.082209×2.170346=0.1784220.082209 \times 2.170346 = 0.178422.

Step 9, 0.2902830.178422=0.1118600.290283 - 0.178422 = 0.111860 and 0.290283+0.178422=0.4687050.290283 + 0.178422 = 0.468705.

Wilson gives 0.1119\mathbf{0.1119} to 0.4687\mathbf{0.4687}, against Wald’s 0.0602 to 0.4398. Both are wide. Neither rescues a 20-question benchmark.

Sanity check. Both endpoints must land inside 0 and 1, every time, for any xx and any nn. That is the whole reason this formula exists, so if an endpoint escapes that range, you made an arithmetic slip. The centre must also sit between p^\hat{p} and 0.5: for x=8x = 8, n=10n = 10 the centre 0.7167 sits between 0.5 and 0.8, as it should. Report Wilson, and let software compute it; the arithmetic above is here so you can see there is no magic in it.

Chapter. Chapter 12

6.5 Bootstrap percentile interval (USE)

In words. Instead of trusting a formula, ask what would have happened if the luck of the draw had fallen differently. Build a new test by drawing questions at random from the ones you already asked, allowing repeats, and score it using the answers the model already gave. Do that ten thousand times, sort the ten thousand scores, and read off the value 2.5% of the way up and the value 97.5% of the way up.

The formula.

interval=( p^(0.025B),  p^(0.975B) )\text{interval} = \Big(\ \hat{p}^{*}_{(\lceil 0.025\,B \rceil)}, \ \ \hat{p}^{*}_{(\lceil 0.975\,B \rceil)}\ \Big)

The symbols.

SymbolHow to say it out loudWhat it means
a resample“resample”a new set of nn questions drawn at random from your nn questions, with replacement, so a question can appear twice or not at all
BB“capital bee”how many resamples you take. B=10,000B = 10{,}000 in this book.
p^\hat{p}^{*}“p hat star”an accuracy computed on a resample rather than on the real test. The star marks it as resampled.
p^(r)\hat{p}^{*}_{(r)}“p hat star bracket r”the rr-th smallest of the BB resampled accuracies, once they are sorted from smallest to largest. The brackets around rr mean the list has been sorted.
 \lceil\ \rceil“ceiling”round up to the next whole number. 4.1=5\lceil 4.1 \rceil = 5 and 4.0=4\lceil 4.0 \rceil = 4.
0.025“nought point nought two five”2.5%, the share of resamples left below the lower end
0.975“nought point nine seven five”97.5%, the share of resamples left below the upper end
the space between 0.025 and BB“times”multiply
the outer round brackets and the comma“the interval from ... to ...”they hold the two endpoints

Out loud. “The interval runs from the resampled accuracy two and a half percent of the way up the sorted list, to the resampled accuracy ninety-seven and a half percent of the way up.”

Worked, step by step, part one: what one resample looks like. Take a made-up 5-question test where the model got questions 1, 2 and 4 right and questions 3 and 5 wrong. Write that as 1,1,0,1,01, 1, 0, 1, 0, so the real score is 3÷5=0.63 \div 5 = 0.6.

Step 1, draw 5 question numbers at random from 1 to 5, allowing repeats. Suppose you draw 2, 2, 5, 1, 4.

Step 2, look up the model’s answer for each of those questions: question 2 was right (1), question 2 again (1), question 5 was wrong (0), question 1 was right (1), question 4 was right (1).

Step 3, score the resample. 1+1+0+1+1=41 + 1 + 0 + 1 + 1 = 4, so p^=4÷5=0.8\hat{p}^{*} = 4 \div 5 = 0.8.

One resample, one number. The computer does this BB times.

Worked, step by step, part two: reading off the endpoints. This is the part you do by hand.

Step 1, write down B=10,000B = 10{,}000.

Step 2, work out the lower rank. 0.025×10,000=2500.025 \times 10{,}000 = 250. Rounding 250 up leaves it at 250.

Step 3, work out the upper rank. 0.975×10,000=9,7500.975 \times 10{,}000 = 9{,}750.

Step 4, sort the 10,000 resampled accuracies from smallest to largest, and read off the 250th value and the 9,750th value.

On the lab run, seeded 20260912 so it reproduces exactly, those two values are 0.05 and 0.45: an interval of 5.0%\mathbf{5.0\%} to 45.0%\mathbf{45.0\%}, with the 10,000 resampled accuracies having a standard deviation of 0.0974.

Sanity check. Both ranks must be whole numbers between 1 and BB, and the lower rank must be smaller than the upper one. The interval itself must land inside 0 and 1 automatically, because every resampled accuracy is a real accuracy computed on a real number of questions. Then compare it with the Wald interval on the same data, which was 6.0%6.0\% to 44.0%44.0\%. Two different methods agreeing closely is a good sign; two methods disagreeing wildly means one of them is being used outside its conditions.

Chapter. Chapter 12

6.6 Paired difference and its standard error (USE)

In words. When two models sat the same test, compare them question by question instead of score against score. For each question write down whether model B beat model A on that one question. Average those per-question results, and the difficulty of the questions stops being a source of noise.

The formula.

di=ci(B)ci(A),dˉ=1ni=1ndid_i = c_i^{(B)} - c_i^{(A)}, \qquad \bar{d} = \frac{1}{n}\sum_{i=1}^{n} d_i
sd=1n1i=1n(didˉ)2,SEdˉ=sdn,interval=dˉ±zSEdˉs_d = \sqrt{\frac{1}{n-1}\sum_{i=1}^{n}\big(d_i - \bar{d}\big)^2}, \qquad SE_{\bar{d}} = \frac{s_d}{\sqrt{n}}, \qquad \text{interval} = \bar{d} \pm z^\star\, SE_{\bar{d}}

The symbols.

SymbolHow to say it out loudWhat it means
ci(A)c_i^{(A)}“c sub i bracket A”1 if model A got question ii right, 0 if not. The raised (A)(A) is a label naming the model, not a power.
ci(B)c_i^{(B)}“c sub i bracket B”the same thing for model B
did_i“d sub i”the difference on question ii. It can only be -1, 0 or +1.
-“minus”subtract
dˉ\bar{d}“d bar”the average of all the did_i values. It always equals the difference in the two accuracies.
the bar over the dd“bar”it marks an average
1n\dfrac{1}{n}“one over en”dividing by nn after adding nn things up is how you take an average
i=1n\sum_{i=1}^{n}“the sum from i equals one to en”add up what follows, once for every question
sds_d“ess sub d”the standard deviation of the differences: how much the per-question results vary
(didˉ)2\big(d_i - \bar{d}\big)^2“bracket d sub i minus d bar, squared”how far one question’s difference sits from the average difference, squared so the minus signs cannot cancel
n1n-1“en minus one”you divide by one fewer than the number of questions when computing a standard deviation from a sample
x\sqrt{\phantom{x}}“the square root of”the number which, multiplied by itself, gives what is underneath
SEdˉSE_{\bar{d}}“ess ee sub d bar”the standard error of the average difference
n\sqrt{n}“the square root of en”the square root of the number of questions
zz^\star“z star”1.96, for 95%
±“plus or minus”do it twice, once subtracting and once adding

Out loud. “The difference on a question is one model’s result minus the other’s. The average difference is the sum of those divided by the number of questions. The standard deviation of the differences is the square root of the sum of their squared distances from that average, divided by one fewer than the number of questions. The standard error is that standard deviation divided by the square root of the number of questions. The interval is the average difference plus or minus 1.96 standard errors.”

Worked, step by step. On 20 questions, Qwen2.5-3B was right on 19 and Qwen2.5-1.5B on 14. The contingency table says where the differences are.

3B correct3B wrong
1.5B correct140
1.5B wrong51

So on 5 questions the 3B won and the 1.5B lost, giving di=+1d_i = +1. On 14 questions both were right and on 1 question both were wrong, giving di=0d_i = 0 fifteen times. On no question did the 1.5B win.

Step 1, average the differences. dˉ=5÷20=0.25\bar{d} = 5 \div 20 = 0.25. Check it against the accuracies: 0.950.70=0.250.95 - 0.70 = 0.25. They agree, as they always must.

Step 2, work out each squared distance from that average. For the 5 questions where di=1d_i = 1: (10.25)2=(0.75)2=0.5625(1 - 0.25)^2 = (0.75)^2 = 0.5625. For the 15 questions where di=0d_i = 0: (00.25)2=(0.25)2=0.0625(0 - 0.25)^2 = (-0.25)^2 = 0.0625.

Step 3, add them all up. 5×0.5625=2.81255 \times 0.5625 = 2.8125 15×0.0625=0.937515 \times 0.0625 = 0.9375 2.8125+0.9375=3.752.8125 + 0.9375 = 3.75

Step 4, divide by n1=19n - 1 = 19, then take the square root. 3.75÷19=0.1973683.75 \div 19 = 0.197368 0.197368=0.444262\sqrt{0.197368} = 0.444262

Step 5, divide by the square root of nn. 20=4.472136\sqrt{20} = 4.472136 0.444262÷4.472136=0.0993400.444262 \div 4.472136 = \mathbf{0.099340}

Step 6, build the interval. 1.96×0.099340=0.1947061.96 \times 0.099340 = 0.194706 0.250.194706=0.0552940.25 - 0.194706 = 0.055294 0.25+0.194706=0.4447060.25 + 0.194706 = 0.444706

The interval is 0.0553 to 0.4447, that is +5.5\mathbf{+5.5} to +44.5\mathbf{+44.5} percentage points.

Sanity check. dˉ\bar{d} must equal the difference in the two accuracies; if it does not, you mis-tallied the contingency table. Every did_i must be -1, 0 or +1, and the four cells of the table must add to nn: 14+0+5+1=2014 + 0 + 5 + 1 = 20. The questions both models got right, and the questions both got wrong, contribute di=0d_i = 0, which pulls the standard deviation down. That is the payoff from pairing, and 6.7 measures how big it is.

Chapter. Chapter 13, Measure your measurement

6.7 The unpaired standard error, shown for contrast (READ)

In words. This is the standard error you would use if the two models had sat two different tests. Work out the wobble for each model separately, add the two wobbles, and take the square root. Using it on paired data throws away the fact that the questions were the same, and you pay for that in a wider interval.

The formula.

SEunpaired=p^A(1p^A)nA+p^B(1p^B)nBSE_{\text{unpaired}} = \sqrt{\frac{\hat{p}_A(1 - \hat{p}_A)}{n_A} + \frac{\hat{p}_B(1 - \hat{p}_B)}{n_B}}

The symbols.

SymbolHow to say it out loudWhat it means
SEunpairedSE_{\text{unpaired}}“ess ee sub unpaired”the answer: the standard error of the difference, computed the wrong way for this data
p^A\hat{p}_A“p hat sub A”the accuracy of model A
p^B\hat{p}_B“p hat sub B”the accuracy of model B
nAn_A, nBn_B“en sub A”, “en sub B”the two test sizes, both 20 here
1p^A1 - \hat{p}_A“one minus p hat sub A”the share model A got wrong
++“plus”add the two pieces together, inside the square root
the fraction bars“divided by”divide each top by its own bottom
x\sqrt{\phantom{x}}“the square root of”applies to the whole sum, not to one piece of it

Out loud. “The unpaired standard error is the square root of model A’s accuracy times one minus it, divided by its number of questions, plus model B’s accuracy times one minus it, divided by its number of questions.”

Worked, step by step, with p^A=0.70\hat{p}_A = 0.70, p^B=0.95\hat{p}_B = 0.95, and n=20n = 20 for both.

Step 1, model A’s piece. 10.70=0.301 - 0.70 = 0.30; 0.70×0.30=0.210.70 \times 0.30 = 0.21; 0.21÷20=0.0105000.21 \div 20 = 0.010500.

Step 2, model B’s piece. 10.95=0.051 - 0.95 = 0.05; 0.95×0.05=0.04750.95 \times 0.05 = 0.0475; 0.0475÷20=0.0023750.0475 \div 20 = 0.002375.

Step 3, add the two pieces. 0.010500+0.002375=0.0128750.010500 + 0.002375 = 0.012875.

Step 4, take the square root. 0.012875=0.113468\sqrt{0.012875} = \mathbf{0.113468}.

Step 5, build the interval around the same difference of 0.25. 1.96×0.113468=0.2223971.96 \times 0.113468 = 0.222397 0.250.222397=0.0276030.25 - 0.222397 = 0.027603 0.25+0.222397=0.4723970.25 + 0.222397 = 0.472397

That is +2.8\mathbf{+2.8} to +47.2\mathbf{+47.2} percentage points.

methodSESE95% interval, percentage pointswidth
paired (6.6)0.099340+5.5 to +44.539.0
unpaired (6.7)0.113468+2.8 to +47.244.4

Divide one standard error by the other: 0.113468÷0.099340=1.1420.113468 \div 0.099340 = 1.142. The unpaired interval is 1.14 times wider on exactly the same data.

Sanity check. The unpaired standard error should come out larger than the paired one on the same data, so if yours comes out smaller, check that you added inside the square root rather than outside it. Say the size of the gain plainly: 1.14 times is a modest gain, not a dramatic one. Pairing pays most when two systems agree often and their disagreements are balanced, and here the disagreements all fall one way.

Chapter. Chapter 13

6.8 McNemar’s exact test (READ)

In words. Throw away every question the two models agreed on, because a question they both got right, or both got wrong, tells you nothing about which model is better. Look only at the questions where they disagreed. If the two models were equally good, each disagreement would fall either way like a coin toss. Work out how unlikely it would be for a run of coin tosses to lean as hard as yours did.

First build the table of counts.

Table 1:Two models, twenty shared questions. The diagonal from top left to bottom right holds the concordant pairs, where the models agreed; the other diagonal holds the discordant pairs.

model B rightmodel B wrong
model A rightaacc
model A wrongbbdd

The formula.

p=2k=0min(b,c)(b+ck)(12)b+c,capped at 1p = 2 \sum_{k=0}^{\min(b,\,c)} \binom{b+c}{k} \left(\frac{1}{2}\right)^{b+c}, \qquad \text{capped at } 1

The symbols.

SymbolHow to say it out loudWhat it means
pp“p”, or “the p-value”the answer: how likely a lean this big would be if the two models were equally good. A small pp is evidence they are not.
==“equals”the two sides are the same number
2“two”multiply by 2 to make the test two-sided, because a lean in either direction would have been interesting
k=0min(b,c)\sum_{k=0}^{\min(b,c)}“the sum from k equals zero to the minimum of b and c”add up what follows for k=0k = 0, then k=1k = 1, and so on, stopping at whichever of bb and cc is smaller
aa“ay”how many questions both models answered correctly
dd“dee”how many questions both models got wrong
bb“bee”how many questions only model B got right
cc“see”how many questions only model A got right
b+cb+c“b plus c”the number of discordant pairs: the questions where the two models disagreed
min(b,c)\min(b, c)“the minimum of b and c”whichever of the two is smaller
(b+ck)\binom{b+c}{k}“b plus c choose k”how many different ways there are to pick kk items out of b+cb+c. For example (52)=10\binom{5}{2} = 10.
(12)b+c\left(\frac{1}{2}\right)^{b+c}“a half, to the power b plus c”the chance of one particular run of b+cb+c coin tosses. The 12\tfrac{1}{2} is the null hypothesis: each disagreement is as likely to fall either way as a fair coin.
the raised b+cb+c“to the power of”multiply 12\tfrac{1}{2} by itself b+cb+c times
“capped at 1”“capped at one”if the arithmetic gives an answer above 1, report 1, because no probability exceeds 1

Out loud. “The p-value is twice the sum, over every outcome at least as lopsided as the one you saw, of the number of ways that outcome could happen multiplied by the chance of one particular run of coin tosses.”

Worked, step by step. The lab’s table, with Qwen2.5-1.5B as model A and Qwen2.5-3B as model B.

3B right3B wrong
1.5B righta=14a = 14c=0c = 0
1.5B wrongb=5b = 5d=1d = 1

Step 1, read off the two discordant counts. b=5b = 5 and c=0c = 0. All 5 disagreements favour the larger model and none favours the smaller.

Step 2, add them to get the number of coin tosses. b+c=5+0=5b + c = 5 + 0 = 5.

Step 3, find where the sum stops. min(b,c)=min(5,0)=0\min(b, c) = \min(5, 0) = 0, so the sum has exactly one term, the one with k=0k = 0.

Step 4, work out that term’s “choose” part. (50)=1\binom{5}{0} = 1, because there is exactly one way to pick nothing out of five.

Step 5, work out the coin-toss part. That means a half multiplied by itself five times.

(12)5=12×12×12×12×12=132=0.03125\left(\frac{1}{2}\right)^{5} = \frac{1}{2} \times \frac{1}{2} \times \frac{1}{2} \times \frac{1}{2} \times \frac{1}{2} = \frac{1}{32} = 0.03125

Step 6, multiply the two parts. 1×0.03125=0.031251 \times 0.03125 = 0.03125.

Step 7, double it, because the test is two-sided. 2×0.03125=232=0.06252 \times 0.03125 = \frac{2}{32} = \mathbf{0.0625}

Five coin tosses all landing the same way. That is what a 25-point gap amounts to here.

Sanity check. The pp-value must land between 0 and 1, which is why the formula says to cap it. A quick check on Step 4: the “choose” numbers for 5 are 1,5,10,10,5,11, 5, 10, 10, 5, 1, and they add to 32, which is 25. If your “choose” numbers do not add to 2b+c2^{b+c}, you computed one of them wrongly. The other check is on the four cells: a+b+c+da + b + c + d must equal nn, so 14+0+5+1=2014 + 0 + 5 + 1 = 20.

Chapter. Chapter 13


7. What a run costs

These three belong to the sustainability thread and are stated here for completeness. The full accounting, including everything these numbers leave out, is in the cost model appendix.

7.1 Energy per token (READ)

In words. Time the model while it is writing, and measure how much power the graphics card is drawing while it works. Multiply the power by the time to get the total energy used, then divide by how many words came out. The answer is the energy cost of one token.

The formula.

Etoken=Pˉ×tntokensE_{\text{token}} = \frac{\bar{P} \times t}{n_{\text{tokens}}}

The symbols.

SymbolHow to say it out loudWhat it means
EtokenE_{\text{token}}“E sub token”the answer, in joules per token. One joule is one watt drawn for one second.
==“equals”the two sides are the same number
Pˉ\bar{P}“P bar”the mean power draw, in watts, over the window in which the model was generating
the bar over the PP“bar”it marks an average. Power moves up and down while the model runs, so this is the average of many readings.
×\times“times”multiply
tt“tee”how many seconds the generation took
ntokensn_{\text{tokens}}“en sub tokens”how many tokens came out
the fraction bar“divided by”divide the total energy on top by the token count underneath

Out loud. “The energy per token is the average power in watts multiplied by the number of seconds, divided by the number of tokens produced.”

Worked, step by step. The 0.5B model generated 85 tokens in 3.0677 seconds while the graphics card drew a mean of 21.254 watts.

Step 1, multiply power by time to get total energy in joules. 21.254×3.0677=65.200921.254 \times 3.0677 = 65.2009 joules

Step 2, divide by the number of tokens. 65.2009÷85=0.767165.2009 \div 85 = \mathbf{0.7671} joules per token

Worked again, for the 3B model, which generated 88 tokens in 5.6539 seconds at a mean of 30.203 watts.

Step 1, 30.203×5.6539=170.764730.203 \times 5.6539 = 170.7647 joules.

Step 2, 170.7647÷88=1.9405170.7647 \div 88 = \mathbf{1.9405} joules per token.

Sanity check. The answer must be positive, and it must fall if the model gets faster at the same power draw, because the same energy is then spread over more tokens. A quick reality check on the size: the card was drawing about 21 watts and producing about 28 tokens per second, and 21÷2821 \div 28 is about 0.75, which is close to the 0.7671 computed above. If your answer is out by a factor of a thousand, you probably mixed up watts with milliwatts, or seconds with milliseconds.

Chapter. Chapter 1, Chapter 7, Chapter 14

7.2 Joules per token to watt-hours per 1,000 tokens (USE)

In words. Joules are a unit almost nobody has a feel for. Watt-hours are the unit on an electricity bill. This converts one into the other and scales it up to a thousand tokens, which is roughly a long answer.

The formula.

Wh per 1,000 tokens=Etoken×10003600\text{Wh per 1{,}000 tokens} = \frac{E_{\text{token}} \times 1000}{3600}

The symbols.

SymbolHow to say it out loudWhat it means
Wh per 1,000 tokens\text{Wh per 1{,}000 tokens}“watt hours per thousand tokens”the answer, in watt-hours
==“equals”the two sides are the same number
EtokenE_{\text{token}}“E sub token”the joules per token from 7.1
×\times“times”multiply
1000“one thousand”scale up from one token to a thousand tokens
3600“three thousand six hundred”the number of seconds in an hour, which is what turns joules into watt-hours
the fraction bar“divided by”divide the top by the bottom

Out loud. “Watt-hours per thousand tokens is the joules per token multiplied by one thousand, divided by three thousand six hundred.”

Worked, step by step, for the 0.5B model.

Step 1, start from Etoken=0.7671E_{\text{token}} = 0.7671 joules per token.

Step 2, multiply by 1,000. 0.7671×1000=767.10.7671 \times 1000 = 767.1 joules per thousand tokens.

Step 3, divide by 3,600. 767.1÷3600=0.2131767.1 \div 3600 = \mathbf{0.2131} watt-hours per 1,000 tokens.

Worked again, for the 3B model.

Step 1, Etoken=1.9405E_{\text{token}} = 1.9405 joules per token.

Step 2, 1.9405×1000=1940.51.9405 \times 1000 = 1940.5.

Step 3, 1940.5÷3600=0.53901940.5 \div 3600 = \mathbf{0.5390} watt-hours per 1,000 tokens.

Step 4, compare the two. 1.9405÷0.7671=2.531.9405 \div 0.7671 = 2.53. The 3B model costs 2.53 times the energy per token of the 0.5B.

Sanity check. Watt-hours per thousand tokens will always be a smaller number than joules per token, because 3,600 is much bigger than 1,000. If your answer came out larger, you divided and multiplied the wrong way round. For scale: a 60-watt light bulb uses 60 watt-hours in an hour, so 0.2131 watt-hours is that bulb running for about thirteen seconds.

Chapter. Chapter 7, Chapter 14

7.3 Marginal energy above idle (READ)

In words. A graphics card uses power even when nothing is running. Subtract that background draw before working out the cost of a token, and you get the extra energy that the model actually caused, rather than the cost of the machine being switched on.

The formula.

Eabove idle=(PˉPidle)×tntokensE_{\text{above idle}} = \frac{\big(\bar{P} - P_{\text{idle}}\big) \times t}{n_{\text{tokens}}}

The symbols.

SymbolHow to say it out loudWhat it means
Eabove idleE_{\text{above idle}}“E sub above idle”the answer: the extra joules per token caused by running the model
==“equals”the two sides are the same number
Pˉ\bar{P}“P bar”the mean power in watts while the model was generating
PidleP_{\text{idle}}“P sub idle”the power the same hardware draws with nothing running. Measured at 13.834 watts on the machine used for this book.
-“minus”subtract
the round brackets“bracket”they say to do the subtraction first, before multiplying by tt
×\times“times”multiply
tt“tee”how many seconds the generation took
ntokensn_{\text{tokens}}“en sub tokens”how many tokens came out
the fraction bar“divided by”divide the top by the bottom

Out loud. “The energy above idle is the average power minus the idle power, multiplied by the number of seconds, divided by the number of tokens.”

Worked, step by step, for the 0.5B model.

Step 1, subtract the idle power from the mean power. Do this first, because of the brackets. 21.25413.834=7.42021.254 - 13.834 = 7.420 watts

Step 2, multiply by the time. 7.420×3.0677=22.76237.420 \times 3.0677 = 22.7623 joules

Step 3, divide by the token count. 22.7623÷85=0.267822.7623 \div 85 = \mathbf{0.2678} joules per token above idle

Step 4, compare with the full figure from 7.1. 0.2678÷0.7671=0.34910.2678 \div 0.7671 = 0.3491

So about 35% of the energy is the model working, and the other 65%, roughly two thirds, is the graphics card being powered on at all.

Sanity check. This answer must always be smaller than the answer from 7.1, because you subtracted something positive before dividing. If it is not, check that you did the subtraction inside the brackets first. If the answer comes out negative, the mean power you measured was below idle, which means the measurement window was wrong: it probably included time before the model started working.

Chapter. Chapter 7


8. Use it or read it, in one table

§FormulaChapterTag
2.1Share of parameters in a component3USE
2.2Size of a stored matrix3USE
3.1Softmax4USE
3.2Ratio form, pi/pj=ezizjp_i/p_j = e^{z_i - z_j}4READ
3.3Softmax with temperature5USE
3.4Temperature as a power of the probabilities5READ
3.5Entropy in bits5USE
3.6Tokens holding 90% of the probability5USE
4.1Quantization scale7USE
4.2Quantize and dequantize7USE
4.3Quantization error relative to the weight spread7READ
4.4Blockwise quantization7USE
4.5Bytes per weight, with block-scale overhead6, 7USE
4.6Model size on disk3, 6, 7USE
5.1Dot product8USE
5.2Length of a vector8USE
5.3Cosine similarity9USE
5.4Angle from a cosine9READ
5.5Retrieval by nearest cosine10USE
6.1Sample proportion11USE
6.2Standard error of a proportion12USE
6.3Wald confidence interval12USE
6.4Wilson confidence interval12READ
6.5Bootstrap percentile interval12USE
6.6Paired difference and its standard error13USE
6.7Unpaired standard error, for contrast13READ
6.8McNemar’s exact test13READ
7.1Energy per token1, 7, 14READ
7.2Watt-hours per 1,000 tokens7, 14USE
7.3Marginal energy above idle7READ

Counting them: 30 formulas, 21 to use and 9 to read. If you can carry out the twenty-one and explain the nine, you can do every calculation this course asks for.