Repository navigation
Conversation
…real length BatchRotatingKVCache.make_mask (mlx-lm and mlx-vlm) trims the left padding by _idx - max_size + 1 for a multi-token update. After the window wraps in place, _idx is the ring cursor, but the update first puts the ring in temporal order (_idx becomes the window length) and then drops that many oldest columns. The stock mask keeps the old padding count, so a left-padded row loses its oldest valid key in every batched verify forward after its sliding window wraps (batched speculative verify over sliding-window layers, such as the Gemma 4 caches cache_rollback already undo-logs). The patch runs the stock mask with _idx set to the buffer length for that case. CPU regression test: a left-padded row of a wrapped batch window attends like the same row alone (fails before the fix). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
|
Your mask fix is correct, and the test fails on main as described. In oMLX this case can't happen today, though. Gemma 4 MTP runs one request at a time, and the batched verify path doesn't use BatchRotatingKVCache. I ran two requests past the window on a real server and never saw a padded row on a wrapped cache. Since the bug lives in mlx-lm and mlx-vlm, could you send the two-line fix there? oMLX will get it with the next pin bump, so I'm closing this one. |
Contributor
Author
|
Thanks for checking it on a real server. Agreed it can't happen in oMLX today. Sent the fix with its test upstream: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BatchRotatingKVCache.make_maskdrops the oldest valid key of a left-padded row on every multi-token update after its sliding window wraps. This happens in both mlx-lm and mlx-vlm.For
N > 1the mask trims the left padding by_idx - max_size + 1. After a wrap,_idxis the ring cursor. The update itself first puts the ring back in temporal order, so_idxbecomes the window length, and then it drops that many oldest columns. The mask still uses the cursor, so the padding count it removes is off and the row's first real key gets masked.On main this is reachable in batched speculative verify over sliding-window layers.
cache_rollbackalready undo-logs bothBatchRotatingKVCacheclasses for that path. For Gemma 4,gemma4_verify_attentiontakes verify blocks of 2 or 3 tokens on its own per-token route, which ignores the mask. Blocks of 4 or more go through the stock mask and hit this.The fix wraps
make_maskin the same place the undo log is attached. For a multi-token update on a rotated cache whose cursor is inside the buffer, it calls the stock mask with_idxset to the buffer length, then restores the cursor. Single-token updates and caches that have not wrapped are untouched.The same bug is in upstream mlx-lm. I can send the two-line fix there as well.
Validation
Mac mini M5 Pro, 64 GB. mlx-lm 0.32.3.dev8, mlx-vlm 0.7.11.
TestRotatingCacheMtpUndo::test_wrapped_batch_window_masks_multi_token_update_like_one_row, formlx_lmandmlx_vlm. A left-padded row of a wrapped batch window has to attend exactly like the same row run alone.2 failed, 455 passed. The padded row at the 3-token step keeps the wrong column (AssertionError: (2, 0)).tests/test_mlx_lm_mtp_patch.pypasses on the GPU: 457 passed. On CPU it gives 355 passed, 102 skipped.Checklist
🤖 Generated with Claude Code