Back to Module 2.
Problem 2.1 (Four tokens).A completion has tokens
given a prompt . Write as a sum of four conditionals. Which tokens are allowed to appear inside the conditional for ?
Solution
Solution to Problem 2.1.
The conditional for
may depend on , , and . It may not depend on or . That is the causal constraint the mask enforces.
Problem 2.2 (What the cache stores).The prompt has
tokens and you are about to generate the next token after already generating completion tokens. Per layer, how many new query vectors and how many new key vectors does a cached step compute? What goes wrong if the rotary position of that new token is set to ?
Solution
Solution to Problem 2.2.
A cached step computes one new query and one new key (and one new value) per head group, for the single new position. The previous
keys are read from the cache. Setting the rotary index to rotates the new token as if it were the first token of the sequence, so its dot products with the cached keys use the wrong relative positions. The cache contents can be numerically right and the attention still wrong.
Problem 2.3 (Compile).You turn on
torch.compileand greedy decoding, and a unit test that compares token ids to an eager run fails on a late token. Name one cause that is not “the compiler changed the model,” and name the quantity that should still match if both runs are the same function up to rounding.
Solution
Solution to Problem 2.3.
A likely cause is a cache that was not reset between the two runs, or a dtype cast that the eager path and the compiled path apply in a different order. Compilation is allowed to reassociate floating-point operations. Greedy ids can diverge from a one-ulp logit change once two top logits are close. The quantity that should stay close is the logit vector itself, not a long chain of argmax decisions. Compare logits on one forward pass before debugging the loop.