Manuscript Offprint

arXiv:2610.01230

HHR: Hierarchical Hash Retrieval for Efficient LLM Generation

Lianjun Liu, Tiantian Zheng, You Huang, Weiqi Yan, Mingte Qiu, Huazhong Liu, Xiaofeng Zhu, Yunshan Zhong

Abstract

Efficient long-context inference is essential for large language models (LLMs), yet it poses a severe computational bottleneck. Hash-based retrieval offers an efficient alternative by encoding queries and keys into binary codes and using Hamming distance for key selection. However, this leads to a critical mismatch between Hamming distance and attention relevance. Query-Key logits depend jointly on directional similarity and feature magnitudes, whereas hash binarization discards magnitude information, causing both false-positive retrieval of low-logit keys and false-negative omission of high-logit keys. To address these failures, we propose Hierarchical Hash Retrieval (HHR), a coarse-to-fine framework that progressively improves retrieval accuracy through Geometry-Aware Key Routing (GKR) and Learned Hash Projection (LHP). GKR learns a head-wise orthogonal transformation to redistribute feature magnitudes and derive more discriminative page-level logit bounds, enabling effective pruning of low-logit keys while preserving important candidates. LHP then learns a head-wise projection space that aligns Hamming distance with the true Query-Key relevance ranking for fine-grained retrieval. By combining GKR and LHP, HHR suppresses false positives and recovers false negatives, substantially improving the fidelity of hash-based sparse attention. Extensive experiments across diverse LLMs and benchmarks demonstrate that HHR achieves superior performance over existing methods. For example, on LongBench, HHR improves the average score by 1.10 points and, at a context length of 128K, achieves up to a 3.30×3.30\times decoding speedup and a 2.83×2.83\times end-to-end speedup for Llama-3.1-8B-Instruct. The code is publicly available at https://github.com/lianjun113-sudo/HHR.

1 Introduction

The ability to process long contexts has become a critical requirement for large language models (LLMs) [46, 48] in a wide range of real-world applications, including document understanding, retrieval-augmented generation, and multi-turn reasoning over extended dialogues [37, 41, 51]. However, the complexity of the self-attention mechanism with respect to sequence length imposes a severe computational bottleneck, making efficient long-context inference a pressing challenge [36, 39, 41]. To mitigate this, sparse attention methods have been extensively explored [10, 12, 42, 54]. These methods reduce the number of attended keys by selecting a small subset of important positions, thereby circumventing the need for full attention over the entire context [5].

Among the various sparse attention strategies [6, 31], hash-based retrieval has attracted considerable interest due to its adaptability and efficiency [4, 7]. By projecting queries and keys into a compact space and encoding them into binary codes, hash-based methods first adaptively extract the important keys based on Hamming distance, which can be executed using efficient XOR and POPCNT operations, and then

∗^{*}Corresponding author: yszhong01@gmail.com perform attention computation on these selected keys. For instance, MagicPIG [4] employs locality-sensitive hashing to select a small subset of candidate keys from the full KV cache. HATA [12] learns binary hash representations for queries and keys to approximate Query-Key similarity via Hamming distance.

Despite these gains, we uncover a critical mismatch in hash-based retrieval, where the magnitude information discarded for efficient hashing is essential for determining attention relevance. Specifically, Query-Key logits depend jointly on directional similarity and magnitudes, whereas hash binarization preserves only the signs of projected features and discards their magnitudes. As a result, Hamming distance deviates from the true Query-Key relevance, leading to inaccurate retrieval. As shown in Fig. 2, this mismatch gives rise to two complementary types of retrieval failure. First, directionally similar keys yield small Hamming distances to the query yet low logits due to their low magnitudes, becoming false positives that waste budget in Region III. Second, keys with large magnitudes yield high logits to the query but diverge directionally, producing large Hamming distances and becoming false negatives that miss important tokens in Region II.

To address both failure modes, as shown in Fig. 1, we propose Hierarchical Hash Retrieval (HHR), a novel coarse-to-fine framework that tackles false positives and false negatives. HHR comprises two complementary modules, including Geometry-Aware Key Routing (GKR) for coarse-grained pruning, and Learned Hash Projection (LHP) for fine-grained hash retrieval. In the first stage, GKR partitions keys into non-overlapping pages and estimates the maximum logit of each page using per-dimension extrema. Pages with bounds below a threshold are discarded to eliminate low-relevance keys early. To obtain more discriminative bounds while preserving exact logits, GKR learns a head-wise orthogonal transformation that redistributes feature magnitudes across dimensions. The resulting extrema tighten the bounds for irrelevant pages, effectively pruning Region III false positives without sacrificing important keys. In the second stage, LHP performs fine-grained hashing over the surviving candidates. Unlike conventional random projections, LHP learns a head-wise projection that aligns Hamming distance with the true relevance ranking. Differentiable sign relaxation enables gradient-based training, while orthogonality, bit-wise decorrelation, and balance regularization improve the quality of the binary space. Consequently, LHP substantially reduces Region II false negatives by assigning a more favourable hash space.

Extensive experiments across various LLMs and benchmarks demonstrate the effectiveness of the proposed HHR. For example, on LongBench, HHR achieves average scores of 46.96, 44.46, and 45.66 on Llama-3.1-8B-Instruct, Mistral-7B-Instruct, and Qwen3-4B, respectively, outperforming SOTA by 1.10, 2.82, and 2.69. At the same time, HHR achieves up to a 3.30×3.30\times decode speedup and a 2.83×2.83\times end-to-end speedup at a 128K context length on Llama-3.1-8B-Instruct.

References (52)