AI / ML Theory
Chapter 6 · 18 min

6.1 — Attention as a soft, differentiable dictionary lookup

Goal: Build the attention operation from first principles, starting from a hard dictionary lookup and softening it through softmax until we reach attn(q, K, V) = Σᵢ softmax(s(q, kᵢ)) vᵢ.

1. Why this session exists

Chapter 5.6 ended with three structural requirements for the operator that would replace recurrence:

  1. Unbounded conditioning — no fixed-length window.
  2. Constant-length gradient paths — no exponential decay over distance.
  3. Parallel forward pass — every position computable in one batched matrix multiply.

Together with the fourth desideratum from 4.3 §5 — content-addressed mixing, where weights depend on the tokens themselves — these four properties almost uniquely specify the operation we are about to build.

This session derives the shape of attention from a single starting point: the dictionary lookup, a fundamental operation in computer science. Softening it into a differentiable function gives us attention. The specific similarity score and the batched matrix form come in 6.2.[2, 3, 4]


2. A hard dictionary lookup

A dictionary D={(k1,v1),(k2,v2),…,(km,vm)}\mathcal{D} = \{ (k_1, v_1), (k_2, v_2), \dots, (k_m, v_m) \} stores mm key-value pairs. A lookup with query qq returns the value viv_i whose key kik_i exactly matches qq:

lookup(q,D)=vi⋆,i⋆=arg⁡max⁡i1[ki=q].\text{lookup}(q, \mathcal{D}) = v_{i^\star}, \qquad i^\star = \arg\max_i \mathbb{1}[k_i = q].

If no key matches, the lookup returns nothing (or a default). This is what every hash map, Python dict, and database does.

For a neural network, hard lookup is useless for two reasons:

  1. It is discrete. arg⁡max⁡\arg\max has zero gradient almost everywhere. Backprop cannot pass through it.
  2. It assumes exact match. In a learned model, the query and keys are continuous vectors. We need graded matching — "this key is somewhat like this query" — not exact equality.

We need a continuous, gradient-friendly relaxation.


3. Softening the lookup

Replace "the single best matching key" with "a weighted average over all keys, weighted by how well each key matches the query."

First, replace exact equality by a continuous similarity score s(q,ki)∈Rs(q, k_i) \in \mathbb{R}: large when the query and key are close, small (or negative) when they disagree. The specific functional form of ss is a design choice — 6.2 will pick the scaled dot product. For now, ss is any function we like.

Next, turn the mm similarity scores s(q,k1),…,s(q,km)s(q, k_1), \dots, s(q, k_m) into a probability distribution over keys with softmax (see 1.7):

αi=exp⁡(s(q,ki))∑j=1mexp⁡(s(q,kj)),i=1,…,m.\alpha_i = \frac{\exp(s(q, k_i))}{\sum_{j=1}^{m} \exp(s(q, k_j))}, \qquad i = 1, \dots, m.

The weights satisfy αi≥0\alpha_i \ge 0 and ∑iαi=1\sum_i \alpha_i = 1. They are a soft assignment of the query to the keys: αi≈1\alpha_i \approx 1 if key ii stands out, αi≈1/m\alpha_i \approx 1/m if all keys look similar.

Finally, return the weighted average of values:

 attn(q, K, V)=∑i=1mαi vi,αi=exp⁡(s(q,ki))∑j=1mexp⁡(s(q,kj)). \boxed{\ \text{attn}(q,\, K,\, V) = \sum_{i=1}^{m} \alpha_i\, v_i, \qquad \alpha_i = \frac{\exp(s(q, k_i))}{\sum_{j=1}^{m} \exp(s(q, k_j))}. \ }

That is attention. The query qq produces a distribution over keys via softmax of similarities, and the output is the values, averaged under that distribution.


4. Reading the formula

Three useful ways to see what this does:

(a) Soft argmax. As ss scales up (multiply every similarity by a large constant), softmax sharpens. In the limit, αi→1[i=i⋆]\alpha_i \to \mathbb{1}[i = i^\star], where i⋆=arg⁡max⁡is(q,ki)i^\star = \arg\max_i s(q, k_i), and attention reduces to the hard lookup. Attention is the differentiable version of "pick the best key, return its value."

(b) Convex combination. attn(q,K,V)=∑iαivi\text{attn}(q, K, V) = \sum_i \alpha_i v_i with α∈Δm−1\alpha \in \Delta^{m-1} (the probability simplex). The output always lies in the convex hull of the values {v1,…,vm}\{v_1, \dots, v_m\}. It cannot extrapolate beyond what the values express — it can only mix them.

(c) Kernel smoothing. If s(q,k)=−∥q−k∥2/2σ2s(q, k) = -\|q - k\|^2 / 2 \sigma^2, the weights αi\alpha_i are exactly the kernel weights of Nadaraya–Watson regression (Nadaraya, 1964; Watson, 1964) with a Gaussian kernel. Attention is, structurally, kernel-smoothing regression where the kernel is parameterised by the score function ss and the data are the (ki,vi)(k_i, v_i) pairs. The connection is more than analogy: many recent papers re-derive attention from this nonparametric-regression view.


5. Why this satisfies our four desiderata

