← Questions

Causal hard attention with a KV cache

0
OpenAICodingLast asked Oct 2026

Tokens arrive one at a time, each with a token embedding and a position embedding, plus key and value weight matrices. At each step, add the two embeddings to get the hidden vector, use it as the query, compute its key and value and append them to a key cache and a value cache. Then attend "hard": pick the cached key (from this step or earlier) with the highest dot product with the query, earliest on ties, and output its cached value. Return the outputs for every step, reusing the cache rather than recomputing past keys and values.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding