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

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

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 to 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.31x 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—only half as slow as the auto-regressive baseline on an A100, which attains 7.78x on our optimized offloading system. Additionally, TriForce performs 4.86x than DeepSpeed-Zero-Inference on a single RTX 4090 GPU. TriForce's robustness is highlighted by its consistently outstanding performance across various temperatures.

Introduction: The KV Cache Bottleneck

As Large Language Models (LLMs) move toward processing massive contexts exceeding 100K tokens, they encounter a significant performance wall: the Key-Value (KV) cache bottleneck. In standard auto-regressive generation, the model must load the entire KV cache—the stored activations of all previous tokens—from the GPU memory to its processors for every single new token generated.

Because this cache grows linearly with sequence length, the time spent moving this data eventually dwarfs the actual computation time. This leads to high latency and low GPU utilization. While existing solutions often employ "cache eviction" (permanently deleting older tokens), these methods are lossy and can lead to hallucinations or a loss of context. TriForce addresses this by introducing a hierarchical, lossless acceleration strategy.

The TriForce Architecture

TriForce utilizes a two-layered hierarchical approach to speculative decoding, designed to minimize both weight-loading and KV cache-loading overhead. The system operates through three distinct levels of inference:

  1. Tiny Draft Model: A lightweight model (e.g., Llama-68M) with a minimal cache provides the initial speculation. This handles the bottleneck caused by model weights at shorter sequence lengths.
  2. Retrieval-based Drafting (Self-Speculation): The target model itself acts as an intermediate drafter. Instead of loading the full KV cache, it performs attention against a sparse, retrieved portion (typically 3-4%). This "self-speculation" maintains high accuracy while significantly reducing data movement.
  3. Full Verification: The final step involves the full target model and the 100% complete KV cache. This ensures the system is mathematically lossless, meaning the output is identical to standard, slower inference.
The hierarchical structure of TriForce, showing the draft model, retrieval-based target model, and full target model with their respective memory footprints and latencies.

To further optimize performance, TriForce exploits contextual locality. The authors observed that adjacent tokens tend to attend to similar parts of the KV cache. By reusing the retrieved sparse cache across multiple steps, the system amortizes the cost of the retrieval process.

Key Insights and Design Principles

  • Attention Sparsity: Research confirms that in a 120K context, as few as 4K tokens can often recover over 96% of the attention score. This validates the use of sparse retrieval for drafting.
  • The Dual Bottleneck: The hierarchy is specifically designed to tackle the shifting bottleneck in LLM inference. At short sequence lengths, model weights are the primary bottleneck; at long lengths, the KV cache takes over. TriForce's multi-tier approach addresses both.
  • Retrieval vs. Eviction: Unlike lossy methods like StreamingLLM that permanently discard tokens, TriForce keeps the full cache (often offloaded to the CPU) and only pulls the necessary "chunks" into the GPU as needed.

Performance Evaluation

The researchers evaluated TriForce using long-context datasets including PG-19 (books), NarrativeQA (story comprehension), and the synthetic Needle In A Haystack benchmark.

TriForce demonstrated substantial speedups across various hardware configurations:

  • On-Chip Speedup: On an NVIDIA A100 GPU, TriForce achieved a 2.31x speedup for Llama2-7B with a 128K context.
  • Offloading Performance: On consumer-grade hardware (two RTX 4090s), TriForce achieved a 7.78x speedup over optimized baselines by offloading the KV cache to the CPU.
  • Comparison to Baselines: It performed 4.86x faster than DeepSpeed-ZeRO-Inference on a single RTX 4090 and maintained superior accuracy compared to lossy methods.
Accuracy vs. Latency comparison showing TriForce achieving lossless performance at significantly lower latency than baselines.

As shown in the performance comparison, TriForce (represented by the star) achieves the same 1.0 accuracy (losslessness) as a full cache but at a fraction of the latency. Methods like StreamingLLM and H2O show a clear trade-off where accuracy drops as they attempt to reduce latency through cache eviction.

Conclusion

TriForce offers a scalable and lossless solution for the growing demands of long-sequence LLM generation. By combining hierarchical speculative decoding with dynamic sparse KV cache retrieval, it effectively bypasses the memory bottlenecks that plague standard inference. While retrieval overhead and hardware bandwidth remain factors to consider, TriForce sets a new standard for efficient, high-fidelity long-context generation on both enterprise and consumer hardware.

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 to 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