Data & Systems50 minutes

Masked Attention

Find why a small transformer returns NaN and fix the causes.

Python

Task

This is a small transformer written with plain Python lists: an embedding, one causal self-attention layer with two heads, and a projection to scores over a six-token vocabulary. Its weights are fixed. Some requests return NaN, and the tokens it samples are not the ones the same weights give in the reference implementation. The weights are not the problem. Find the causes and fix them. Do not hide them.

Requirements

  1. transformer(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.
  2. {op: "mask", length} returns {mask}: a length by length table where mask[query][key] is 1 when the query position may look at the key position and 0 when it may not. A position may look at itself and at earlier positions, never at later ones. length is an integer from 1 to 8 (ValueError containing length otherwise).
  3. {op: "weights", tokens} returns {weights}: weights[head][query][key], the attention weights of each of the two heads. Every query spreads a total weight of 1 over the keys it may look at, and gives 0 to the rest.
  4. {op: "logits", tokens} returns {logits}: one row of six scores per position. The scores at a position depend only on the tokens at that position and before it.
  5. {op: "generate", tokens, steps} returns {tokens}: the input followed by steps greedy picks, each the highest-scoring next token (ties go to the lower token). steps is a non-negative integer (ValueError containing steps otherwise), and tokens plus steps must fit in 8 (ValueError containing length otherwise).
  6. Every number in a result is finite and rounded to 6 decimals with the rounded() helper. The weight tables at the top of the file 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 small transformer returns NaN, and its samples do not match the reference. The weights are fine. Find the bugs and fix them where they start: do not hide them. Use the AI assistant however you like. Before you change anything, show me the smallest input that goes wrong and tell me what you expect its attention weights to be.

  • 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 was the smallest input that showed each problem, and what did you expect to see for it?
  • How did you check that the scores at a position depend only on that position and the ones before it?
  • Which suggestion from the assistant would have hidden a problem without fixing it, and how did you tell?