Decode One Token against the Cached Prefix

Trace one incremental attention step from the current residual record through Q/K/V projection, cache append, causal attention over the prefix, residual updates, and one vocabulary-logit row.

Suppose a layer already caches tt positions. Normalize the current token's layer input and project one query, key, and value per head:

qt,kt,vt:(B,h,1,dh).q_t,k_t,v_t:(B,h,1,d_h).

Append the new key and value:

K0:t=[K0:t1;kt],V0:t=[V0:t1;vt].K_{0:t}=[K_{0:t-1};k_t], \qquad V_{0:t}=[V_{0:t-1};v_t].

The current query is allowed to read all t+1t+1 entries, so its score tensor is

St=qtK0:tTdh:(B,h,1,t+1).S_t=\frac{q_tK_{0:t}^{\mathsf T}}{\sqrt{d_h}} :(B,h,1,t+1).

No upper-triangular mask is needed for this single final query if the cache contains only its valid prefix. A padding or batched-validity mask may still be needed.

Calculate One Head

Let dh=2d_h=2, current query q=[1,0]q=[1,0], keys

k0=[1,0],k1=[0,1],k2=[1,1],k_0=[1,0],\quad k_1=[0,1],\quad k_2=[1,1],

and values

v0=[1,0],v1=[0,2],v2=[1,2].v_0=[1,0],\quad v_1=[0,2],\quad v_2=[1,2].

The scaled scores are approximately [0.7071,0,0.7071][0.7071,0,0.7071]. Their softmax is [0.4011,0.1978,0.4011][0.4011,0.1978,0.4011]. The mixed value is

0.4011v0+0.1978v1+0.4011v2[0.8022,1.1978].0.4011v_0+0.1978v_1+0.4011v_2 \approx[0.8022,1.1978].

Join all heads, apply WOW_O, add the attention residual, apply the current token's norm and MLP, and continue to the next layer. Only the current token travels through these updates during decode.

Q1. Calculate a cached score row

With dh=2d_h=2, q=[1,1]q=[1,1], and cached keys [1,0][1,0] and [0,2][0,2], calculate the two scaled dot-product scores.

Compute it first, then check your number.

Hint
The unscaled dot products are 1 and 2.
Solution
The scores are [1/2,2/2][0.7071,1.4142][1/\sqrt2,2/\sqrt2]\approx[0.7071,1.4142].
Not attempted
Review

Not marked done.

Append Exactly Once

Appending the current key twice, appending along the head axis, or forgetting one layer's append can all leave later operations with plausible ranks. Assert that every layer's cache length grows by exactly one per consumed input token.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerDecode One Token against the Cached Prefixhttps://llmprimer.com/transformers/autoregressive-generation-and-efficient-inference/decode-one-token-against-the-cached-prefix© 2026 LLM Primer