Skip to content

fix(mtp): mask a multi-token update of a wrapped batch window by its real length - #5235

Closed
avifenesh wants to merge 1 commit into
jundot:mainfrom
avifenesh:fix/rotating-mask-wrap
Closed

avifenesh wants to merge 1 commit into
jundot:mainfrom
avifenesh:fix/rotating-mask-wrap

Conversation

@avifenesh

Copy link
Copy Markdown
Contributor

Summary

BatchRotatingKVCache.make_mask drops 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 > 1 the mask trims the left padding by _idx - max_size + 1. After a wrap, _idx is the ring cursor. The update itself first puts the ring back in temporal order, so _idx becomes 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_rollback already undo-logs both BatchRotatingKVCache classes for that path. For Gemma 4, gemma4_verify_attention takes 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_mask in 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 _idx set 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.

  • New test TestRotatingCacheMtpUndo::test_wrapped_batch_window_masks_multi_token_update_like_one_row, for mlx_lm and mlx_vlm. A left-padded row of a wrapped batch window has to attend exactly like the same row run alone.
  • On main (c74f1b1) it fails for both caches: 2 failed, 455 passed. The padded row at the 3-token step keeps the wrong column (AssertionError: (2, 0)).
  • On this branch tests/test_mlx_lm_mtp_patch.py passes on the GPU: 457 passed. On CPU it gives 355 passed, 102 skipped.
  • Not run: an end-to-end Gemma 4 MTP batch with 4-token verify blocks past the window. The test drives the cache directly.

Checklist

  • I read CONTRIBUTING.md.
  • I understand every change in this PR and can explain it in review.
  • For UI or UX changes, I attached before and after screenshots. (No UI change.)

🤖 Generated with Claude Code

…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>
@jundot

jundot commented Nov 18, 2026

Copy link
Copy Markdown
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.

@avifenesh

Copy link
Copy Markdown
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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants