Linear algebra problem set, part b: inner products, SVD and low rank, matmul accounting and the roofline
Overview
Section titled “Overview”| Module | S-M03b · solve · none · Pass 3 · 3 to 4 h |
| You build | answers 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) |
| Contract | none: a pen and paper set |
| Tests | course/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 |
| Needs | no module. Reading: S-M03a, M03.5 (SVD, Eckart-Young, least squares), M03.6 (inner products, cosine, projection), and the Linear Algebra topic |
| Used by | no 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 |
| Milestone | MS-P3 (the Pass 3 gate runs ol check on every solve part of the pass) |
| Optional depth | Strang, 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) |
Key Takeaways
Section titled “Key Takeaways”- Cosine similarity divides the inner product by both lengths, and Cauchy-Schwarz keeps it in (q1, q3).
- A projection onto a line divides by , not by (q2).
- Singular values are the square roots of the eigenvalues of , never the diagonal of , and a matrix has of them (q4, q5, q8).
- Truncating the SVD after 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- factorization of a matrix costs numbers (q7).
- A matmul does FLOPs on 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).
How to work this chapter
Section titled “How to work this chapter”ol start S-M03b # writes solve/S-M03b.toml and one file per proofol 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 proof1. Why now
Section titled “1. Why now”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.
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| , | inner product and length | scalars |
| cosine similarity | scalar in | |
| projection of onto the line through | vector | |
| singular values | scalars | |
| truncated SVD of rank | matrix | |
| , | spectral and Frobenius norms | scalars |
| FLOP | one floating-point add or multiply | |
| arithmetic intensity: FLOPs per byte moved between memory and the processor | FLOP/byte | |
| , | peak compute (FLOP/s) and memory bandwidth (byte/s) |
2.1 Inner products, cosine, projection
Section titled “2.1 Inner products, cosine, projection”The inner product carries both length and angle; cosine similarity keeps the angle. Projecting onto the line through keeps the component of along : the coefficient in with is . The Cauchy-Schwarz inequality (q3) is what makes the cosine a cosine.
2.2 SVD and low rank
Section titled “2.2 SVD and low rank”with orthonormal , and . Multiplying out, , so are the eigenvalues of (q8). The spectral norm is and the Frobenius norm is . Eckart-Young: the truncated SVD is the closest rank- matrix, with and .
2.3 FLOPs, bytes, and the roofline
Section titled “2.3 FLOPs, bytes, and the roofline”with of shape and of shape has entries, each a sum of products: multiplies and adds, so FLOPs (the usual convention counts the -th add even though a sum of terms needs only ). The least it can move is reading and once and writing once: bytes in fp32. Arithmetic intensity is the ratio, FLOPs per byte. The roofline model says a kernel runs at most at FLOP/s: below the ridge point it is memory-bound (faster memory helps, more compute does not), above it compute-bound. For a square matmul the intensity grows like ; for a matrix-vector product it stays near , whatever .
3. Worked example by hand
Section titled “3. Worked example by hand”These are siblings of q1, q6, and q10, not graded problems.
Cosine. , : , , (). Written in solve/ as 1/2.
Singular values and truncation. is already diagonal with non-negative entries in order, so and . , and has . For the diagonal says nothing: , so .
Roofline. A machine with TFLOP/s and GB/s has its ridge at FLOP/byte. An fp32 matmul has intensity , so it is compute-bound from , . A matrix-vector product at intensity about runs at most at GFLOP/s, 2.5% of peak.
4. The problem set
Section titled “4. The problem set”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).
Inner products and projections
Section titled “Inner products and projections”q1. Let and . Give (a) the inner product and (b) the cosine of the angle between and . [number]
q2. Give the orthogonal projection of onto the line spanned by . [vector]
q3. Prove the Cauchy-Schwarz inequality for vectors , with equality exactly when one is a multiple of the other. Conclude that cosine similarity lies in . [proof]
SVD and low rank
Section titled “SVD and low rank”q4. . Give (a) its largest singular value and (b) its smallest . [number]
q5. Give the singular values of in descending order. [vector]
q6. A matrix has singular values , and is its truncated SVD of rank . Give (a) , (b) , (c) the smallest with . [number]
q7. A weight matrix of shape is replaced by a product with of shape and of shape (a rank- adapter). Give the number of parameters in and together. [expr in r]
q8. Let be an SVD of a real matrix. Prove that (i) the columns of are eigenvectors of with eigenvalues , so the singular values are the square roots of the eigenvalues of , and (ii) . [proof]
Matmul accounting and the roofline
Section titled “Matmul accounting and the roofline”q9. (a) Give the number of floating-point operations (one multiply and one add per term) of with of shape and of shape . [expr in M, N, K] (b) A matrix-vector product with of shape in fp32 (4 bytes per number) reads and once and writes once. Give its arithmetic intensity, floating-point operations per byte moved. [expr in N]
q10. A square fp32 matmul reads and once and writes once. (a) Give its arithmetic intensity in FLOP per byte. [expr in N] (b) A machine has a peak of TFLOP/s and a memory bandwidth of GB/s. Give the smallest at which this matmul reaches the compute-bound side of the roofline (intensity at least the ridge point). [number]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Dividing the inner product by one length, or by squared lengths | a “cosine” outside or one that changes when a vector is rescaled | q1 b (canaries 4/15, 4/5) |
| Dividing by instead of in a projection | the projection scaled by | q2 (canary with sqrt(3)) |
| Reading singular values off the diagonal | wrong whenever is not diagonal with non-negative entries | q4 (canary 1) |
| Reporting eigenvalues of as singular values | the square of the right answer | q4 (canaries without the square root) |
| Listing singular values, or not sorting them | a padded or unordered spectrum; low_rank keeps the wrong directions | q5 (canaries) |
| Confusing the spectral and the Frobenius error, or summing values instead of squares | the wrong truncation rank chosen for a budget | q6 (canaries) |
| Counting multiply-adds as FLOPs, or numbers as bytes | intensities off by a factor 2 or 4; the wrong side of the ridge | q9 a, q10 a and b (canaries) |
| Forgetting the vector reads in a matrix-vector product | an intensity of exactly 1/2 instead of slightly less | q9 b (canary) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | S-M03a | elimination, bases, eigenvalues, QR |
| Back | M03.6 | cosine_sim and project are q1 and q2 as code |
| Back | M03.5 | svd, low_rank (q4 to q7), and the eigenvalue bridge of q8 |
| Forward | L2.3 | PPMI-SVD embeddings keep the top singular directions; analogies rank by cosine |
| Forward | L6.6 | LoRA’s parameters (q7) and PiSSA’s truncated SVD |
| Forward | L9.1 | the tiled matmul: intensity and the ridge point decide the tile size |
| Forward | L8.2 | decoding is a matrix-vector product per token: memory-bound (q9 b) |