Model and Inference EngineeringTransformer and model mechanicsStaff
How can mismatched RoPE cache conventions corrupt incremental decoding?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
Full-prefill logits match a reference, but cached decoding diverges. Prefill stores keys after rotary position embedding, while a new decode reader also rotates each cached key at its original position. Explain the resulting score error, show why position-zero and prefill-only tests miss it, and design the cache contract and differential tests needed for a safe repair.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
Rotary position embedding, or RoPE, encodes position by rotating pairs of coordinates in queries and keys. Cache correctness therefore depends on whether those coordinates have already been rotated, not merely on the tensor’s dimensions.
Look at what the cache stores, not just its shape. With rotary position embeddings, the query at position i is rotated by i and the key at position j by j. Their dot product then carries the relative phase between those positions. RoFormer gives the underlying construction. One valid cache convention stores keys after rotation. Another stores raw projected keys and rotates them when read. Both can produce the right attention scores. Mixing them cannot. If prefill stored a rotated key and decode rotates it again on retrieval, the key has effectively been given position 2j while the query still has position i.
Why might prefill tests pass? A full prefill path can compute attention with freshly rotated Q and K and never exercise the cache-reader conversion that decode uses. Even the first generated token may look plausible if a common prefix masks the error. Compare a full forward pass over the prompt plus one extra token with a cached prefill followed by one-token decode, layer by layer. Use more than a single position. Position zero hides a second rotation because its angle is zero. Check short and long positions, odd cache block boundaries, and a nonzero prefix offset. The first divergent attention logits are more useful than a final text comparison.
I would make the cache contract explicit in the type or metadata: raw versus rotated keys, the position indices and RoPE scaling configuration used, and whether any fused attention kernel applies rotation internally. On append, a new token must follow the same convention as the prefetched tokens. On transfer between prefill and decode workers, both ends need the same contract. A sliding window can keep logical positions increasing even when physical slots wrap, so slot number must not quietly become the rotation index. Then remove one rotation at the correct boundary and prove parity on a stored checkpoint across representative context lengths. A broad change that “rotates all cached keys for consistency” could break the prefill-to-decode handoff again.
For an illustrative coordinate pair, suppose a token’s position requires a 90-degree rotation. A raw key(1,0) becomes(0,1) when stored. Rotating it again produces(-1,0). A query vector(1,0) has dot product 0 with the correctly stored pair and-1 with the twice-rotated pair. The numbers are a simplified geometric example, not the frequency of a particular checkpoint. They demonstrate that a second rotation changes the attention score even though dtype and tensor shape remain valid.
Run the comparison on the same checkpoint and cache dtype before introducing quantization or a new kernel. A raw-key cache is valid only when the reader consistently rotates each key once for the intended position. A rotated-key cache is valid only when readers consume that representation without applying the position transform again. Store or derive the RoPE scaling parameters with transferred state as well. Two workers agreeing that keys are rotated can still disagree about which rotation was used.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux