LLMLingua: Compressing Prompts for Accelerated Inference of Large Language Models
Large language models (LLMs) have been applied in various applications due to their astonishing capabilities. With advancements in technologies such as chain-of-thought (CoT) prompting and in-context learning (ICL), the prompts fed to LLMs are becoming increasingly lengthy, even exceeding tens of thousands of tokens. To accelerate model inference and reduce cost, this paper presents LLMLingua, a coarse-to-fine prompt compression method that involves a budget controller to maintain semantic integrity under high compression ratios, a token-level iterative compression algorithm to better model the interdependence between compressed contents, and an instruction tuning based method for distribution alignment between language models. We conduct experiments and analysis over four datasets from different scenarios, i.e., GSM8K, BBH, ShareGPT, and Arxiv-March23; showing that the proposed approach yields state-of-the-art performance and allows for up to 20x compression with little performance loss.1
Introduction. The widespread adoption of ChatGPT has transformed numerous scenarios by harnessing the powerful generalization and reasoning capabilities of large language models (LLMs). In practical applications, crafting suitable prompts is crucial and usually involves techniques such as chain-ofthought, in-context learning, and retrieving related documents or historical conversations (Wei et al., 2022; Chase, 2022). While these methods can elicit highly effective generations by activating LLMs’ domain-specific knowledge, they often require longer prompts. Therefore, striking a balance between the massive computational demands of LLMs and the need for longer prompts has become an urgent issue. Some studies attempt to accelerate model inference by modifying the parameters of LLMs through quantization (Dettmers et al., 2022; Xiao et al., 2023), compression (Frantar and Alistarh, 2023), etc. However, these approaches may be not suitable when the LLMs can be accessed via APIs only. Approaches that attempt to reduce the length of original prompts while preserving essential information have emerged lately. These approaches are grounded in the concept that natural language is inherently redundant (Shannon, 1951) and thus can be compressed. Gilbert et al. (2023) also indicate that LLMs can effectively reconstruct source code from compressed text descriptions while maintaining a high level of functional accuracy. Therefore, we follow this line of studies to compress a long prompt into a shorter one without any gradient flow through the LLMs to support applications based on a larger range of LLMs. In terms of information entropy, tokens with lower perplexity (PPL) contribute less to the overall entropy gains of the language model. In other words, removing tokens with lower perplexity has a relatively minor impact on the LLM’s comprehension of the context. Motivated by this, Li (2023) propose Selective-Context, which first employs a small language model to compute the selfinformation of each lexical unit (such as sentences, phrases, or tokens) in original prompts, and then drops the less informative content for prompt compression. However, this method not only ignores the interdependence between the compressed contents but also neglects the correspondence between the LLM being targeted and the small language model used for prompt compression. This paper proposes LLMLingua, a coarseto-fine prompt compression method, to address the aforementioned issues. Specifically, we first present a budget controller to dynamically allocate different compression ratios to various components in original prompts such as the instruction, demonstrations, and the question, and meanwhile, perform coarse-grained, demonstration-level compression to maintain semantic integrity under high compression ratios. We further introduce a tokenlevel iterative algorithm for fine-grained prompt compression. Compared with Selective Context, it can better preserve the key information within the prompt by taking into account the conditional dependencies between tokens. Additionally, we pose the challenge of distribution discrepancy between the target LLM and the small language model used for prompt compression, and further propose an instruction tuning based method to align the distribution of both language models. We validate the effectiveness of our approach on four datasets from different domains, i.e., GSM8K and BBH for reasoning and ICL, ShareGPT for conversation, and Arxiv-March23 for summarization. The results show that our method yields state-ofthe-art performance across the board. Furthermore, we conduct extensive experiments and discussions to analyze why our approach attains superior performance. To our best knowledge, we are the first to evaluate reasoning and ICL capabilities in the domain of efficient LLMs.
Related work. Efficient large language models have gained significant attention in recent research community, especially with the growing prominence of Chat- GPT. Most of these methods aim to reduce the costs of inference and fine-tuning by modifying the model parameters through quantization (Dettmers et al., 2022; Frantar et al., 2023; Xiao et al., 2023), compression (Frantar and Alistarh, 2023), instruct tuning (Taori et al., 2023; Chiang et al., 2023; Xu et al., 2023), or delta tuning (Hu et al., 2022). A line of studies attempt to optimize inference costs from the perspective of the input prompts. Motivated by the observation of the abundance of identical text spans between the input and the generated result, Yang et al. (2023) directly copy tokens from prompts for decoding to accelerate the inference process of LLMs. Some approaches focus on compressing prompts, specifically, learning special tokens via prompt tuning of LLMs to reduce the number of tokens to be processed during inference (Mu et al., 2023; Ge et al., 2022; Wingate et al., 2022; Chevalier et al., 2023; Ge et al., 2023). Unfortunately, these methods are usually tailored to particular tasks and some of them (Mu et al., 2023; Chevalier et al., 2023) even require to fine-tune the whole language model, which severely limits their application scenarios. Furthermore, there are some studies (Chase, 2022; Zhang et al., 2023) that attempt to utilize LLMs to summarize dialog or data, thereby forming memory and knowledge. However, these approaches require multiple invocations of LLMs, which are quite costly. Some methods reduce the prompt length by selecting a subset of demonstrations. For example, Zhou et al. (2023) introduces a reinforcement learning based algorithm to allocate a specific number of demonstrations for each question. Some other methods focus on token pruning (Goyal et al., 2020; Kim and Cho, 2021; Kim et al., 2022; Rao et al., 2021; Modarressi et al., 2022) and token merging (Bolya et al., 2023). However, these approaches are proposed for smaller models such as BERT, ViT. Moreover, they depend on fine-tuning the models or obtaining intermediate results during inference. The most similar work to this paper is Selective- Context (Li, 2023), which evaluates the informativeness of lexical units by computing selfinformation with a small language model, and drops the less informative content for prompt compression. This paper is inspired by Selective- Context and further proposes a coarse-to-fine framework to address its limitations.
Method. A prompt compression system is designed to generate a compressed prompt ex = {exi}eL i=1 from a given original prompt x = (xins, xdems, xque), where xins = {xins i}Lins i=1 , xdems = {xdems i }Ldems i=1 , and xque = {xque i }Lque i=1 denote the instruction, demonstrations, and the question in the original prompt x. eL, Lins, Ldems, and Lque represent the numbers of tokens in ex, xins, xdems, and xque, respectively. Let L= Lins + Ldems + Lque denote the total sequence length of x, the compression rate is defined as τ= eL/L, τ∈[0, 1], and the compression ratio is 1/τ. A smaller value of τimplies a lower inference cost, which is preferable. Let exGrepresent the LLM-generated results derived by ex and xG denotes the tokens derived by x, the distribution of exGis expected to be as similar to xGas possible. This can be formulated as:
In this section, we elaborate on the proposed coarseto-fine prompt compression approach, LLMLingua. First, we introduce a budget controller to dynamically allocate different compression ratios to various components in prompts and meanwhile, perform coarse-grained, demonstration-level compression to maintain semantic integrity under high compression ratios. Next, we describe the proposed iterative prompt algorithm designed to retain knowledge from the prompt while compressing. Finally, we introduce alignment to address the distribution gap between the small model and black-box large models. Figure 1 show the framework.
The budget controller here is designed to allocate different budgets, i.e., compression ratio, to different components in a prompt such as instructions, demonstrations, and questions, at the sentence or demonstration level. There are two considerations: (i) In general, the instruction and the question in a prompt have a direct influence on the generated results, as they should contain all the necessary knowledge to generate the following answer. On the contrary, if there are multiple demonstrations in the original prompt, the conveyed information may be redundant. Therefore, a tailored budget controller is required to allocate more budget (i.e., smaller compression ratios) for instructions and questions, and less budget for demonstrations. (ii) When a high compression ratio is required, token-level dropout as in Li (2023) might make the compressed prompts too trivial and thus lose vital information from the original prompt. Consequently, sentence-level dropout should be employed instead to preserve a certain degree of linguistic integrity. Especially in the case of multiple redundant demonstrations, we can even perform demonstration-level control to meet the compression requirement. Algorithm 1 illustrates the overall procedure of the budget controller.
Derive compression ratio for demonstrations. We first compute the compression rate for demonstrations τdems according to the target overall compression rate τand the pre-defined compression rate for instructions and questions, i.e., τins and τque, respectively.
Demonstration-level prompt compression. With the derived τdems for demonstrations, we then perform a coarse-grained demonstration-level prompt compression: we construct D, a subset of demonstrations from xdems. Specifically, we first employ a small language model Ms, such as GPT-2 or LLaMA, to compute the perplexity of each demonstration in xdems. Then, we select demonstrations in descending order of their perplexity values, until adding one more Adjust compression ratios for instruction and question. After obtaining the coarse-grained compression result D = {xi} eLD i=1, we allocate the remaining budget to the instruction and the question:
Utilizing perplexity for prompt compression encounters the intrinsic limitation, i.e., the independence assumption, similar to the shortcomings of the Mask Language Model (Yang et al., 2019) as:
Finally, tokens in each s jwith the PPL greater than γjare retained in the compressed prompt.
Discussion. Compare with Generation-based Methods We do not develop our approach based on LLM generation primarily for three reasons: i) The content and length of the generated text are uncontrollable. Uncontrollable length requires more iterations to satisfy the constraint of the compression ratio. Uncontrollable content leads to low overlap between the generated text and the original prompt, particularly for complex prompts with multi-step inference, which may lose significant amounts of reasoning paths or even generate completely unrelated demonstrations. ii) The computational cost is high. Small language models struggle to handle such complex tasks, and using models like GPT-4 for compression would further increase computational overhead. Moreover, even powerful generation models like GPT-4 struggle to retain effective information from prompts as shown in Table 2. iii) The compressed prompts obtained from generation models are complete and continuous sentences, usually resulting in a lower compression ratio compared to our coarse-to-fine method.
Compare with Prompt Engineering methods Our method is orthogonal to Prompt Engineering methods, such as prompt retrieval and prompt ordering. Our work focuses on compressing welldesigned prompts, and it performs well on complex and fine-tuned prompts like GSM8K. Moreover, the perplexity-based demonstration filtering method used in our budget controller can also be applied to scenarios such as prompt retrieval. This demonstrates the compatibility and adaptability of our approach in various LLMs settings.
Conclusion. We introduce a coarse-to-fine algorithm for prompt compression, named LLMLingua, which is based on the small LM’s PPL for black-box LLMs. Our approach consists of three modules: Budget Controller, Iterative Token-level Compression, and Alignment. We validate the effectiveness of our approach on 4 datasets from different domains, i.e., GSM8K, BBH, ShareGPT, and Arxiv-March23, demonstrating that our method achieves state-ofthe-art performance across all datasets, with up to 20x compression with only a 1.5 point performance drop. Moreover, we observe that LLMs can effectively restore compressed prompts, and prompt compression contributes to a reduction in generated text length. Our approach holds substantial practical implications, as it not only reduces computational costs but also offers a potential solution for accommodating longer contexts in LLMs. The method of compressing prompts has the potential to enhance downstream task performance by compressing longer prompts and to improve the LLMs’s inference efficiency by compressing the KV cache.
Limitations. There are also some limitations in our approach. For instance, we might observe a notable performance drop when trying to achieve excessively high compression ratios such as 25x-30x on GSM8K, as shown in Figure 3.
It is shown that as the compression ratio increases especially around 25x-30x, all methods as well as ours will experience a substantial performance drop. In comparison with other methods, this performance drop derived from our approach is significantly shifted to much higher compression ratios. We owe this to the Budget Controller and the Iterative Token-level Prompt Compression algorithm, which enable our method to maintain the original prompt information even at some extreme compression ratios. The upper limit of the compression ratio for different prompts varies, depending on factors such as prompt length, task type, and the number of sentences involved. Additionally, there may be subtle differences between the tokenizers used by the small language model and the black-box LLM, which may result in an underestimation of the prompt’s token length.
Lines of inquiry this paper opens 24
Research framings built by reading the notes related to this paper — the questions it feeds into.
How can language models effectively manage long-context memory without quality degradation?- Why are long-context workloads most vulnerable to compute shortages?
- Can compressed context distilled into smaller models preserve teacher performance reliably?
- Why do long-context inputs truncated for relevance lose cache reuse benefits?
- What efficiency gains could prevent an inference compute bottleneck?
- Does inference demand for memory-hungry accelerators explain the shift toward memory-dominant bills of materials?
- Are inference speed and context length also within parity on consumer GPU hardware?
- How do architectural changes affect inference efficiency independent of model size?
- How much of token demand growth comes from context length versus user volume?
- Why does context pruning and tool summarization break cached prompt representations?
- Do cache benefits hold with different system prompt sizes and tool counts?
- What makes prompt rephrasing expose brittleness in deployed agents?
- Can compression aggressiveness be set automatically for new environments without benchmark testing?
- Does environment noise level affect how aggressively agents should compress context?
- How much do typical users actually save from declining AI inference costs?
- Can inference demand itself change based on model price and speed?
- Does building inference infrastructure differ economically from building AI applications?
- Could correcting information gaps reduce spending on expensive closed AI models?