In long-context attention, a common optimization is to compress a key/value sequence into a much shorter one so that downstream modules can operate on fewer positions. One heavy-but-accurate variant partitions the key sequence into non-overlapping blocks of size and collapses each block into a single compressed key using a content-dependent softmax weighting with a learned positional bias.
Given key matrix (with divisible by ), a learned scoring vector , and a positional bias , the compressed key at block index is defined as:
Blocks do not overlap (unlike sliding-window compression), so the output has shape .
Implement compress_kv(K, w, b, m) that takes:
K: array-like of shape (S, D) (assume S % m == 0)w: array-like of shape (D,) — scoring vectorb: array-like of shape (m,) — positional biasm: int — block sizeReturn a nested Python list of shape (S/m, D) with the compressed keys.