Repository navigation
Conversation
This was referenced Nov 23, 2026
betizzel
force-pushed
the
pr/inkling-int8-global-kv
branch
3 times, most recently
from
November 23, 2026 04:38
0cc679b to
ebb250d
Compare
- 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
force-pushed
the
pr/inkling-int8-global-kv
branch
from
November 23, 2026 05:06
ebb250d to
617b635
Compare
betizzel
marked this pull request as ready for review
November 23, 2026 06:17
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 whenAttentionis built; default off) caches each global key/value row (one token, one head) as itshead_dimvalues in int8 followed by their FP32 absmax scale as 4 more int8 bytes: one ordinary int8 array ofhead_dim + 4(kernels.kv8_pack/kv8_unpack). Athead_dim128 that is 132 bytes a head-token instead of 256. Sliding layers stay BF16.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.PACKEDtemplate constant makes them multiply the int8 values by the row's FP32 scale, which sits at byteshead_dim..head_dim+3(4-byte aligned, since rows arehead_dim + 4bytes). Every other path (the masked fallback, short caches) unpacks first.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.
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.test_models.py,test_inkling.py,test_cache.py,test_model_ops.py,test_speculative.py,test_server.py,test_server_cluster.pyandtest_processors.pypass (217).Full suite on this branch (
pytest mlx_vlm/tests, distributed tests excluded): the only failures are the 13test_embedding_gemma2_*cases thatmainalso fails in this environment.