Skip to content

Linear algebra problem set, part b: inner products, SVD and low rank, matmul accounting and the roofline

ModuleS-M03b · solve · none · Pass 3 · 3 to 4 h
You buildanswers in solve/S-M03b.toml (14 checked by SymPy) and 2 proofs in solve/S-M03b/q3.md and q8.md (self-graded against their rubrics)
Contractnone: a pen and paper set
Testscourse/solve/S-M03b/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M03b/problems.md and in section 4
Needsno module. Reading: S-M03a, M03.5 (SVD, Eckart-Young, least squares), M03.6 (inner products, cosine, projection), and the Linear Algebra topic
Used byno call site (a solve set). It checks the definitions behind M03.5 and M03.6, which L2.3, L6.6, L7.6, and C1 call, and the FLOP and byte counts L9.1 tiles its matmul around
MilestoneMS-P3 (the Pass 3 gate runs ol check on every solve part of the pass)
Optional depthStrang, Introduction to Linear Algebra, sections 1.2, 4.2, 7.1 to 7.2; Trefethen and Bau, Numerical Linear Algebra, lectures 4 and 5; Williams, Waterman, and Patterson, “Roofline: an insightful visual performance model for multicore architectures” (CACM 2009)
  • Cosine similarity divides the inner product by both lengths, and Cauchy-Schwarz keeps it in [−1,1][-1, 1] (q1, q3).
  • A projection onto a line divides by a⋅aa \cdot a, not by ∥a∥\lVert a \rVert (q2).
  • Singular values are the square roots of the eigenvalues of A⊤AA^\top A, never the diagonal of AA, and a matrix has min⁡(m,n)\min(m, n) of them (q4, q5, q8).
  • Truncating the SVD after rr terms leaves an error of exactly the first dropped singular value in the spectral norm and the root of the sum of squares of the dropped ones in the Frobenius norm (q6); a rank-rr factorization of a d×dd \times d matrix costs 2dr2dr numbers (q7).
  • A matmul does 2MNK2MNK FLOPs on O(MK+KN+MN)O(MK + KN + MN) bytes, so its intensity grows with size; a matrix-vector product does about half a FLOP per byte whatever its size, which is why decoding is memory-bound (q9, q10).
Terminal window
ol start S-M03b # writes solve/S-M03b.toml and one file per proof
ol check S-M03b # SymPy checks the answers, then asks each proof rubric (y/n)
ol check S-M03b --regrade # ask the rubrics again after you change a proof

Pass 3 builds the first models that live on vectors: L2.3 factors a word co-occurrence matrix with M03.5’s SVD and ranks neighbours with M03.6’s cosine similarity, and later L6.6 and L7.6 reuse the same low-rank factorization on weight matrices. Before the code, you need to compute these quantities by hand on small cases: an angle, a projection, a singular value, the error of a truncation. The set closes with arithmetic you will need again in Pass 6, where L9.1 tiles the C matmul: how many FLOPs and how many bytes a product costs, and when the machine is waiting on memory instead of computing.

SymbolMeaningType / shape
a⋅ba \cdot b, ∥a∥\lVert a \rVertinner product ∑iaibi\sum_i a_i b_i and length a⋅a\sqrt{a \cdot a}scalars
cos⁡θ=a⋅b∥a∥∥b∥\cos\theta = \frac{a \cdot b}{\lVert a \rVert \lVert b \rVert}cosine similarityscalar in [−1,1][-1, 1]
proj⁡ab=a⋅ba⋅aa\operatorname{proj}_a b = \frac{a \cdot b}{a \cdot a} aprojection of bb onto the line through aavector
σ1≥σ2≥…\sigma_1 \ge \sigma_2 \ge \dotssingular valuesscalars ≥0\ge 0
ArA_rtruncated SVD of rank rrmatrix
∥⋅∥2\lVert \cdot \rVert_2, ∥⋅∥F\lVert \cdot \rVert_Fspectral and Frobenius normsscalars
FLOPone floating-point add or multiply
IIarithmetic intensity: FLOPs per byte moved between memory and the processorFLOP/byte
PP, β\betapeak compute (FLOP/s) and memory bandwidth (byte/s)

The inner product a⋅b=∥a∥∥b∥cos⁡θa \cdot b = \lVert a \rVert \lVert b \rVert \cos\theta carries both length and angle; cosine similarity keeps the angle. Projecting bb onto the line through aa keeps the component of bb along aa: the coefficient cc in b=c a+rb = c\,a + r with r⊥ar \perp a is c=(a⋅b)/(a⋅a)c = (a \cdot b)/(a \cdot a). The Cauchy-Schwarz inequality ∣a⋅b∣≤∥a∥∥b∥\lvert a \cdot b \rvert \le \lVert a \rVert \lVert b \rVert (q3) is what makes the cosine a cosine.

