Multi-head and grouped-query attention
Share KV heads across contiguous groups of query heads.
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[h][t][d], k[g][s][d], v[g][s][value_width] and mask[t][s] are nonempty with compatible dimensions. Query head count H is a multiple of KV count G. Map query head h to floor(h/(H/G)), then return masked scaled attention per head. The same function covers MHA when G=H and MQA when G=1. Fully masked queries return zeros.
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
Share KV heads across contiguous groups of query heads.
Reproduction
{"q":[[[1]],[[1]],[[1]],[[1]]],"k":[[[1]],[[1]]],"v":[[[2]],[[8]]],"mask":[[true]]}[[[2]],[[2]],[[8]],[[8]]]
{"q":[[[1]],[[2]]],"k":[[[1]]],"v":[[[2]]],"mask":[[true]]}[[[2]],[[2]]]
{"q":[[[1]],[[1]]],"k":[[[1]],[[1]]],"v":[[[2]],[[3]]],"mask":[[true]]}[[[2]],[[3]]]
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.