Differential-KV (DKV) is a sparse KV-cache inference runtime designed for high-efficiency, memory-bounded long-context Large Language Model (LLM) inference across Apple Silicon (MLX) and CUDA GPUs.
ResearchGate: https://www.researchgate.net/publication/410864213
Author: Om Chimurkar (Newton School of Technology, Rishihood University)
Paper: https://doi.org/10.5281/zenodo.21539110
Try a demo here: https://huggingface.co/spaces/Om1232/Differential-KV
The Key-Value (KV) cache is the primary memory bottleneck in long-context LLM inference: its memory footprint scales linearly
Differential-KV addresses this by partitioning the KV cache into fixed micro-blocks of size
-
Anchor Token (
$a_k, a_v$ ): The first token in the block, preserved in exact precision to serve as an anchor reference. -
Joint
$K \mid V$ Low-Rank SVD Delta: A truncated Singular Value Decomposition (SVD) of rank$r = 32$ (with layer-adaptive variation: early layers$0.75r$ , mid layers$1.5r$ , late layers$0.5r$ ) capturing shared structural variation across key and value projections ($U \in \mathbb{R}^{(B_s-1) \times r}$ ,$V_K, V_V \in \mathbb{R}^{r \times d_{\text{head}}}$ ). -
Exact Residual Tokens (
$R = 128$ default budget): Tokens with high reconstruction error or key semantic structures (digits, mathematical formulas, entity names, relational connectives) kept uncompressed. - Bounded Dense Recency Window: A sliding window holding the most recent sequence tokens uncompressed.
-
Low-Rank Space Query Scoring: Scores queries directly in the low-rank subspace (
$O(r \cdot d_{\text{head}})$ dot products per block) without ever decompressing or materializing the full Key matrix$K$ . - Flash-Style LSE Combine: Merges sparse low-rank/residual attention scores with the dense recency window using numerically stable fp32 log-sum-exp (LSE) accumulation and fp16 operands.
-
Sub-Quadratic Prefill: Uses block-sparse attention during prefill, reducing prompt processing complexity from
$O(L^2)$ down to$O(L \cdot K)$ , enabling prefill crossover where Differential-KV prefilling outperforms dense baselines at long contexts ($\ge 32\text{k}$ ).
# 1. Clone repository with submodules
git clone --recurse-submodules https://github.com/Omc12/Differential-KV.git
cd Differential-KV
# 2. Setup virtual environment and dependencies
make setup
# 3. Launch interactive DKV Chat CLI (downloads default model Qwen/Qwen2.5-1.5B-Instruct on first run)
make chat
# Or specify a custom model:
make chat MODEL=Qwen/Qwen2.5-0.5B-Instruct# 1. Setup python virtual environment
make setup
# 2. Build CUDA native extensions or Triton kernel paths
# See BUILD.md for detailed instructions on cuSOLVER/cuBLAS & Triton requirements| Command | Description |
|---|---|
make setup |
Creates dkv_venv Python virtualenv & installs required packages |
make chat |
Starts interactive terminal CLI in Direct Mode |
make serve |
Launches OpenAI-compatible REST API gateway on http://localhost:8000 |
make test |
Runs needle-in-a-haystack (NIAH) recall guardrail tests at 8k & 16k context |
make native |
Compiles high-performance C++ engine (dkv_native) with Metal/CUDA support |
The Differential-KV terminal interface (ACTIVE_RUNTIME/serving/cli.py) supports two primary operational modes: Direct Execution Mode and Client-Server Mode.
Runs model weights directly in-process via MLX or PyTorch:
python ACTIVE_RUNTIME/serving/cli.py --model Qwen/Qwen2.5-1.5B-Instruct --preset mid --serving-mode balancedConnects the CLI UI to a running make serve API server instance:
# Terminal 1: Start API server
make serve MODEL=Qwen/Qwen2.5-1.5B-Instruct
# Terminal 2: Connect CLI client
python ACTIVE_RUNTIME/serving/cli.py --api-url http://localhost:8000/v1| Flag | Type | Default | Description |
|---|---|---|---|
--model |
str |
Qwen/Qwen2.5-0.5B-Instruct |
HuggingFace model ID or local directory path |
--api-url |
str |
None |
API Gateway base URL. When provided, runs CLI in Client Mode |
--serving-mode |
choice |
balanced |
KV Cache strategy: lightweight, balanced, performance, long-context, fused-sparse
|
--preset |
choice |
mid |
Hardware optimization preset: low, mid, high
|
--rank |
int |
32 |
SVD rank for KV compression (capped at |
--micro-block-size |
int |
256 |
Number of tokens per compressed KV micro-block ( |
--batch-size |
int |
4 |
Maximum continuous batching size for engine |
--load-in-4bit |
flag |
False |
Load model weights using 4-bit NF4 quantization |
--load-in-8bit |
flag |
False |
Load model weights using 8-bit quantization |
--max-tokens |
int |
16384 |
Maximum tokens to generate per response |
--temperature |
float |
0.7 |
Sampling temperature |
--top-p |
float |
0.9 |
Top-p nucleus sampling probability |
--repetition-penalty |
float |
1.15 |
Repetition penalty factor |
--draft-model |
str |
None |
Optional draft model ID for speculative decoding |
--max-resident-sessions |
int |
4 |
Maximum active resident chat sessions held in VRAM |
Differential-KV runtime behaviors can be fine-tuned using environment variables:
| Variable | Default | Scope | Description |
|---|---|---|---|
DKV_COMPRESSED_DECODE |
auto |
MLX | Controls sparse decode (on, off, auto). Auto engages sparse decode at sequence lengths |
DKV_ENGAGE_THRESHOLD |
Budget-gated | Native / MLX | Context length at which sparse decode engages. Keeps dense decode for short contexts where dense fits. |
DKV_HIGH_QUALITY_ROUTING |
0 |
Cross-runtime |
0 = Fast bounded-K pruning (attends top-$K$ blocks, context-independent speed). 1 = High-Quality routing (dynamic candidate routing). |
DKV_CACHE_LIMIT_GB |
1 |
MLX | Buffer-cache allocation cap in GB (halves peak prefill RAM). |
DKV_TOPK_BLOCKS |
16 |
Both engines | Number of compressed micro-blocks routed per decode step. |
DKV_MAX_RESIDUAL |
128 |
Both engines | Number of exact residual token rows stored per block ( |
DKV_SVD_SEED |
1234 |
MLX | SVD random state seed for deterministic compression. |
DKV_EARLY_LAYER_RANK_BOOST |
0 |
MLX | Set 1 to boost SVD rank ( |
DKV_MAX_RANK_EARLY |
0 |
MLX | Cap for early layer boosted rank ( |
DKV_PROFILE_CB |
0 |
Both engines | Enable layer-wise routing, GPU kernel, and readback latency profiling logs. |
DKV_CB_GQA_ROUTE |
on |
Native | Grouped-Query Attention (GQA) head-averaging in native routing loop ( |
DKV_CB_ROUTE_ALL |
on |
Native | Forces routing across all resident blocks to prevent candidate screening drops. |
DKV_FUSED_DECODE |
0 |
MLX | Experimental Metal decode kernel ( |
| Feature | ACTIVE_RUNTIME/ (Python Overlay) |
dkv_native/ (C++ Engine) |
|---|---|---|
| Language | Python 3.10+ (with optional C++ dkv_core extension) |
Pure C++17 |
| Backends | MLX (Apple Silicon) / PyTorch + Triton (CUDA) | forked llama.cpp / ggml (Metal & CUDA) |
| Model Format | HuggingFace Transformers / mlx-community |
GGUF |
| Primary Target | Research, rapid iteration, serving gateway | Production edge deployment, minimal host overhead |
| Status | Reference accuracy (4kβ64k NIAH 100% exact recall) | Experimental / Work-In-Progress |
All benchmark results are empirically measured on a single host: Apple M3 with 8.6 GB unified memory, evaluating Qwen2.5-1.5B-Instruct (int4) using rank benchmarks/results/results_latest.json).
| Context Length | Runtime / Engine | Prefill Time (s) | Decode Speed (tok/s) | Peak Allocator Memory (GB) | Needle Recalled |
|---|---|---|---|---|---|
| 4k | DKV Active (Compressed) | 6.16s | 33.3 | 1.55 GB | Yes |
| MLX Dense Baseline | 5.00s | 68.9 | 1.68 GB | Yes | |
| PyTorch Dense Baseline | 0.83s | 3.6 | 8.05 GB | Yes | |
| 8k | DKV Active (Compressed) | 13.37s | 31.4 | 1.67 GB | Yes |
| MLX Dense Baseline | 11.08s | 54.5 | 1.79 GB | Yes | |
| PyTorch Dense Baseline | 0.48s | 3.5 | 8.05 GB | Yes | |
| 16k | DKV Active (Compressed) | 29.77s | 29.7 | 2.20 GB | Yes |
| MLX Dense Baseline | 27.04s | 48.2 | 2.03 GB | Yes | |
| PyTorch Dense Baseline | OOM | OOM | OOM (8.58 GB) | OOM | |
| 32k | DKV Active (Compressed) | 72.33s | 27.2 | 3.12 GB | Yes |
| MLX Dense Baseline | 75.00s | 37.5 | 2.45 GB | Yes | |
| PyTorch Dense Baseline | OOM | OOM | OOM (8.58 GB) | OOM | |
| 64k | DKV Active (Compressed) | 477.12s | 21.4 | 5.04 GB | Yes |
| MLX Dense Baseline | 820.98s | 20.2 | 3.23 GB | Yes | |
| PyTorch Dense Baseline | OOM | OOM | OOM (8.58 GB) | OOM |
π‘ Key Benchmark Takeaways:
- Prefill Crossover at 64k: At extreme context (
$64\text{k}$ ), DKV prefilling beats MLX Dense prefilling by$1.72\times$ (477.1s vs 821.0s), demonstrating sub-quadratic scaling ($O(L \cdot K)$).- Baseline Differentiation: Standard PyTorch Dense (
AutoModelForCausalLM) suffers an Out-Of-Memory (OOM) failure at$\ge 16\text{k}$ context on an 8.6 GB memory host, whereas MLX unified memory optimization enables MLX Dense to complete 64k.- Needle Recall & Heuristic-Protection Transparency:
- Pattern-Protected NIAH Needle Recall (Digits/Codes): 100% exact recall (protected by
streaming_sparse_ingest.pycontent-aware regex safeguards).- Unassisted Prose Entity Recall (
benchmarks/prose_fact_recall.py): ~60% recall under default unassisted SVD compression. Toggle regex rules viaDKV_DISABLE_REGEX_HEURISTICS=1.
The residual budget
| Residuals ( |
Passcode Recall | Decode Speed (tok/s) | KV Cache Store Size (GB) | Compression Ratio vs. Dense |
|---|---|---|---|---|
| 8 | Yes | 21.4 | 0.124 GB | |
| 16 | Yes | 21.1 | 0.139 GB | |
| 32 | Yes | 20.5 | 0.167 GB | |
| 64 | Yes | 21.5 | 0.224 GB | |
| 128 (Default) | Yes | 19.6 | 0.338 GB |
| Component | Dimensions / Shape | Bytes | Note |
|---|---|---|---|
| 16,320 B | Low-rank core | ||
| 32,768 B | Low-rank core | ||
| Anchors |
1,024 B | Exact block reference | |
| Key Min/Max | 1,024 B | Decode router bounds | |
| Scalars / Metadata | scale, seq_len | 8 B | Per-block control |
| Low-Rank Core Total | 51,144 B | 49.9 KiB (Fixed) | |
| Exact residuals ( |
131,072 B | 128.0 KiB | |
| Exact residuals ( |
65,536 B | 64.0 KiB | |
| DKV Block ( |
182,216 B | 177.9 KiB ( |
|
| DKV Block ( |
116,680 B | 113.9 KiB ( |
|
| Dense Block | 262,144 B | 256.0 KiB ( |
Evaluated on Qwen2.5-1.5B-Instruct (int4) under up to
| Task Category | Task | Evaluated Metric | DKV Score |
|---|---|---|---|
| Multi-Document QA | qa_2 |
Exact Match / Substring | 100.0% |
| Single-Needle Recall | niah_single |
Needle Retrieval | 100.0% |
| Multi-Key Recall | niah_multikey |
Key-Value Retrieval | 100.0% |
| Multi-Value Recall | niah_multivalue |
Value Association | 100.0% |
| Variable Tracking | variable_tracking |
Chain-of-Thought Variable | 100.0% |
| Common Words Extraction | cwe |
Frequency Extraction | 100.0% |
| Category | Task | Metric | DKV Score |
|---|---|---|---|
| Single-Doc QA | qasper |
F1 Score | 87.90% |
multifieldqa-en |
F1 Score | 62.61% | |
narrativeqa |
F1 Score | 24.56% | |
| Multi-Doc QA | 2wikimultihopqa |
F1 Score | 9.68% |
hotpotqa |
F1 Score | 5.11% | |
musique |
F1 Score | 4.97% | |
| Summarization | multi_news |
ROUGE-L | 26.48 |
gov_report |
ROUGE-L | 20.38 | |
qmsum |
ROUGE-L | 7.79 | |
| Code Completion | repobench-p |
Code Sim | 30.00% |
lcc |
Code Sim | 24.33% | |
| Synthetic & Few-Shot | passage_count |
Accuracy | 65.05% |
samsum |
ROUGE-L | 58.99 | |
passage_retrieval-en |
Rank Accuracy | 1.58% | |
trec |
Accuracy | 2.56% | |
triviaqa |
F1 Score | 0.13% |
π‘ Routing & Pruning Insight (
DKV_TOPK_BLOCKS=16):
- High-Efficiency Pruning Trade-off: The default benchmark setting (
DKV_TOPK_BLOCKS=16) routes only 16 micro-blocks ($16 \times 256 = 4,096$ tokens) per decode step out of the full 32,768 ($32\text{k}$ ) context window, pruning 87.5% of sequence blocks to achieve$>30$ tok/s decode speed on unified memory.- Multi-Hop Retrieval Scaling: For complex multi-document reasoning tasks (such as
2wikimultihopqa,hotpotqa, andmusique) where evidence is scattered across multiple distant passages, increasing the candidate budget toDKV_TOPK_BLOCKS=32or64($8\text{k}$ β$16\text{k}$ attended tokens) expands evidence gathering and further boosts multi-hop recall.
-
Early-Layer Boosting: Boosts SVD rank by up to
$2\times$ in the first 15% of network layers to safeguard syntactic representations (DKV_EARLY_LAYER_RANK_BOOST=1). -
Content-Aware Dynamic Boosting: Automatically detects micro-blocks containing numerical data, mathematical formulas (e.g., LaTeX
$$,\sum), or formal definition patterns, boosting block SVD rank by$1.5\times$ on-the-fly.
To ensure critical factual information is retained, residuals are selected using three combined IDF-weighted priority signals:
- Owner-Capture: Entity names accompanying high reconstruction error tokens.
- Edge-Capture: Relational connectives with potential low-rank key collision.
- Coverage Bonus: Enforces uniform spread across block token positions to prevent localized error clustering.
- Tiered CPU-GPU KV Offloading: Maintains a heat score for each micro-block, evicting cold blocks to pinned host RAM when GPU pool utilization exceeds 80%.
- Step-Ahead Async Prefetch: Background prefetching retrieves cold blocks into GPU memory before the subsequent decode step touch point, hiding PCIe transfer latency.
Execute test suites from repository root using dkv_venv:
# 1. Kernel Parity Oracle Test (MLX)
pytest ACTIVE_RUNTIME/tests/test_dkv_kernel_parity.py -q
# 2. Needle-in-a-Haystack Recall Benchmark (Canonical)
cd benchmarks && python niah_recall.py --bench --ctx 4096 8192 16384 32768 \
--model mlx-community/Qwen2.5-1.5B-Instruct-4bit
# 3. Multi-Entity Relational Binding Test
cd benchmarks && python relational_ab.py --mode sparse --natural --spread
# 4. Native C++ Engine Honest Sweep
cd dkv_native/tests && ./test_niah_native.shIf you use Differential-KV in your research or project, please cite:
@misc{chimurkar_2026_21539110,
author = {Chimurkar, Om},
title = {DKV: Anchor + Low-Rank Differential KV-Cache
Compression for Scalable Long-Context Inference},
month = jul,
year = 2026,
publisher = {Zenodo},
version = {v1},
doi = {10.5281/zenodo.21539110},
url = {https://doi.org/10.5281/zenodo.21539110},
}Persistent DOI: https://doi.org/10.5281/zenodo.21539110
For build instructions and native compilation details, see BUILD.md.
Differential-KV is released under the MIT License β see LICENSE.
Vendored third-party code keeps its own license and is not covered by the root LICENSE:
| Component | Path | License |
|---|---|---|
llama.cpp / ggml |
dkv_native/third_party/llama.cpp/ |
MIT β LICENSE (Β© 2023β2026 The ggml authors) |
nlohmann/json |
dkv_native/third_party/llama.cpp/licenses/ |
MIT β LICENSE-jsonhpp |
Both are MIT and therefore compatible with this project's license; their copyright notices are retained in place, as MIT requires.