| Method | Context limit | Accuracy |
|---|---|---|
| Plain | 4k | 0.327 |
| Plain + HOMER | None | 0.358 |
| PI | 8k | 0.366 |
| NTK | 8k | 0.379 |
| YaRN | 8k | 0.310 |
| NTK + HOMER | None | 0.388 |
Large language models (LLMs) have shown remarkable performance in various natural language processing tasks. However, a primary constraint they face is the context limit, i.e., the maximum number of tokens they can process. Previous works have explored architectural changes and modifications in positional encoding to relax the constraint, but they often require expensive training or do not address the computational demands of self-attention. In this paper, we present Hierarchical cOntext MERging (HOMER), a new training-free scheme designed to overcome the limitations. HOMER uses a divide-and-conquer algorithm, dividing long inputs into manageable chunks. Each chunk is then processed collectively, employing a hierarchical strategy that merges adjacent chunks at progressive transformer layers. A token reduction technique precedes each merging, ensuring memory usage efficiency. We also propose an optimized computational order reducing the memory requirement to logarithmically scale with respect to input length, making it especially favorable for environments with tight memory restrictions. Our experiments demonstrate the proposed method's superior performance and memory efficiency, enabling the broader use of LLMs in contexts requiring extended context. Code is available at https://github.com/alinlab/HOMER.
HOMER consists of two steps, hierarchical merging of the intermediate hidden states (context embeddings) and propagative refinement of the lower-layer embeddings. The result is a compact, fixed-length embedding for each layer, which can be used in place of a standard kv-cache.
The long input is divided into uniform chunks, and each chunk is processed with ordinary self-attention in the early layers. The initial and concluding parts of the prompt are attached to every chunk as shared prefixes and suffixes, so that each chunk contains the instruction and the ending tokens.
In the intermediate layers, adjacent chunks are merged by concatenation. To keep the merged chunk short, each chunk is first shortened by pruning the tokens that receive the least attention from the final token of the chunk. Because simple attention-based pruning is biased toward tokens near the end, the attention logits are calibrated by subtracting a position-dependent bias logit. Reduction and merging are repeated until all chunks become a single chunk. The same position ids are reused across chunks without scaling.
Hierarchical merging leaves the upper-layer embeddings short and the lower-layer embeddings long. When a token is pruned in an upper layer, the corresponding tokens are also pruned in the lower layers, so the pruning decisions of the upper layers propagate down. This yields embeddings of the same length for every layer, which can directly replace the kv-cache for further generation.
Illustration of the propagative refinement process.
Viewing each chunk as a node, hierarchical merging is a traversal of a binary tree from the leaves to the root. Processing the chunks in depth-first order while applying propagative refinement reduces the memory requirement from linear to logarithmic in the input length.
We use Llama-2 as the base model, and compare against three training-free positional encoding scaling methods, Position Interpolation (PI), NTK-aware scaling, and YaRN. Their context limit is the original 4k limit multiplied by the scaling factor. Since HOMER can be applied on top of these methods, we also report it combined with the best-performing baseline for each task.
(a) Passkey retrieval accuracy.
(b) Peak memory usage.
(c) Average inference time.
(a) Passkey retrieval accuracy with Llama-2-7b-chat. (b) Memory required to process long inputs. (c) Average time to generate 100 tokens. Efficiency is measured on a single A100 GPU. Peak memory of the baselines at 64k is estimated, as they do not fit in a single A100 GPU.
The model is asked to retrieve a random number hidden inside distracting text. HOMER keeps an accuracy of around 80% up to 32k tokens, eight times the pre-trained context length, and applying HOMER on top of YaRN improves it further.
| Context | Llama-2-7b-chat | Llama-2-13b-chat | |||||||
|---|---|---|---|---|---|---|---|---|---|
| Method | limit | 4K | 8K | 16K | 32K | 4K | 8K | 16K | 32K |
| Plain | 4k | 1.000 | 0.000 | - | - | 1.000 | 0.000 | 0.000 | 0.000 |
| Plain + HOMER | None | 0.990 | 0.924 | 0.890 | 0.776 | 1.000 | 0.944 | 0.882 | 0.804 |
| PI | 8k | 0.432 | 0.356 | 0.000 | - | 0.600 | 0.544 | 0.000 | 0.000 |
| 16k | 0.006 | 0.006 | 0.006 | 0.000 | 0.022 | 0.028 | 0.018 | 0.000 | |
| NTK | 8k | 0.812 | 0.000 | 0.000 | 0.000 | 0.866 | 0.000 | 0.000 | 0.000 |
| 16k | 0.516 | 0.652 | 0.000 | 0.000 | 0.626 | 0.692 | 0.000 | 0.000 | |
| 32k | 0.106 | 0.194 | 0.162 | 0.000 | 0.286 | 0.570 | 0.442 | 0.000 | |
| YaRN | 8k | 0.996 | 0.002 | 0.000 | - | 1.000 | 0.464 | 0.000 | 0.000 |
| 16k | 0.844 | 0.756 | 0.000 | 0.000 | 0.980 | 0.952 | 0.214 | 0.000 | |
| 32k | 0.702 | 0.654 | 0.696 | 0.002 | 0.926 | 0.888 | 0.836 | 0.026 | |
| 64k | 0.678 | 0.358 | 0.148 | 0.026 | 0.902 | 0.826 | 0.364 | 0.224 | |
| YaRN + HOMER | None | 0.996 | 0.984 | 0.876 | 0.802 | 1.000 | 1.000 | 0.974 | 0.860 |
Passkey retrieval accuracy, averaged over 500 samples. The best values are in bold and the second-best are underlined. Empty values (-) indicate NaN.
On the QuALITY validation set, baselines receive documents clipped to their context limit, while HOMER receives the full document. HOMER improves accuracy by over 3% compared to plain Llama, and reaches 38.8% when applied on top of NTK, the best-performing positional encoding scaling method.
| Method | Context limit | Accuracy |
|---|---|---|
| Plain | 4k | 0.327 |
| Plain + HOMER | None | 0.358 |
| PI | 8k | 0.366 |
| NTK | 8k | 0.379 |
| YaRN | 8k | 0.310 |
| NTK + HOMER | None | 0.388 |
Accuracy on the QuALITY validation set.
We measure perplexity on 25 long documents from PG-19. Preceding contexts are compressed with HOMER, and the perplexity of the next segment is computed on top of the compressed context. HOMER keeps perplexity low on documents up to 64k tokens, and applying it on top of YaRN lowers perplexity further.
| Context | Llama-2-7b | Llama-2-13b | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Method | limit | 4K | 8K | 16K | 32K | 64K | 4K | 8K | 16K | 32K | 64K |
| Plain | 4k | 6.72 | - | - | - | - | 6.14 | >102 | >103 | >103 | >103 |
| Plain + HOMER | None | 6.72 | 7.29 | 7.78 | 8.43 | 9.64 | 6.13 | 6.60 | 6.87 | 7.13 | 7.59 |
| PI | 8k | 7.91 | 8.19 | - | - | - | 6.96 | 7.19 | >102 | >103 | >103 |
| 16k | >10 | >10 | >10 | - | - | >10 | >10 | >10 | >102 | >103 | |
| NTK | 8k | 6.97 | >10 | >102 | - | - | 6.26 | 9.62 | >102 | >103 | >103 |
| 16k | 7.59 | 7.95 | >10 | >102 | >103 | 6.76 | 7.05 | >10 | >103 | >103 | |
| 32k | 8.42 | 8.97 | 9.76 | >10 | >102 | 7.42 | 7.90 | 8.45 | >10 | >103 | |
| YaRN | 8k | 6.79 | 7.40 | - | - | - | 6.19 | 6.59 | >102 | >103 | >103 |
| 16k | 7.00 | 7.32 | 8.98 | - | - | 6.36 | 6.65 | 7.83 | >102 | >103 | |
| 32k | 7.50 | 8.05 | 8.78 | >10 | - | 6.65 | 7.05 | 7.40 | 8.85 | >102 | |
| 64k | 8.49 | >10 | >10 | >10 | >10 | 7.17 | 8.32 | >10 | >10 | >10 | |
| YaRN + HOMER | None | 6.79 | 7.09 | 7.52 | 7.95 | 8.83 | 6.19 | 6.51 | 6.78 | 7.02 | 7.44 |
Perplexity on 25 long documents from PG-19, truncated to the evaluation length. The best values are in bold and the second-best are underlined. Empty values (-) indicate NaN.
Perplexity on 25 long documents from PG-19, measured with Llama-2-7b.
We compare the token pruning criterion and the lower-layer refinement method with alternatives, using passkey retrieval on 16k contexts. Calibrated attention-based pruning and propagative refinement both perform best.
| Token pruning criterion | Accuracy |
|---|---|
| Random | 0.006 |
| Attention-based top-K | 0.056 |
| + calibration | 0.890 |
| Lower-layer refinement | Accuracy |
|---|---|
| No refinement | 0.002 |
| Random | 0.116 |
| Layer-wise top-K | 0.040 |
| Propagative refinement | 0.890 |
Passkey retrieval accuracy on 500 samples with 16k contexts.
HOMER reduces peak memory usage by over 70% when running inference on 64k inputs. The savings come from processing chunks separately in early layers, progressively reducing tokens, producing a compact kv-cache for generation, and the logarithmic-memory computation order.
| Peak GPU memory (GB) | |||||
|---|---|---|---|---|---|
| Setup | 4k | 8k | 16k | 32k | 64k |
| Baselines | 18.2 | 23.6 | 34.4 | 56.7 | >80 |
| HOMER | 16.3 (-10.8%) | 16.5 (-30.1%) | 16.7 (-51.4%) | 17.6 (-68.9%) | 21.3 (at least -73.4%) |
| + Baselines | 19.2 (+5.5%) | 20.1 (-14.6%) | 20.3 (-41.1%) | 20.8 (-63.4%) | 22.5 (at least -71.8%) |
Peak memory usage on a single A100 GPU with Flash Attention 2. A single value is reported for all baselines (plain Llama, PI, NTK, and YaRN), as they share the same memory requirement.
@inproceedings{song2024hierarchical,
author = {Song, Woomin and Oh, Seunghyuk and Mo, Sangwoo and Kim, Jaehyung and Yun, Sukmin and Ha, Jung-Woo and Shin, Jinwoo},
title = {Hierarchical Context Merging: Better Long Context Understanding for Pre-trained {LLM}s},
booktitle = {International Conference on Learning Representations},
pages = {1589--1607},
year = {2024},
}