Skip to content

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.

Open SmolLM2-135M’s checkpoint and you will see the response to that pressure: k_proj.weight is 192×576192 \times 576, a third of q_proj’s 576×576576 \times 576. 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 HH query heads share HkvH_{kv} 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 HkvH_{kv} 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.
SymbolMeaningType / shape
xxthe layer inputfloat32[B, T, d]
HH, HkvH_{kv}query heads, key/value headsint, Hkv∣HH_{kv} \mid H
nrepn_\text{rep}H/HkvH / H_{kv}, query heads per kv headint
dhd_hhead width (d_head; not necessarily d/Hd / H)int
qqqueries after RoPEfloat32[B, H, T, d_h]
kk, vvkeys (after RoPE) and valuesfloat32[B, H_{kv}, T_k, d_h]
TT, TkT_knew tokens in this call, keys visible (cached plus new)int
WWwindowint or none
σh\sigma_hlearned sink logit of head hh (sinks)float32[H]
atja_{tj}attention weight of query tt on key jjfloat

Start from what you know. Each head computes scaled dot-product scores qt⋅kj/dhq_t \cdot k_j / \sqrt{d_h}, a softmax over the visible keys, and a weighted average of the values; the heads are concatenated and projected back to dd by o_proj. RoPE rotates qq and kk (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 1/dh1/\sqrt{d_h}, the head width, because a dot product of dhd_h unit-variance products has variance dhd_h.

Multi-query attention (Shazeer 2019) keeps one key/value head for all query heads (Hkv=1H_{kv} = 1). Grouped-query attention (Ainslie et al.) keeps HkvH_{kv} of them, each serving a contiguous group of nrepn_\text{rep} query heads.

repeat_kv expands [B,Hkv,T,dh][B, H_{kv}, T, d_h] to [B,H,T,dh][B, H, T, d_h] with output head hh reading input head ⌊h/nrep⌋\lfloor h / n_\text{rep} \rfloor: (a,b)→(a,a,a,b,b,b)(a, b) \to (a, a, a, b, b, b), the order the checkpoints were trained with. Tiling, (a,b,a,b,a,b)(a, b, a, b, a, b), 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.

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 tt of this call sits at key index i=Tk−T+ti = T_k - T + t (with no cache, Tk=TT_k = T and i=ti = t). Key jj is visible when all of these hold:

  • j≤ij \le i: causal, always;
  • i−j<Wi - j < W when a window is set: the query and the W−1W - 1 keys before it;
  • the optional mask allows it (True means may attend, broadcast to [B,H,T,Tk][B, H, T, T_k]; this is how padding is expressed).

Hidden scores are set to −∞-\infty, so their weights are exactly 0, and a row with nothing visible gives zeros, not NaN.

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 [B,Hkv,T,dh][B, H_{kv}, T, d_h] 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 HkvH_{kv} heads, which is where GQA’s saving happens: 2Hkvdh2 H_{kv} d_h 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.

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 σh\sigma_h appended to every row before the softmax and dropped afterwards:

atj=estjeσh+∑j′estj′.a_{tj} = \frac{e^{s_{tj}}}{e^{\sigma_h} + \sum_{j'} e^{s_{tj'}}}.

The weights now sum to 1−psink≤11 - p_\text{sink} \le 1. A large σh\sigma_h means the head attends to almost nothing, and a very negative one recovers plain attention. Adding σh\sigma_h 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 xx, 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.

