Linear algebra for LLMsPart 7: Decompositions and low rank

The SVD

8 min

Rotate, stretch, rotate

A=U Σ V⊤\displaystyle A = U\,\Sigma\,V^\top

Any m×nm \times n matrix factors this way. V⊤V^\top turns the input (an orthogonal matrix: rotation or reflection), Σ\Sigma stretches each axis by a singular value σi≥0\sigma_i \ge 0, and UU turns the result into place. Unlike eigenvectors, the SVD always exists, for every shape.

Write

A=UΣV⊤A = U\Sigma V^\top: U,VU, V orthogonal, Σ\Sigma diagonal with σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \dots \ge 0.

Our matrix

Me₁Me₂

Ae1=[3,4]Ae_1 = [3, 4] and Ae2=[0,5]Ae_2 = [0, 5]. It’s not a rotation or a pure stretch. The SVD will split it into exactly those pieces.

Singular values come from A⊤AA^\top A

A⊤A=A^\top A =
λ=45, 5  ⇒  σ1=45≈6.708,σ2=5≈2.236\displaystyle \lambda = 45,\ 5 \;\Rightarrow\; \sigma_1 = \sqrt{45} \approx 6.708, \quad \sigma_2 = \sqrt 5 \approx 2.236

A⊤A=VΣ2V⊤A^\top A = V\Sigma^2 V^\top is symmetric, so the last deck applies: its eigenvalues are the σi2\sigma_i^2 and its eigenvectors are the columns of VV. Here those are [1,1]/2[1, 1]/\sqrt2 (λ=45\lambda = 45) and [−1,1]/2[-1, 1]/\sqrt2 (λ=5\lambda = 5).

Write

σi=λi(A⊤A)\sigma_i = \sqrt{\lambda_i(A^\top A)}; VV = eigenvectors of A⊤AA^\top A.

Then UU from ui=Avi/σiu_i = Av_i / \sigma_i

u1=Av1σ1=[3,9]/235=[1,3]10\displaystyle u_1 = \frac{A v_1}{\sigma_1} = \frac{[3, 9]/\sqrt2}{3\sqrt5} = \frac{[1, 3]}{\sqrt{10}}
u2=Av2σ2=[−3,1]/25=[−3,1]10\displaystyle u_2 = \frac{A v_2}{\sigma_2} = \frac{[-3, 1]/\sqrt2}{\sqrt5} = \frac{[-3, 1]}{\sqrt{10}}

Each right singular vector viv_i lands on σi\sigma_i times a unit vector uiu_i. The uiu_i come out orthogonal too: (1)(−3)+(3)(1)=0(1)(-3) + (3)(1) = 0.

Write

Avi=σiuiAv_i = \sigma_i u_i.

The three factors

=

V⊤V^\top rotates by −45∘-45^\circ, Σ\Sigma stretches the axes by 6.708 and 2.236, UU rotates by about 71.6∘71.6^\circ. Multiplied back, they give AA. Check: det⁡A=15=σ1σ2\det A = 15 = \sigma_1 \sigma_2.

What the numbers mean

  • σ1\sigma_1 is the most AA can stretch any unit vector: max⁡∣x∣=1∣Ax∣=σ1\max_{|x| = 1} |Ax| = \sigma_1, reached at x=v1x = v_1.
  • σmin⁡\sigma_{\min} is the least.
  • The number of non-zero σi\sigma_i is the rank.
  • A unit circle maps to an ellipse with half-axes σ1u1\sigma_1 u_1 and σ2u2\sigma_2 u_2.

Write

σ1\sigma_1 = max stretch. Rank = number of non-zero σi\sigma_i.

✏️ Your turn

Singular values of a diagonal matrix

Compute A⊤AA^\top A and the singular values.

Work it out on paper, then go to the next card to check.

Answer

A⊤A=[9004]  ⇒  σ1=3,σ2=2\displaystyle A^\top A = \begin{bmatrix} 9 & 0 \\ 0 & 4 \end{bmatrix} \;\Rightarrow\; \sigma_1 = 3, \quad \sigma_2 = 2

Singular values are never negative, even though the matrix has a −3-3: signs and swaps live in UU and VV.

For a weight matrix

U, S, Vh = torch.linalg.svd(W, full_matrices=False)
# W ≈ U @ torch.diag(S) @ Vh;  S is sorted largest first

In a transformer

For a 4096×143364096 \times 14336 weight there are 4,096 singular values. How fast they fall off tells you how much of the matrix a few directions explain: the next deck turns that into compression and LoRA.

📌 Recap

The SVD in one card

  • A=UΣV⊤A = U\Sigma V^\top for every matrix: rotate, stretch, rotate.
  • σi2\sigma_i^2 = eigenvalues of A⊤AA^\top A; Avi=σiuiAv_i = \sigma_i u_i.
  • σ1\sigma_1 = largest stretch; rank = number of non-zero σi\sigma_i.
  • Example: [3045]\begin{bmatrix} 3 & 0 \\ 4 & 5 \end{bmatrix} has σ=45,5\sigma = \sqrt{45}, \sqrt5.

Write

The four lines above.