A=UΣV⊤A = U\Sigma V^\top with orthonormal UU, VV and σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \dots \ge 0. Multiplying out, A⊤A=VΣ2V⊤A^\top A = V\Sigma^2 V^\top, so σi2\sigma_i^2 are the eigenvalues of A⊤AA^\top A (q8). The spectral norm is σ1\sigma_1 and the Frobenius norm is ∑iσi2\sqrt{\sum_i \sigma_i^2}. Eckart-Young: the truncated SVD ArA_r is the closest rank-rr matrix, with ∥A−Ar∥2=σr+1\lVert A - A_r \rVert_2 = \sigma_{r+1} and ∥A−Ar∥F=∑i>rσi2\lVert A - A_r \rVert_F = \sqrt{\sum_{i > r}\sigma_i^2}.

C=ABC = AB with AA of shape M×KM \times K and BB of shape K×NK \times N has MNMN entries, each a sum of KK products: KK multiplies and KK adds, so 2MNK2MNK FLOPs (the usual convention counts the KK-th add even though a sum of KK terms needs only K−1K - 1). The least it can move is reading AA and BB once and writing CC once: 4(MK+KN+MN)4(MK + KN + MN) bytes in fp32. Arithmetic intensity is the ratio, FLOPs per byte. The roofline model says a kernel runs at most at min⁡(P,Iβ)\min(P, I\beta) FLOP/s: below the ridge point I=P/βI = P/\beta it is memory-bound (faster memory helps, more compute does not), above it compute-bound. For a square N×NN \times N matmul the intensity grows like NN; for a matrix-vector product it stays near 1/21/2, whatever NN.

These are siblings of q1, q6, and q10, not graded problems.

Cosine. u=(1,0,1)u = (1, 0, 1), v=(1,1,0)v = (1, 1, 0): u⋅v=1u \cdot v = 1, ∥u∥=∥v∥=2\lVert u \rVert = \lVert v \rVert = \sqrt{2}, cos⁡θ=1/2\cos\theta = 1/2 (60°60°). Written in solve/ as 1/2.

Singular values and truncation. A=(2001)A = \begin{pmatrix} 2 & 0 \\ 0 & 1 \end{pmatrix} is already diagonal with non-negative entries in order, so U=V=IU = V = I and σ=(2,1)\sigma = (2, 1). A1=(2000)A_1 = \begin{pmatrix} 2 & 0 \\ 0 & 0 \end{pmatrix}, and A−A1=(0001)A - A_1 = \begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix} has ∥⋅∥2=∥⋅∥F=1=σ2\lVert \cdot \rVert_2 = \lVert \cdot \rVert_F = 1 = \sigma_2. For (0−310)\begin{pmatrix} 0 & -3 \\ 1 & 0 \end{pmatrix} the diagonal says nothing: A⊤A=(1009)A^\top A = \begin{pmatrix} 1 & 0 \\ 0 & 9 \end{pmatrix}, so σ=(3,1)\sigma = (3, 1).

Roofline. A machine with P=1P = 1 TFLOP/s and β=50\beta = 50 GB/s has its ridge at 1012/(5×1010)=2010^{12}/(5 \times 10^{10}) = 20 FLOP/byte. An fp32 N×NN \times N matmul has intensity 2N3/(12N2)=N/62N^3 / (12 N^2) = N/6, so it is compute-bound from N/6≥20N/6 \ge 20, N≥120N \ge 120. A matrix-vector product at intensity about 1/21/2 runs at most at 50×109/2=2550 \times 10^9 / 2 = 25 GFLOP/s, 2.5% of peak.

Write each answer in solve/S-M03b.toml; lettered parts are their own tables:

[q1.b]
answer = "4/(3*sqrt(5))"
[q5]
answer = "[3, 2]"
[q9.a]
answer = "2*M*N*K"
[q3]
proof = "S-M03b/q3.md"

Give exact values (sqrt(5), not 2.236).

q1. Let u=(1,2,2)u = (1, 2, 2) and v=(2,0,1)v = (2, 0, 1). Give (a) the inner product u⋅vu \cdot v and (b) the cosine of the angle between uu and vv. [number]

q2. Give the orthogonal projection of b=(1,2,3)b = (1, 2, 3) onto the line spanned by a=(1,1,1)a = (1, 1, 1). [vector]