Take d=2d = 2, H=2H = 2, Hkv=1H_{kv} = 1, dh=2d_h = 2, with RoPE off (inv_freq = [0], so every angle is 0). Choose weights so that q_proj makes head 0’s query xx and head 1’s −x-x; k=xk = x; v=(x0,2x1)v = (x_0, 2 x_1); and o_proj keeps entry 0 of head 0 and entry 1 of head 1. The tokens are x(0)=(1,0)x^{(0)} = (1, 0) and x(1)=(0,1)x^{(1)} = (0, 1). Both heads read the single kv head: k(0)=(1,0)k^{(0)} = (1, 0), k(1)=(0,1)k^{(1)} = (0, 1), v(0)=(1,0)v^{(0)} = (1, 0), v(1)=(0,2)v^{(1)} = (0, 2).

  1. Token 0 sees only key 0, so both heads output v(0)=(1,0)v^{(0)} = (1, 0) and the output is (1,0)(1, 0).
  2. For token 1, head 0 has q=(0,1)q = (0, 1) and scores (0,1)/2=(0,0.707107)(0, 1) / \sqrt 2 = (0, 0.707107). Since e0.707107=2.028115e^{0.707107} = 2.028115, the weights are (1,2.028115)/3.028115=(0.330237,0.669763)(1, 2.028115) / 3.028115 = (0.330237, 0.669763), and the head output is 0.330237 v(0)+0.669763 v(1)=(0.330237,1.339527)0.330237\, v^{(0)} + 0.669763\, v^{(1)} = (0.330237, 1.339527).
  3. For token 1, head 1 has q=(0,−1)q = (0, -1), scores (0,−0.707107)(0, -0.707107), weights (0.669763,0.330237)(0.669763, 0.330237), and head output (0.669763,0.660474)(0.669763, 0.660474).
  4. o_proj keeps entry 0 of head 0 and entry 1 of head 1: (0.330237,0.660474)(0.330237, 0.660474).

One kv head served two query heads that attend in opposite directions.

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.

The checkpoint fixes the parameter shapes: q_proj is d→Hdhd \to H d_h, k_proj and v_proj are d→Hkvdhd \to H_{kv} d_h, and o_proj is Hdh→dH d_h \to d with no bias. dhd_h is a separate argument because some models set it independently of d/Hd / H.

Attention needs one key per query head. The cheapest correct way to get there is to compute HkvH_{kv} keys and values and index them, so query head hh reads kv head ⌊h/nrep⌋\lfloor h / n_\text{rep} \rfloor. 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 HkvH_{kv} heads). That fixes where cache.update sits in the forward, after apply_rope and before repeat_kv.

With a cache, this call’s TT queries are the last TT of TkT_k keys. Every visibility rule (causal, window) is a comparison between absolute key indices i=Tk−T+ti = T_k - T + t and jj, so one boolean [T, Tk] matrix captures them all, and the user mask is ANDed in. Hidden scores become −∞-\infty 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-−∞-\infty row.

A learned sink is one extra column of logits. Concatenate it, softmax over Tk+1T_k + 1 entries, then slice off the sink, and the remaining weights sum to 1−psink1 - p_\text{sink}. 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 qq and kk, so the layer needs no special case for YaRN: the s2s^2 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.

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 up

np.repeat (each element nrepn_\text{rep} times in a row) and not np.tile (the whole array nrepn_\text{rep} times) is the entire difference between the checkpoint’s grouping and a silently wrong model.

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 vis

With no cache Tk=TT_k = T and this is the ordinary lower triangle. In one-token decoding T=1T = 1 and the single row is “all keys” (or the last WW).

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 None

The 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.

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 H=2H = 2 and Hkv=1H_{kv} = 1, nrep=2n_\text{rep} = 2 and repeat_kv gives both query heads kv head 0. For token 1, head 0’s scores are (0,1)/2(0, 1)/\sqrt 2, _visible(2, 2, None) keeps both keys, the softmax gives (0.330237,0.669763)(0.330237, 0.669763), and o_proj picks entry 0 of head 0 and entry 1 of head 1: (0.330237,0.660474)(0.330237, 0.660474).

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.

Projections cost O(BTd(H+2Hkv)dh)O(B T d (H + 2H_{kv}) d_h), and scores and values cost O(BHTTkdh)O(B H T T_k d_h), the same as MHA. The cache, and the k_proj and v_proj weights, shrink by H/HkvH / H_{kv}: 3 times for SmolLM2, 8 for Llama-2-70B.

The solution repeats kv heads in memory, not in the kernel. repeat_kv materialises HH 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 −∞-\infty 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 eσh+∑ese^{\sigma_h} + \sum e^{s} 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.

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 (a,b)→(a,a,a,b,b,b)(a, b) \to (a, a, a, b, b, b) 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 HkvH_{kv} 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 t−Wt - W must be invisible and t−W+1t - W + 1 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 s2s^2 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 ⌊h/nrep⌋\lfloor h / n_\text{rep} \rfloor, 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_rope must run before cache.update.

The full list of pitfalls:

PitfallSymptomHow the solution avoids it
tiling kv heads instead of repeating themquery head 1 reads kv head 1, not 0: loads fine, wrong logitsnp.repeat(np.arange(Hkv), n_rep) in repeat_kv
scaling by 1/d1/\sqrt{d} instead of 1/dh1/\sqrt{d_h}attention too flat whenever d≠dhd \ne d_hmultiplies by dh**-0.5
a causal mask that ignores the cached prefixdecoded tokens see only the first keys_visible offsets queries by Tk - T
adding the sink to every scoreno effect at all (softmax is shift invariant)concatenates a sink column and slices it off after the softmax
a window one key too wide (≤W\le W)one extra key per query; outputs drift from the reference(i - j) < window, strictly
rotating keys after the cacheevery cached key re-rotated to the newest position: generation degrades after the promptapply_rope runs before cache.update
mask polarity invertedonly padding is readvis & mask, then masked_fill(scores, ~vis, -inf): True means may attend
per-row positions ignoredleft-padded rows rotated wronglyaccepts [B, T] positions and inserts a head axis into the tables
a bias on o_proj with qkv_biasQwen2 checkpoints fail to loado_proj is always bias=False
caching the repeated headsthe cache is nrepn_\text{rep} times too largerepeat_kv is applied only after cache.update
softmax over the wrong axisweights sum to 1 over queriesF.softmax(..., axis=-1), the key axis
sinks as constantsthe sinks never trainsinks is a Tensor with requires_grad=True and enters the graph through the concatenated column
ignoring the attention scalingYaRN models lose their temperaturepasses rope.attention_scaling to rope_cos_sin

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.

  • Multi-query attention (MQA): all query heads share one key/value head.
  • Grouped-query attention (GQA): HH query heads share HkvH_{kv} key/value heads, each serving a contiguous group of nrepn_\text{rep} query heads.
  • repeat_kv: the expansion that lets query head hh read kv head ⌊h/nrep⌋\lfloor h / n_\text{rep} \rfloor.
  • Cache hook: the update(layer, k, v) protocol through which a KV cache receives rotated, unrepeated keys and values.
  • Sliding window: a limit WW so query tt reads only keys t−W+1..tt - W + 1 .. t.
  • Learned sink: a per-head extra logit in every softmax row, dropped afterwards, letting a head attend to nothing.
  • HH query heads share HkvH_{kv} key/value heads; query head hh reads kv head ⌊h/nrep⌋\lfloor h / n_\text{rep} \rfloor, each kv head repeated nrepn_\text{rep} times in a row.
  • The cache hook receives only the HkvH_{kv} heads, so the KV cache shrinks by H/HkvH / H_{kv}: exactly kv_bytes_per_token.
  • With a cache, the new queries are the last TT of TkT_k keys: the causal mask uses that offset, and keys are rotated with their own positions before they are cached.
  • A sliding window of WW lets query tt read keys t−W+1..tt - W + 1 .. t.
  • A learned sink is one extra logit per head in every softmax row, dropped afterwards: a head may attend to nothing.
  1. Why is cache.update called after apply_rope and before repeat_kv?
    AnswerAfter 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`.
  2. Why does _visible add Tk - T to the query index?
    AnswerWith 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.
  3. Why is adding σh\sigma_h to every score not a sink?
    AnswerSoftmax 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.
  4. Why is the scale 1/dh1/\sqrt{d_h} and not 1/d1/\sqrt{d}?
    AnswerEach 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$.
  5. What does a fully masked row produce, and why does that matter?
    AnswerAll 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.
  6. Why does tiling kv heads, (a,b,a,b,a,b)(a, b, a, b, a, b), load without error?
    AnswerTiling 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.