AI & ML Quick Path Progress

All cheat sheets

Cheat sheet: the maths behind language models

The few calculations that appear again and again. Each one is worked with small numbers in a lesson.

Softmax

  • Turns scores into chances that add up to 1.
  • Subtract the largest score first: same chances, no overflow.
chance(i) = e^(score i - max) / sum of e^(score - max)
scores 1000 and 1001 give 0.27 and 0.73

Temperature

  • Divide the scores by T before softmax.
  • T below 1 sharpens; T above 1 flattens.
chance(i) = e^(score i / T) / sum of e^(score / T)
Three small bar charts of the same five next-word scores. At temperature 0.2 refund has 0.92 and replacement 0.08, the others 0. At 1.0 refund 0.51, replacement 0.31, discount 0.11, apology 0.07, banana 0. At 2.0 the bars are flatter: refund 0.37, replacement 0.29, discount 0.17, apology 0.14, banana 0.03. Low temperature 0.2 refund 0.92 replacement 0.08 discount 0.00 apology 0.00 banana 0.00 Sharp: one word dominates Temperature 1.0 refund 0.51 replacement 0.31 discount 0.11 apology 0.07 banana 0.00 Balanced High temperature 2.0 refund 0.37 replacement 0.29 discount 0.17 apology 0.14 banana 0.03 Flat: odd words get a chance The same five next-word scores at three temperatures (the numbers from the executed run in this lesson).
A low temperature makes one word dominate; a high temperature flattens the chances so odd words, like banana, slip in.

A text as a chain of chances

  • The chance of a whole text is the product of next-token chances.
  • Programs add logarithms instead, to avoid numbers too small to store.
0.1 x 0.05 x 0.2 = 0.001

Attention as a weighted mix

  • A word receives the sum of other words' values times weights that add up to 1.
  • Real attention scores a query against every key, scales, then uses softmax.
0.75 x 10 + 0.25 x 2 = 8
A seven by seven grid for the sentence The cat sat because it was tired. Each row is a word, each column is a word it can look at. Cells hold made-up attention weights, darker for larger, and each row adds up to 1. Cells above the diagonal are greyed out because the causal mask hides later words. In the row for it, 0.75 goes to cat and 0.25 to sat. word being looked at The cat sat bec. it was tired The 1 cat 0.3 0.7 sat 0.1 0.6 0.3 because 0.05 0.2 0.65 0.1 it 0 0.75 0.25 0 0 was 0.05 0.1 0.05 0.1 0.6 0.1 tired 0 0.1 0.05 0.05 0.3 0.1 0.4 Each row is one word working out its new meaning. The numbers in the row are its attention weights and add up to 1. Darker = more attention. Hidden by the causal mask: a word cannot look at later words. Look at the row for "it": most of its attention goes to "cat" (0.75). Made-up weights (the row for "it" is the one used in the lesson's maths box).
Each row shows how one word spreads its attention over the words before it. Later words are hidden by the causal mask.

RMSNorm

  • Divide a vector by its root mean square (no subtracting the average).
vector (1, 7): mean of squares = (1 + 49) / 2 = 25, root = 5
result (0.2, 1.4)

Counting parameters

  • Each layer adds about 12 x width squared (attention 4, feed-forward 8).
  • The word table adds vocabulary x width.
parameters = 12 x layers x width^2 + vocabulary x width

Memory and work

  • Weights memory = parameters x bytes per weight.
  • Full training state is about 16 bytes per parameter before activations.
  • Training work is about 6 x parameters x tokens.
8 billion x 16 bytes = about 128 GB
A bar for about 16 bytes per parameter in full training with the AdamW optimiser: weights 2 bytes, gradients 2 bytes and optimiser state 12 bytes, before activations. Below it, an 8-billion-parameter model needs about 128 gigabytes of training state, and a bar marks that one 80 gigabyte GPU holds only part of it, so it does not fit. Memory per parameter: full training with AdamW 2 bytes 2 bytes 12 bytes Weights Gradients Optimiser state: AdamW keeps extra numbers per weight about 16 bytes per parameter, before activations (a common mixed-precision breakdown: 2 + 2 + 12 bytes) An 8-billion-parameter model, full fine-tuning: 8 billion × 16 bytes = about 128 GB of training state one 80 GB GPU does not fit Activations (saved for the backward pass) come on top. LoRA and QLoRA cut this memory far more than the other tools.
Full training needs about 16 bytes per parameter before activations: 8 billion parameters is about 128 GB, more than one 80 GB GPU. LoRA and QLoRA cut this a lot.

LoRA

  • Two thin matrices next to a frozen big one.
100 x 2 + 2 x 100 = 400 numbers, 4% of 10,000
LoRA-FA trains r x d; VeRA trains r + d
A large square labelled frozen, 10,000 numbers, 100 by 100. Beside it, plus a tall thin rectangle of 100 by 2 times a wide thin rectangle of 2 by 100, which are the only parts trained. A box says LoRA trains 100 times 2 plus 2 times 100, which is 400 numbers, or 4 percent of 10,000. One layer of a model: a grid of 100 × 100 weights frozen 10,000 numbers 100 × 100 + 100 × 2 × 2 × 100 → LoRA trains only the two thin pieces: 100 × 2 + 2 × 100 = 400 numbers, which is 4% of 10,000 The big grid stays frozen. The two thin pieces are the "adapter": a small file that holds what was learned. QLoRA does the same and also stores the frozen layer in 4-bit numbers to save memory. The numbers are the lesson's example. In a real model the layers are thousands wide, so the share is far below 4%.
LoRA leaves the big layer frozen and trains two thin pieces next to it: 400 numbers instead of 10,000 in the lesson's example. QLoRA also stores the frozen layer in 4-bit.

Latency

  • ITL is the time from the first to the last token divided by the number of gaps.
(5.5 - 0.5) / (101 - 1) = 0.05 s