Pack documents into language-model batches
Create fixed-size next-token batches with document-aware loss and attention masks.
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 documents contains lists of nonnegative token IDs. Make next-token pairs within each document before packing, omitting documents with fewer than two tokens. Pack pairs in order into chunks of length width (>0) and pad the final chunk. Return a list of objects input_ids, labels, positions and mask (a Boolean matrix). Padding uses pad_id, label -1, position 0 and cannot attend or be attended to. Within a chunk, tokens attend only to earlier or equal positions from the same document. A document’s positions continue across chunks.
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
Create fixed-size next-token batches with document-aware loss and attention masks.
Reproduction
{"documents":[[2,3,4],[8,9]],"width":4,"pad_id":0}[{"input_ids":[2,3,8,0],"labels":[3,4,9,-1],"positions":[0,1,0,0],"mask":[[true,false,false,false],[true,true,false,false],[false,false,true,false],[false,false,false,false]]}]{"documents":[[1],[],[2]],"width":3,"pad_id":0}[]
{"documents":[[2,3,4,5,6]],"width":2,"pad_id":0}[{"input_ids":[2,3],"labels":[3,4],"positions":[0,1],"mask":[[true,false],[true,true]]},{"input_ids":[4,5],"labels":[5,6],"positions":[2,3],"mask":[[true,false],[true,true]]}]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.