NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

RelayAttention for Efficient Large Language Model Serving with Long System Prompts

Lei Zhu, Xinjiang Wang, Wayne Zhang, Rynson W. H. Lau

Abstract

A practical large language model (LLM) service may involve a long system prompt, which specifies the instructions, examples, and knowledge documents of the task and is reused across requests. However, the long system prompt causes throughput/latency bottlenecks as the cost of generating the next token grows w.r.t the sequence length. This paper aims to improve the efficiency of LLM services that involve long system prompts. Our key observation is that handling these system prompts requires heavily redundant memory accesses in existing causal attention computation algorithms. Specifically, for batched requests, the cached hidden states (i.e., key-value pairs) of system prompts are transferred from off-chip DRAM to on-chip SRAM multiple times, each corresponding to an individual request. To eliminate such a redundancy, we propose RelayAttention, an attention algorithm that allows reading these hidden states from DRAM exactly once for a batch of input tokens. RelayAttention is a free lunch: it maintains the generation quality while requiring no model retraining, as it is based on a mathematical reformulation of causal attention. We have observed significant performance improvements to a production-level system, vLLM, through integration with RelayAttention. The improvements are even more profound with longer system prompts.

Introduction

In modern LLM applications, "system prompts"—which contain instructions, few-shot examples, or retrieved knowledge documents—are becoming increasingly long. These prompts are typically shared across many different user requests. While systems like vLLM (using PagedAttention) have solved the memory footprint issue by allowing multiple requests to point to the same physical memory for shared prompts, they haven't solved the memory bandwidth bottleneck.

Standard attention algorithms still fetch the Key-Value (KV) cache for that shared system prompt from off-chip DRAM to on-chip SRAM separately for every single request in a batch. Because LLM inference is notoriously memory-bound, this redundant reading of the same data creates a massive latency and throughput bottleneck, especially as system prompts grow to thousands of tokens. RelayAttention addresses this by ensuring shared hidden states are read from DRAM exactly once per batch.

Methods

RelayAttention is an "exact" algorithm that produces the same mathematical output as standard attention but optimizes how data moves through hardware. It achieves this through three primary mechanisms:

  1. From GEMV to GEMM: Standard autoregressive decoding uses General Matrix-Vector multiplication (GEMV), which is limited by memory speed. RelayAttention groups queries from a batch together, turning the operation into a General Matrix-Matrix multiplication (GEMM) for the shared system prompt portion. This allows the GPU to load the system KV cache once and reuse it for all queries.
  2. Three-Step Computation: The mechanism decomposes attention into distinct phases to handle shared and unique data separately.
  3. Relay Fusion: A mathematical rescaling trick (a convex combination) is used to fuse the results of system and context attention into the final output without loss of precision.
The three-step computation process of RelayAttention: System Attention, Context Attention, and Relay Fusion.

Key Results

The authors integrated RelayAttention into the vLLM framework and evaluated it against standard baselines on NVIDIA A40 and A100 GPUs using datasets like ShareGPTv3 and MMLU.

  • Throughput and Latency: On Llama2-7B, RelayAttention achieved up to 2.0× higher throughput and sustained a 2.2× higher request rate in interactive scenarios.
  • Scaling with Prompt Length: As shown in the performance data, the latency of PagedAttention grows significantly with system prompt length, while RelayAttention maintains a much lower, more stable latency profile.
  • Efficiency Gains: For 5-shot MMLU tests, RelayAttention provided a 76% reduction in processing time.
Latency comparison between PagedAttention and RelayAttention across varying system prompt lengths, highlighting the efficiency of Relay Fusion.

Conclusion and Limitations

RelayAttention offers a "free lunch" for LLM serving, providing significant speedups without requiring model retraining or fine-tuning. However, its benefits are most pronounced with larger batch sizes ($b > 1$) where redundancy exists. Additionally, if the unique user context becomes significantly longer than the shared system prompt, the relative efficiency gains diminish as context-specific attention begins to dominate the computation time. Future work may involve extending this to multi-tenant environments where different requests share different prefixes.

Summary updated

BibTeX
@inproceedings{zhu2024relayattention,
  author = {Lei Zhu and Xinjiang Wang and Wayne Zhang and Rynson W. H. Lau},
  title = {{RelayAttention for Efficient Large Language Model Serving with Long System Prompts}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=o3SWIEJxs6},
}
Figure