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 positions. Normalize the current token's layer input and project one query, key, and value per head:
Append the new key and value:
The current query is allowed to read all entries, so its score tensor is
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 , current query , keys
and values
The scaled scores are approximately . Their softmax is . The mixed value is
Join all heads, apply , 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 , , and cached keys and , calculate the two scaled dot-product scores.
Compute it first, then check your number.
Hint
Solution
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.