Linear algebra for LLMsPart 10: Capstone and reference

Cheat sheet

5 min

Everything from the ten parts on one page. Each heading links to the part’s first deck.

Vectors

  • Dot product: a⋅b=∑iaibi=∥a∥ ∥b∥cos⁡θa \cdot b = \sum_i a_i b_i = \|a\|\,\|b\| \cos\theta
  • Norm: ∥a∥=a⋅a\|a\| = \sqrt{a \cdot a}; cosine: cos⁡θ=a⋅b∥a∥ ∥b∥\cos\theta = \dfrac{a \cdot b}{\|a\|\,\|b\|}
  • Random directions in dd dimensions: cos⁡θ≈0±1/d\cos\theta \approx 0 \pm 1/\sqrt{d}
  • Unit-variance entries: q⋅kq \cdot k has standard deviation dk\sqrt{d_k}

Matrices

  • Shapes: (m×k)(k×n)=m×n(m \times k)(k \times n) = m \times n; inner sizes must match
  • (AB)ij=Ai,:⋅B:,j(AB)_{ij} = A_{i,:} \cdot B_{:,j}; ABAB = sum of kk outer products A:,kBk,:A_{:,k} B_{k,:}
  • (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top; AB≠BAAB \ne BA in general
  • Cost: 2mkn2mkn FLOPs; group products so the vectors stay vectors
  • * is elementwise (⊙\odot), @ is the matrix product

Transformations

  • Column jj of MM is where eje_j lands
  • Rotation: [cos⁡θ−sin⁡θsin⁡θcos⁡θ]\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}, R⊤R=IR^\top R = I
  • det⁡\det = area scale; det⁡=0\det = 0 ⇔ not invertible ⇔ a non-zero null space
  • rank⁡(uv⊤)=1\operatorname{rank}(uv^\top) = 1; rank⁡(AB)≤min⁡(rank⁡A,rank⁡B)\operatorname{rank}(AB) \le \min(\operatorname{rank} A, \operatorname{rank} B)
  • Projection onto unit uu: (x⋅u) u(x \cdot u)\,u, matrix uu⊤uu^\top

Tensors

  • Broadcasting: align from the right; each axis equal or 1
  • view keeps memory order; split heads with view(B, T, h, d_h).transpose(1, 2)
  • Reductions remove the axis; keepdim=True keeps it as size 1
  • einsum('bhqd,bhkd->bhqk', q, k) = QK⊤QK^\top per batch and head

Attention

  • softmax⁡(z)i=ezi−max⁡z/∑jezj−max⁡z\operatorname{softmax}(z)_i = e^{z_i - \max z} / \sum_j e^{z_j - \max z}
  • Attn⁡=softmax⁡ ⁣(QK⊤dk+mask)V\operatorname{Attn} = \operatorname{softmax}\!\left(\dfrac{QK^\top}{\sqrt{d_k}} + \text{mask}\right) V; mask −∞-\infty above the diagonal; softmax along rows
  • Heads: h⋅dh=dh \cdot d_h = d; GQA shares each K/V head across h/hkvh / h_{kv} query heads
  • KV cache: 2⋅L⋅hkv⋅dh⋅bytes2 \cdot L \cdot h_{kv} \cdot d_h \cdot \text{bytes} per token

The transformer

  • nn.Linear(i, o): weight (o,i)(o, i), y=xW⊤+by = xW^\top + b
  • Block: x+=Attn⁡(norm⁡(x))x \mathrel{+}= \operatorname{Attn}(\operatorname{norm}(x)), then x+=MLP⁡(norm⁡(x))x \mathrel{+}= \operatorname{MLP}(\operatorname{norm}(x))
  • RMSNorm: x/rms⁡(x)⊙gx / \operatorname{rms}(x) \odot g, no mean-centering, no bias
  • SwiGLU: (SiLU⁡(xWg)⊙xWu)Wd(\operatorname{SiLU}(xW_g) \odot xW_u) W_d, 3d dff3 d\, d_{ff} parameters
  • RoPE: rotate pair ii by p θip\,\theta_i, θi=b−2i/dh\theta_i = b^{-2i/d_h}; scores depend on p−qp - q
  • Classic block: 12d212d^2 parameters; forward ≈ 2N2N FLOPs per token

Low rank

  • Av=λvAv = \lambda v; symmetric ⇒\Rightarrow real λ\lambda, orthogonal eigenvectors
  • SVD: A=UΣV⊤=∑iσiuivi⊤A = U \Sigma V^\top = \sum_i \sigma_i u_i v_i^\top; σi2\sigma_i^2 = eigenvalues of A⊤AA^\top A
  • Best rank-rr: keep the top rr terms; error σr+12+…\sqrt{\sigma_{r+1}^2 + \dots} (Frobenius)
  • LoRA: W0+αrBAW_0 + \tfrac{\alpha}{r} BA, r(din+dout)r(d_{\text{in}} + d_{\text{out}}) parameters, B=0B = 0 at init
  • Spectral norm ∥A∥2=σ1\|A\|_2 = \sigma_1; condition number σ1/σn\sigma_1 / \sigma_n

Gradients

  • A gradient has its parameter’s shape; descent: w←w−η ∂L/∂ww \leftarrow w - \eta\, \partial L/\partial w
  • Y=XWY = XW: ∂L/∂W=X⊤G\partial L/\partial W = X^\top G, ∂L/∂X=GW⊤\partial L/\partial X = GW^\top; bias: sum GG’s rows
  • Softmax + cross-entropy: ∂L/∂z=p−onehot⁡(y)\partial \mathcal{L}/\partial z = p - \operatorname{onehot}(y)
  • Training ≈ 6ND6ND FLOPs; Adam in mixed precision ≈ 16 bytes per parameter

Hardware

  • Bytes: FP32 4, BF16 2, FP8 and INT8 1, INT4 ½; BF16 has FP32’s range
  • Intensity II = FLOPs / bytes; matrix × vector I≈1I \approx 1, batch BB gives I≈BI \approx B
  • Decode ≤ bandwidth / bytes per token; prefill ≈ 2NT+4LT2d2NT + 4LT^2 d
  • INT8: s=max⁡∣x∣/127s = \max|x| / 127, q=round⁡(x/s)q = \operatorname{round}(x/s), error ≤s/2\le s/2
  • Online softmax: ℓ′=ℓ em−m′+∑ezj−m′\ell' = \ell\, e^{m - m'} + \sum e^{z_j - m'}