HATA: Trainable and Hardware-Efficient Hash-Aware Top-k Attention for Scalable Large Model Inference

Ping Gong, Jiawei Yi, Shengnan Wang, Juncheng Zhang, Zewen Jin, Ouxiang Zhou, Ruibo Liu, Guanbin Xu, Youhui Bai, Bowen Ye, Kun Yuan, Tong Yang, Gong Zhang, Renhai Chen, Feng Wu, Cheng Li

Abstract

The "memory wall" remains the primary bottleneck in scaling Large Language Model (LLM) inference, particularly for long-context applications. As sequence lengths grow, the overhead of loading the Key-Value (KV) cache from memory for every generated token becomes prohibitive. We introduce HATA (Hash-Aware Top-k Attention), a hardware-efficient framework that accelerates inference by reframing token selection as a lightweight binary hashing problem. By training head-specific hash functions with an ordinal loss, HATA preserves the relative importance of tokens while enabling the use of fast bitwise operations. Our results demonstrate that HATA achieves up to a 7.2× speedup over vanilla attention implementations with negligible accuracy loss across diverse long-context benchmarks.

1. Introduction

In the current landscape of LLM deployment, autoregressive generation is memory-bound rather than compute-bound. Each new token requires the model to attend to all previous tokens, necessitating the storage and retrieval of a massive KV cache. For long sequences, loading this cache from High Bandwidth Memory (HBM) to the GPU's SRAM accounts for over 70% of total runtime.

While Top-$k$ attention—selecting only a subset of the most relevant tokens—offers a theoretical solution, identifying these tokens often requires computing the very attention scores the method seeks to avoid. Existing approximations, such as low-rank projections or block-wise heuristics, frequently suffer from high computational overhead or significant accuracy degradation. HATA addresses this by utilizing a "Learning-to-Hash" approach optimized for modern GPU hardware.

2. The HATA Framework

HATA's core innovation lies in its ability to perform token selection using binary codes and Hamming distances, which are significantly faster to compute than floating-point dot products.

2.1 Trainable Hashing and Ordinal Loss

Unlike traditional Locality Sensitive Hashing (LSH) which uses random projections, HATA employs a trainable weight matrix ($W_H$) for each attention head. This allows the model to learn the specific semantic patterns relevant to different heads. The training process utilizes a specialized ordinal loss function. Instead of performing numerical regression to predict exact attention scores, the loss function ensures that tokens with higher attention scores in the original model result in binary codes with smaller Hamming distances. This focus on ranking rather than magnitude is sufficient for Top-$k$ selection and simplifies the learning objective.

2.2 Hardware-Efficient Inference

To translate theoretical gains into wall-clock speedups, HATA incorporates several hardware-level optimizations:

  • Hamming Score Operator: The system uses native GPU instructions (XOR and popcount) to calculate distances between 128-bit binary codes.
  • Kernel Fusion: Hash encoding, cache updates, and token selection are fused into single CUDA kernels, minimizing the overhead associated with multiple kernel launches.
  • FlashAttention Integration: The sparse "gather" operation required to fetch the top-$k$ tokens is integrated directly into the FlashAttention workflow, optimizing data movement between HBM and SRAM.

3. Experimental Evaluation

We evaluated HATA across a comprehensive suite of long-context benchmarks designed to test retrieval, summarization, and reasoning:

  • RULER: Tests retrieval and aggregation over contexts up to 128K tokens.
  • InfiniteBench: Evaluates ultra-long context capabilities with average lengths of ~214K tokens.
  • LongBench-e & LongBench-v2: Multi-task benchmarks covering QA and code understanding.
  • Needle-in-a-Haystack: A standard test for fact retrieval within massive documents.

4. Results and Analysis

HATA demonstrates superior performance in both speed and accuracy compared to state-of-the-art (SOTA) methods like Loki and Quest.

4.1 Speed and Efficiency

HATA achieves a peak speedup of 7.2× compared to the standard vLLM full attention implementation. Ablation studies indicate that hardware-specific optimizations (Score, FusedAttn, and Encode) are vital; the fully optimized HATA is 6.53× faster than a naive PyTorch implementation of the same logic.

4.2 Accuracy and Scalability

On the RULER benchmark (128K context), HATA maintains near-perfect accuracy even when restricted to small token budgets. In "Needle-in-a-Haystack" tests, HATA achieved 100% retrieval accuracy while processing only ~1.5% of the total tokens, illustrating its extreme efficiency in identifying salient information.

5. Discussion and Future Work

The success of HATA stems from two key insights:

  1. Ranking > Regression: For attention pruning, the relative order of tokens is more important than the absolute attention values.
  2. Hardware-Native Operations: Leveraging bitwise operations allows the algorithm to align with the strengths of modern GPU architectures.

Despite its performance, HATA has limitations. The hash weights are currently static; exploring dynamic weights that adapt during inference could yield further gains. Additionally, while HATA excels in Multi-Head Attention (MHA) and Group-Query Attention (GQA), future research is needed to adapt the framework for newer architectures like Multi-Latent Head Attention (MLA) used in models such as DeepSeek-V3.

6. Conclusion

HATA provides a scalable and hardware-efficient solution to the memory bottleneck in LLM inference. By combining trainable binary hashing with deep hardware integration, it enables high-fidelity long-context processing at a fraction of the computational cost of vanilla attention.

Summary updated

BibTeX
@inproceedings{gong2025hata,
  author = {Ping Gong and Jiawei Yi and Shengnan Wang and Juncheng Zhang and Zewen Jin and Ouxiang Zhou and Ruibo Liu and Guanbin Xu and Youhui Bai and Bowen Ye and Kun Yuan and Tong Yang and Gong Zhang and Renhai Chen and Feng Wu and Cheng Li},
  title = {{HATA: Trainable and Hardware-Efficient Hash-Aware Top-k Attention for Scalable Large Model Inference}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure