Availit World How AI really works, in your language

Lesson 5 of 4614 minFacts checked October 20, 2026

Matrices: machines that transform vectors

A matrix is a grid of numbers that turns one vector into another. A language model is, more than anything else, a long chain of them. By the end of this lesson you will count the 8 billion weights of a real model yourself.

In this lesson you will

  • Read a matrix and multiply it by a vector, by hand
  • See a matrix as something that rotates, stretches and squashes space
  • Count how many multiplications a model does for each token, and why that suits GPUs
  • Find the matrices inside a real model, and add up its parameters
On this page
  1. A grid of numbers
  2. Matrix times vector
  3. A matrix moves space
  4. Many vectors at once
  5. Inside a real model: count the weights yourself
  6. Remember
  7. Check yourself

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.

One row at a time
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 = 10
row 2: [1, 3, 0] · [4, 1, 2] = 4 + 3 + 0 = 7
M × [4, 1, 2] = [10, 7]

Two rows, so two numbers come out.

(rows × columns) matrix × (columns) vector = (rows) vector

The 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 has the rows [1, 2] and [0, −1]. What is that matrix times the vector [3, 1]?

Show the answer

Answer: [5, −1] Row 1: 1×3 + 2×1 = 5. Row 2: 0×3 + (−1)×1 = −1. So the answer is [5, −1].

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.

Try itBend the page

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.

Counting the work
(m × n) matrix × (n × p) matrix = (m × p) matrix

The 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 steps

One for every pair of numbers that meet.

1,000 tokens × 4,096 × 4,096 ≈ 16.8 billion steps

Just 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.

The matrices in one layer of Llama 3.1 8B
Name in the fileWhat it is forNumbers in, numbers outWeights
q_projAttention: what each token is looking for4096 → 409616.8 million
k_projAttention: what each token offers4096 → 10244.2 million
v_projAttention: what each token passes on4096 → 10244.2 million
o_projAttention: mixing the results back4096 → 409616.8 million
gate_proj, up_projFeed-forward: two matrices that widen the vector4096 → 14336 (each)117.4 million
down_projFeed-forward: squeezing it back14336 → 409658.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.

Adding it up
one layer ≈ 218.1 million weights

The six rows above, plus two tiny lists of 4,096 for normalisation.

32 layers × 218.1 million ≈ 6.98 billion

Llama 3.1 8B has 32 layers.

2 × 128,256 × 4,096 ≈ 1.05 billion

One 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 billion

Exactly 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.)

0 of 5 sorted
  • 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.

Check yourself

5 questions. Tap the answer you think is right.

0 of 5 answered

A matrix has 3 rows and 5 columns. Which vector can it multiply?

Show the answer

Answer: A vector of 5 numbers The vector needs as many numbers as the matrix has columns: 5. The answer then has 3 numbers, one per row.

How do you get the first number of matrix × vector?

Show the answer

Answer: Take the dot product of the first row with the vector Each row, dotted with the vector, gives one number of the answer.

A matrix has a determinant of 0. What does it do to the page?

Show the answer

Answer: Flattens it, so different points land in the same place Determinant 0 means areas become 0: the page is squashed onto a line or a point, and the original can no longer be recovered.

Roughly how many operations does a 70-billion-weight model do per token?

Show the answer

Answer: About 140 billion About 2 per weight per token (a multiply and an add): 2 × 70 billion = 140 billion.

Where are most of a language model’s parameters?

Show the answer

Answer: In its matrices Nearly all weights are entries of matrices: the attention and feed-forward matrices of each layer, plus the embedding and output matrices.

Lesson complete

Answer every question above to complete this lesson.

Your progress is saved on this device only.

Next lessonLearning is walking downhill
Previous lesson All lessons

Choose your language