idempotent-kv: Zero-Copy In-Place KV-Cache Compaction for LLMs
Eliminate 100% of auxiliary VRAM allocations during KV-cache context eviction in vLLM, SGLang, and Hugging Face inference engines.
🌟 Interactive Live Showcase: huggingface.co/spaces/aecetin/idempotent-ai-showcase
🚀 The Bottleneck: The Memory Wall
In long-context autoregressive LLM inference (32k to 128k+ tokens), intermediate Key and Value activations consume tens of gigabytes of accelerator memory. To prevent out-of-memory (OOM) faults, dynamic eviction policies discard low-importance context tokens.
However, standard deep learning runtimes execute compaction using out-of-place gathering (torch.gather / cudaMalloc):
- Auxiliary VRAM Spikes: Allocating secondary buffers ($O(N \cdot D)$) triggers transient OOM faults.
- Memory Bus Congestion: Double-buffering saturates HBM bandwidth with redundant read/write transactions.
- OS Allocation Latency Jitter: Dynamic
cudaMalloccalls introduce unpredictable stalls in real-time serving.
⚡ The Solution: In-Situ Idempotent Permutations
idempotent-kv reorganizes multi-dimensional tensors directly within existing memory allocations using $O(1)$ scalar hardware registers:
- Idempotent Invariant: Enforces $f(f(x)) = f(x)$, locking retained tokens into stabilized attractor basins $[0, K-1]$.
- In-Register 2-Cycle Fast-Path: Swaps mutually transposed tokens directly within thread registers.
- Bitmask-Free Cycle Follower: Identifies cycle leaders on-the-fly via strictly minimal index traversal.
- 100% Zero Auxiliary VRAM: Exactly 0.00 MB secondary memory allocated.
- Bit-Exact Numerical Parity: Zero approximation error ($\Delta = 0.000000$, 0 NaN).
📊 Benchmark: NVIDIA RTX PRO 500 Blackwell (sm_120)
Workload: Batch=1, Heads=32, SeqLen=8,192, HeadDim=128, Capacity=4,096 (50% Eviction, float16)
| Implementation | Latency (ms) | Peak Aux VRAM | VRAM Saved | Numerical Diff |
|---|---|---|---|---|
| PyTorch Out-of-Place | 3.137 ms | 96.00 MB | Baseline | 0.000000 |
idempotent-kv (Ours) |
2.016 ms | 0.00 MB | 96.00 MB (100%) | 0.000000 |
| Improvement | 1.56x Faster | 0.00 MB | 100% Eliminated | Bit-Exact |
📦 Installation
git clone https://github.com/aemre-cetin/idempotent-kv.git
cd idempotent-kv
pip install -e .
Requirements: torch >= 2.0.0, triton >= 2.1.0.
🛠️ Quickstart
import torch
from idempotent_kv import InplaceKVCompactor
compactor = InplaceKVCompactor()
# Key and Value caches [Batch, Heads, SeqLen, HeadDim] on GPU
key_cache = torch.randn((1, 32, 8192, 128), dtype=torch.float16, device="cuda")
value_cache = torch.randn((1, 32, 8192, 128), dtype=torch.float16, device="cuda")
# Top-k active token indices to retain
active_indices = torch.randperm(8192, device="cuda")[:4096]
# Build idempotent permutation map f(x)
target_map = compactor.build_idempotent_map(
batch=1, heads=32, seq_len=8192,
active_indices=active_indices, capacity=4096, device="cuda"
)
# In-place compaction: zero secondary global memory allocated
compacted_k, compacted_v = compactor.compact(
key_cache, value_cache, target_map, capacity=4096
)
# Returns key_cache[:, :, :4096, :] and value_cache[:, :, :4096, :]
vLLM Integration Hook
from idempotent_kv.integrations import VLLMInplaceCompactionHook
# Attach directly to vLLM attention worker
compaction_hook = VLLMInplaceCompactionHook(capacity=4096, protected_prefix_len=4)
# In-place context eviction during forward pass
compacted_k, compacted_v = compaction_hook(
key_cache, value_cache, attention_scores=cumulative_attention_weights
)
🚀 Official vLLM Upstreaming Proposal: Track the community RFC and upstreaming discussion at vLLM Issue #55463.
🛡️ Patent & Intellectual Property Notice
The mathematical formulations, state-transition architectures, and in-situ hardware compaction kernels implemented in this library are protected under pending patent application with the United States Patent and Trademark Office:
- U.S. Patent Application Number:
64/148,668 - Confirmation Number:
5890 - Status: PATENT PENDING
- First Named Inventor: Dr. Ahmet Emre ÇETİN
Academic evaluation, non-commercial research, and open-source collaboration are permitted under the terms of the Apache 2.0 License. Commercial deployment in proprietary hardware or commercial cloud runtimes is subject to licensing agreements with the author.
📜 Academic Citation
@article{cetin2026idempotentkv,
title={Zero-Copy In-Place Compaction and Idempotent Associative Routing of Dynamic Key-Value Cache Tensors in Deep Learning Accelerators},
author={Cetin, A. Emre},
journal={arXiv preprint},
year={2026},
note={U.S. Patent Application No. 64/148,679 (Confirmation No. 4824)}
}
@article{cetin2013idempotent,
title={Idempotent Permutations},
author={Cetin, A. E.},
journal={arXiv:1307.3877 [cs.DS]},
year={2013}
}
📄 License
Licensed under the Apache License, Version 2.0. Copyright © 2026 Dr. A. Emre ÇETİN. All Rights Reserved.
📄 Scientific Publication
The theoretical foundations, mathematical proofs, and hardware benchmarks on NVIDIA Blackwell (sm_120) are published in:
- Research Paper:
idempotent_kv_cache_paper.pdf - Patent Application: Protected under U.S. Patent Application No.:
64/148,679(Confirmation No. 4824, claiming priority under 35 U.S.C. § 119(e) to64/148,668). - Inventor: Dr. A. Emre ÇETİN (
aemre.cetin@gmail.com).
💼 Commercialization & Enterprise Licensing
Institutional investor pitch, enterprise ROI analysis, TAM/SAM/SOM market sizing, and multi-year commercialization roadmap are detailed in commercialization.md.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file idempotent_kv-0.2.1.tar.gz.
File metadata
- Download URL: idempotent_kv-0.2.1.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f40664a627060e2f171a34da1d1475fc29cb9d789e30f3ac1721e4e09f7c671a
|
|
| MD5 |
8d9406a15fbf10504c916cb6ef44a952
|
|
| BLAKE2b-256 |
67a898035d46501a6e7555037d3e400dd692895c5e6a626c65f462d6bdf28edf
|
File details
Details for the file idempotent_kv-0.2.1-py3-none-any.whl.
File metadata
- Download URL: idempotent_kv-0.2.1-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8eba49da48234e5282136a2414f374a102ffff3016104c398845cfeb0fbb5ec7
|
|
| MD5 |
817bb73ca341f7aa7e9b8f5b1fe6e808
|
|
| BLAKE2b-256 |
4414ce1da61e172cc5fc3f59e8decd71c7fc16ddcd6e2b637601b6d28e89c4ad
|