Vectors and Matrix Multiplication
Dot products, matrix shapes, and the one rule that governs whether two arrays can multiply.
Aug 3, 2026
A vector is an ordered list of numbers. A matrix is a rectangular grid of them, described by its shape: an matrix has rows and columns.
The dot product
The dot product of two vectors of the same length multiplies them element-by-element and sums the results:
[1, 2, 3] · [4, 5, 6] = 1*4 + 2*5 + 3*6 = 32One number out. For unit-length vectors, it is positive when they point in a similar direction, zero when they are perpendicular, and negative when they oppose. For unnormalized vectors, magnitude also affects the result. Attention learns query and key vectors so this score can help decide what information to read.
Matrix multiplication
Multiplying an matrix by an matrix produces an matrix, in which entry is the dot product of row of the first with column of the second.
(m × n) @ (n × p) -> (m × p)
^^^^^^^ these must matchThe inner dimensions must agree and they vanish from the result. This rule catches many shape errors and lets you check the dimensions claimed by an architecture diagram.
In code this is usually written A @ B. It is not commutative: A @ B and
B @ A are different operations and often have incompatible shapes.
The transpose of a matrix swaps its rows and columns. If has shape , then has shape . This is why the attention score product has shape : it contains one score for each pair of token positions.
Why it dominates the cost
A single by multiply performs multiply-adds. Under the common convention that counts a multiplication and an addition separately, that is about FLOPs. Matrix multiplication accounts for most arithmetic in common dense transformer layers, which is why model cost is often quoted in FLOPs and accelerators are designed around this operation.
An embedding lookup is equivalent to selecting a row rather than performing a dense multiplication. After that lookup, the query/key/value projections, attention scores, feed-forward block, and final vocabulary projection are all dominated by matrix multiplications, with normalization and other elementwise operations between them.