- Python 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| cross_model_kv | ||
| examples | ||
| results | ||
| tests | ||
| tools | ||
| .gitignore | ||
| LICENSE | ||
| pyproject.toml | ||
| README.md | ||
vllm-cross-model-kv
KV cache transfer between two different models in vLLM. A small model computes the prefill, a bigger model of the same family answers from that cache through a learned linear mapping, so the big model skips most of its own prefill.
This is an independent implementation of NVIDIA's paper
Cross-Model KV Cache Transfer in LLM Families: A Closed-Form Linear Mapping
for Prefill Reuse inside the real vLLM
serving path. The authors did not publish code, so everything here is built
from the paper text against vLLM v0.26.0.
It runs on stock vLLM. No fork: the whole thing is a standard V1 KV connector
loaded through kv_connector_module_path, plus offline tools to fit the
mapper.
Measured on two RTX 3090s (PCIe 3.0, no NVLink), Qwen3-0.6B source and Qwen3-1.7B target, BF16:
- held-out R² of the mapping: K 0.760, V 0.625 (paper-scale calibration, 500 sequences, k=8)
- HellaSwag-1000 retention 0.940, WikiText-2 tail-PPL ratio 1.100
- 32K context: warm TTFT 1645 ms against 3657 ms full prefill (2.22x); the mapper itself takes 626 ms where re-prefill takes 3657 ms (5.8x)
- mapping quality gets better with length: tail-PPL ratio 1.090 on 1K chunks, 1.0022 on 32K chunks
- 0 import failures across every eval; any load or mapping error fails closed into a normal recompute
How it works
client
|
v
source vLLM :18110, GPU0, Qwen3-0.6B target vLLM :18120, GPU1, Qwen3-1.7B
computes prompt KV, CrossModelKVConnector:
exports block-aligned prefix ----> load canonical KV
to shared storage (/dev/shm) inverse source RoPE on K
ridge mapping K and V
target RoPE on K
scatter into paged blocks
recompute the last <=16 tokens
normal decode
The paper's claim is that KV caches of models inside one family are close to linearly related. For every target layer you pick the top-k most predictive source layers, strip RoPE from K so the regression works in content space, and fit one ridge regression per target layer (lambda 0.01, closed form, no gradient training). V has no positional rotation and maps directly. The fit takes about 15 minutes for the 0.6B to 1.7B pair on two 3090s, with the fp64 covariance accumulation and eval GEMMs on GPU.
The connector handles the serving side: block-aligned safe prefix (the target always recomputes at least the last token, so it produces its own logits), manifests with model/tokenizer/mapper hashes, atomic publication, and an event log. Any mismatch or damaged file is a visible miss, never a silent decode on a broken cache.
Requirements
- vLLM
0.26.0(pinned; the V1 connector API is experimental and moves) - a model pair with the same tokenizer and matched KV geometry: same
num_key_value_heads, samehead_dim. Qwen3-0.6B and Qwen3-1.7B is such a pair (8 KV heads, head_dim 128, 28 layers both) - two GPUs, one per model. Nothing here needs NVLink or P2P
Install
pip install vllm==0.26.0
git clone https://github.com/alesha-pro/vllm-cross-model-kv
cd vllm-cross-model-kv
pip install -e ".[tools]"
Quick start
Serve the identity pair first (same model both sides, passthrough mapper). This proves the transfer path on your machine before any cross-model math:
python tools/make_passthrough_mapper.py \
--model Qwen/Qwen3-0.6B \
--out mappers/identity-qwen3-0.6b
./examples/serve_pair.sh identity
python examples/transfer_gate.py \
--tokenizer Qwen/Qwen3-0.6B \
--control-url http://127.0.0.1:18130/v1 \
--target-event-log /tmp/cross-kv-logs/target-events.jsonl \
--expect-identical
Then the real pair with a fitted mapper. Both mappers from the runs below are on Hugging Face, so you can skip the fit: anonymousmaharaj/vllm-cross-model-kv
./examples/serve_pair.sh cross mappers/ridge-qwen3-0.6b-to-1.7b-k8-n500
The consumer side is configured like any vLLM KV connector:
{"kv_connector": "CrossModelKVConnector",
"kv_connector_module_path": "cross_model_kv.connector",
"kv_role": "kv_consumer",
"kv_load_failure_policy": "recompute",
"kv_connector_extra_config": {
"storage_path": "/dev/shm/cross-model-kv",
"mapper_path": "mappers/ridge-qwen3-0.6b-to-1.7b-k8-n500",
"source_model_path": "Qwen/Qwen3-0.6B",
"event_log_path": "/tmp/cross-kv-logs/target-events.jsonl"}}
Fit your own mapper
The full offline pipeline, FineWeb-Edu calibration like the paper (1024-token sequences, stride 4):
# 1. pin calibration sequences and splits
python tools/prepare_calibration.py --model Qwen/Qwen3-0.6B --out work/calibration
# 2. run both servers in export mode, dump KV for the same tokens
./examples/serve_pair.sh export
python tools/export_calibration.py --sequences work/calibration/sequences.jsonl ...
# 3. single-layer probes -> top-k source layers per target layer
python tools/select_layers.py --sequences work/calibration/sequences.jsonl ...
# 4. ridge fit with held-out gates, fp32 + bf16 artifacts
python tools/fit_ridge.py --source-model Qwen/Qwen3-0.6B \
--target-model Qwen/Qwen3-1.7B --fit-device cuda \
--out mappers/ridge-qwen3-0.6b-to-1.7b-k8-n500
Every script writes a manifest (dataset revision, sample IDs, seeds, model revisions, code state), so a mapper artifact is reproducible from its manifest alone.
Results
Full raw data lives in results/: fit reports, quality reports, latency runs with exact configs, the layer-selection heatmap.
Held-out R² across the calibration sweep, all cells past the gates (K >= 0.50, V >= 0.30):
| sequences | k | R²_K | R²_V |
|---|---|---|---|
| 200 | 1 | 0.724 | 0.575 |
| 200 | 4 | 0.731 | 0.574 |
| 200 | 8 | 0.704 | 0.524 |
| 500 | 1 | 0.730 | 0.588 |
| 500 | 4 | 0.757 | 0.621 |
| 500 | 8 | 0.760 | 0.625 |
Two things I did not expect. At 200 sequences k=8 loses to k=4 (8192 features against 30K observations, plain overfitting), and only wins at the paper's 500-sequence scale. So the paper's k=8 choice quietly assumes paper-scale calibration data. And k=1 is almost enough: a single source layer per target layer already gives 0.73/0.59, because the layer correspondence between the two models is nearly diagonal.
Quality with the final k=8 n500 mapper, serving path, identical tokens both legs: HellaSwag-1000 acc_norm retention 0.940 (gate 0.85), WikiText-2 tail-PPL ratio 1.100 (gate 1.25), zero fallbacks in the event log.
TTFT p50, greedy, both GPUs at 350 W, k=8 n500 mapper:
| context | full prefill | warm mapped | mapper alone |
|---|---|---|---|
| 8K | 526 ms | 458 ms (1.15x) | 179 ms |
| 32K | 3657 ms | 1645 ms (2.22x) | 626 ms |
Cold cascade at 32K (source prefill + transfer + mapping + tail, nothing warm) is 4402 ms against 6373 ms for prefilling both models from scratch (1.45x). Under concurrency 8 at 8K the warm path holds 2393 ms p50 per request against 3928 ms for full prefill at near-equal throughput, with zero import failures.
The fixed transfer overhead is 30-50 ms, so short prompts lose: at 64 tokens full prefill wins easily. The crossover on this hardware is between 1K and 8K, and the win grows with length because mapping cost is linear in tokens while prefill is superlinear.
What this does and does not claim
The paper reports 97.6% retention and 17-25x mapper-vs-reprefill on Qwen3 14B to 32B, measured on an 8x H100 NVLink node inside one research harness. Those numbers do not transfer here and I did not try to match them. This repo shows the mechanism holds end-to-end in a real serving stack on consumer PCIe hardware: a different pair, real paged KV, real process boundary, real failure paths, TTFT measured at the API.
Scope limits, honestly stated:
- one family, same tokenizer, matched KV geometry. This is not a universal cross-model cache
- small-to-large direction only, prompt-only handoff
- quality gates are HellaSwag plus prefix-conditioned WikiText-2 PPL, not a full eval suite
Repo layout
cross_model_kv/ the connector package (scheduler side, worker side, ridge
mapper, RoPE, canonical layout, manifests, storage, events)
tools/ offline pipeline: calibration, export, layer selection,
ridge fit, quality eval, service benchmarks
examples/ serve_pair.sh plus transfer and failure gates
tests/ CPU unit tests (layout, RoPE round-trip, ridge recovery,
manifests, calibration export)
results/ raw reports from the 2x3090 runs behind every number above
License
Apache-2.0. Not affiliated with NVIDIA or the vLLM project. Paper: arXiv:2608.03893.