mlx-bun library API
    Preparing search index...

    KV cache — port of mlx-lm cache.py KVCache: preallocated in steps of 256 along the sequence axis, updated in place via slice_update.

    Reads: each named read appends through updateAndFetch and attends the fetched prefix with the stock fused SDPA, the call every graph makes after updateAndFetch today, a window through windowRead (that SDPA unless the composition names a read computing its bits otherwise). Decode passes no mask and a window the fused causal mask; at one query the two compute the same bits, so a one-row window needs no case of its own. This cache has no sliding window (graphs compose RotatingKVCache for sliding layers), so only the bidirectional and encoder reads build a mask array.

    Implements

    Index
    • Parameters

      • masks: AttentionMasks = ...

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

      • turboQuantKernels: TurboQuantKernels = uncomposedTurboQuantKernels

        The TurboQuant read of the graph that built this cache: a TurboQuant conversion of this storage (TurboQuantKVCache.fromKVCache, the delayed TurboQuant rows) attends with it, and copies of this cache carry it. Qwen 3.5's makeCache passes denseTurboQuantKernels, Gemma 4's and MiniCPM5's deferredTurboQuantKernels; a graph that reads TurboQuant storage only through the deprecated fetches passes none.

      • windowRead: DenseRead = sdpaRead

        The read of a causal window (appendWindow, appendCommitted): the stock fused SDPA, or the read the composition names (foldedWindowRead, bit for bit the same). Row layouts made from this storage and its clones read their windows with it.

      Returns KVCache

    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.

    offset: number = 0
    ropeOffsetArr?: MlxArray

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

    turboQuantKernels: TurboQuantKernels = uncomposedTurboQuantKernels

    The TurboQuant read of the graph that built this cache: a TurboQuant conversion of this storage (TurboQuantKVCache.fromKVCache, the delayed TurboQuant rows) attends with it, and copies of this cache carry it. Qwen 3.5's makeCache passes denseTurboQuantKernels, Gemma 4's and MiniCPM5's deferredTurboQuantKernels; a graph that reads TurboQuant storage only through the deprecated fetches passes none.

    values: MlxArray | null = null
    windowRead: DenseRead = sdpaRead

    The read of a causal window (appendWindow, appendCommitted): the stock fused SDPA, or the read the composition names (foldedWindowRead, bit for bit the same). Row layouts made from this storage and its clones read their windows with it.

    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

    • Physical cache storage consumed by one additional token for one row. Recurrent caches return 0 because their state does not grow with the sequence. Optional for stateless/training adapters that are never admitted or persisted.

      Returns number

    • 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

    • 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

      Returns void