TriForce: Lossless Acceleration of Long Sequence Generation with Hierarchical Speculative Decoding

Hanshi Sun, Zhuoming Chen, Xinyu Yang, Yuandong Tian, Beidi Chen

TL;DR

TriForce is a hierarchical speculative decoding system designed to accelerate long-sequence generation in Large Language Models (LLMs) without any loss in model quality. By combining a lightweight draft model with a novel retrieval-based partial Key-Value (KV) cache strategy, TriForce mitigates the severe memory bottlenecks of long-context inference, achieving up to 2.31× speedup on an A100 GPU and up to 7.78× speedup in GPU-CPU offloading scenarios.

What Problem Does This Paper Address?

As LLMs are increasingly deployed to process massive contexts (such as entire books, long codebases, or hours of video), serving them efficiently has become a major engineering challenge. The primary culprit is the Key-Value (KV) cache. To avoid recomputing past tokens during generation, LLMs store their intermediate states in memory. However, this KV cache grows linearly with sequence length.

Because LLMs generate text token-by-token (autoregressively), the entire massive KV cache must be reloaded from memory for every single token generated. This creates a severe memory bandwidth bottleneck, leaving GPU computational cores idling and causing high latency.

Existing solutions generally fall into two camps, both with major drawbacks:

  1. KV Cache Eviction (Lossy): Methods like StreamingLLM or $H_2O$ permanently discard older or less "important" tokens to keep the cache size fixed. This saves memory but causes permanent information loss, leading to hallucinations and poor performance on tasks requiring long-range dependencies.
  2. Speculative Decoding: This leverages a smaller, faster "draft" model to predict tokens, which the larger "target" model verifies in parallel. However, training a small draft model that can handle 100K+ context windows is computationally prohibitive, and training-free draft methods suffer from low token acceptance rates as context scales.

Methods

TriForce introduces Hierarchical Speculative Decoding to tackle the dual bottlenecks of model weights and KV cache size sequentially. Instead of a single draft model, TriForce sets up a two-level speculation hierarchy:

  1. Level 1 (Drafting the Draft): A tiny, lightweight model (e.g., Llama-68M) utilizing a strict, small eviction cache (StreamingLLM) quickly generates initial token candidates. This addresses the model weights bottleneck.
  2. Level 2 (The Intermediate Draft): The target model (e.g., Llama2-7B) runs these candidates through a partial, retrieval-based KV cache. This serves as a high-quality intermediate draft layer.
  3. Final Verification: The target model verifies the tokens using its full KV cache. Because the final step uses the complete cache, the system remains mathematically lossless—the output distribution is identical to standard autoregressive decoding.

Crucially, TriForce replaces standard cache eviction with Retrieval-based Drafting. It divides the full KV cache into small chunks. During the retrieval phase, it calculates the attention between the current query and the average key within each chunk, dynamically pulling only the most relevant context into the intermediate draft layer.

Figure 1: Left: TRIFORCE employs retrieval-based drafting and hierarchical speculation to effectively address the dual bottlenecks. It integrates two models and three caches, comprising a draft model, a target model, a StreamingLLM cache for the draft model, alongside a retrieval cache and a full cache for the target model. The process initiates by repeatedly drafting for γ1 steps, assisting the target model with retrieved partial KV cache in generating over γ2 tokens, which will be further verified by the target model using full KV cache. Right: Evaluating the Llama2-7B-128K on a needle retrieval task indicates that KV cache eviction-based methods, such as StreamingLLM, require a trade-off between latency and accuracy. In contrast, our TRIFORCE maintains low latency without sacrificing accuracy.

Datasets

The authors evaluated TriForce across several long-context benchmarks:

  • PG-19 test set: A benchmark consisting of long books, used to evaluate long-sequence language modeling.
  • NarrativeQA: A reading comprehension dataset based on entire movie scripts and books.
  • Needle-in-a-Haystack (Needle Retrieval): A synthetic task designed to test whether a model can retrieve a specific piece of information hidden deep within a long context window.