q3. Prove the Cauchy-Schwarz inequality ∣a⋅b∣≤∥a∥ ∥b∥\lvert a \cdot b \rvert \le \lVert a \rVert \, \lVert b \rVert for vectors a,b∈Rna, b \in \mathbb{R}^n, with equality exactly when one is a multiple of the other. Conclude that cosine similarity lies in [−1,1][-1, 1]. [proof]

q4. A=(1101)A = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}. Give (a) its largest singular value σ1\sigma_1 and (b) its smallest σ2\sigma_2. [number]

q5. Give the singular values of A=(023000)A = \begin{pmatrix} 0 & 2 \\ 3 & 0 \\ 0 & 0 \end{pmatrix} in descending order. [vector]

q6. A 3×33 \times 3 matrix AA has singular values 5,3,15, 3, 1, and ArA_r is its truncated SVD of rank rr. Give (a) ∥A−A1∥2\lVert A - A_1 \rVert_2, (b) ∥A−A1∥F\lVert A - A_1 \rVert_F, (c) the smallest rr with ∥A−Ar∥F≤3/2\lVert A - A_r \rVert_F \le 3/2. [number]

q7. A weight matrix of shape 4096×40964096 \times 4096 is replaced by a product BCBC with BB of shape 4096×r4096 \times r and CC of shape r×4096r \times 4096 (a rank-rr adapter). Give the number of parameters in BB and CC together. [expr in r]

q8. Let A=UΣV⊤A = U \Sigma V^\top be an SVD of a real m×nm \times n matrix. Prove that (i) the columns of VV are eigenvectors of A⊤AA^\top A with eigenvalues σi2\sigma_i^2, so the singular values are the square roots of the eigenvalues of A⊤AA^\top A, and (ii) max⁡∥x∥=1∥Ax∥=σ1\max_{\lVert x \rVert = 1} \lVert A x \rVert = \sigma_1. [proof]

q9. (a) Give the number of floating-point operations (one multiply and one add per term) of C=ABC = AB with AA of shape M×KM \times K and BB of shape K×NK \times N. [expr in M, N, K] (b) A matrix-vector product y=Wxy = Wx with WW of shape N×NN \times N in fp32 (4 bytes per number) reads WW and xx once and writes yy once. Give its arithmetic intensity, floating-point operations per byte moved. [expr in N]

q10. A square N×NN \times N fp32 matmul reads AA and BB once and writes CC once. (a) Give its arithmetic intensity in FLOP per byte. [expr in N] (b) A machine has a peak of 1010 TFLOP/s and a memory bandwidth of 100100 GB/s. Give the smallest NN at which this matmul reaches the compute-bound side of the roofline (intensity at least the ridge point). [number]

PitfallSymptomCaught by
Dividing the inner product by one length, or by squared lengthsa “cosine” outside [−1,1][-1, 1] or one that changes when a vector is rescaledq1 b (canaries 4/15, 4/5)
Dividing by ∥a∥\lVert a \rVert instead of a⋅aa \cdot a in a projectionthe projection scaled by ∥a∥\lVert a \rVertq2 (canary with sqrt(3))
Reading singular values off the diagonalwrong whenever AA is not diagonal with non-negative entriesq4 (canary 1)
Reporting eigenvalues of A⊤AA^\top A as singular valuesthe square of the right answerq4 (canaries without the square root)
Listing max⁡(m,n)\max(m, n) singular values, or not sorting thema padded or unordered spectrum; low_rank keeps the wrong directionsq5 (canaries)
Confusing the spectral and the Frobenius error, or summing values instead of squaresthe wrong truncation rank chosen for a budgetq6 (canaries)
Counting multiply-adds as FLOPs, or numbers as bytesintensities off by a factor 2 or 4; the wrong side of the ridgeq9 a, q10 a and b (canaries)
Forgetting the vector reads in a matrix-vector productan intensity of exactly 1/2 instead of slightly lessq9 b (canary)
DirectionModuleHow it uses this
BackS-M03aelimination, bases, eigenvalues, QR
BackM03.6cosine_sim and project are q1 and q2 as code
BackM03.5svd, low_rank (q4 to q7), and the eigenvalue bridge of q8
ForwardL2.3PPMI-SVD embeddings keep the top singular directions; analogies rank by cosine
ForwardL6.6LoRA’s 2dr2dr parameters (q7) and PiSSA’s truncated SVD
ForwardL9.1the tiled matmul: intensity and the ridge point decide the tile size
ForwardL8.2decoding is a matrix-vector product per token: memory-bound (q9 b)