bugger.
All challenges/Pythonhardllm-encoder-mlm-v1
THE INVESTIGATION

Build a bidirectional encoder for masked tokens

Run a tiny BERT-style masked-language-model forward pass.

LLM ArchitectureArchitecture builds

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 tokens is an aligned token list and valid flags mark nonpadding positions. Replace each position listed in masked with mask_id before gathering token_embedding and adding position_embedding at zero-based positions. Use the provided projected_attention helper for bidirectional self-attention with valid keys, then add its output to each input row. Return vocabulary logits from lm_head only at the listed mask positions, preserving their order. Positions in masked are unique and valid. The example uses one attention layer and no FFN or norm.

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

Run a tiny BERT-style masked-language-model forward pass.

Reproduction

01 Masked first position reads the right context
{"tokens":[0,1,2],"valid":[true,true,true],"masked":[0],"mask_id":3,"token_embedding":[[1,0],[0,1],[3,3],[1,1]],"position_embedding":[[0,0],[0,1],[1,0]],"attention":{"q":[[1,0],[0,1]],"k":[[1,0],[0,1]],"v":[[1,0],[0,1]],"out":[[1,0],[0,1]]},"lm_head":[[1,0,-1],[0,1,2]]}
[[4.807233,3.917386,3.027538]]
02 No positions requested
{"tokens":[0],"valid":[true],"masked":[],"mask_id":3,"token_embedding":[[1,0],[0,1],[3,3],[1,1]],"position_embedding":[[0,0],[0,1],[1,0]],"attention":{"q":[[1,0],[0,1]],"k":[[1,0],[0,1]],"v":[[1,0],[0,1]],"out":[[1,0],[0,1]]},"lm_head":[[1,0,-1],[0,1,2]]}
[]
03 Masked targets cannot leak original IDs
{"tokens":[2,1,2],"valid":[true,true,true],"masked":[0],"mask_id":3,"token_embedding":[[1,0],[0,1],[3,3],[1,1]],"position_embedding":[[0,0],[0,1],[1,0]],"attention":{"q":[[1,0],[0,1]],"k":[[1,0],[0,1]],"v":[[1,0],[0,1]],"out":[[1,0],[0,1]]},"lm_head":[[1,0,-1],[0,1,2]]}
[[4.807233,3.917386,3.027538]]

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.