Linear Algebra for AI
How continuous geometric coordinate arrays (Vectors, Matrices, Tensors) represent every piece of text, memory, and computation inside modern Large Language Models.
Scalars, Vectors, Matrices & Tensors
The Hierarchy of AI Data StructuresEvery piece of data moving through a neural network has a specific mathematical shape:
- Scalar ($0\text{D}$): A single number (e.g., loss $\mathcal{L} = 1.42$, temperature $T = 0.7$).
- Vector ($1\text{D}$): A 1D array of numbers with dimension $d_{\text{model}}$ (e.g., GPT-3 embedding has $d = 12,288$ numbers).
- Matrix ($2\text{D}$): A 2D grid $[M \times N]$ used for weight projections ($W_Q, W_K, W_V$) or a sequence of tokens $[S \times D]$.
- Tensor ($3\text{D}+$): Multi-dimensional container representing batches: $[\text{Batch Size}, \text{Num Heads}, \text{Sequence Length}, d_k]$.
Matrix Multiplication (GEMM) & Feature Projection
How Neural Networks Transform MeaningGeneral Matrix Multiplication (GEMM) is the fundamental workhorse of all AI hardware. When an input vector $\mathbf{x}$ is multiplied by a weight matrix $\mathbf{W}$, each row of $\mathbf{W}$ acts as a custom feature detector:
$$\mathbf{y} = \mathbf{W} \mathbf{x} + \mathbf{b}$$In Self-Attention, Query ($\mathbf{Q}$), Key ($\mathbf{K}$), and Value ($\mathbf{V}$) matrices are produced by 3 parallel matrix multiplications against the input token sequence:
$$\mathbf{Q} = \mathbf{X} \mathbf{W}_Q, \quad \mathbf{K} = \mathbf{X} \mathbf{W}_K, \quad \mathbf{V} = \mathbf{X} \mathbf{W}_V$$Vector Dot Product & Matrix Transposition ($Q K^T$)
The Engine of Attention ScoresThe dot product between two vectors $\mathbf{u} \cdot \mathbf{v} = \sum_{i=1}^d u_i v_i$ computes directional agreement. When two token vectors point in the same direction, their dot product is large and positive.
In Transformer Attention, transposing Key matrix $\mathbf{K}$ to $\mathbf{K}^T$ allows computing dot products between every single pair of tokens simultaneously in a single GPU operation:
$$\text{Scores} = \mathbf{Q} \mathbf{K}^T \in \mathbb{R}^{N \times N}$$