A grid of numbers
A matrix is a rectangle of numbers, arranged in rows and columns. Its shape is written rows × columns: a 2 × 3 matrix has 2 rows and 3 columns, so 6 numbers.
M = | 2 0 1 |
| 1 3 0 |A 2 × 3 matrix. Each row is a vector of 3 numbers.
You can think of a matrix as a stack of vectors, one per row. That view makes the next step easy.
Matrix times vector
To multiply a matrix by a vector, take the dot product of each row with the vector. Each row gives one number of the answer.
M × [4, 1, 2]Multiply the matrix M above by the vector [4, 1, 2].
row 1: [2, 0, 1] · [4, 1, 2] = 8 + 0 + 2 = 10row 2: [1, 3, 0] · [4, 1, 2] = 4 + 3 + 0 = 7M × [4, 1, 2] = [10, 7]Two rows, so two numbers come out.
(rows × columns) matrix × (columns) vector = (rows) vectorThe vector must have as many numbers as the matrix has columns. The answer has as many numbers as it has rows.
Notice what happened: a vector of 3 numbers went in, and a vector of 2 came out. A matrix can change the length of a vector. Models use this all the time. In Llama 3.1 8B, one matrix takes each token’s 4,096 numbers and makes 14,336 of them, so the model has more room to work, and another squeezes them back down to 4,096.
Quick check
A matrix moves space
Here is the picture that makes matrices click. A 2 × 2 matrix takes every point of a flat page to a new point. Apply it to every corner of a shape and the whole shape moves: it can turn, stretch, lean over, flip like a mirror image, or be flattened onto a line.
The faint letter is the original; the solid one is after the matrix. The two arrows show where the matrix sends the steps “one to the right” and “one up”: they are the matrix’s two columns. Try each preset, then type your own numbers.
This interactive part needs JavaScript.
The demo also shows the determinant: how much the matrix multiplies areas. A stretch by 2 doubles them. A rotation keeps them the same (1). A mirror gives a negative number, because it flips the shape over. And when the determinant is 0, the page is flattened onto a line: different points land in the same place, and there is no way to tell them apart afterwards. Information has been lost.
Many vectors at once
A model never handles one vector at a time. A prompt of 1,000 tokens is 1,000 vectors. Stack them as the rows of one big matrix, and a single matrix multiplication transforms them all at once: every row of one matrix meets every column of the other in a dot product.
(m × n) matrix × (n × p) matrix = (m × p) matrixThe inner sizes must match. The answer takes its rows from the first and its columns from the second.
work = m × n × p multiply-and-add stepsOne for every pair of numbers that meet.
1,000 tokens × 4,096 × 4,096 ≈ 16.8 billion stepsJust one of the seven big matrices in just one of the 32 layers of Llama 3.1 8B.
A useful rule: running a model costs about 2 operations per weight for each token (one multiply and one add). An 8-billion-weight model does about 16 billion operations to read or write a single token. That is why AI runs on GPUs. Their thousands of cores, and special units called tensor cores that multiply small blocks of matrices in one step, are built for exactly this.
Inside a real model: count the weights yourself
Almost every weight in a language model belongs to a matrix. Here are the matrices in one layer of Llama 3.1 8B, using the names they have in its weight files. You will learn what each one does in part 4; for now, just count.
| Name in the file | What it is for | Numbers in, numbers out | Weights |
|---|---|---|---|
| q_ | Attention: what each token is looking for | 4096 → 4096 | 16.8 million |
| k_ | Attention: what each token offers | 4096 → 1024 | 4.2 million |
| v_ | Attention: what each token passes on | 4096 → 1024 | 4.2 million |
| o_ | Attention: mixing the results back | 4096 → 4096 | 16.8 million |
| gate_ | Feed-forward: two matrices that widen the vector | 4096 → 14336 (each) | 117.4 million |
| down_ | Feed-forward: squeezing it back | 14336 → 4096 | 58.7 million |
Shapes from the model’s config.json: hidden size 4,096, 8 key-value heads of 128 (hence 1,024), feed-forward size 14,336.
one layer ≈ 218.1 million weightsThe six rows above, plus two tiny lists of 4,096 for normalisation.
32 layers × 218.1 million ≈ 6.98 billionLlama 3.1 8B has 32 layers.
2 × 128,256 × 4,096 ≈ 1.05 billionOne matrix turns each of the 128,256 tokens into a vector at the start, another turns vectors back into token scores at the end.
6.98 + 1.05 ≈ 8.03 billionExactly the figure on the model card. The “8B” is all matrices.
Can these be multiplied? (A matrix with n columns needs a vector of n numbers.)
-
A 2 × 3 matrix and a vector of 3 numbers
Answer: It fits
-
A 2 × 3 matrix and a vector of 2 numbers
Answer: It does not fit
-
A 4,096 × 4,096 matrix and a token vector of 4,096 numbers
Answer: It fits
-
A (1,000 × 4,096) matrix times a (4,096 × 1,024) matrix
Answer: It fits
-
A (1,000 × 4,096) matrix times a (1,024 × 4,096) matrix
Answer: It does not fit
Remember
- A matrix is a grid of numbers with a shape of rows × columns.
- Matrix × vector: the dot product of each row with the vector. It can change the vector’s length (4,096 → 14,336).
- Geometrically, a matrix rotates, stretches, shears, mirrors or flattens space. The determinant says how areas change; 0 means information is lost.
- Multiplying an (m × n) matrix by an (n × p) one takes m × n × p steps. A model does about 2 operations per weight per token, which is why GPUs and tensor cores matter.
- A model’s parameters are almost all matrices: Llama 3.1 8B’s 8.03 billion can be counted from four numbers in its config.
Words to know
- Matrix
- A rectangle of numbers in rows and columns.
- Shape
- The size of a matrix, written rows × columns.
- Matrix multiplication
- Combining two matrices by taking dot products of rows with columns. Most of a model’s work.
- Determinant
- How much a square matrix multiplies areas (or volumes). Zero means it flattens space and loses information.
- Tensor
- A grid of numbers with any number of directions: a vector, a matrix, a stack of matrices…
- Tensor core
- A part of a GPU that multiplies small blocks of matrices in one step.
- Projection
- In model files (
q_proj,down_proj…): a matrix that turns vectors into vectors of another kind or size.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Essence of Linear Algebra (3Blue1Brown, videos)
Chapters 3 and 4 show matrices moving space, exactly like the demo, with beautiful animation.
-
Scaling Laws for Neural Language Models
Section 2 explains the “2 operations per weight per token” rule and counts a transformer’s parameters, much as you just did.
-
The Llama 3 Herd of Models
Table 3 lists the shapes of the 8B, 70B and 405B models. Try counting the 70B one.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonLearning is walking downhill