mlx-bun library API
    Preparing search index...

    Rotating (sliding-window) KV cache — port of mlx-lm RotatingKVCache with keep=0 (gemma4's configuration): a ring buffer of max_size entries, so decode attends over at most the window. RoPE offsets use the true position; masks use the buffer-clamped offset.

    Reads: the sliding window is maxSize, the window every graph passes for the layers it gives this cache. Decode writes in place at the ring head and attends the whole ring with no mask (eviction enforces the window). A window writes by concatenation, in temporal order, and attends under the causal mask, materialized with the sliding window once the window binds. Both use the stock fused SDPA, as the graphs do after updateAndFetch today, which picks the in-place write for one position and concatenation otherwise. The method picks here, so a one-row window concatenates: the same bits until the ring wraps, a different key order after.

    Implements

    Index
    keys: MlxArray | null = null
    masks: AttentionMasks = ...

    The per-forward masks of the model this cache belongs to. Row layouts made from this cache (state/layout) share them.

    maxSize: number
    minimumReusableOffset?: number

    Earliest prefix whose retained representation can resume generation. Irreversible transitions may preserve bytes while invalidating older precision boundaries. Reuse policy must not trim below this offset.

    offset: number = 0
    ropeOffsetArr?: MlxArray

    Set only by compiled-decode trace adapters (see Cache).

    values: MlxArray | null = null
    STEP: 256
    • Window: L new positions per row, causal within the window, for prefill chunks, prefill tails and speculative verify windows. Query i of a row sees the row's earlier valid positions and window positions 0..i, limited to the cache's sliding window. Left padding and prepared prefill padding are never visible. Outputs at padded query positions are unspecified and the caller ignores them. To append context without attending, make this call and dispose the read unused.

      Parameters

      Returns AttentionRead

    • Stable storage identity for compatibility guards and persistence. REQUIRED: every capability predicate (isPlainKvCache, isRotating*, packages/inference/src/state/persistence.ts codecs) is a string compare on this value, so a missing override used to fail OPEN into wrong routing (PRs #42/#43: BatchedRotatingCache shipped without one and batched joins dropped running rows). Wrappers report their own kind plus the inner kind.

      Returns string

    • The live window in chronological order: the newest min(offset, maxSize) positions. A multi-token write leaves the buffer oversized in temporal order (#updateConcat keeps maxSize - 1 history rows plus the whole block), so the window is the tail of that order, never its head. optiq's kv_view._read_cache_temporal takes the head, which is the oldest rows in that state; the batched layouts and rotatingSourcePosition already take the tail.

      Returns [MlxArray, MlxArray]

    • Drop the last n tokens (future writes overwrite them). bypass (default false → unchanged behavior) skips the isTrimmable guard for the speculative-decode rollback case (trim the last n ≤ γ tips right after a

      1 concat write); rotating caches then physically shrink the buffer tail.

      Parameters

      • n: number
      • bypass: boolean = false

      Returns void