Review

Review the complete single-head self-attention layer, its tensor shapes, projection roles, mask rules, output interface, costs, interpretation limits, and a practical audit order.

The Complete Layer

For batch-major input

XRB×T×dmodel,X\in\mathbb R^{B\times T\times d_{model}},

one single-head self-attention layer without projection biases is

Q=XWQ,WQ:dmodel×dk,K=XWK,WK:dmodel×dk,V=XWV,WV:dmodel×dv,S=QKdk,S:B×T×T,A=softmaxlast(S+M),A:B×T×T,H=AV,H:B×T×dv,Y=HWO,WO:dv×dmodel.\begin{aligned} Q&=XW_Q, & W_Q&:d_{model}\times d_k,\\ K&=XW_K, & W_K&:d_{model}\times d_k,\\ V&=XW_V, & W_V&:d_{model}\times d_v,\\ S&=\frac{QK^\top}{\sqrt{d_k}}, & S&:B\times T\times T,\\ A&=\operatorname{softmax}_{last}(S+M), & A&:B\times T\times T,\\ H&=AV, & H&:B\times T\times d_v,\\ Y&=HW_O, & W_O&:d_v\times d_{model}. \end{aligned}

The transpose swaps the final two axes of KK within each batch item. The mask is applied before softmax. Softmax normalizes key-position columns within every query row. The output projection returns the feature axis to dmodeld_{model}.

Distinctions to Keep

Self-attention and cross-attention

In self-attention, Q, K, and V are projected from the same source sequence. In cross-attention, queries come from one sequence and keys and values from another. The mask is a separate choice: self-attention may be causal or bidirectional.

Score, weight, value, and contribution

SijS_{ij} is an unnormalized query-key match. AijA_{ij} is its row-normalized weight. vjv_j is the content vector at source position jj. AijvjA_{ij}v_j is that source's vector contribution to the reading at position ii. None of these quantities can be substituted for the others.

Causal and padding masks

A causal mask removes real future positions. A padding mask removes artificial positions added to make sequence lengths equal in a batch. They may be combined, but they express different rules.

An attention layer and a Transformer block

This chapter's layer ends at Y=HWOY=HW_O. It still lacks position information, multiple heads, a residual update, normalization, and a position-wise MLP. Those components change the architecture and cannot be inferred from the word attention.

What the Projection Ablations Establish

  • Changing WQW_Q changes what each position requests.
  • Changing WKW_K changes how each position can be matched.
  • Changing WVW_V changes routed content without directly changing attention weights.
  • Changing WOW_O changes how weighted value features are written into model width without changing the attention weights.
  • Setting WO=0W_O=0 makes this branch's output zero for the current forward pass; it does not by itself measure how an entire trained network would compensate.

These are controlled mathematical statements. Semantic labels for trained features require further evidence.

Benefit, Cost, and Limits

One self-attention layer permits a direct information path between allowed positions. Dense attention also creates T2T^2 query-key scores per sequence and head. Known training tokens can be processed together under a causal mask, but autoregressive generation still waits for each newly selected token.

An attention heat map records weights in one operation. It is not a complete causal explanation of a model output because values, projections, residual paths, later computation, and alternative weight patterns also matter.

Audit Order

When an implementation fails, inspect the first incorrect object in dependency order:

  1. input and projection shapes;
  2. Q, K, and V shapes and selected rows;
  3. score shape and scale;
  4. allowed and forbidden mask entries;
  5. finite softmax values and row sums;
  6. exact zeroes at forbidden positions;
  7. weighted value shape and selected rows;
  8. output-projection orientation and final width;
  9. independence between batch items.

A passing final shape is weak evidence. Many axis mistakes still return a finite tensor with the expected dimensions.

If a Step Is Unclear

Check Yourself

  • Can you derive every intermediate shape from B,T,dmodel,dk,dvB,T,d_{model},d_k,d_v?
  • Can you explain why Q and K widths must match while V may differ?
  • Can you identify the softmax axis from the meaning of one query row?
  • Can you explain the output projection without calling it merely a reshape?
  • Can you distinguish batch sharing of parameters from mixing batch data?
  • Can you state the direct-path benefit together with quadratic dense cost?
  • Can you separate an observed attention weight from a causal claim?

One head produces one learned routing-and-content computation. The next chapter asks what changes when several heads operate in parallel subspaces and their results must be joined without losing track of any axis.

Pause and reflect

What can you now explain without looking back, and what should you revisit? The note stays with this review.

Review

Not marked done.