Build a bidirectional encoder for masked tokens
Run a tiny BERT-style masked-language-model forward pass.
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
{"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]]
{"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]]}[]
{"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.
Read the report, then sign in to start your investigation.