Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Mask a multi-token update of a wrapped BatchRotatingKVCache by its re…
…al length

For N > 1, make_mask trims the left padding by _idx - max_size + 1. After the
window wraps, _idx is the ring cursor, but the update first puts the ring in
temporal order (the length becomes the buffer length) and then drops that many
oldest columns, so a left-padded row lost its oldest valid key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
avifenesh and claude committed Nov 18, 2026
commit 88df6e796caeaf59eb98af3f4564c8b119fac177
4 changes: 3 additions & 1 deletion mlx_lm/models/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,9 @@ def make_mask(
rinds = rinds[None]
mask = linds >= rinds
mask &= linds < rinds + window_size
if (trim_size := self._idx - self.max_size + int(N > 1)) > 0:
# A multi-token update first puts a rotated cache in temporal order.
idx = self.keys.shape[2] if N > 1 and self.rotated else self._idx
if (trim_size := idx - self.max_size + int(N > 1)) > 0:
left_padding = left_padding - trim_size

rotated = N == 1 and (self.rotated or self._idx >= self.max_size)
Expand Down
33 changes: 31 additions & 2 deletions tests/test_prompt_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,34 @@ def test_batch_kv_cache_offset_not_mutated_in_place(self):
self.assertEqual(cache_a.offset.tolist(), [6, 7, 6, 1, 4])
self.assertEqual(cache_a.left_padding.tolist(), [2, 1, 2, 7, 4])

def test_batch_rotating_wrapped_multi_token_matches_single_row(self):
# A padded row must attend to the same keys as the row alone.
W, pad = 8, 6
mx.random.seed(0)
chunks = [7, 1, 1, 1, 3, 1, 3]
T = sum(chunks)
k, v, q = (mx.random.normal((2, 1, T, 4)) for _ in range(3))
batch = BatchRotatingKVCache(max_size=W, left_padding=[pad, 0])
alone = RotatingKVCache(max_size=W)
s = 0
for n in chunks:
mask = batch.make_mask(n, return_array=True)
bk, bv = batch.update_and_fetch(k[:, :, s : s + n], v[:, :, s : s + n])
out = mx.fast.scaled_dot_product_attention(
q[:, :, s : s + n], bk, bv, scale=1.0, mask=mask
)
lo = max(s, pad)
if s + n > lo:
amask = alone.make_mask(s + n - lo, return_array=True, window_size=W)
ak, av = alone.update_and_fetch(
k[:1, :, lo : s + n], v[:1, :, lo : s + n]
)
ref = mx.fast.scaled_dot_product_attention(
q[:1, :, lo : s + n], ak, av, scale=1.0, mask=amask
)
self.assertTrue(mx.allclose(out[:1, :, lo - s :], ref, atol=1e-3))
s += n

def test_batch_rotating_kv_cache(self):
cache = BatchRotatingKVCache(max_size=4, left_padding=[2, 0])
mask = cache.make_mask(4)
Expand Down Expand Up @@ -611,10 +639,11 @@ def test_batch_rotating_kv_cache(self):
cache.update_and_fetch(k, v)

mask = cache.make_mask(2)
# The update drops one padding slot, so the first real key is column 2.
expected = mx.array(
[
[False, False, False, True, True, True, True, True, False],
[False, False, False, True, True, True, True, True, True],
[False, False, True, True, True, True, True, True, False],
[False, False, True, True, True, True, True, True, True],
]
)
self.assertTrue(mx.array_equal(mask.squeeze(), expected))
Expand Down