bugger.
All challenges/Pythonmediumllm-scaled-attention-v1
THE INVESTIGATION

Scaled dot-product attention and masks

Implement masked scaled dot-product attention from projected queries, keys and values.

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, 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

01 Causal and unrestricted rows
{"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]]
02 Fully masked query
{"q":[[1]],"k":[[2]],"v":[[7,8]],"mask":[[false]]}
[[0,0]]
03 Exactly one visible key
{"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.

    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.