13.5 Grouped-Query Attention with a Cache Hook, Windows, and Sinks
The multi-head attention of Multi-head attention gives every query head its own key and value head. At inference the engine caches keys and values for every past token (you will build that cache in KV cache and generate, and the Rust engine in Serving keeps one too), so cache memory, not compute, limits how many requests run at once.
Concepts from other sections (18)
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.
- 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
- Padding mask: a per-step boolean that freezes a finished sequence’s state and zeros its output. Read more: 9.2 LSTM (torch gate order)
- RoPE: rotating pair of a query or key at position by , so scores depend only on relative position. Read more: 13.3 Rotary Position Embeddings
- Attention scaling: a factor applied to both tables, multiplying every score by . Read more: 13.3 Rotary Position Embeddings
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3 in Mathematics
- Variance: the expected squared distance from the mean; its square root is the standard deviation. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller in Mathematics
- Attention sink: an early token that a trained model parks unwanted attention on; StreamingLLM keeps the first tokens visible forever. Read more: 13.7 Sliding Windows and Attention Sinks
- op library (
F): the module of differentiable functions, each a NumPy forward plus one closed-form VJP joined byfrom_op. Read more: 6.3 The op library and gradcheck_all - logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- YaRN: a per-pair ramp between keeping and dividing frequencies, by turns over the trained context, plus a temperature . Read more: 13.4 Context Extension: PI, NTK, YaRN, Llama 3 Scaling, and ALiBi
- 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
- 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
- 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
- causal mask: the rule ; each position reads itself and the past. Read more: 11.2 Masks: causal, padding, sliding window, additive
- Model card: the model’s interface documentation: details, intended and out-of-scope use, evaluation with uncertainty, limitations, and data. Read more: 35.1 Datasheet and model card
- FlashAttention: attention computed tile by tile with an online softmax, so the score matrix is never stored. Read more: 22.5 FlashAttention forward in C
- PagedAttention: KV-cache management in fixed-size blocks mapped through a block table, like virtual memory. Read more: Chapter 20: Inference Engines and GPU Deployment
- block table: a request’s list of the physical KV blocks holding its tokens, used by attention to read the paged cache. Read more: 20.6 Inside an Inference Engine: How vLLM and SGLang Run a Model
Open SmolLM2-135M’s checkpoint and you will see the response to that pressure: k_proj.weight is , a third of q_proj’s . The model has 9 query heads but only 3 key/value heads, and plain multi-head attention cannot load it. Newer models add two more things to the same layer: a sliding window (Mistral, gpt-oss) so the cache stops growing, and learned sinks (gpt-oss) so a head can decline to attend. This section builds that attention layer, together with the hook through which the KV cache will later plug in.
The layer builds on Rotary Position Embeddings (rope_cos_sin, apply_rope, RopeSpec), Linear and Module from Module system and basic layers, F.matmul, F.masked_fill, F.softmax, and F.concat from The op library and gradcheck_all, and the Tensor of Tensor, broadcasting backward, and no_grad. From Mathematics it uses PCG32 from Modular arithmetic, hashing, and PCG32 in Python for default initialization and kv_bytes_per_token from Counting: parameters, FLOPs, KV bytes, and memory plans for what the cache must hold; Stable numerics covers the masked softmax, and Scaled dot-product attention is the attention it extends. The reference solution is gqa.py, with GQAttention, repeat_kv, _visible, the KVCacheHook protocol, and its smallest implementation ConcatKVCache, against the contract course/contracts/py/tinyllm/modern/gqa.pyi.
Learning objectives
- Explain how query heads share key/value heads and why the grouping must repeat each kv head in a row.
- State the visibility rule for a query in a cached call, including a sliding window and a padding mask.
- Place the cache hook so the cache stores only rotated heads, and explain both constraints.
- Implement learned sinks as an extra softmax column and explain why adding a constant to every score does nothing.
- Work a two-head, one-kv-head layer by hand.
Notation
Section titled “Notation”| Symbol | Meaning | Type / shape |
|---|---|---|
| the layer input | float32[B, T, d] | |
| , | query heads, key/value heads | int, |
| , query heads per kv head | int | |
head width (d_head; not necessarily ) | int | |
| queries after RoPE | float32[B, H, T, d_h] | |
| , | keys (after RoPE) and values | float32[B, H_{kv}, T_k, d_h] |
| , | new tokens in this call, keys visible (cached plus new) | int |
window | int or none | |
learned sink logit of head (sinks) | float32[H] | |
| attention weight of query on key | float |
Attention with rotated heads
Section titled “Attention with rotated heads”Start from what you know. Each head computes scaled dot-product scores , a softmax over the visible keys, and a weighted average of the values; the heads are concatenated and projected back to by o_proj. RoPE rotates and (with RopeSpec’s frequencies, layout, rotary width, and attention scaling) before the scores, so a score depends on the relative position of query and key only. The scale is , the head width, because a dot product of unit-variance products has variance .
Sharing kv heads
Section titled “Sharing kv heads”Multi-query attention (Shazeer 2019) keeps one key/value head for all query heads (). Grouped-query attention (Ainslie et al.) keeps of them, each serving a contiguous group of query heads.
repeat_kv expands to with output head reading input head : , the order the checkpoints were trained with. Tiling, , loads without error and is wrong. Seen another way, GQA is MHA whose kv heads come in identical groups, so an MHA layer with repeated kv weights gives the same output. Because indexing accumulates gradients, a kv head’s gradient is the sum over its copies.
Who may see whom
Section titled “Who may see whom”Once a cache is involved, positions within a call and positions in the sequence differ, so the visibility rule has to be stated carefully. Query of this call sits at key index (with no cache, and ). Key is visible when all of these hold:
- : causal, always;
- when a window is set: the query and the keys before it;
- the optional
maskallows it (Truemeans may attend, broadcast to ; this is how padding is expressed).
Hidden scores are set to , so their weights are exactly 0, and a row with nothing visible gives zeros, not NaN.
The cache hook
Section titled “The cache hook”Decoding one token at a time, recomputing every past key and value is wasted work. With cache given, after RoPE the layer calls cache.update(layer, k, v) with this call’s keys and values (numpy arrays, after rotation, before repetition) and attends over everything the cache returns.
Two consequences follow from where that call sits. First, the cache stores heads, which is where GQA’s saving happens: numbers per token per layer, exactly kv_bytes_per_token. Second, keys must be rotated with their own positions before they are stored, because a key cached at position 5 must stay rotated by 5. ConcatKVCache is the smallest implementation (it concatenates along time); the KVCache of KV cache and generate and the paged cache of A paged KV cache in Python implement the same update.
Learned sinks
Section titled “Learned sinks”A softmax must put weight 1 somewhere, so a head with nothing useful to read dumps it on some token, often the first; StreamingLLM calls this an “attention sink”. gpt-oss gives each head a learned logit appended to every row before the softmax and dropped afterwards:
The weights now sum to . A large means the head attends to almost nothing, and a very negative one recovers plain attention. Adding to every score instead does nothing at all, because softmax ignores a constant shift.
As for the backward, every step is an op from the op library or from the RoPE section, so autograd reaches , the four projections, their biases, and the sinks. With a cache, the cached keys and values are constants: the cache is for inference, and no gradient flows into it.
Working it by hand
Section titled “Working it by hand”Take , , , , with RoPE off (inv_freq = [0], so every angle is 0). Choose weights so that q_proj makes head 0’s query and head 1’s ; ; ; and o_proj keeps entry 0 of head 0 and entry 1 of head 1. The tokens are and . Both heads read the single kv head: , , , .
- Token 0 sees only key 0, so both heads output and the output is .
- For token 1, head 0 has and scores . Since , the weights are , and the head output is .
- For token 1, head 1 has , scores , weights , and head output .
o_projkeeps entry 0 of head 0 and entry 1 of head 1: .
One kv head served two query heads that attend in opposite directions.
The interface
Section titled “The interface”class KVCacheHook(Protocol): def update(self, layer: int, k_new: NDArray, v_new: NDArray) -> tuple[NDArray, NDArray]: ...class ConcatKVCache: # update(...), seq_len(layer=0)def repeat_kv(x: Tensor, n_rep: int) -> Tensor: ... # [B, Hkv, T, dh] -> [B, Hkv n_rep, T, dh]class GQAttention(Module): def __init__(self, d, n_heads, n_kv_heads, d_head, rope: RopeSpec, qkv_bias=False, window=None, sinks=False, rng=None): ... def forward(self, x, positions, mask=None, cache=None, layer=0) -> Tensor: ... # [B, T, d] -> [B, T, d]GQAttention.forward takes positions of shape [T] (shared) or [B, T] (per row), an optional boolean mask, and an optional cache with a layer index. repeat_kv and the cache hook are public because later chapters reuse them.
Building it step by step
Section titled “Building it step by step”The checkpoint fixes the parameter shapes: q_proj is , k_proj and v_proj are , and o_proj is with no bias. is a separate argument because some models set it independently of .
Attention needs one key per query head. The cheapest correct way to get there is to compute keys and values and index them, so query head reads kv head . Indexing gives the backward for free: copies of a kv head sum their gradients.
The cache must see the smallest thing that can be stored: keys after RoPE (so each keeps its own position) and before repeat_kv (so it holds heads). That fixes where cache.update sits in the forward, after apply_rope and before repeat_kv.
With a cache, this call’s queries are the last of keys. Every visibility rule (causal, window) is a comparison between absolute key indices and , so one boolean [T, Tk] matrix captures them all, and the user mask is ANDed in. Hidden scores become before the softmax, never after; then their weights are exactly zero and the rows still sum to one over what is visible. F.softmax returns zeros for an all- row.
A learned sink is one extra column of logits. Concatenate it, softmax over entries, then slice off the sink, and the remaining weights sum to . Because the sink column is built from the sinks tensor, it trains. Finally, RoPE’s attention scaling is applied inside rope_cos_sin to both and , so the layer needs no special case for YaRN: the arrives in the scores automatically.
The cache hook and its smallest implementation
Section titled “The cache hook and its smallest implementation”class KVCacheHook(Protocol): """The seam L8.2's KVCache (and L8.3's paged cache) implements.""" def update(self, layer, k_new, v_new) -> tuple[NDArray, NDArray]: ...
class ConcatKVCache: def update(self, layer, k_new, v_new): # copy: the caller's arrays may be reused or mutated after this call k_new, v_new = np.array(k_new, copy=True), np.array(v_new, copy=True) if k_new.ndim != 4 or k_new.shape != v_new.shape: raise ValueError(...) if layer in self._k: old = self._k[layer] # a chunk must extend the same batch, heads and head width; only time grows if old.shape[:2] != k_new.shape[:2] or old.shape[3] != k_new.shape[3]: raise ValueError(...) self._k[layer] = np.concatenate([old, k_new], axis=2) self._v[layer] = np.concatenate([self._v[layer], v_new], axis=2) else: self._k[layer], self._v[layer] = k_new, v_new # return everything cached so far: the layer attends over all of it return self._k[layer], self._v[layer]A Protocol rather than a base class keeps the attention layer ignorant of how a cache stores things: the KV cache of the inference chapter preallocates, the paged cache pages, and both just implement update. The cache works on numpy arrays because it is an inference-only object; no gradient flows into cached keys.
Repeating kv heads
Section titled “Repeating kv heads”def repeat_kv(x, n_rep): if n_rep < 1 or x.ndim != 4: raise ValueError(...) if n_rep == 1: return x # MHA: nothing to do # head h reads kv head h // n_rep: [0, 0, 1, 1, ...], not [0, 1, 0, 1, ...] idx = np.repeat(np.arange(x.shape[1]), n_rep) return x[:, idx] # fancy indexing: gradients of copies add upnp.repeat (each element times in a row) and not np.tile (the whole array times) is the entire difference between the checkpoint’s grouping and a silently wrong model.
Visibility
Section titled “Visibility”def _visible(T, Tk, window): """bool [T, Tk]: query t (key index Tk - T + t) may read key j.""" i = np.arange(T)[:, None] + (Tk - T) # absolute index of each new query j = np.arange(Tk)[None, :] vis = j <= i # causal if window is not None: vis &= (i - j) < window # the query and the W - 1 keys before it return visWith no cache and this is the ordinary lower triangle. In one-token decoding and the single row is “all keys” (or the last ).
The constructor
Section titled “The constructor” if min(d, n_heads, n_kv_heads) < 1 or n_heads % n_kv_heads: raise ValueError(...) # groups must divide evenly if d_head is None: if d % n_heads: raise ValueError(...) d_head = d // n_heads # ... validate d_head, rope.rotary_dim (even, 2..d_head) and window >= 1 r = rng if rng is not None else PCG32(0).substream("init") # ... # draw order q, k, v, o: fixed, so a seeded init is reproducible self.q_proj = Linear(d, n_heads * d_head, bias=qkv_bias, rng=r) self.k_proj = Linear(d, n_kv_heads * d_head, bias=qkv_bias, rng=r) self.v_proj = Linear(d, n_kv_heads * d_head, bias=qkv_bias, rng=r) self.o_proj = Linear(n_heads * d_head, d, bias=False, rng=r) # never a bias, even for Qwen2 self.sinks = Tensor(np.zeros(n_heads, dtype=np.float32), requires_grad=True) if sinks else NoneThe attribute names q_proj, k_proj, v_proj, o_proj, and sinks are the Hugging Face state-dict keys, so the Llama loader reads safetensors by name with no renaming table.
The forward
Section titled “The forward” def forward(self, x, positions, mask=None, cache=None, layer=0): # ... validate x is [B, T, d] B, T, _ = x.shape H, Hkv, dh = self.n_heads, self.n_kv_heads, self.d_head pos = np.asarray(positions) if pos.shape not in ((T,), (B, T)): # shared or per-row positions raise ValueError(...) q = self._heads(self.q_proj(x), B, T, H) # [B, H, T, dh] k = self._heads(self.k_proj(x), B, T, Hkv) # [B, Hkv, T, dh] v = self._heads(self.v_proj(x), B, T, Hkv) cos, sin = rope_cos_sin(pos, self.rope.inv_freq, self.rope.attention_scaling) if pos.ndim == 2: # per-row positions: [B, 1, T, r/2] broadcasts over heads cos, sin = cos[:, None], sin[:, None] q = apply_rope(q, cos, sin, self.rope.layout, self.rope.rotary_dim) k = apply_rope(k, cos, sin, self.rope.layout, self.rope.rotary_dim) if cache is not None: # The cache keeps Hkv heads, not H: that is the point of GQA. # Keys are already rotated by their own positions. ka, va = cache.update(layer, k.data, v.data) k, v = Tensor(ka, dtype=x.dtype), Tensor(va, dtype=x.dtype) # constants Tk = k.shape[2] vis = _visible(T, Tk, self.window) if mask is not None: m = np.asarray(mask) # ... check m broadcasts to [B, H, T, Tk] vis = vis & m.astype(bool) # True = may attend n_rep = H // Hkv # scale by the HEAD width: q . k sums dh products scores = F.matmul(q, F.transpose(repeat_kv(k, n_rep), 2, 3)) * (dh**-0.5) scores = F.masked_fill(scores, ~vis, -np.inf) if self.sinks is not None: # one extra logit per head in every row: weight it may give to nothing col = F.reshape(self.sinks, (1, H, 1, 1)) * np.ones((B, 1, T, 1)) p = F.softmax(F.concat([scores, col], axis=-1), axis=-1)[..., :Tk] else: p = F.softmax(scores, axis=-1) # over keys, the last axis o = F.matmul(p, repeat_kv(v, n_rep)) # [B, H, T, dh] return self.o_proj(F.reshape(F.transpose(o, 1, 2), (B, T, H * dh)))Walk the hand example through it. With and , and repeat_kv gives both query heads kv head 0. For token 1, head 0’s scores are , _visible(2, 2, None) keeps both keys, the softmax gives , and o_proj picks entry 0 of head 0 and entry 1 of head 1: .
The sink column is built by multiplying the [1, H, 1, 1] sinks by a [B, 1, T, 1] array of ones. Broadcasting turns it into one logit per (batch, head, query), and because the product involves the sinks tensor, its gradient is the sum over every row the sink appeared in. The full solution is in gqa.py.
Why this design
Section titled “Why this design”Projections cost , and scores and values cost , the same as MHA. The cache, and the k_proj and v_proj weights, shrink by : 3 times for SmolLM2, 8 for Llama-2-70B.
The solution repeats kv heads in memory, not in the kernel. repeat_kv materialises copies of each key for the matmul, whereas production kernels index kv head h / n_rep directly and never copy. The copy here is temporary and lets the layer reuse F.matmul and its backward unchanged; only the cache, which lives for the whole generation, must be small.
Combining causal, window, and padding into one boolean vis and filling once with is simpler and safer than adding separate bias tensors: there is one polarity (True means may attend) and one place where scores are hidden. Writing the sink denominator by hand would need a custom, numerically careful softmax; concatenating a column reuses F.softmax’s max-subtraction and backward.
The tradeoff you accept is that the cache stores float arrays and the layer re-wraps them as fresh Tensors each call, so the cache is never part of the graph. That rules out training through a cache, which nobody needs.
What can go wrong
Section titled “What can go wrong”The checks cover numerics, structure, and the cache. The hand example must come out exactly. Hugging Face goldens cover Llama GQA, an extra mask, Qwen2 biases, and gpt-oss sinks and window with gapped per-row positions, comparing outputs and every gradient; SmolLM2 depends on them. A float64 gradcheck covers every parameter, with biases, window, and sinks. repeat_kv must map and sum the gradients of copies, and GQA must equal MHA with grouped kv weights, which is what GQA is.
For the cache, prefill followed by decoding through the cache must equal the full forward, with and without window and sinks, since cached generation depends on it. The hook must receive only heads, with bytes equal to kv_bytes_per_token, which the scheduler in Continuous batching uses for admission. ConcatKVCache must append, copy, and reject bad chunks.
Structural properties come next. Future tokens must change nothing, bit for bit. With a window, key must be invisible and visible. A large sink must drive the output to 0 and a very negative one must recover plain attention. A fully masked row must give zeros, no NaN, and leave other rows intact, as padded batches require. Shifting all positions must leave outputs unchanged, and per-row positions must equal separate calls, which is what left padding and continued caches need. YaRN’s attention scaling must appear as on the scores. The parameter names and shapes must be Hugging Face’s, with no o_proj bias, the d_head default, and the fixed draw order. Finally, bad heads, d_head, window, rotary width, input, positions, or mask must fail loudly.
test_gqa.py writes the whole layer out in numpy float64 with the module’s own state_dict(): the projections, RoPE as complex multiplication, per head the scores of kv head , the causal and window mask, the optional sink column, the softmax, the values, and o_proj. It compares forward with and without window and sinks, decodes through ConcatKVCache against the full forward, and checks the head order of repeat_kv, the mask, per-row positions, the parameter names, the attention scaling, the cache’s copies, and the validation. The oracle indexes kv head h // n_rep per query head directly instead of repeating, so it is an independent statement of the grouping.
Warning: Rotating keys after the cache, rather than before, passes every prefill test. It then re-rotates every cached key to the newest position, and generation degrades right after the prompt.
apply_ropemust run beforecache.update.
The full list of pitfalls:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
| tiling kv heads instead of repeating them | query head 1 reads kv head 1, not 0: loads fine, wrong logits | np.repeat(np.arange(Hkv), n_rep) in repeat_kv |
| scaling by instead of | attention too flat whenever | multiplies by dh**-0.5 |
| a causal mask that ignores the cached prefix | decoded tokens see only the first keys | _visible offsets queries by Tk - T |
| adding the sink to every score | no effect at all (softmax is shift invariant) | concatenates a sink column and slices it off after the softmax |
| a window one key too wide () | one extra key per query; outputs drift from the reference | (i - j) < window, strictly |
| rotating keys after the cache | every cached key re-rotated to the newest position: generation degrades after the prompt | apply_rope runs before cache.update |
| mask polarity inverted | only padding is read | vis & mask, then masked_fill(scores, ~vis, -inf): True means may attend |
| per-row positions ignored | left-padded rows rotated wrongly | accepts [B, T] positions and inserts a head axis into the tables |
a bias on o_proj with qkv_bias | Qwen2 checkpoints fail to load | o_proj is always bias=False |
| caching the repeated heads | the cache is times too large | repeat_kv is applied only after cache.update |
| softmax over the wrong axis | weights sum to 1 over queries | F.softmax(..., axis=-1), the key axis |
| sinks as constants | the sinks never train | sinks is a Tensor with requires_grad=True and enters the graph through the concatenated column |
| ignoring the attention scaling | YaRN models lose their temperature | passes rope.attention_scaling to rope_cos_sin |
Where this leads
Section titled “Where this leads”Sliding windows and attention sinks builds sliding-window caches and StreamingLLM sinks on window and sinks. In the Llama-family model, this layer is the self_attn of every decoder layer (SmolLM2: 9 query heads over 3 kv heads), on the way to SmolLM2-135M logits that match Hugging Face. The KVCache.update of KV cache and generate is the hook, and later the C kernels in FlashAttention forward in C and Paged attention for decode in C take Hkv and read kv head h / n_rep directly.
Further reading
Section titled “Further reading”- Shazeer, “Fast Transformer Decoding: One Write-Head is All You Need” (2019).
- Ainslie et al., “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints” (2023).
- Beltagy et al., “Longformer” (2020), section 3.1.
- Xiao et al., “Efficient Streaming Language Models with Attention Sinks” (2023).
- OpenAI, “gpt-oss-120b and gpt-oss-20b model card” (2025).
- Hugging Face
LlamaAttentionandGptOssAttentionintransformers/models/llama/modeling_llama.py: interchangeable attention backends (eager, SDPA, FlashAttention). - FlashAttention-2 (
num_heads_k) and vLLMpaged_attention: no repetition in memory; the kernel indexes kv headh / n_repdirectly. - vLLM PagedAttention and SGLang RadixAttention: block tables, prefix sharing, and eviction, which this book builds in A paged KV cache in Python and Radix prefix cache.
Key terms
Section titled “Key terms”- Multi-query attention (MQA): all query heads share one key/value head.
- Grouped-query attention (GQA): query heads share key/value heads, each serving a contiguous group of query heads.
repeat_kv: the expansion that lets query head read kv head .- Cache hook: the
update(layer, k, v)protocol through which a KV cache receives rotated, unrepeated keys and values. - Sliding window: a limit so query reads only keys .
- Learned sink: a per-head extra logit in every softmax row, dropped afterwards, letting a head attend to nothing.
Summary
Section titled “Summary”- query heads share key/value heads; query head reads kv head , each kv head repeated times in a row.
- The cache hook receives only the heads, so the KV cache shrinks by : exactly
kv_bytes_per_token. - With a cache, the new queries are the last of keys: the causal mask uses that offset, and keys are rotated with their own positions before they are cached.
- A sliding window of lets query read keys .
- A learned sink is one extra logit per head in every softmax row, dropped afterwards: a head may attend to nothing.
Review questions
Section titled “Review questions”- Why is
cache.updatecalled afterapply_ropeand beforerepeat_kv?Answer
After RoPE, so each key is stored rotated by its own position; rotating later would rotate every cached key to the newest position. Before `repeat_kv`, so the cache holds $H_{kv}$ heads and realises GQA's memory saving, matching `kv_bytes_per_token`. - Why does
_visibleaddTk - Tto the query index?Answer
With a cache the new queries are the last $T$ of $T_k$ positions. Without the offset, a decoded token would be treated as position 0 and could see only key 0. - Why is adding to every score not a sink?
Answer
Softmax is invariant to adding a constant to a whole row, so the weights would not change. A sink must be a separate entry in the denominator that takes weight away from every key, which is what the concatenated column does. - Why is the scale and not ?
Answer
Each score is a dot product of two $d_h$-dimensional vectors, so with unit-variance entries its variance is $d_h$. Dividing by $\sqrt{d_h}$ keeps scores at unit scale regardless of the model width; $\sqrt d$ makes attention too flat whenever $d \ne d_h$. - What does a fully masked row produce, and why does that matter?
Answer
All scores are $-\infty$, and the masked softmax returns zeros instead of NaN, so the output for that row is zero and other rows are unaffected. Padded positions in a batch hit this case constantly. - Why does tiling kv heads, , load without error?
Answer
Tiling and repeating produce tensors of the same shape, so nothing in the layer can detect the difference. Only the outputs reveal it: query head 1 reads kv head 1 instead of 0, so every logit is wrong.