Scaled dot-product attention and masks
Implement masked scaled dot-product attention from projected queries, keys and values.
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 and v are nonempty rectangular matrices with compatible dimensions: q and k share a width, and k and v share a row count. mask has shape query count × key count and true entries may participate. Return one weighted value row per query. Scale logits by 1/sqrt(query width). A fully masked row yields an all-zero value vector.
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 masked scaled dot-product attention from projected queries, keys and values.
Reproduction
{"q":[[1,0],[0,1]],"k":[[1,0],[0,2]],"v":[[2,3],[7,8]],"mask":[[true,false],[true,true]]}[[2,3],[6.022148,7.022148]]
{"q":[[1]],"k":[[2]],"v":[[7,8]],"mask":[[false]]}[[0,0]]
{"q":[[2]],"k":[[2],[3]],"v":[[4],[7]],"mask":[[false,true]]}[[7]]
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.