Key Insights and Ideas

  • Dual Memory Bottlenecks: The authors quantified that while model weights dominate memory bottlenecks at shorter context lengths, the KV cache scales up to occupy the vast majority of GPU memory (up to 80%+) as context lengths reach 128K.
  • Extreme Attention Sparsity: The paper reveals that in a 120K token context, over 96% of the total attention score is concentrated in just 4K tokens. This means a tiny fraction of the KV cache is sufficient to achieve a high speculative acceptance rate.
  • Contextual Locality: The authors discovered that the context tokens required by adjacent generated tokens are highly similar. This allows TriForce to construct its retrieval cache once and reuse it across multiple decoding steps, drastically amortizing the overhead of cache selection.

Key Results

  • On-Chip Speedups: For Llama2-7B-128K on a single NVIDIA A100 GPU, TriForce delivers up to a 2.31× speedup over the standard Hugging Face autoregressive baseline.
  • Offloading Breakthroughs: On consumer hardware (two RTX 4090 GPUs) where the full KV cache must be offloaded to CPU memory, TriForce achieves an impressive 7.78× speedup for Llama2-7B-128K and 7.94× speedup for Llama2-13B-128K. It generates tokens at 0.108 seconds per token—only half as slow as an A100 running standard autoregression.
  • Outperforming DeepSpeed: On a single RTX 4090, TriForce runs 4.86× faster than DeepSpeed-ZeRO-Inference.
  • Temperature Robustness: While vanilla speculative decoding performance degrades at higher sampling temperatures, TriForce maintains a stable token acceptance rate above 0.9 even at a temperature of 1.0.

Limitations and Future Work

  • Draft-Target Capacity Gap: The acceptance rate of the very first speculation phase can be limited by the massive capability gap between the tiny 68M draft model and the 7B target model.
  • Tree-Based Speculation: The current implementation relies on chain-based speculation. The authors note that integrating TriForce with tree-based speculative decoding structures (like Sequoia) could allow the system to validate multiple candidate paths simultaneously, unlocking even higher speedups under larger speculation budgets.

Summary updated

Abstract

With large language models (LLMs) widely deployed in long content generation recently, there has emerged an increasing demand for efficient long-sequence inference support. However, key-value (KV) cache, which is stored to avoid re-computation, has emerged as a critical bottleneck by growing linearly in size with the sequence length. Due to the auto-regressive nature of LLMs, the entire KV cache will be loaded for every generated token, resulting in low utilization of computational cores and high latency. While various compression methods for KV cache have been proposed to alleviate this issue, they suffer from degradation in generation quality. We introduce TriForce, a hierarchical speculative decoding system that is scalable for long sequence generation. This approach leverages the original model weights and dynamic sparse KV cache via retrieval as a draft model, which serves as an intermediate layer in the hierarchy and is further speculated by a smaller model to reduce its drafting latency. TriForce not only facilitates impressive speedups for Llama2-7B-128K, achieving up to 2.31$\times$ on an A100 GPU but also showcases scalability in handling even longer contexts. For the offloading setting on two RTX 4090 GPUs, TriForce achieves 0.108s/token$\unicode{x2014}$only half as slow as the auto-regressive baseline on an A100, which attains 7.78$\times$ on our optimized offloading system. Additionally, TriForce performs 4.86$\times$ than DeepSpeed-Zero-Inference on a single RTX 4090 GPU. TriForce's robustness is highlighted by its consistently outstanding performance across various temperatures. The code is available at https://github.com/Infini-AI-Lab/TriForce.
BibTeX
@inproceedings{sun2024triforce,
  author = {Hanshi Sun and Zhuoming Chen and Xinyu Yang and Yuandong Tian and Beidi Chen},
  title = {{TriForce: Lossless Acceleration of Long Sequence Generation with Hierarchical Speculative Decoding}},
  booktitle = {COLM},
  year = {2024},
  url = {https://openreview.net/forum?id=HVK6nl3i97},
}
Figure