Example 1
Arguments: [
10,
2,
3
]
Expected result: [
0,
1,
7,
8,
9
]Keep the two sink positions 0,1 and the latest three positions 7,8,9.
KV memory and quantization · Medium
Problem 35 of 50 · Python and NumPy
Return the sorted token positions retained by a cache that keeps the first sink_tokens positions and the last window_tokens positions of a length-token sequence. Clip both regions to the existing sequence and remove overlap. The window includes the newest token. A zero-length region contributes no positions.
def retained_cache_positions(length, sink_tokens, window_tokens):Arguments: [
10,
2,
3
]
Expected result: [
0,
1,
7,
8,
9
]Keep the two sink positions 0,1 and the latest three positions 7,8,9.
Arguments: [
4,
3,
3
]
Expected result: [
0,
1,
2,
3
]The prefix and tail overlap and together cover all four positions without duplicates.