Look at the four requirements from §1 against the attention formula:

  1. Unbounded conditioning. The sum is over all mm keys; nothing is fixed-length. To make the input prefix x<tx_{<t} available at position tt, we will set keys and values from positions 11 through t−1t - 1, and the entire prefix participates in computing the output.
  2. Constant-length gradient paths. The output at any query position is a single weighted sum of all values. Backprop from the output to any viv_i traverses exactly one multiplicative step (the weight αi\alpha_i) and one softmax derivative. There is no exponential decay with mm.
  3. Parallel forward pass. When we batch the formula across nn queries simultaneously — say Q∈Rn×dk,K∈Rm×dk,V∈Rm×dvQ \in \mathbb{R}^{n \times d_k}, K \in \mathbb{R}^{m \times d_k}, V \in \mathbb{R}^{m \times d_v} — it becomes two matrix multiplies and one softmax (the derivation is in 6.2). One large GEMM, fully parallelisable on the GPU.
  4. Content-addressed mixing. The weights αi=softmax(s(q,ki))\alpha_i = \text{softmax}(s(q, k_i)) depend on what is in qq and kik_i, not on any fixed offset. This is the structural shift from convolution that 4.3 §4 anticipated.

All four properties come "for free" from the soft-lookup formulation. Attention is not designed to satisfy them one by one — it satisfies them simultaneously by being the right shape.


6. The roles: query, key, value

Three slots, each with its own job:

  • Query qq. The "what am I looking for?" vector. Produced by the position that is receiving information.
  • Key kik_i. The "what do I represent?" vector. Produced by the position that is being looked at. The query–key inner product is the similarity score.
  • Value viv_i. The "what should I contribute if selected?" vector. The output is a weighted sum of values.

In a hard dictionary, keys and values are tied: the key identifies a slot and the value is the slot's content. In attention, they are decoupled. The same source token will produce one kk vector for the matching role and a separate vv vector for the content role, via two different learnable linear projections (6.3). This separation is what gives the attention layer the flexibility to express different patterns at different layers — a token can be a strong "match target" without contributing strongly to the output, and vice versa.[1]


7. The cost of softmax

The softmax in attention is the only non-trivially non-linear operation in the layer (everything else is matrix multiplies). It has three structural consequences worth noting now and revisiting later:

(a) Quadratic memory. Computing αij\alpha_{ij} for all (i,j)(i, j) pairs of nn queries and mm keys requires the n×mn \times m matrix of similarity scores in memory. This is the O(T2)O(T^2) memory cost that all "efficient attention" variants in 6.13 (coming in Part 2) attack.

(b) Saturation risk. Softmax saturates: if one similarity score is much larger than the others, the corresponding αi→1\alpha_i \to 1 and the rest →0\to 0. Gradients with respect to the "losing" keys go to zero. The dk\sqrt{d_k} scaling in 6.2 is the design choice that keeps softmax in its non-saturated regime by default.

(c) Always nonzero. A perfect "hard match" (zero weight on all non-matching keys) is unattainable for finite scores. Attention always mixes in a little bit of every value. This is a feature for differentiability — gradients flow everywhere — and a cost for capacity, because no single value can be selected exactly.


8. What we keep going forward

The shape of attention is fixed:

attn(q, K, V)=∑i=1mexp⁡(s(q,ki))∑jexp⁡(s(q,kj)) vi.\text{attn}(q,\, K,\, V) = \sum_{i=1}^{m} \frac{\exp(s(q, k_i))}{\sum_j \exp(s(q, k_j))}\, v_i.

What remains is to specify three things:

  • The similarity function ss (6.2 — scaled dot product).
  • Where Q,K,VQ, K, V come from (6.3 — three learned linear projections of the same input, for self-attention).
  • The autoregressive / parallelism wiring (6.4 multi-head, 6.5 causal mask, 6.6 positional info, 6.7 RoPE) plus the per-position non-linearity (6.8 FFN).

By the end of Part 1 (session 6.8) you will have every piece of one Transformer block in matrix form. Part 2 stacks the blocks, adds normalisation and residuals, and walks through nanoGPT line by line.

Self-test

(a) Show that softmax with similarity scores s′=c⋅ss' = c \cdot s approaches a hard argmax as c→∞c \to \infty. What happens as c→0c \to 0? Use the two limits to argue that softmax interpolates between "uniform mixture" and "hard lookup."

(b) Suppose the query qq matches key k1k_1 exactly, s(q,k1)=5s(q, k_1) = 5, and all other keys score 00. Compute α1,α2,…,αm\alpha_1, \alpha_2, \dots, \alpha_m in closed form for m=4m = 4. How close is α1\alpha_1 to 11?

(c) Show that the attention output attn(q,K,V)=∑iαivi\text{attn}(q, K, V) = \sum_i \alpha_i v_i always lies in the convex hull of {v1,…,vm}\{v_1, \dots, v_m\}. What does this imply about the geometric reach of a single attention layer in the value space?

(d) Identify the four desiderata from §1 in the attention formula. For each one, point to the specific piece of the formula that delivers it.

(e) In a hard dictionary, the lookup of an unmatched query returns nothing. In soft attention, what is the output when no key strongly matches the query (all s(q,ki)s(q, k_i) approximately equal)? Interpret this behaviour geometrically.


References

  1. [1]

    Press, O., & Wolf, L. (2017). "Using the output embedding to improve language models". EACL. arXiv:1608.05859 — Weight tying between input embedding and output projection (5.2 §6.1).

    ↩
  2. [2]

    Prince, S. J. D. (2023). Understanding Deep Learning, §12 (Sequence-to-Sequence Models) and §13 (Transformers). MIT Press. — Attention as soft lookup (6.1), scaled dot-product attention (6.2), multi-head attention (6.4).

    ↩
  3. [3]

    Bahdanau, D., Cho, K., & Bengio, Y. (2014). "Neural machine translation by jointly learning to align and translate". arXiv:1409.0473 — First "neural attention" paper; additive attention as a precursor to scaled dot-product (6.1, 6.2).

    ↩
  4. [4]

    3Blue1Brown. "But what is a GPT? Visual intro to Transformers" and "Attention in transformers, step-by-step". 3blue1brown.com — Visual introduction to attention (6.1, 6.2).

    ↩