| Type | Dim. | Top-1 | Top-2 | Top-3 | Avg. |
|---|---|---|---|---|---|
| Attention | 160 | 6.91 | 7.70 | 7.81 | 7.47 |
| Cosine-HS | 5120 | 9.40 | 9.63 | 9.80 | 9.61 |
| Cosine-Q | 160 | 6.48 | 6.74 | 6.93 | 6.72 |
| Cosine-K | 160 | 6.77 | 7.31 | 7.41 | 7.16 |
| Cosine-V | 160 | 5.77 | 6.57 | 6.57 | 6.30 |
As large language models increasingly gain popularity in real-world applications, processing extremely long contexts, often exceeding the model's pre-trained context limits, has emerged as a critical challenge. While existing approaches to efficient long-context processing show promise, recurrent compression-based methods struggle with information preservation, whereas random access approaches require substantial memory resources. We introduce REFORM, a novel inference framework that efficiently handles long contexts through a two-phase approach. First, it incrementally processes input chunks while maintaining a compressed KV cache, constructs cross-layer context embeddings, and utilizes early exit strategy for improved efficiency. Second, it identifies and gathers essential tokens via similarity matching and selectively recomputes the KV cache. Compared to baselines, REFORM achieves over 52% and 34% performance gains on RULER and BABILong respectively at 1M context length. It also outperforms baselines on ∞-Bench, RepoEval, and MM-NIAH, demonstrating flexibility across diverse tasks and domains. Additionally, REFORM reduces inference time by 30% and peak memory usage by 5%, achieving both efficiency and superior performance.
REFORM (REcurrent chunked Forwarding with On-demand cache RecoMputation) processes a long input in two phases. The first phase reads the input with a compressed, fixed-size KV cache and uses it only to build token-level context embeddings. The second phase uses these embeddings to find the parts of the input that matter for the query, and recomputes a full KV cache for just those tokens.
The input is split into chunks (32k tokens in our experiments). Each chunk is forwarded conditioned on the previous KV cache, and the cache is then compressed by attention-based token eviction following H2O. Position IDs are reassigned so that the tokens in the compressed cache are consecutive, which lets the model handle inputs longer than its pre-trained context limit.
Unlike approaches that generate directly from the compressed cache, REFORM uses it only to construct context embeddings. Since the best embeddings often come from lower layers, the forward pass can stop after the topmost layer used for embedding extraction. This early exit saves computation and also removes the need to keep the KV cache for the upper layers.
To choose the embeddings, we compare the token-level retrieval performance of attention scores, cosine similarity between hidden states, and cosine similarity between attention QKV states, measured by mean normalized rank (MNR, lower is better) on multi-hop QA. With carefully selected heads, cosine similarity on QKV states outperforms both attention scores and hidden states, while using far fewer dimensions than hidden states.
| Type | Dim. | Top-1 | Top-2 | Top-3 | Avg. |
|---|---|---|---|---|---|
| Attention | 160 | 6.91 | 7.70 | 7.81 | 7.47 |
| Cosine-HS | 5120 | 9.40 | 9.63 | 9.80 | 9.61 |
| Cosine-Q | 160 | 6.48 | 6.74 | 6.93 | 6.72 |
| Cosine-K | 160 | 6.77 | 7.31 | 7.41 | 7.16 |
| Cosine-V | 160 | 5.77 | 6.57 | 6.57 | 6.30 |
Best-3 MNR scores (lower is better) for different similarity search methods, measured with Mistral-Nemo-Instruct-2407 and averaged over 500 multi-hop QA examples.
MNR scores (lower is better) of value heads across layers, Mistral-Nemo-Instruct-2407.
The best-performing heads are not necessarily in the final layers, which motivates the early exit strategy. We also select heads on a synthetic key-value retrieval task to capture more complex input patterns. Head selection uses short synthetic data (8k tokens), but the benefit carries over to inputs with millions of tokens. The selected heads' embeddings are normalized and concatenated, so a single cosine similarity search equals averaging the per-head similarity scores.
After encoding, REFORM runs a token-level similarity search between the query (the last part of the input) and the rest of the input. Scores are max-pooled over the query tokens and then over 128 adjacent tokens, to keep the selected segments contiguous. The highest-scoring tokens are gathered, together with the initial and final 256 tokens, and forwarded through the model again to recompute the KV cache used for generation. This gives random access to earlier context without storing the full KV cache.
We compare against StreamingLLM, TOVA, H2O, InfiniPot, and InfLLM, as well as a truncation baseline that drops the middle of the input. Recurrent baselines use a 32k KV cache budget and chunk size, and InfLLM uses a 32k active KV cache budget. REFORM uses a recomputation budget of 8k tokens for Mistral-NeMo-Instruct-2407 and 16k tokens for the other models.
A needle sentence is placed at different depths of a long collection of Paul Graham essays, and the model is asked to retrieve it. With Qwen2.5-7B-Instruct, REFORM answers correctly at every depth and context length up to 1M tokens.
Retrieval accuracy of Qwen2.5-7B-Instruct, averaged over 20 samples.
RULER contains harder needle-in-a-haystack variants as well as aggregation and question answering tasks, and BABILong adds tasks such as multi-hop reasoning. We extend RULER beyond its original 128k limit to 1M tokens using the same recipe. REFORM outperforms the baselines by a large margin on both benchmarks.
| RULER | BABILong | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Method | 64k | 128k | 200k | 300k | 400k | 500k | 1M | 64k | 128k | 256k | 512k | 1M | |
| Mistral-Nemo-Instruct-2407 | |||||||||||||
| Truncation | 32.6 | 20.4 | 17.8 | 15.2 | 12.3 | 12.5 | 10.8 | 32.2 | 26.2 | 17.0 | 13.6 | 14.0 | |
| StreamingLLM | 27.6 | 13.8 | 11.6 | 9.3 | 7.2 | 7.1 | 4.7 | 38.8 | 23.4 | 15.4 | 11.0 | 6.2 | |
| TOVA | 21.6 | 15.3 | 14.0 | 11.8 | 7.9 | 8.7 | 4.6 | 37.8 | 23.6 | 14.4 | 9.6 | 3.4 | |
| H2O | 15.1 | 7.4 | 7.8 | 5.6 | 4.2 | 5.7 | 3.6 | 38.0 | 25.2 | 16.2 | 7.2 | 3.6 | |
| InfiniPot | 26.9 | 19.4 | 15.6 | 14.5 | 12.7 | 13.4 | 12.0 | 39.6 | 26.8 | 18.6 | 11.2 | 8.8 | |
| InfLLM | 52.7 | 39.7 | 28.5 | 24.9 | 20.9 | 22.0 | 23.3 | 40.6 | 34.0 | 23.6 | 13.0 | 9.6 | |
| REFORM (Ours) | 79.9 | 81.1 | 83.0 | 84.6 | 84.1 | 83.5 | 75.5 | 57.4 | 51.4 | 50.6 | 47.6 | 48.8 | |
| Qwen2.5-7B-Instruct | |||||||||||||
| Truncation | 46.3 | 25.1 | 21.8 | 17.4 | 14.9 | 15.2 | 11.3 | 48.4 | 33.4 | 27.4 | 20.0 | 15.6 | |
| StreamingLLM | 43.5 | 25.3 | 18.7 | 17.3 | 11.8 | 11.8 | 9.1 | 53.4 | 40.6 | 33.2 | 23.8 | 19.6 | |
| TOVA | 66.2 | 27.7 | 25.7 | 25.8 | 21.9 | 20.4 | 17.0 | 56.0 | 46.6 | 40.6 | 29.4 | 21.8 | |
| H2O | 51.8 | 20.9 | 18.5 | 17.1 | 11.6 | 12.1 | 8.7 | 57.0 | 41.6 | 36.4 | 24.6 | 18.8 | |
| InfiniPot | 65.7 | 51.7 | 39.2 | 33.9 | 27.8 | 26.7 | 23.7 | 59.6 | 51.0 | 53.4 | 48.2 | 40.2 | |
| InfLLM | 47.1 | 34.2 | 29.2 | 24.0 | 22.0 | 23.2 | 23.8 | 43.0 | 29.2 | 20.4 | 15.4 | 11.4 | |
| REFORM (Ours) | 78.2 | 75.8 | 74.7 | 74.9 | 74.9 | 73.0 | 75.1 | 61.6 | 60.4 | 59.8 | 58.8 | 58.8 | |
Average performance over all tasks at each context length.
∞-Bench includes more realistic tasks built from long books and dialogues. REFORM achieves the best average score with both models.
| Method | R.PK | R.Num | R.KV | En.Sum | En.QA | En.MC | En.Dia | Zh.QA | C.Debug | M.Find | Avg. |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Mistral-Nemo-Instruct-2407 | |||||||||||
| Truncation | 27.1 | 21.4 | 3.6 | 13.7 | 16.0 | 51.1 | 11.5 | 25.2 | 28.9 | 20.3 | 21.9 |
| StreamingLLM | 28.1 | 15.3 | 0.0 | 12.5 | 12.6 | 45.9 | 6.5 | 19.2 | 27.2 | 0.0 | 16.7 |
| TOVA | 82.2 | 47.0 | 0.0 | 12.3 | 13.8 | 47.2 | 8.0 | 6.6 | 25.1 | 4.6 | 24.7 |
| H2O | 31.5 | 9.5 | 0.0 | 14.2 | 17.6 | 49.3 | 6.0 | 21.2 | 26.1 | 15.7 | 19.1 |
| InfiniPot | 84.1 | 13.6 | 0.0 | 11.9 | 17.1 | 52.0 | 7.0 | 11.3 | 27.4 | 15.7 | 24.0 |
| InfLLM | 100.0 | 100.0 | 1.0 | 16.9 | 17.4 | 58.1 | 7.0 | 24.5 | 24.1 | 27.1 | 37.6 |
| REFORM (Ours) | 100.0 | 100.0 | 88.2 | 18.2 | 18.0 | 70.3 | 18.5 | 26.7 | 25.9 | 36.0 | 50.2 |
| Qwen2.5-7B-Instruct | |||||||||||
| Truncation | 27.1 | 27.1 | 7.4 | 29.0 | 13.3 | 43.2 | 15.0 | 9.34 | 37.1 | 45.7 | 25.4 |
| StreamingLLM | 28.8 | 28.8 | 6.0 | 29.2 | 8.6 | 52.4 | 14.5 | 9.51 | 32.5 | 28.6 | 23.9 |
| TOVA | 100.0 | 100.0 | 1.2 | 29.4 | 8.6 | 56.8 | 15.0 | 10.65 | 34.3 | 42.6 | 39.8 |
| H2O | 93.1 | 85.4 | 0.0 | 31.0 | 11.0 | 56.3 | 15.5 | 11.97 | 34.8 | 44.6 | 38.4 |
| InfiniPot | 100.0 | 99.8 | 0.8 | 30.6 | 11.3 | 59.0 | 17.0 | 9.99 | 36.6 | 44.9 | 41.0 |
| InfLLM | 100.0 | 99.8 | 1.6 | 27.6 | 9.6 | 38.0 | 12.0 | 10.41 | 29.7 | 45.1 | 37.4 |
| REFORM (Ours) | 100.0 | 100.0 | 32.8 | 27.8 | 16.5 | 61.6 | 21.5 | 11.81 | 33.0 | 21.7 | 42.7 |
Results on 10 datasets from ∞-Bench. C.Run and M.Calc are omitted since no method achieved a nonzero score with these models.
On RepoEval, a repository-level code completion benchmark, the entire repository except the target file is given as context to Qwen2.5-Coder-1.5B/7B-Instruct. On the multi-modal benchmark MM-NIAH, we apply REFORM to Pixtral-12B-2409 without any modification. REFORM gives the best results on both.
| RepoEval | MM-NIAH | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Method | 1.5B API | 1.5B Line | 7B API | 7B Line | Retrieval | Counting | Reasoning | Avg. | |
| Truncate | 54.8 | 63.9 | 59.2 | 59.5 | 72.2 | 18.7 | 51.2 | 47.4 | |
| StreamingLLM | 55.0 | 62.7 | 59.9 | 58.4 | 71.9 | 17.8 | 49.8 | 46.5 | |
| TOVA | 54.7 | 62.2 | 59.7 | 59.8 | 82.9 | 18.8 | 54.1 | 52.0 | |
| H2O | 55.1 | 63.4 | 61.2 | 59.6 | 83.3 | 18.9 | 53.5 | 51.9 | |
| InfiniPot | 59.4 | 68.4 | 66.2 | 63.8 | 85.4 | 18.8 | 54.7 | 53.0 | |
| InfLLM | 61.8 | 66.8 | 64.3 | 66.3 | N/A | N/A | N/A | N/A | |
| REFORM (Ours) | 65.3 | 72.4 | 68.7 | 69.4 | 89.2 | 22.0 | 61.3 | 57.5 | |
RepoEval reports edit similarity on API-level and line-level completion. MM-NIAH reports performance normalized across input lengths. InfLLM is not evaluated on MM-NIAH since its implementation only supports text-based models.
We compare against RAG with sparse and dense retrievers on the needle-in-a-haystack tasks from RULER at 300k tokens. REFORM's retrieval embeddings are conditioned on the entire input and need no external retriever. Combining REFORM's scores with a dense retriever (25% dense, 75% REFORM) improves results further.
| Method | Single | M.Key | M.Value | M.Query |
|---|---|---|---|---|
| Sparse RAG | 86.7 | 77.3 | 88.5 | 90.0 |
| Dense RAG | 87.3 | 57.3 | 82.5 | 78.0 |
| REFORM | 99.3 | 93.3 | 98.5 | 100.0 |
| + RAG | 99.3 | 94.7 | 99.0 | 100.0 |
Mistral-NeMo-Instruct-2407, RULER at 300k.
Replacing H2O with StreamingLLM or TOVA as the compression method gives comparable results. Random heads lower performance, and the worst heads cause a large drop. Smaller max-pooling kernels (5 or 17 tokens, compared with 129) also degrade performance. REFORM is faster and uses less memory than the recurrent baselines, since early exit removes computation and KV cache for the upper layers.
| Method | RULER | BABILong |
|---|---|---|
| REFORM (Ours) | 84.6 | 47.6 |
| w/ StreamingLLM | 82.7 | 44.6 |
| w/ TOVA | 81.4 | 46.8 |
| w/ Random heads | 80.3 | 43.0 |
| w/ Worst heads | 44.7 | 22.8 |
| w/ Kernel size 5 | 18.4 | 36.8 |
| w/ Kernel size 17 | 39.4 | 45.8 |
| Method | Time (s) | Memory (GB) |
|---|---|---|
| StreamingLLM | 36.58 | 37.34 |
| H2O | 41.33 | 37.85 |
| TOVA | 39.46 | 37.06 |
| InfiniPot | 40.90 | 37.06 |
| InfLLM | 129.14 | 51.62 |
| REFORM (Ours) | 27.24 | 35.00 |
Mistral-NeMo-Instruct-2407. (Left) Ablation on RULER 300k and BABILong 512k. (Right) Inference time and peak memory for generating 10 tokens from 256k inputs on a single H100, averaged over 10 samples.
@inproceedings{song2025reform,
author = {Song, Woomin and Jayanthi, Sai Muralidhar and Ronanki, Srikanth and Mysore Sathyendra, Kanthashree and Shin, Jinwoo and Galstyan, Aram and Katiyar, Shubham and Bodapati, Sravan Babu},
title = {Compress, Gather, and Recompute: {REFORM}ing Long-Context Processing in Transformers},
booktitle = {Advances in Neural Information Processing Systems},
year = {2025},
}