Compress prefix blocks alongside local attention
Build a causal local-and-compressed attention view for one query.
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 keys and values hold aligned nonempty token rows, position is the last visible index and window (>0) keeps that many recent raw tokens. Group the prefix before the recent window into consecutive full blocks of ratio tokens. Ignore an incomplete prefix block. Mean-pool keys and values over each block. Score pooled keys by q dot pooled key, choose up to top_k (null means all), breaking ties by lower block ID. Combine chosen summaries and all recent raw rows in one scaled softmax. Output selected_blocks, local_tokens and output. This lab uses mean pooling to isolate boundaries and selection.
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
Build a causal local-and-compressed attention view for one query.
Reproduction
{"q":[1],"keys":[[0],[2],[1],[5],[2],[3],[1000.0]],"values":[[0],[4],[2],[6],[8],[3],[9]],"position":4,"window":2,"ratio":2,"top_k":1}{"selected_blocks":[0],"local_tokens":[3,4],"output":[6.024634]}{"q":[1],"keys":[[0],[2],[1],[5],[2],[3],[1000.0]],"values":[[0],[4],[2],[6],[8],[3],[9]],"position":2,"window":4,"ratio":2,"top_k":1}{"selected_blocks":[],"local_tokens":[0,1,2],"output":[3.150421]}{"q":[1],"keys":[[0],[2],[1],[5],[2],[3],[1000.0]],"values":[[0],[4],[2],[6],[8],[3],[9]],"position":3,"window":1,"ratio":2,"top_k":2}{"selected_blocks":[0],"local_tokens":[3],"output":[5.928055]}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.