Skip to content

fix(cache): correct mask offset for batched quantized KV caches #75

Closed

Description

@inureyes

Batched quantized KV caches (BatchQuantizedKVCache and BatchTurboQuantKVCache) build an incorrect causal mask for left-padded sequences, and they lack a dedicated mask-construction method.

Problem

For batched decode with quantized KV caches, the causal mask must be built from the number of tokens actually stored in the buffer. Using the cache offset field is wrong: offset starts at -left_padding[i] and is negative for padded sequences, so it produces incorrect causal mask shapes. The effect is wrong attention masking during batched quantized decode.

Fix

  • Track the actual number of buffered tokens separately from offset in both BatchQuantizedKVCache and BatchTurboQuantKVCache, advancing it during decode alongside offset.
  • Add a make_mask method to both caches that uses the buffered-token count (not offset) as the mask offset and excludes per-sequence padding positions via left_padding.
  • Add a causal-mask helper that accepts a left_padding argument so padded positions are masked out per sequence.

Acceptance criteria

  • Both batched quantized cache types expose a correct make_mask.
  • The mask offset reflects the number of buffered tokens, not the possibly-negative offset.
  • Left-padding positions are excluded from the causal mask.
  • The single-token decode path returns no mask, while a non-zero left-padding case returns one.
  • Unit tests cover the buffered-token advance, mask shape, and left-padding exclusion.

Activity

  1. added
    type:bug
    area:core
    priority:medium
    status:ready
    on May 24, 2026
  2. self-assigned this
    on May 29, 2026
  3. added this to the 0.1 milestone on Jun 5, 2026
  4. added
    status:done
    and removed
    status:ready
    on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corepriority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions