bugger.
All challenges/Pythonmediumllm-grouped-query-attention-v1
THE INVESTIGATION

Multi-head and grouped-query attention

Share KV heads across contiguous groups of query heads.

LLM ArchitectureTransformer components

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

01 Four query heads share two distinct KV groups
{"q":[[[1]],[[1]],[[1]],[[1]]],"k":[[[1]],[[1]]],"v":[[[2]],[[8]]],"mask":[[true]]}
[[[2]],[[2]],[[8]],[[8]]]
02 MQA uses a single KV group
{"q":[[[1]],[[2]]],"k":[[[1]]],"v":[[[2]]],"mask":[[true]]}
[[[2]],[[2]]]
03 MHA assigns one KV group per head
{"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.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.