NumPy allowed · plain Python earns 10 bonus points
Q · Kᵀ / √d scores normalized with softmax
Manage non-contiguous physical memory allocation for LLM key-value caches using PagedAttention virtual memory mapping.
Implement allocate_paged_blocks(seq_len, new_tokens, block_table, free_blocks, block_size). Calculate total tokens total_tokens = seq_len + new_tokens. Total physical blocks needed is ceil(total_tokens / block_size). If existing len(block_table) is insufficient, allocate additional blocks from the front of free_blocks. If not enough free blocks remain, raise MemoryError. Calculate internal_fragmentation_tokens (unused token slots in the last block). Return a dict with block_table, remaining_free_blocks, and internal_fragmentation_tokens.
allocate_paged_blocks(seq_len: int, new_tokens: int, block_table: list[int], free_blocks: list[int], block_size: int) -> dict