mlx-bun library API
    Preparing search index...

    Affine-quantized keys and values for one full-attention layer, bf16 first: the lego --kv-quant N (and a kv_config.json layer) composes.

    Numbers: today's served path and mlx-lm's (quantized_kv_start). Storage is a KVCache until an append leaves it holding at least start positions (the first append when start is 0). That append is read bf16 and then, before it returns, the storage converts (KVCache.toQuantized, the reuse floor at a nonzero start, materialized at once). Every later append quantizes its positions and is read by the quantized storage's own reads, with kernelSet's kernels. Stored values below the offset therefore equal the convert-after path's bit for bit, and so does every read. A QuantizedKVCache built empty is the other composition: it quantizes the first append too, so its first chunk's output differs (tests/state/bf16-first-kv.test.ts records by how much), and it has no mlx-lm oracle.

    The transition keys only on this cache's stored length and runs only inside its appends (or when it adopts storage that already holds start positions), so the caller's append boundaries are part of the numbers: a caller that splits one planned prefill chunk into two appends moves the conversion to the split. The rows layout the scheduler batches (DelayedQuantizedKVCache) converts with this same code but only at each row's planned chunk boundary (TransitioningKvRows). The transition is irreversible: trimming below the start afterwards keeps the quantized storage, so a verify window that crosses the start converts even if its tail is later rejected.

    The row layouts, clones and saved files take the storage, never this cache (composed): its signature is its own (kv:bf16-first: and the storage's), and its storage is what the convert-after path saves.

    Hierarchy (View Summary)

    Implements

    Index
    • storage: bf16 or affine storage of this layer to adopt (restored, cloned or extracted elsewhere); affine storage is re-homed to this lego's kernels, bf16 storage that already holds start positions converts now.

      Parameters

      • groupSize: number
      • bits: number
      • start: number

        Positions held in bf16 before the conversion; 0 converts after the first append.

      • kernelSet: AffineKernelSet

        The affine kernel set the converted storage attends with, named by the composition (composedAffineKernels, or the KV4 set).

      • masks: AttentionMasks = ...

        The per-forward masks of the model this cache belongs to.

      • Optionalstorage: Cache

      Returns Bf16FirstQuantizedKVCache

    bits: number
    composed: ComposedStorage = ...

    The storage the row layouts, clones and saved files take (see the class).

    groupSize: number
    kernelSet: AffineKernelSet

    The affine kernel set the converted storage attends with, named by the composition (composedAffineKernels, or the KV4 set).

    masks: AttentionMasks = ...

    The per-forward masks of the model this cache belongs to.

    start: number

    Positions held in bf16 before the conversion; 0 converts after the first append.

    • 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

    • 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