9.8 Linear algebra problem set, part b: inner products, SVD and low rank, matmul accounting and the roofline
The later sections of this chapter build the first tools that live on vectors. word2vec skip-gram with negative sampling, and the PPMI-SVD baseline in AI Engineering factors a word co-occurrence matrix with the SVD of SVD, Eckart-Young low rank, and least squares and ranks neighbours with the cosine similarity of Inner products, projections, cosine similarity, and top-k. Later, LoRA: low-rank adaptation with PiSSA initialization and merging and Multi-head latent attention reuse the same low-rank factorization on weight matrices, and the TinyStories capstone fits a scaling law with the same least squares.
Concepts from other sections (14)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Cosine similarity: the inner product divided by both lengths; depends only on direction. Read more: 9.6 Inner products, projections, cosine similarity, and top-k
- Inner product: . Read more: 9.6 Inner products, projections, cosine similarity, and top-k
- Eigenvector / eigenvalue: a non-zero and scalar with . Read more: 9.4 Eigenvalues, power iteration, and the spectral radius
- roofline: the bound on a loop’s speed from peak compute , memory bandwidth , and arithmetic intensity . Read more: 22.3 Cache-blocked, packed, batch-invariant matmul in C in AI Engineering
- memory-bound: limited by the rate at which data can be read, not by arithmetic throughput. Read more: 22.7 Fused int4 and int8 dequantize-matmul in C in AI Engineering
- Frobenius norm: , the square root of the sum of squared entries. Read more: 9.5 SVD, Eckart-Young low rank, and least squares
- arithmetic intensity: FLOPs per byte moved; for a linear layer at BF16 it equals the number of tokens in the step. Read more: 20.4.6 Load tests and the deployment runbook in AI Engineering
- Orthonormal: a set of unit-length vectors that are pairwise orthogonal. Read more: 9.3 Orthogonality, Householder QR, and orthogonal initialization
- Spectral norm: , the largest stretch of a unit vector. Read more: 9.5 SVD, Eckart-Young low rank, and least squares
- Orthogonal projection: , the closest point to on the line through . Read more: 9.6 Inner products, projections, cosine similarity, and top-k
- Discriminant: , whose sign decides whether a quadratic has two, one, or no real roots. Read more: 6.4 Polynomials, Horner’s rule, and stable quadratic roots
- Determinant: a single number for a square matrix: the factor by which scales signed volume. It is zero exactly when squashes space flat and so cannot be inverted, and . Read more: Chapter 9: Linear Algebra
- LoRA: a frozen Linear plus a trainable low-rank update . Read more: 12.6 LoRA: low-rank adaptation with PiSSA initialization and merging in AI Engineering
- ridge point: the intensity that separates memory-bound from compute-bound work. Read more: 20.4.6 Load tests and the deployment runbook in AI Engineering
Before the code, it pays 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 that comes back when Cache-blocked, packed, batch-invariant matmul in C 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.
This is a pen-and-paper set. It assumes the linear algebra problem set, part a, the SVD and inner-product sections, and the chapter opening. Every problem is followed by a collapsed worked answer with the final result, the derivation, and the common wrong answers and why they are wrong. There are 14 exact answers (counting lettered parts) and 2 proofs (q3, q8); model proofs for those two are also in course/solutions/solve/S-M03b/. Try each problem before opening its answer.
Learning objectives
- Compute inner products, cosines, and projections, and prove the Cauchy-Schwarz inequality.
- Find singular values through the eigenvalues of and compute the error of a truncated SVD in both norms.
- Count the parameters of a low-rank factorization.
- Count the FLOPs and bytes of a matmul and a matrix-vector product, and use the roofline to decide whether a kernel is memory-bound or compute-bound.
The ideas in brief
Section titled “The ideas in brief”These are the facts the problems draw on. The notation:
| 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) |
Inner products, cosine, projection
Section titled “Inner products, cosine, projection”The inner product carries both length and angle; cosine similarity keeps the angle by dividing by both lengths. Projecting onto the line through keeps the component of along : the coefficient in with is , divided by and not by . The Cauchy-Schwarz inequality (q3) is what makes the cosine a cosine, keeping it in .
SVD and low rank
Section titled “SVD and low rank”with orthonormal , and . Multiplying out, , so are the eigenvalues of (q8): singular values are never simply the diagonal of , and a matrix has of them. The spectral norm is and the Frobenius norm is . Eckart-Young: the truncated SVD is the closest rank- matrix, with and . A rank- factorization of a matrix costs numbers.
FLOPs, bytes, and the roofline
Section titled “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 , which is why decoding is memory-bound.
Three examples worked by hand
Section titled “Three examples worked by hand”These are siblings of q1, q6, and q10, not problems of the set.
Cosine. , : , , ().
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.
How the answers are graded
Section titled “How the answers are graded”The problems are grouped by topic, in the order above. Answers are exact (, not 2.236). Lettered parts are answered separately. The tag after each problem says what kind of answer is expected.
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]
Answer
(a) . (b) . (a) . (b) and , so . The cosine_sim of Inner products, projections, cosine similarity, and top-k divides by both lengths in the same way. Common mistakes for (b): , which divides by , and , which divides by only; neither is invariant under rescaling the vectors.
q2. Give the orthogonal projection of onto the line spanned by . [vector]
Answer
. . Its project divides by for the same reason. Common mistakes: , which divides by instead of , and , which is the residual rather than the projection.
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]
Answer
Claim. For , , with equality exactly when one is a scalar multiple of the other.
Proof. If , both sides are 0 and : equality holds. Let . A squared length is never negative, so for every real , Choose : Multiplying by gives , and taking non-negative square roots gives the inequality.
Equality. If equality holds, the chain gives for that , so . Conversely, if , then (and symmetrically if ).
Cosine. For nonzero , dividing by gives , with exactly for parallel or opposite vectors.
A complete proof treats separately, expands , picks (or uses the discriminant of the quadratic in ), and proves both directions of the equality case. Model proof also in q3.md.
SVD and low rank
Section titled “SVD and low rank”q4. . Give (a) its largest singular value and (b) its smallest . [number]
Answer
(a) . (b) . has trace 3 and determinant 1, so its eigenvalues are . The singular values are their square roots, and gives them in closed form ( and ). Check: . Common mistakes: 1 for either part, read off the diagonal (singular values are not the diagonal unless is diagonal and non-negative), and , the eigenvalues of without the square root.
q5. Give the singular values of in descending order. [vector]
Answer
. , so the singular values are 3 and 2, listed in descending order as the svd of SVD, Eckart-Young low rank, and least squares returns them (the signs and the order of the columns live in and ). Common mistakes: , the column order, and : a matrix has singular values.
q6. A matrix has singular values , and is its truncated SVD of rank . Give (a) , (b) , (c) the smallest with . [number]
Answer
(a) . (b) . (c) . By Eckart-Young, has singular values . (a) Its spectral norm is the largest, : the first dropped singular value. (b) Its Frobenius norm is . (c) , while , so . Common mistakes: 1 for (a) (the last singular value rather than the first dropped), for (a) (the Frobenius error), and 4 for (b) (summing the dropped values instead of their squares).
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]
Answer
. has entries and has , so together : the that LoRA (LoRA: low-rank adaptation with PiSSA initialization and merging) trains instead of . At that is 65 536 parameters, 0.4% of the full matrix. Common mistakes: (one factor only) and .
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]
Answer
Claim. Let with () and () orthogonal and () zero except , . Then (i) for each column of (with beyond ), and (ii) .
Proof. (i) Using , , where is diagonal with entries . With and : . The form a basis, so these are all the eigenvalues, and .
(ii) Let and ; because is orthogonal. Then At , and , so the bound is attained.
The essential steps are the two orthogonality cancellations ( in (i), in (ii)) and showing the maximum is attained, not just bounded. This is why Eckart-Young’s spectral error is . Model proof also in q8.md.
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]
Answer
(a) . (b) . (a) has entries, each multiplies and adds. (b) A matrix-vector product does FLOPs and moves bytes (, , ), so its intensity is just under for every . That is why decoding, one matrix-vector product per weight matrix per token, is memory-bound. Common mistakes: for (a), counting multiply-adds instead of FLOPs; for (b), forgetting to move and ; and , the square matmul’s intensity.
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]
Answer
(a) . (b) . (a) FLOPs over bytes gives . It grows with , which is why tiling a large matmul (Cache-blocked, packed, batch-invariant matmul in C) pays off. (b) The ridge point is FLOP/byte, and gives . Common mistakes: for (a) (numbers counted instead of 4-byte values), (multiply-adds instead of FLOPs), 100 for (b) (the ridge point itself, not ), and 150 (from the intensity ).
Common mistakes
Section titled “Common mistakes”The wrong answers above fall into a few patterns, each with a recognizable symptom:
| Mistake | Symptom | The habit that avoids it |
|---|---|---|
| Dividing the inner product by one length, or by squared lengths | a “cosine” outside or one that changes when a vector is rescaled | divide by , each length once (q1 b) |
| Dividing by instead of in a projection | the projection scaled by | divide by (q2) |
| Reading singular values off the diagonal | wrong whenever is not diagonal with non-negative entries | go through the eigenvalues of (q4) |
| Reporting eigenvalues of as singular values | the square of the right answer | take the square root at the end (q4) |
| Listing singular values, or not sorting them | a padded or unordered spectrum; low_rank keeps the wrong directions | list values in descending order (q5) |
| Confusing the spectral and the Frobenius error, or summing values instead of squares | the wrong truncation rank chosen for a budget | use for the spectral error and the root of the sum of squares for the Frobenius error (q6) |
| Counting multiply-adds as FLOPs, or numbers as bytes | intensities off by a factor 2 or 4; the wrong side of the ridge | count 2 FLOPs per term and 4 bytes per fp32 number (q9 a, q10) |
| Forgetting the vector reads in a matrix-vector product | an intensity of exactly 1/2 instead of slightly less | count the bytes of , , and (q9 b) |
Where this leads
Section titled “Where this leads”Looking back, this set builds on the elimination, bases, eigenvalues, and QR of the linear algebra problem set, part a. q1 and q2 are cosine_sim and project from Inner products, projections, cosine similarity, and top-k done by hand, and q4 to q8 are svd, low_rank, and the eigenvalue bridge of SVD, Eckart-Young low rank, and least squares.
Looking forward, in AI Engineering: the PPMI-SVD embeddings of word2vec skip-gram with negative sampling, and the PPMI-SVD baseline keep the top singular directions, and its analogies rank by cosine. LoRA: low-rank adaptation with PiSSA initialization and merging trains LoRA’s parameters (q7) and initializes from PiSSA’s truncated SVD. In Cache-blocked, packed, batch-invariant matmul in C, intensity and the ridge point decide the tile size. And in The KV cache, incremental decoding, and generate, decoding is a matrix-vector product per token, and so memory-bound (q9 b).
Further reading
Section titled “Further reading”- 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).
Summary
Section titled “Summary”- 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).