Skip to main content

RandKV

Deterministic, prompt-protected random KV-cache eviction for reasoning models.

CI

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.

Metadata

Release files for randkv 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for randkv 0.1.0
File Size Uploaded Provenance
randkv-0.1.0.tar.gz 18.8 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for randkv 0.1.0
File Interpreter ABI Platform
randkv-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.3 kB

Release files / randkv-0.1.0.tar.gz

Download URL randkv-0.1.0.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e08028485a14e71ac71cbfdaa3ef82062ff272713b161e6e57a7f703fd7e22a9
BLAKE2b-256 checksum
How to use checksums
41b5406cfd639cfb1f953f18e73d60339cc79e90feddbf1296d36a5d57438cd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Release files / randkv-0.1.0-py3-none-any.whl

Download URL randkv-0.1.0-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57a802a52591e0ca08e5d63eaf7957b2fd681a1397c893899471973323e56bf1
BLAKE2b-256 checksum
How to use checksums
43c8c5a0245b127bcd20fd8c2dafed821efa61104644c198b852935c86b3d04a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Security for randkv 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for randkv-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page