RandKV
Deterministic, prompt-protected random KV-cache eviction for reasoning models.
Status: v0.1 Hugging Face milestone. The framework-independent policy and a batch-size-one Transformers 5.16 cache adapter are implemented. vLLM, batched generation, and optimized kernels are not implemented; no throughput claim is made yet.
Policy quickstart
from randkv import RandomEvictionPolicy
policy = RandomEvictionPolicy()
retained = policy.select(range(5000), prompt_length=128)
The zero-config policy keeps the complete prompt, a recent 64-token buffer, and a deterministic random sample under a persistent 2,048-position budget.
Use an explicit configuration for experiments:
from randkv import RandKVConfig, RandomEvictionPolicy
policy = RandomEvictionPolicy(RandKVConfig(budget=1024, buffer_size=64, seed=42))
heads = policy.select_heads(
range(4096),
prompt_length=256,
num_kv_heads=8,
layer=0,
request_id="request-17",
eviction_index=0,
)
Every head receives an independent draw. Seed derivation includes the request, eviction, layer, and head identity, so concurrent callers do not share mutable random-number-generator state.
Hugging Face
pip install "randkv[transformers]"
Use the one-call generation adapter:
from transformers import AutoModelForCausalLM, AutoTokenizer
from randkv import RandKVConfig
from randkv.transformers import generate
model_id = "Qwen/Qwen3-4B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
inputs = tokenizer("Solve x^2 = 9.", return_tensors="pt").to(model.device)
output = generate(model, **inputs, max_new_tokens=4096)
For an explicit budget or cache inspection, construct the cache directly:
from randkv.transformers import RandKVCache
cache = RandKVCache(
model_config=model.config,
prompt_length=inputs.input_ids.shape[-1],
randkv_config=RandKVConfig(budget=2048),
)
output = model.generate(**inputs, past_key_values=cache, max_new_tokens=4096)
print(cache.stats())
The initial adapter intentionally supports batch size one, greedy/sampling generation, and full-attention decoder models only. Beam search and models with sliding, chunked, or linear-attention layers fail explicitly.
Run a real-checkpoint smoke test (downloads the model from Hugging Face):
.venv/bin/python scripts/smoke_transformers.py \
--model Qwen/Qwen3-0.6B \
--budget 512 \
--buffer-size 64 \
--max-new-tokens 640 \
--output-json results/qwen3-0.6b-smoke.json
The JSON result records exact PyTorch and Transformers versions, device, budget, buffer, seed, physical cache lengths, eviction counts, and throughput.
Here budget is the persistent budget K, not the instantaneous tensor size.
Immediately after eviction the cache contains K + r positions. Between
rounds it can grow to K + 2r - 1 before the next r-token buffer triggers
compaction.
Recorded smoke results live in results/. They validate
compatibility and eviction invariants; they are not comparative benchmarks.
Run the local dense-versus-RandKV microbenchmark:
.venv/bin/python scripts/benchmark_transformers.py \
--model Qwen/Qwen3-0.6B \
--budget 32 \
--buffer-size 8 \
--max-new-tokens 128 \
--trials 3 \
--output-json results/qwen3-0.6b-mps-microbenchmark.json
This measures single-request adapter overhead. It is not evidence for the paper's vLLM serving-throughput claim.
Test
The policy core has no runtime dependencies:
PYTHONPATH=src python -m unittest discover -s tests -v
Method
RandKV follows Random Attention: the original prompt is never evicted, while older generated positions are sampled uniformly and independently per KV head. A recent buffer is excluded from selection until the next eviction event.
RandKV is an independent packaging and integration project. It is not an official Salesforce project.
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 randkv-0.1.0.tar.gz.
File metadata
- Download URL: randkv-0.1.0.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e08028485a14e71ac71cbfdaa3ef82062ff272713b161e6e57a7f703fd7e22a9
|
|
| MD5 |
d507c984870db01b9e8dac71ac6aced5
|
|
| BLAKE2b-256 |
41b5406cfd639cfb1f953f18e73d60339cc79e90feddbf1296d36a5d57438cd8
|
Provenance
The following attestation bundles were made for randkv-0.1.0.tar.gz:
Publisher:
release.yml on DaBestCode/randkv
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
randkv-0.1.0.tar.gz -
Subject digest:
e08028485a14e71ac71cbfdaa3ef82062ff272713b161e6e57a7f703fd7e22a9 - Sigstore transparency entry: 2742246779
- Sigstore integration time:
-
Permalink:
DaBestCode/randkv@dc4be8e653ac1fa0f01739b8d3b11fe2e6e6a4d4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/DaBestCode
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@dc4be8e653ac1fa0f01739b8d3b11fe2e6e6a4d4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file randkv-0.1.0-py3-none-any.whl.
File metadata
- Download URL: randkv-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57a802a52591e0ca08e5d63eaf7957b2fd681a1397c893899471973323e56bf1
|
|
| MD5 |
c0902cceb909bc067a70d709fbd80085
|
|
| BLAKE2b-256 |
43c8c5a0245b127bcd20fd8c2dafed821efa61104644c198b852935c86b3d04a
|
Provenance
The following attestation bundles were made for randkv-0.1.0-py3-none-any.whl:
Publisher:
release.yml on DaBestCode/randkv
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
randkv-0.1.0-py3-none-any.whl -
Subject digest:
57a802a52591e0ca08e5d63eaf7957b2fd681a1397c893899471973323e56bf1 - Sigstore transparency entry: 2742246879
- Sigstore integration time:
-
Permalink:
DaBestCode/randkv@dc4be8e653ac1fa0f01739b8d3b11fe2e6e6a4d4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/DaBestCode
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@dc4be8e653ac1fa0f01739b8d3b11fe2e6e6a4d4 -
Trigger Event:
push
-
Statement type: