Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mit-han-lab/streaming-llm
[ICLR 2024] Efficient Streaming Language Models with Attention Sinks
https://github.com/mit-han-lab/streaming-llm
Last synced: 3 days ago
JSON representation
[ICLR 2024] Efficient Streaming Language Models with Attention Sinks
- Host: GitHub
- URL: https://github.com/mit-han-lab/streaming-llm
- Owner: mit-han-lab
- License: mit
- Created: 2023-09-29T17:45:40.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-11T08:14:43.000Z (5 months ago)
- Last Synced: 2024-12-03T07:03:06.073Z (10 days ago)
- Language: Python
- Homepage: https://arxiv.org/abs/2309.17453
- Size: 38.3 MB
- Stars: 6,704
- Watchers: 65
- Forks: 369
- Open Issues: 44
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- Awesome-LLM-Productization - streaming-llm - deploy LLMs for infinite-length inputs without sacrificing efficiency and performance. (Models and Tools / LLM Deployment)
- AiTreasureBox - mit-han-lab/streaming-llm - 12-07_6708_0](https://img.shields.io/github/stars/mit-han-lab/streaming-llm.svg)|Efficient Streaming Language Models with Attention Sinks| (Repos)
- StarryDivineSky - mit-han-lab/streaming-llm - --但我们表明,当文本长度超过缓存大小时,它会失败。我们观察到一个有趣的现象,即注意力下沉,即保留初始词元的 KV 将在很大程度上恢复窗口注意力的性能。在本文中,我们首先证明了注意力下沉的出现是由于对初始令牌作为“接收器”的强烈注意力得分,即使它们在语义上并不重要。基于上述分析,我们引入了StreamingLLM,这是一个高效的框架,使使用有限长度注意力窗口训练的LLM能够推广到无限序列长度,而无需任何微调。StreamingLLM可以使Llama-2,MPT,Falcon和Pythia使用多达400万个词元或更多词元执行稳定高效的语言建模。此外,发现在预训练期间添加占位符令牌作为专用的注意力接收器可以进一步改进流式处理部署。在流设置中,StreamingLLM 的性能优于滑动窗口重新计算基线高达 22.2 倍的加速。 (A01_文本生成_文本对话 / 大语言对话模型及数据)
README
# Efficient Streaming Language Models with Attention Sinks
[[paper](http://arxiv.org/abs/2309.17453)] [[slides](assets/StreamingLLM.pdf)][[video](https://youtu.be/hvJsEzP34o8)]![schemes](figures/schemes.png)
https://github.com/mit-han-lab/streaming-llm/assets/40906949/2bd1cda4-a0bd-47d1-a023-fbf7779b8358
## TL;DR
We deploy LLMs for infinite-length inputs without sacrificing efficiency and performance.## News
- [2024/02] StreamingLLM is covered by [MIT News as a spotlight](https://news.mit.edu/2024/new-way-let-ai-chatbots-converse-all-day-without-crashing-0213)!
- [2024/01] StreamingLLM is integrated by HPC-AI Tech [SwiftInfer](https://github.com/hpcaitech/SwiftInfer) to support infinite input length for LLM inference.
- [2024/01] StreamingLLM is integrated by NVIDIA [TensorRT-LLM](https://github.com/NVIDIA/TensorRT-LLM/tree/main/examples/llama#run-llama-with-streamingllm)!
- [2023/12] StreamingLLM is integrated by CMU, UW, and OctoAI, enabling endless and efficient LLM generation on [iPhone](https://x.com/davidpissarra/status/1735761373261427189?s=20)!
- [2023/12] StreamingLLM is integrated by HuggingFace Transformers [PR](https://github.com/huggingface/transformers/pull/26681).
- [2023/10] StreamingLLM is integrated into [Intel Extension for Transformers](https://github.com/intel/intel-extension-for-transformers).
- [2023/10] [Attention Sinks](https://github.com/tomaarsen/attention_sinks), a third-party implementation enables StreamingLLM on more Huggingface LLMs.## Abstract
Deploying Large Language Models (LLMs) in streaming applications such as multi-round dialogue, where long interactions are expected, is urgently needed but poses two major challenges. Firstly, during the decoding stage, caching previous tokens' Key and Value states (KV) consumes extensive memory. Secondly, popular LLMs cannot generalize to longer texts than the training sequence length. Window attention, where only the most recent KVs are cached, is a natural approach --- but we show that it fails when the text length surpasses the cache size. We observe an interesting phenomenon, namely attention sink, that keeping the KV of initial tokens will largely recover the performance of window attention. In this paper, we first demonstrate that the emergence of attention sink is due to the strong attention scores towards initial tokens as a ``sink'' even if they are not semantically important. Based on the above analysis, we introduce StreamingLLM, an efficient framework that enables LLMs trained with a finite length attention window to generalize to infinite sequence length without any fine-tuning. We show that StreamingLLM can enable Llama-2, MPT, Falcon, and Pythia to perform stable and efficient language modeling with up to 4 million tokens and more. In addition, we discover that adding a placeholder token as a dedicated attention sink during pre-training can further improve streaming deployment. In streaming settings, StreamingLLM outperforms the sliding window recomputation baseline by up to 22.2x speedup.## Usage
### Environment Setup
```bash
conda create -yn streaming python=3.8
conda activate streamingpip install torch torchvision torchaudio
pip install transformers==4.33.0 accelerate datasets evaluate wandb scikit-learn scipy sentencepiecepython setup.py develop
```### Run Streaming Llama Chatbot
```bash
CUDA_VISIBLE_DEVICES=0 python examples/run_streaming_llama.py --enable_streaming
```## FAQ
1. **What does "working on infinite-length inputs" imply for LLMs?**
Handling infinite-length text with LLMs presents challenges. Notably, storing all previous Key and Value (KV) states demands significant memory, and models might struggle to generate text beyond their training sequence length. StreamingLLM addresses this by retaining only the most recent tokens and attention sinks, discarding intermediate tokens. This enables the model to generate coherent text from recent tokens without a cache reset — a capability not seen in earlier methods.2. **Is the context window of LLMs expanded?**
No. The context window remains unchanged. Only the most recent tokens and attention sinks are retained, discarding middle tokens. This means the model can only process the latest tokens. The context window remains constrained by its initial pre-training. For instance, if Llama-2 is pre-trained with a context window of 4096 tokens, then the maximum cache size for StreamingLLM on Llama-2 remains 4096.
3. **Can I input an extensive text, like a book, into StreamingLLM for summarization?**
While you can input a lengthy text, the model will only recognize the latest tokens. Thus, if a book is an input, StreamingLLM might only summarize the concluding paragraphs, which might not be very insightful. As emphasized earlier, we neither expand the LLMs' context window nor enhance their long-term memory. StreamingLLM's strength lies in generating fluent text from recent tokens without needing a cache refresh.
4. **What is the ideal use case for StreamingLLM?**
StreamingLLM is optimized for streaming applications, such as multi-round dialogues. It's ideal for scenarios where a model needs to operate continually without requiring extensive memory or dependency on past data. An example is a daily assistant based on LLMs. StreamingLLM would let the model function continuously, basing its responses on recent conversations without needing to refresh its cache. Earlier methods would either need a cache reset when the conversation length exceeded the training length (losing recent context) or recompute KV states from recent text history, which can be time-consuming.
5. **How does StreamingLLM relate to recent works on context extension?**
StreamingLLM is orthogonal to recent context extension methods and can be integrated with them. In StreamingLLM's context, "context extension" refers to the possibility of using a larger cache size to store more recent tokens. For a practical demonstration, refer to Figure 9 in our paper, where we implement StreamingLLM with models like LongChat-7B-v1.5-32K and Llama-2-7B-32K-Instruct.
## TODOs
We will release the code and data in the following order, please stay tuned!- [x] Release core code of StreamingLLM, including Llama-2, MPT, Falcon, and Pythia.
- [x] Release perplexity evaluation code
- [x] Release Streaming Llama Chatbot demo.
- [ ] Release StreamEval dataset and evaluation code.## Citation
If you find StreamingLLM useful or relevant to your project and research, please kindly cite our paper:
```bibtex
@article{xiao2023streamingllm,
title={Efficient Streaming Language Models with Attention Sinks},
author={Xiao, Guangxuan and Tian, Yuandong and Chen, Beidi and Han, Song and Lewis, Mike},
journal={arXiv},
year={2023}
}
```