Hierarchical Context Merging: Better Long Context Understanding for Pre-trained LLMs

1KAIST, 2University of Michigan, 3Carnegie Mellon University, 4Hanyang University ERICA, 5NAVER
*Equal contribution   †Work done at KAIST   ‡Work done at Mohamed bin Zayed University of Artificial Intelligence
ICLR 2024
Overview of HOMER. A long context is split into chunks that are processed independently in early transformer layers, then reduced and merged with adjacent chunks at intermediate layers until a single chunk remains. Propagative refinement then gathers lower-layer embeddings according to the top-layer token indices, producing fixed-length layer-wise embeddings.

HOMER extends the context limit of pre-trained LLMs without training, by hierarchically merging chunks of a long input across transformer layers.

Abstract

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.

Method

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.

Hierarchical Merging of Context Embeddings

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.

Propagative Refinement

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 propagative refinement: tokens pruned at the top layer (b) are removed from every lower layer, leaving the same tokens 1, 4, and 6 at all layers (c).

Illustration of the propagative refinement process.

Computation Order for Memory-Limited Environments

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.

Results

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.

Passkey retrieval accuracy versus context length for Llama-2-7b-chat with HOMER and baselines.

(a) Passkey retrieval accuracy.

Peak GPU memory usage versus context length for HOMER and baselines.

(b) Peak memory usage.

Average inference time for generating 100 tokens versus context length for HOMER and baselines.

(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.

Passkey Retrieval

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.

ContextLlama-2-7b-chatLlama-2-13b-chat
Methodlimit4K8K16K32K4K8K16K32K
Plain4k1.0000.000--1.0000.0000.0000.000
Plain + HOMERNone0.9900.9240.8900.7761.0000.9440.8820.804
PI8k0.4320.3560.000-0.6000.5440.0000.000
16k0.0060.0060.0060.0000.0220.0280.0180.000
NTK8k0.8120.0000.0000.0000.8660.0000.0000.000
16k0.5160.6520.0000.0000.6260.6920.0000.000
32k0.1060.1940.1620.0000.2860.5700.4420.000
YaRN8k0.9960.0020.000-1.0000.4640.0000.000
16k0.8440.7560.0000.0000.9800.9520.2140.000
32k0.7020.6540.6960.0020.9260.8880.8360.026
64k0.6780.3580.1480.0260.9020.8260.3640.224
YaRN + HOMERNone0.9960.9840.8760.8021.0001.0000.9740.860

Passkey retrieval accuracy, averaged over 500 samples. The best values are in bold and the second-best are underlined. Empty values (-) indicate NaN.

Question Answering

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.

MethodContext limitAccuracy
Plain4k0.327
Plain + HOMERNone0.358
PI8k0.366
NTK8k0.379
YaRN8k0.310
NTK + HOMERNone0.388

Accuracy on the QuALITY validation set.

Language Modeling

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.

ContextLlama-2-7bLlama-2-13b
Methodlimit4K8K16K32K64K4K8K16K32K64K
Plain4k6.72----6.14>102>103>103>103
Plain + HOMERNone6.727.297.788.439.646.136.606.877.137.59
PI8k7.918.19---6.967.19>102>103>103
16k>10>10>10-->10>10>10>102>103
NTK8k6.97>10>102--6.269.62>102>103>103
16k7.597.95>10>102>1036.767.05>10>103>103
32k8.428.979.76>10>1027.427.908.45>10>103
YaRN8k6.797.40---6.196.59>102>103>103
16k7.007.328.98--6.366.657.83>102>103
32k7.508.058.78>10-6.657.057.408.85>102
64k8.49>10>10>10>107.178.32>10>10>10
YaRN + HOMERNone6.797.097.527.958.836.196.516.787.027.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 versus context length on 25 PG-19 documents with Llama-2-7b, for HOMER and baselines, up to 64K tokens.

Perplexity on 25 long documents from PG-19, measured with Llama-2-7b.

Ablation

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 criterionAccuracy
Random0.006
Attention-based top-K0.056
  + calibration0.890
Lower-layer refinementAccuracy
No refinement0.002
Random0.116
Layer-wise top-K0.040
Propagative refinement0.890

Passkey retrieval accuracy on 500 samples with 16k contexts.

Memory Efficiency

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)
Setup4k8k16k32k64k
Baselines18.223.634.456.7>80
HOMER16.3 (-10.8%)16.5 (-30.1%)16.7 (-51.4%)17.6 (-68.9%)21.3 (at least -73.4%)
  + Baselines19.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.

BibTeX

@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},
}