Skip to content

Inkling 2: opt-in int8 KV cache for the global layers (MLX_VLM_INKLING_KV8) - #2805

Open
betizzel wants to merge 3 commits into
Neywa-Labs:mainfrom
betizzel:pr/inkling-int8-global-kv
Open

betizzel wants to merge 3 commits into
Neywa-Labs:mainfrom
betizzel:pr/inkling-int8-global-kv

Conversation

@betizzel

@betizzel betizzel commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This PR includes two earlier commits. The first is the relative-attention flash kernel by @jundot (Jun Kim), from ml-explore/mlx-lm#2086, credited with a Co-authored-by trailer. I had proposed it separately as #2802 and closed that, so as not to get ahead of his PR. It runs only in inference; training keeps the differentiable masked path. The second is the split-K decode kernel from #2803. Only the last commit, "Inkling 2: opt-in int8 KV cache for the global layers", is this PR's own change. The int8 rows are read directly by both kernels; without the flash kernel, prefill would unpack each global layer's rows instead, so the evidence below (measured with both kernels) depends on it.

Problem

At long context, Inkling 2 Mini's memory goes to the global layers' K/V cache: 32 KB per token in BF16. On a 48 GB Mac that is what limits the context: with the BF16 cache, a 256K-token document stopped at 19% free memory.

Fix

  • MLX_VLM_INKLING_KV8=1 (read when Attention is built; default off) caches each global key/value row (one token, one head) as its head_dim values in int8 followed by their FP32 absmax scale as 4 more int8 bytes: one ordinary int8 array of head_dim + 4 (kernels.kv8_pack / kv8_unpack). At head_dim 128 that is 132 bytes a head-token instead of 256. Sliding layers stay BF16.
  • One scale per row is enough because keys are L2-clipped and values RMS-normalised before they are cached.
  • Rows are packed after the heads are prepared and before update_and_fetch. Because the cache holds an ordinary array, the cache classes, batch padding/merge/filter, speculative trim/rollback and prefix-cache copies need no changes. AttentionSinkKVCache's sink row is zeros, and int8 zeros with a zero scale unpack to zeros.
  • The flash kernel's tile loader and the split-K decode kernel read packed rows directly. A PACKED template constant makes them multiply the int8 values by the row's FP32 scale, which sits at bytes head_dim..head_dim+3 (4-byte aligned, since rows are head_dim + 4 bytes). Every other path (the masked fallback, short caches) unpacks first.
  • Prefix-cache checkpoints record the layout. apc_key_dependencies() returns a marker when int8 is on, so a checkpoint never restores into a cache of the other layout, including from the disk tier after a restart with the flag toggled. BF16 keys are unchanged.

Evidence

Measured on an Apple M5 Pro (48 GB) on a fork with the same change, running Inkling 2 Mini with 8-bit attention weights. These numbers were not re-measured on this branch.

  • Global KV per token: 32 KB → 16.9 KB.
  • One cache, engine only, on needle documents (3 codes each; all found):
Context BF16 cache int8 cache
128K decode 22.9 tok/s, peak 27.0 GB, prefill 414 tok/s decode 25.0 tok/s, peak 24.96 GB, prefill 409 tok/s
256K stopped at 19% free memory ran with 26% free, decode 19.2 tok/s
400K — 22% free, decode 15.4 tok/s
  • Quality:
    • The BF16 cache's greedy answer, teacher-forced through the int8 cache at 32K / 128K: mean KL 0.0017 / 0.0026, top-1 agreement 98.3% / 98.0%. Answer log-probability -0.1213 vs -0.1205 at 32K and -0.132 vs -0.133 at 128K.
    • Short context, teacher-forced KL against an FP32 reference: 0.086 for the int8 cache vs 0.077 for the BF16 cache.
    • Long-retrieval answers that looped, on 16 new ~32K documents answered with both caches in one process: 4 vs 4.
  • Short context: decode at 512 / 8K tokens is 40.6 / 39.9 tok/s with the int8 cache vs 41.6 / 39.6 with BF16. Packing costs ~0.5 ms a token at short context, which is why the flag stays opt-in.

