Repository navigation
fix(cache): correct mask offset for batched quantized KV caches - #76
Merged
Merged
Conversation
Batched quantized KV caches built an incorrect causal mask for left-padded sequences and lacked a dedicated mask-construction method. Mirrors the upstream mlx-vlm fix in PR #1208 (commit 2a55b80). The mask offset must come from the number of tokens actually stored in the buffer. The cache `offset` field starts at `-left_padding[i]` and is negative for padded sequences, so using it as the mask offset produced incorrect causal mask shapes and wrong attention masking during batched quantized decode. Changes: - Track the buffered-token count (`idx`) separately from `offset` in both `BatchQuantizedKVCache` and `BatchTurboQuantKVCache`, advancing it in `update_after_decode` alongside `offset`. The `filter` and `extend` paths trim and combine `idx` consistently with per-sequence left padding. - Add `make_mask(n)` to both caches using `idx` (not `offset`) as the mask offset and `left_padding` to exclude per-sequence padding positions. The single-token, no-padding case returns no mask. - Add `create_causal_mask_with_left_padding` to `utils.rs`, a causal-mask builder that accepts a `left_padding` argument so padded positions are masked out per sequence. Verified with cargo fmt, cargo clippy (metal,accelerate, all-targets, -D warnings), and 33 unit tests covering the buffered-token advance, the idx-vs-offset mask offset, left-padding exclusion, and the single-token no-mask path.
inureyes
added a commit
that referenced
this pull request
May 26, 2026
Bump version to 0.0.31. Highlights: MiniCPM-V 4.6 and RT-DETRv2 model support plus an Anthropic-style /v1/messages API endpoint (#82, #80, #74); fixes for missing or null chat-message content, Gemma 3n VLM per_layer_inputs, Qwen3.5 MTP verify attention, and batched quantized KV cache mask offsets (#91, #86, #78, #76); and a pinned 1.93.1 Rust toolchain (#87, #90).
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…up#76) Batched quantized KV caches built an incorrect causal mask for left-padded sequences and lacked a dedicated mask-construction method. Mirrors the upstream mlx-vlm fix in PR lablup#1208 (commit 2a55b80). The mask offset must come from the number of tokens actually stored in the buffer. The cache `offset` field starts at `-left_padding[i]` and is negative for padded sequences, so using it as the mask offset produced incorrect causal mask shapes and wrong attention masking during batched quantized decode. Changes: - Track the buffered-token count (`idx`) separately from `offset` in both `BatchQuantizedKVCache` and `BatchTurboQuantKVCache`, advancing it in `update_after_decode` alongside `offset`. The `filter` and `extend` paths trim and combine `idx` consistently with per-sequence left padding. - Add `make_mask(n)` to both caches using `idx` (not `offset`) as the mask offset and `left_padding` to exclude per-sequence padding positions. The single-token, no-padding case returns no mask. - Add `create_causal_mask_with_left_padding` to `utils.rs`, a causal-mask builder that accepts a `left_padding` argument so padded positions are masked out per sequence. Verified with cargo fmt, cargo clippy (metal,accelerate, all-targets, -D warnings), and 33 unit tests covering the buffered-token advance, the idx-vs-offset mask offset, left-padding exclusion, and the single-token no-mask path.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
Bump version to 0.0.31. Highlights: MiniCPM-V 4.6 and RT-DETRv2 model support plus an Anthropic-style /v1/messages API endpoint (lablup#82, lablup#80, lablup#74); fixes for missing or null chat-message content, Gemma 3n VLM per_layer_inputs, Qwen3.5 MTP verify attention, and batched quantized KV cache mask offsets (lablup#91, lablup#86, lablup#78, lablup#76); and a pinned 1.93.1 Rust toolchain (lablup#87, lablup#90).
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.
Closes #75
Batched quantized KV caches built an incorrect causal mask for left-padded sequences and lacked a dedicated mask-construction method. Mirrors the upstream mlx-vlm fix in PR #1208 (commit 2a55b80).
Root cause
The mask offset must come from the number of tokens actually stored in the buffer. The cache
offsetfield starts at-left_padding[i]and is negative for padded sequences, so using it as the mask offset produced incorrect causal mask shapes and wrong attention masking during batched quantized decode.Fix
idx) separately fromoffsetin bothBatchQuantizedKVCacheandBatchTurboQuantKVCache, advancing it inupdate_after_decodealongsideoffset. Thefilterandextendpaths trim and combineidxconsistently with per-sequence left padding.make_mask(n)to both caches usingidx(notoffset) as the mask offset andleft_paddingto exclude per-sequence padding positions. The single-token, no-padding case returns no mask.create_causal_mask_with_left_paddingtoutils.rs, a causal-mask builder that accepts aleft_paddingargument.Verification
cargo fmt --checkclean.cargo clippy --features metal,accelerate --all-targets -- -D warningsclean.