Backpropagate through masked attention
Implement Q, K and V gradients for scaled dot-product attention.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input q, k, v, mask are the projected attention operands; upstream has one value-width gradient row per query. Output grad_q, grad_k and grad_v in input shapes. Use the same 1/sqrt(key width) scale as the forward pass. Fully masked rows contribute zero and shared keys/values accumulate across queries. The supplied probabilities helper computes stable masked softmax.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Implement Q, K and V gradients for scaled dot-product attention.
Reproduction
{"q":[[1,2]],"k":[[1,0],[0,1]],"v":[[2,1],[3,4]],"mask":[[true,true]],"upstream":[[1,0]]}{"grad_q":[[-0.156399,0.156399]],"grad_k":[[-0.156399,-0.312797],[0.156399,0.312797]],"grad_v":[[0.330238,0],[0.669762,0]]}{"q":[[1]],"k":[[1],[2]],"v":[[3],[4]],"mask":[[false,false]],"upstream":[[1]]}{"grad_q":[[0]],"grad_k":[[0],[0]],"grad_v":[[0],[0]]}{"q":[[2]],"k":[[1],[2]],"v":[[4],[5]],"mask":[[true,false]],"upstream":[[3]]}{"grad_q":[[0]],"grad_k":[[0],[0]],"grad_v":[[3],[0]]}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.