10.2 Bahdanau additive attention
The encoder-decoder of the previous section, without attention, squeezes the whole source sentence into one vector, the decoder’s first state. On short inputs that works. On a 30-character date string the decoder has forgotten the start of the input by the time it writes the end, and exact match drops sharply with length. Bahdanau’s fix, the first attention mechanism in neural translation, lets every decoder step look back at all encoder outputs and choose which to read. It is the idea the 2017 transformer’s scaled dot-product attention keeps while dropping the recurrence, so this is where the core operation of every model in the rest of the book is born.
Concepts from other sections (9)
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.
- exact match: the fraction of hypotheses equal to their reference after stripping white space at both ends. Read more: 10.5 Sequence metrics: exact match, BLEU, chrF
- vector-Jacobian product (VJP): the function mapping an output gradient to input gradients without forming the Jacobian. Read more: 6.2 Tensor, broadcasting backward, and no_grad
- Gradcheck: a comparison of a hand-written gradient against central differences of . Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- central differences: the numerical derivative , used as an oracle for analytic gradients. Read more: 6.3 The op library and gradcheck_all
- fully masked row: a row whose entries are all ; by contract its softmax is all zeros. Read more: 22.4 Softmax in C: three-pass and online two-pass
- safetensors: a checkpoint format of an 8-byte header length, a JSON header, and raw little-endian row-major tensor bytes. Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- additive mask: the float form, 0 where allowed and where blocked, added to scores before the softmax. Read more: 11.2 Masks: causal, padding, sliding window, additive
- KV cache: per-layer storage of past keys and values, so each decode step processes only the new token. Read more: 21.2 The KV cache, incremental decoding, and generate
- paged KV cache: a cache stored as fixed-size blocks anywhere in a pool, with a per-sequence table mapping logical blocks to physical ones. Read more: 22.6 Paged attention for decode in C
The section builds on Linear and Module from the module system, the tanh, masked_fill, softmax, and matmul ops of the op library, the Tensor of the tensor section, and PCG32 from Modular arithmetic, hashing, and PCG32 in Mathematics for the default initialization. Two more sections of that book are useful reading: Stable numerics defines the masked softmax used here, and the matrix calculus problem set works its vector-Jacobian product by hand.
The solution is python/tinyllm/seq2seq/additive.py, with length_mask and AdditiveAttention (project_keys, scores, forward). It implements the contract course/contracts/py/tinyllm/seq2seq/additive.pyi, and the oracle tests are in test_additive.py. Plugged into the encoder-decoder, a Bahdanau seq2seq reaches an exact match of at least 0.97 on the dates task.
Learning objectives
- Write the additive score and explain why it must be nonlinear in the pair .
- Mask padding on the scores, to , and show that padded weights are exactly 0 and padding content never matters.
- Handle an empty source without producing NaN.
- Precompute the key projection once per sentence and say what it saves per step.
Notation
Section titled “Notation”The symbols and shapes used throughout:
| Symbol | Meaning | Type / shape |
|---|---|---|
| , | batch size, source length (padded) | int |
| the query: the decoder state that asks | float32[B, Dq] | |
| the key at source position : an encoder output, also the value read | float32[B, S, Dk] | |
| the real length of source | int | |
| mask: true when | bool[B, S] | |
| , , , | parameters query.weight , key.weight , key.bias , v.weight | float32 |
d_attn, the width of the scoring network | int | |
| the score of position | float | |
| the attention weight of position | float in | |
| the context: | float32[B, Dk] |
Reading by weighted average
Section titled “Reading by weighted average”A decoder that must write the next target word needs some of the source, not all of it equally. A weighted average of the encoder outputs, with weights that are nonnegative and sum to 1, reads exactly that. A weight near 1 on one position copies that position; spread weights blend several. The read is a convex combination, so the context is a point “between” the encoder outputs. The weights must depend on what the decoder is doing now, so they are computed from the decoder state , and a softmax over per-position scores is the standard map from real scores to such weights.
Scores, weights, context
Section titled “Scores, weights, context”What should the score be? It must depend on both the query and the key, and nonlinearly. A linear score would rank positions the same way for every query, because the term is constant across , so the decoder could never change where it looks. Adding and inside a and reading out with is the smallest network that couples them. Bahdanau scores each position with exactly this one-hidden-layer network:
The softmax turns arbitrary real scores into positive weights that sum to 1. Every operation is an op-library call, so backward reaches (the decoder learns where to look), the keys (the encoder learns what to offer), and , with no hand-written backward.
Notice two things about the formula. The context has the keys’ width , not : the scoring network decides how much of each key to take, and the keys themselves are what is read. And nothing in the formula looks at the position itself. Permute the keys and the weights permute with them, so attention by itself ignores order; order has to come from the encoder.
Masking padding
Section titled “Masking padding”A batch pads every source to the longest length , and padded positions must never be read. The only way to give a position weight 0 while keeping the rest summing to 1 is to remove it before normalizing: set its score to . Since , its weight is exactly 0 and the real weights still sum to 1. length_mask(lengths, S) builds .
Masking after the softmax, by multiplying the weights by the mask, gets this wrong: the real weights then sum to less than 1, and padding still influences the normalization.
An empty source raises one more question. A row with no real position is all , and a naive softmax computes $0/0 = $ NaN. The masked softmax of Stable numerics defines an all-masked row as zeros, so the context is 0 and no gradient flows back. That is the right meaning of “nothing to read”.
Precomputing the keys
Section titled “Precomputing the keys”The score splits by what changes when. involves only the keys, which are fixed for the whole sentence, while changes at every decoder step. Computing once (project_keys) and passing it to every step as proj saves multiply-adds per step; Bahdanau’s appendix points this out. The result must be identical to recomputing.
The same split decides where the biases go. carries the bias ; and have none. A bias in would duplicate , and a bias in would shift every score equally and cancel in the softmax.
Working it by hand
Section titled “Working it by hand”Make every size 1 (), with and . Take the query , keys , and let the third position be padding (mask [True, True, False]).
- Scores before the mask: , , . The padded one would be the largest.
- After the mask: .
- Softmax: , , ; the sum is , so .
- Context: .
The padded key, the biggest value in the row, contributes nothing. A correct solution reproduces these numbers to float32 tolerance.
Designing the API
Section titled “Designing the API”def length_mask(lengths: ArrayLike, S: int) -> NDArray: ... # bool [B, S]class AdditiveAttention(Module): query_from = "previous" # read by the seq2seq decoder def __init__(self, d_query: int, d_key: int, d_attn: int, rng=None) -> None: ... def project_keys(self, keys: Tensor) -> Tensor: ... # [B, S, A] def scores(self, query: Tensor, keys: Tensor, proj=None) -> Tensor: ... # [B, S], unmasked def forward(self, query, keys, mask, proj=None) -> tuple[Tensor, Tensor]: ... # context, weightsquery_from = "previous" tells the encoder-decoder to read with , before the cell. scores returns unmasked scores; forward applies the mask and returns both the context and the weights, so the decoder can draw alignments.
Building it step by step
Section titled “Building it step by step”The mask
Section titled “The mask”The mask is one broadcast comparison:
def length_mask(lengths, S): n = np.asarray(lengths) # ... (1-D integers in [0, S]; 0 is allowed: an empty source) return np.arange(S)[None, :] < n[:, None] # [B, S], True where s < l_bLengths of 0 are allowed here, unlike in the encoder, because attention has a well-defined answer for an empty source. The comparison is strict: <= would read one padding position per row.
The parameters
Section titled “The parameters”The parameters follow the split above: only key has a bias.
class AdditiveAttention(Module): query_from = "previous" # the decoder reads with s_{t-1}, before the cell
def __init__(self, d_query, d_key, d_attn, rng=None): # ... (positive sizes; default rng PCG32(0).substream("init")) self.query = Linear(d_query, d_attn, bias=False, rng=r) # W self.key = Linear(d_key, d_attn, rng=r) # U, b self.v = Linear(d_attn, 1, bias=False, rng=r) # v
def project_keys(self, keys): # ... (keys must be [B, S, d_key]) return self.key(keys) # U k + b, the step-independent part; no tanh yetproject_keys stops before the on purpose. The query term must be added inside the nonlinearity; caching would compute , a different function.
The scores
Section titled “The scores”The scores broadcast one per batch row over all positions:
def scores(self, query, keys, proj=None): # ... (query must be [B, d_query]) B, S = keys.shape[0], keys.shape[1] if proj is None: proj = self.project_keys(keys) elif proj.shape != (B, S, self.d_attn): raise ValueError(...) # W q is one vector per batch row, added to every source position. wq = F.reshape(self.query(query), (B, 1, self.d_attn)) e = self.v(F.tanh(proj + wq)) # [B, S, 1] return F.reshape(e, (B, S)) # unmasked scoresThe read
Section titled “The read”Finally, mask, softmax, and the weighted sum as a batched matmul:
def forward(self, query, keys, mask, proj=None): # ... (shape checks) B, S = keys.shape[0], keys.shape[1] m = _check_mask(mask, B, S) # bool [B, S] e = F.masked_fill(self.scores(query, keys, proj), ~m, -np.inf) # Mask BEFORE the softmax: a masked score of -inf has weight exactly 0 # and the rest still sum to 1. a = F.softmax(e, axis=-1) ctx = F.matmul(F.reshape(a, (B, 1, S)), keys) # [B, 1, S] @ [B, S, Dk] = [B, 1, Dk] return F.reshape(ctx, (B, self.d_key)), aBecause masked_fill writes a constant, padded keys receive no gradient through the scores, and because their weight is exactly 0 they receive none through the context either: garbage in padding cannot change anything. The context is computed from the live keys Tensor, so the encoder gets its gradient through attention. The full solution is additive.py.
Why this design
Section titled “Why this design”Per decoder step, with proj precomputed, the cost is for , for the and , and for the context. Without precomputation each step would add , the dominant term for long sources.
Why not dot-product scores ? They need (or one projection) and no hidden layer, so they are cheaper; Luong attention and the transformer use them. Additive scores were the first to work and handle mismatched widths naturally; the price is the extra -wide layer per position.
Why and not a large negative number? works when at least one position is real, but on an empty row it gives uniform weights over padding instead of zeros. And why mask the scores rather than the weights? Multiplying weights by the mask after the softmax leaves the real weights summing to less than 1 and lets padded scores shape the normalizer.
Testing it
Section titled “Testing it”The oracle in test_additive.py is the formula itself, written out in numpy with the module’s own weights read from state_dict(). It computes , the mask, the softmax, and the context in float64 and compares them with forward. It adds the hand-worked numbers, a finite-difference check of the gradients of the query and the keys in float64, the masking properties, and the parameter names, and it imports only the contract modules.
Taken together, the checks a correct solution must pass are these:
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| hand example weights and context | unit | the hand-worked numbers to float32 tolerance | pins down the formula |
| length mask | unit | s < lengths[b], bad lengths raise | the only place lengths enter attention |
| matches torch | golden | context, weights, and gradients of the query, keys, and all four parameters against torch, lengths 5, 3, 1 | the encoder and decoder train through this |
| gradcheck of every input and parameter | gradcheck | float64 central differences with a padded row | both sides of the model learn |
| masked weights are exactly zero and rows sum to one | property | exact zeros, positive real weights, rows sum to 1 | padding is never read |
| padding content never matters | property | garbage in padded keys changes nothing bitwise; their gradient is 0 | a batch padded differently translates the same |
| fully masked row reads nothing | boundary | zeros, no NaN, no gradient | empty inputs in a batch |
| precomputed keys equal recomputed | property | proj passed or recomputed, bitwise equal | the decoder passes it at every step |
| permuting the source permutes the weights | property | weights permute, context unchanged | order comes from the encoder, not from attention |
| parameter names and shapes | unit | keys, shapes, order, query_from, seeded init | the safetensors keys of seq2seq checkpoints |
| validation | boundary | wrong widths, missing source axis, wrong or float mask, wrong proj | wiring bugs fail loudly |
What can go wrong
Section titled “What can go wrong”Most attention bugs still produce weights that look plausible, which is why the tests check exact properties. The common ones:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
| masking after the softmax | real weights no longer sum to 1; padding still shapes the normalization | F.masked_fill(..., ~m, -np.inf) on the scores, then F.softmax |
| masking with instead of | an empty source gets uniform weights and reads padding | fills with -np.inf; the masked softmax returns zeros for an all- row |
| softmax over the wrong axis | weights sum to 1 over the batch, not the source | F.softmax(e, axis=-1): the source axis of [B, S] |
| reading detached keys | the encoder gets no gradient through attention | the context is F.matmul(a, keys) on the live keys Tensor |
| mask polarity inverted | only padding is read | fills where ~m, the padding |
no tanh | a linear score: the golden values disagree | self.v(F.tanh(proj + wq)) |
<= in length_mask | one padding position is read per row | np.arange(S)[None, :] < n[:, None], strict |
| caching instead of | the query is added after the nonlinearity | project_keys returns self.key(keys), before any tanh |
Warning: A large negative fill such as passes every test with at least one real position and silently reads padding on an empty source. Use and a softmax that defines the all-masked row.
Where this leads
Section titled “Where this leads”Luong attention keeps the same masked read and builds its masks with length_mask. The encoder-decoder calls attention(s_{t-1}, keys, mask, proj) before each step. In the next part, scaled dot-product attention is this read with a cheaper score, and the model zoo includes the attention variants of the seq2seq rows.
Production systems generalize each piece:
| This piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
AdditiveAttention | torch.nn.MultiheadAttention, F.scaled_dot_product_attention | dot-product scores (no hidden layer), many heads, fused kernels | torch/nn/functional.py (multi_head_attention_forward) |
| masking to | attn_mask and key_padding_mask in torch | boolean and additive masks, broadcast over heads | PyTorch SDPA documentation |
project_keys | the KV cache | keys and values projected once and reused at every step of generation | KV cache and generate; vLLM’s paged KV cache |
| additive scores | location-sensitive attention (Tacotron 2) | adds the previous weights as a feature so speech alignment moves forward | Chorowski et al. 2015 |
Further reading
Section titled “Further reading”- Bahdanau, Cho, and Bengio, “Neural Machine Translation by Jointly Learning to Align and Translate” (ICLR 2015), section 3.1 and appendix A.1.2.
- Graves, “Generating Sequences With Recurrent Neural Networks” (2013), section 5.
- Chorowski et al., “Attention-Based Models for Speech Recognition” (2015).
Key terms
Section titled “Key terms”- attention: a weighted average of encoder outputs whose weights the decoder computes from its state.
- query: the decoder state that asks where to look.
- key: an encoder output, scored against the query and also read as the value.
- additive score: , a one-hidden-layer network on the pair .
- context: the weighted sum that the decoder reads.
- length mask: the boolean array marking real source positions.
Summary
Section titled “Summary”- Attention is a weighted average of the encoder outputs whose weights the decoder computes: a score per source position, a softmax, a sum.
- Padding is masked on the scores, to , before the softmax: padded weights are exactly 0, the rest sum to 1, and padding content never matters.
- An empty source reads nothing: weights and context 0, not NaN.
- The key projection does not depend on the decoder step, so it is computed once per sentence.
- Attention by itself ignores order: permute the source and the weights permute with it.
Review questions
Section titled “Review questions”- Why does
queryhave no bias whilekeydoes?Answer
Both are added inside the same $\tanh$, so one bias is enough. Putting it on the key side makes it part of the precomputed `proj`, so it costs nothing per step. - Why is
project_keysand not ?Answer
The query term must be added *inside* the nonlinearity: $\tanh(W q + U k + b)$. Caching the $\tanh$ would compute $\tanh(U k + b) + W q$, a different function. - In the worked example the padded key had the highest raw score. Why does it get weight exactly 0?
Answer
Its score is replaced by $-\infty$ before the softmax and $e^{-\infty} = 0$. The remaining two weights are normalized among themselves: $1/3.1417$ and $2.1417/3.1417$. - What do
forward’s outputs look like for a row with length 0, and why is that correct?Answer
Weights and context are all zeros, with no NaN and no gradient. There is nothing to read, and the masked softmax defines an all-$-\infty$ row as zeros instead of $0/0$. - Why does permuting the source permute the weights but leave the context unchanged?
Answer
Each score depends only on $(q, k_s)$, not on $s$, so permuting keys permutes scores and weights with them. The context is a sum over positions, and sums do not depend on order. - Why would a linear score be useless?
Answer
The $w^\top q$ term is the same for every position $s$, so it cancels in the softmax and the ranking of positions never depends on the query. The decoder could not change where it looks.