Scalars, Vectors, Matrices & Tensors
In artificial intelligence, everything is represented as a multidimensional array: numerical data, model weights, hidden activations, and gradients. Understanding how dimensions align, contract, and broadcast is the core prerequisite for reading model architectures and debugging CUDA kernels.
1. The Dimensional Ladder
text
Typographic Conventions in AI Papers:
- Scalars ($s, \alpha, \lambda$): Lowercase plain or Greek letters.
- Vectors ($\mathbf{v}, \vec{v}, \mathbf{x}$): Lowercase bold letters. By convention in linear algebra, all vectors are column vectors ($d \times 1$) unless explicitly transposed ($\mathbf{x}^T$ is a $1 \times d$ row vector).
- Matrices ($W, A, \Sigma$): Uppercase letters (often uppercase bold $\mathbf{W}$).
- Tensors ($\mathcal{T}, \mathbf{X}$): Uppercase calligraphic or bold symbols.
2. Standard Tensor Axis Signatures in Transformers
When reading transformer papers (e.g., Llama, GPT-4, Mistral), tensors frequently possess 3 to 5 axes:
text
3. Broadcasting: The Implicit Alignment Rules
In both mathematics and modern tensor libraries (NumPy, PyTorch, JAX), binary operations (addition, subtraction, element-wise multiplication) between tensors of different ranks follow Broadcasting Rules:
The Broadcasting Contract:
Two axes are compatible when:
- They are equal in size, or
- One of the dimensions is 1 (or missing).
text
In mathematical equations, authors often omit explicit expansion notation, writing simply $X + b$. Understanding broadcasting allows you to instantly recognize that $b$ is expanded to match the leading axes of $X$.
4. Einstein Summation (einsum): The Universal Tensor Language
Einstein summation convention simplifies multidimensional contractions by omitting the summation symbol $\sum$ and summing over any index that appears multiple times in a product.
The Notation Rule:
- Any index that appears in the inputs but does not appear in the output signature is contracted (summed over).
- Any index that appears in both inputs and outputs is retained (batched).
Common AI Operators Decoded via Einsum:
python
Why Learn Einsum?: Whenever a research paper describes an exotic tensor contraction—such as Rotary Position Embedding (RoPE) rotations or Multi-Head Latent Attention (MLA)—writing theeinsumstring is the fastest way to verify dimension consistency and eliminate shape bugs.