Shortlist keys for exact sparse attention
Implement causal top-k retrieval followed by exact 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.
Each query has q (full query), index_q (small query) and position (largest visible key index). Input keys, index_keys and values are aligned nonempty lists. Score visible keys by index_q·index_key, keep up to top_k keys, break ties by lower key index and preserve ranking order in selected. Compute exact scaled dot-product attention over just that selection. Return selected (IDs per query) and output. top_k=0 or position=-1 yields empty selection and zero output.
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 causal top-k retrieval followed by exact attention.
Reproduction
{"keys":[[6,0],[0,1],[2,3]],"index_keys":[[0],[2],[2]],"values":[[9],[2],[5]],"top_k":2,"queries":[{"q":[1,0],"index_q":[1],"position":2}]}{"selected":[[1,2]],"output":[[4.413289]]}{"keys":[[6,0],[0,1],[2,3]],"index_keys":[[0],[2],[2]],"values":[[9],[2],[5]],"top_k":0,"queries":[{"q":[1,0],"index_q":[1],"position":2}]}{"selected":[[]],"output":[[0]]}{"keys":[[6,0],[0,1],[2,3]],"index_keys":[[0],[2],[2]],"values":[[9],[2],[5]],"top_k":2,"queries":[{"q":[1,0],"index_q":[1],"position":1}]}{"selected":[[1,0]],"output":[[8.900838]]}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.