Tests

  • test_inkling.py::test_inkling2_kernels_read_kv8_packed_rows: the split-K decode kernel and the flash kernel on packed rows match the same rows dequantized and stay close to the BF16 rows. Covers 5,000 keys, left padding (key start 0 and 700, and a two-row batch for the flash kernel), and K/V passed as strided views of a larger buffer.
  • test_models.py::test_inkling2_kv8_global_cache: a tiny Inkling 2 Mini with the flag on. Global caches hold int8 and sliding caches BF16. Logits stay within 0.06 of the BF16-cache run through a 100-token prefill and 20 decode steps, on plain caches and on a one-row batch cache with left padding. It runs through both the kernels (asserting they received int8 rows and ran) and the masked path (kernels disabled, so the masked path unpacks, including the sink row).
  • test_models.py::test_inkling2_kv8_checkpoints_never_restore_across_layouts: a disk-tier checkpoint stored under one layout is not restored under the other after a restart, in both directions; without the marker the other layout restores all 40 tokens.
  • The new tests fail on the parent commit and pass with this change. The Inkling tests in test_models.py, test_inkling.py, test_cache.py, test_model_ops.py, test_speculative.py, test_server.py, test_server_cluster.py and test_processors.py pass (217).

Full suite on this branch (pytest mlx_vlm/tests, distributed tests excluded): the only failures are the 13 test_embedding_gemma2_* cases that main also fails in this environment.

betizzel and others added 3 commits November 22, 2026 23:03
- Prefill chunks and verify blocks built a heads x queries x keys additive mask, appended a sink column
  and a zero sink K/V row (or read AttentionSinkKVCache's sink row). On left-padded batch caches this is
  about 4 GB per 2,048-token chunk at 41K keys.
- New kernels.relative_attention (from ml-explore/mlx-lm Neywa-Labs#2086, Copyright 2026 Apple Inc., MIT) adds the
  relative bias, sliding window and sink logit inside an online softmax and reads only real keys. A batch
  row's left padding is its first valid key.
- Used for BF16 on Metal with more than one query; the masked path remains for decode, other dtypes, CPU
  and non-Metal GPUs.
- Tests: kernel vs masked path on plain, sink, and left-padded batch caches (sliding and global layers,
  chunk after a cached prefix); tiny-model BF16 prefill on padded batch caches vs plain caches; fallback gates.

Co-authored-by: Jun Kim <64250138+jundot@users.noreply.github.com>
- Single-query decode on a global layer with >= 4,096 cached keys (KVCache, AttentionSinkKVCache, or a one-row BatchKVCache) runs a flash-decoding Metal kernel: each threadgroup reads one 512-key block of one KV head once for all of that head's query heads, applies the relative bias of the newest rel_extent keys and skips a batch row's left padding in-kernel, and a combine pass starts the softmax from the query-dependent sink logit, so no zero sink K/V row is needed.
- SDPA with the banded mask stays the path for prefill, verify blocks, sliding layers, rotating caches, multi-row batches, short caches, non-BF16 and off Metal.
- Tests: the kernel equals SDPA with the bias mask and sinks (partial last block, left padding including whole padded blocks); tiny Inkling 2 Mini decode on plain and one-row batch caches matches the masked path.
…G_KV8)

- MLX_VLM_INKLING_KV8=1 (read when Attention is built; default off) caches each global key/value row (one token, one head) as its head_dim values in int8 followed by their FP32 absmax scale as 4 more int8 bytes: one ordinary int8 array of head_dim + 4 (kernels.kv8_pack / kv8_unpack). At head_dim 128 that is 132 bytes a head-token instead of 256. Sliding layers stay BF16.
- Rows are packed after the heads are prepared and before update_and_fetch, so the cache classes, batch padding/merge/filter, speculative trim/rollback and prefix-cache copies handle them as any other array. AttentionSinkKVCache's sink row is zeros, which unpacks to zeros.
- The flash kernel's tile loader and the split-K decode kernel read packed rows directly (PACKED template: int8 values times the row's FP32 scale at bytes head_dim..head_dim+3). The masked fallback unpacks first.
- Tests: both kernels on packed rows match the same rows dequantized and stay close to the BF16 rows (left padding, strided views); tiny Inkling 2 Mini with the flag on: global caches int8, sliding BF16, logits near the BF16 cache through prefill and decode, plain and one-row batch caches, kernel and masked paths.
@betizzel
betizzel force-pushed the pr/inkling-int8-global-kv branch from ebb250d to 617b635 Compare November 23, 2026 05:06
@betizzel
betizzel marked this pull request as ready for review November 23, 2026 06:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant