One slip, torch.cat([cache[0], q], dim=2) where you meant k, puts queries where keys belong. Every shape is still right, and nothing errors. A prompt fed with no cache still comes out right, because that torch.cat only runs when a cache comes in. From the first cached step on, the key cache holds each new token's query, and every output after the prompt is off.