Decoding Cache
Add a key and value cache to a small decoder.
Task
This is a small, working decoder written with plain Python lists: two layers of causal self-attention with fixed weights. Generation is slow because every step runs the whole sequence through every layer again. Add a key and value cache, switched on with use_cache, so each step runs only the new token. The tokens and the logits must be the same as without the cache. Every result reports rows, the number of attention rows the model computed, so the saving can be checked.
Requirements
- decode(request) takes one request object and returns a small object. tokens is a list of 1 to 8 integers from 0 to 5 (ValueError containing tokens otherwise). Any other op raises ValueError containing op. Do not change the arguments.
- {op: "logits", tokens} returns {logits, rows}: the six scores for the token that follows, rounded to 6 decimals, and the attention rows computed.
- {op: "generate", tokens, steps, use_cache} returns {tokens, logits, rows}: the input followed by steps greedy picks, the six logits behind each pick, and the attention rows computed. steps is a non-negative integer (ValueError containing steps otherwise). use_cache is true or false and defaults to false (ValueError containing use_cache otherwise). tokens plus steps must fit in 8 (ValueError containing length otherwise).
- With use_cache true, tokens and logits are exactly the same as with use_cache false.
- rows counts calls to attend(): one per layer for each position that is run. Without the cache, every step runs every position again. With the cache, every token is run through each layer once: 2 * (len(tokens) + steps - 1) rows when steps is at least 1, and 0 rows when steps is 0.
- The weight tables, attend() and its row count, and the rounding must stay as they are.
How you are graded
- Decomposition 15%
- Prompting the assistant 15%
- Verification 20%
- Judgment about AI output 15%
- Correctness 25%
- Communication 10%
Scores use evidence from your session. A short or incomplete session may not provide enough evidence for every skill. See a sample scorecard
Interview guidance
This decoder works, and it runs the whole sequence again at every generation step. Add a key and value cache so each step only runs the new token. The outputs must match the uncached path exactly. Use the AI assistant however you like. Before you write code, tell me what the cache holds and how you will know the cached path is right.
- AI is allowed and expected: your own Claude, OpenAI or Gemini key, or the free in-browser model.
- You are graded on how you drive and check the AI, not on how much you prompt.
- Run the checks; read what fails.
- A new requirement will arrive partway through.
- Too little AI use is reported as “not enough to grade”, never as a fail.
You will answer 7 debrief questions at the end
- What did you delegate to the AI, and why?
- Name one AI suggestion you rejected or changed, and why.
- Choose one block the AI helped write. Explain what each line does and which test verifies it.
- What is still risky, and what would you test or ship next?
- What does your cache hold, and why is it safe to reuse those values at a later step?
- How did you check the cached path? What would a check on speed alone have missed?
- How long does your cache live, and what would go wrong if it lived longer?