Skip to main content

MATH 345: Linear Algebra and Optimization

Appendix A Transformer Wrap-Up Worksheet

This worksheet asks you to read a simplified transformer block using tools from the course: vectors, dot products, matrix multiplication, affine maps, null spaces, nonlinear functions, Jacobians, gradients, optimization, rank, and low-rank structure.
The goal is not to teach the full engineering of large language models. The goal is to recognize the mathematics and explain how information moves through a simplified model.
Big question. How do the tools from Units 1-6 help us read a transformer-style computation?
How to use this worksheet. Work through the activities in order. Each activity asks you to identify which course tool is being used, rather than to learn a new transformer topic from scratch.

Note A.0.1. Scope of This Worksheet.

This worksheet does not cover tokenization, real training datasets, hardware, backpropagation through attention, model evaluation, alignment, fine-tuning pipelines, or prompt engineering. Those topics belong to other courses. Here we focus on reading a simplified computation.

Note A.0.2. Linked notebook.

For a small computation attached to this worksheet, use Transformer Wrap-Up Lab: A Tiny Attention Block
 1 
sebroc.github.io/MATH345-Course-Materials/labs/#lab-transformer-wrap-up
. The notebook is for interpreting shapes, dot products, masks, weighted averages, logits, and one small loss; it is not a real model-training assignment.
For attention-block code involving row-wise softmax, masks, np.triu, .astype(bool), .copy(), np.outer, and plotting, see the programming quick-reference sections B.13 and B.14.
Worksheet goals. After this appendix, you should be able to:
  1. Read a token matrix \(X\) and check the shapes of \(Q=XW_Q\text{,}\) \(K=XW_K\text{,}\) \(V=XW_V\text{,}\) \(S=QK^T\text{,}\) and \(H=A_{\mathrm{att}}V\text{.}\)
  2. Explain attention as dot-product scores, attention weights, and weighted averages of value vectors.
  3. Distinguish linear, affine, nonlinear, and data-dependent operations inside a transformer-style block.
  4. Interpret an output layer \(\ell=Wh+b\) using null-space language.
  5. Read a training loss and gradient descent update as an optimization problem.
  6. Interpret a low-rank update \(W_{\mathrm{new}}=W+BC\) using rank and storage.
Notation card: one simplified attention head. Suppose a sequence has \(L\) tokens, each represented by a vector in \(\mathbb R^d\text{.}\) Store the token vectors as rows of
\begin{equation*} X\in\mathbb R^{L\times d}. \end{equation*}
Let
\begin{equation*} Q=XW_Q,\qquad K=XW_K,\qquad V=XW_V. \end{equation*}
The score matrix is
\begin{equation*} S=QK^T. \end{equation*}
A nonlinear weighting rule converts each row of \(S\) into attention weights. We write the resulting matrix as
\begin{equation*} A_{\mathrm{att}}. \end{equation*}
The attention output is
\begin{equation*} H=A_{\mathrm{att}}V. \end{equation*}
In transformer attention, the weighting rule is usually row-wise softmax, possibly after scaling and masking. Unit 3 treats row-wise softmax as a nonlinear function.
Flowchart of the main operations in a transformer block.
A vertical flowchart starts with \(X\) and proceeds downward through boxes for linear maps to \(Q\text{,}\) \(K\text{,}\) and \(V\text{,}\) dot-product scores, softmax or masking, weighted values, residual update, and a feed-forward layer. Red arrows connect the boxes in sequence. The final box indicates that the block outputs an updated \(X\text{.}\)
Figure A.0.3. A transformer block is a nonlinear information processor built from linear maps, dot products, weighted averages, and nonlinear operations.

Activity A.0.1. Transformer data card.

Suppose
\begin{equation*} X\in\mathbb R^{4\times 3},\qquad W_Q,W_K,W_V\in\mathbb R^{3\times 2}. \end{equation*}
Define
\begin{equation*} Q=XW_Q,\qquad K=XW_K,\qquad V=XW_V,\qquad S=QK^T. \end{equation*}
A row-wise weighting rule produces
\begin{equation*} A_{\mathrm{att}}\in\mathbb R^{4\times 4}, \end{equation*}
and the attention output is
\begin{equation*} H=A_{\mathrm{att}}V. \end{equation*}
  1. What are the shapes of \(Q,K,V,S,A_{\mathrm{att}},H\text{?}\)
  2. What does \(S_{ij}\) compare?
  3. What does row \(i\) of \(A_{\mathrm{att}}\) control?
  4. Which product forms weighted averages of value vectors?
  5. Which step is nonlinear?
  6. Which units explain these steps?
Tags. [U1-LO3, U1-LO6, U1-LO8, U3-LO7 | C+R | Optional]
Solution.
\(Q,K,V\) are \(4\times 2\text{.}\) The score matrix \(S=QK^T\) is \(4\times 4\text{.}\) The attention-weight matrix \(A_{\mathrm{att}}\) is \(4\times 4\text{,}\) so \(H=A_{\mathrm{att}}V\) is \(4\times 2\text{.}\) The entry \(S_{ij}\) compares the query for token \(i\) with the key for token \(j\text{.}\) Row \(i\) of \(A_{\mathrm{att}}\) gives the weights used to combine rows of \(V\text{.}\) The product \(A_{\mathrm{att}}V\) forms weighted averages. The weighting rule that turns scores into attention weights is nonlinear. Units 1, 2, 3, 5, and 6 supply the main tools.

Activity A.0.2. Linearity audit.

Classify each operation as linear, affine, nonlinear, or data-dependent in the relevant input.
  1. \(\displaystyle X\mapsto XW_Q\)
  2. \(\displaystyle x\mapsto W_1x+b_1\)
  3. \(x\mapsto \sigma(x)\text{,}\) where \(\sigma\) is applied coordinatewise
  4. \(\displaystyle S\mapsto \operatorname{softmax}_{\mathrm{rows}}(S)\)
  5. \(\displaystyle (A_{\mathrm{att}},V)\mapsto A_{\mathrm{att}}V\)
  6. \(\displaystyle (x,\Delta x)\mapsto x+\Delta x\)
  7. \(\displaystyle x\mapsto \operatorname{normalize}(x)\)
Tags. [U1-LO7, U3-LO5, U3-LO7 | C+R | Optional]
Solution.
1 is linear in \(X\text{.}\) 2 is affine in \(x\text{.}\) 3 is nonlinear. 4 is nonlinear. 5 is bilinear in the pair \((A_{\mathrm{att}},V)\text{,}\) and linear in either one if the other is held fixed. 6 is linear as a map of the pair \((x,\Delta x)\text{,}\) and represents vector addition. 7 is generally nonlinear. Thus a transformer-style block can contain many matrix multiplications without being a linear map.

Activity A.0.3. Attention or projection?

Both attention and orthogonal projection use dot products. Match each description with attention, projection, or both.
  1. Forms dot-product scores.
  2. Produces weights used in a weighted average.
  3. Finds the closest vector in a subspace.
  4. Has an orthogonal residual condition.
  5. May use a nonlinear normalization rule on scores.
  6. Is checked by equations such as \(A^Tr=0\text{.}\)
Tags. [U1-LO1, U4-LO4, U4-LO5 | C+R | Optional]
Solution.
1 can appear in both settings. 2 is attention-like. 3 is projection. 4 is projection. 5 is attention-like. 6 is projection. The main distinction is that attention uses scores to mix value vectors, while orthogonal projection solves a closest-vector problem with an orthogonal residual.

Activity A.0.4. Output layer and forgotten hidden directions.

A final hidden vector \(h\) is converted into scores by
\begin{equation*} \ell=Wh+b. \end{equation*}
Suppose \(Wz=0\text{.}\)
  1. Compare \(W(h+z)+b\) with \(Wh+b\text{.}\)
  2. What subspace contains \(z\text{?}\)
  3. What does this say about whether the scores determine the hidden vector uniquely?
  4. Does the bias vector \(b\) change which directions are forgotten?
Tags. [U2-LO3, U2-LO6, U1-LO7 | C+R | Optional]
Solution.
\begin{equation*} W(h+z)+b=Wh+Wz+b=Wh+b. \end{equation*}
The vector \(z\) is in \(\operatorname{null}(W)\text{.}\) Hidden vectors that differ by \(z\) give the same scores. The bias shifts the scores but does not change the condition \(Wz=0\text{.}\)

Activity A.0.5. Reading a training objective.

A simplified next-token training objective is
\begin{equation*} L(\theta)=-\sum_{i=1}^{L-1}\log p_\theta(t_{i+1}\mid t_1,\ldots,t_i). \end{equation*}
A gradient descent step has the form
\begin{equation*} \theta_{k+1}=\theta_k-\alpha\nabla L(\theta_k). \end{equation*}
  1. What are the parameters?
  2. What quantity is being minimized?
  3. Which symbol is the learning rate?
  4. What does \(\nabla L(\theta_k)\) describe locally?
  5. Why is this an optimization problem?
Tags. [U5-LO2, U5-LO3 | C+M+R | Optional]
Solution.
The parameters are collected in \(\theta\text{.}\) The loss \(L(\theta)\) is being minimized. The learning rate is \(\alpha\text{.}\) The gradient \(\nabla L(\theta_k)\) gives the local direction of steepest increase of the loss, so the negative gradient is used for descent. This is an optimization problem because training means adjusting parameters to reduce a loss.

Activity A.0.6. Reading a low-rank update.

A model matrix is updated by
\begin{equation*} W_{\mathrm{new}}=W+BC, \end{equation*}
where
\begin{equation*} W\in\mathbb R^{m\times n},\qquad B\in\mathbb R^{m\times r},\qquad C\in\mathbb R^{r\times n},\qquad r\ll \min(m,n). \end{equation*}
  1. What is the shape of \(BC\text{?}\)
  2. Why is \(\operatorname{rank}(BC)\le r\text{?}\)
  3. How many entries are stored in \(B\) and \(C\text{,}\) compared with \(W\text{?}\)
  4. Does this imply that \(W_{\mathrm{new}}\) has low rank?
Tags. [U2-LO3, U6-LO7 | C+R | Optional]
Solution.
The product \(BC\) has shape \(m\times n\text{,}\) matching \(W\text{.}\) Since \(BC\) factors through an \(r\)-dimensional middle space, its rank is at most \(r\text{.}\) The two factors store \(mr+rn=r(m+n)\) entries, compared with \(mn\) entries for \(W\text{.}\) This does not imply that \(W_{\mathrm{new}}\) has low rank. The update \(BC\) has low rank; the whole matrix \(W+BC\) may still have large rank.

Activity A.0.7. Course concept map.

Complete the table.
Course idea Where it appears in the worksheet Course source
Vectors ____ ____
Dot products ____ ____
Matrix multiplication ____ ____
Affine maps ____ ____
Null space ____ ____
Nonlinear functions ____ ____
Jacobians ____ ____
Gradients ____ ____
Rank and SVD ____ ____
Tags. [U1-LO8, U2-LO3, U3-LO7, U4-LO4, U5-LO2, U6-LO7 | C+R | Optional]
Solution.

Note A.0.4. Wrap-up highlights.

Big question. How do course tools help us read a transformer-style computation?
Notation. \(X\text{,}\) \(Q=XW_Q\text{,}\) \(K=XW_K\text{,}\) \(V=XW_V\text{,}\) \(S=QK^T\text{,}\) \(A_{\mathrm{att}}\text{,}\) \(H=A_{\mathrm{att}}V\text{,}\) \(\ell=Wh+b\text{,}\) \(L(\theta)\text{,}\) \(W_{\mathrm{new}}=W+BC\text{.}\)
Main idea. A transformer-style block is not one linear map. It is a nonlinear function built from linear maps, dot products, weighted averages, affine maps, nonlinear weighting rules, residual additions, normalization, and optimization.
Course synthesis. Unit 1 explains vectors, dot products, matrix products, weighted averages, and affine maps. Unit 2 explains forgotten directions. Unit 3 explains nonlinear functions and local linearization. Unit 4 separates attention from projection. Unit 5 explains loss and gradient descent. Unit 6 explains rank and low-rank updates.
The worksheet is successful if you can read the formulas and explain which course tool is being used at each step.