微軟開源專註加快大模型推斷速度

2024年2月6日 24点热度 0人点赞

#記錄我的2024#

大傢好,又見面了,我是 GitHub 精選君!

背景介紹

在大規模語言模型推理過程中,我們經常會面臨幾個問題:如何加快語言模型的推理速度;如何增強語言模型對關鍵信息的感知;如何壓縮 prompt 提示和 KV-Cache。這些問題讓人頭痛,很多解決起來需要很多人力和資源投入。

今天要給大傢推薦一個微軟開源的項目 LLMLingua,該項目在 GitHub 有超過 1.2k Star,用一句話介紹該項目就是:“To speed up LLMs' inference and enhance LLM's perceive of key information, compress the prompt and KV-Cache, which achieves up to 20x compression with minimal performance loss. ”。

項目介紹

LLMLingua 是一個由微軟開發的專註於加快大規模語言模型 ( LLMs ) 的推斷速度和增強模型對關鍵信息感知的開源項目。通過創新的方式壓縮提示和 KV-Cache,LLMLingua 能達到高達 20 倍的壓縮效果,而且性能損失微乎其微。無論是 GPT2-samll,還是 LLaMA-7B,LLMLingua 都能有效地識別並移除非必需的提示字符串,從而實現高效的大規模語言模型推斷。在微軟的 LLMLingua 項目中,還推出了 LongLLMLingua 工具,它專門解決 LLMs 中在交流的中部模型迷失的問題,增強了長上下文信息處理的能力。隻使用 1/4 的 Token,就可以提高達 21.4% 的 RAG 性能。

當前大模型的應用現狀:

LLMLingua 的架構:

LLMLingua 的效果:

如何使用

使用 LLMLingua 的過程非常簡單,通過 pip 即可快速安裝,運行 pip install llmlingua。然後,你就可以使用(Long)LLMLingua 來壓縮你的提示了,代碼如下:

from llmlingua import PromptCompressor
llm_lingua = PromptCompressor()
compressed_prompt = llm_lingua.compress_prompt(prompt, instruction="", question="", target_token=200)
# > {'compressed_prompt': 'Question: Sam bought a dozen boxes, each with 30 highlighter pens inside, for $10 each box. He reanged five of boxes into packages of sixlters each and sold them $3 per. He sold the rest theters separately at the of three pens $2. How much did make in total, dollars?\nLets think step step\nSam bought 1 boxes x00 oflters.\nHe bought 12 * 300ters in total\nSam then took 5 boxes 6ters0ters.\nHe sold these boxes for 5 *5\nAfterelling these  boxes there were 3030 highlighters remaining.\nThese form 330 / 3 = 110 groups of three pens.\nHe sold each of these groups for $2 each, so made 110 * 2 = $220 from them.\nIn total, then, he earned $220   $15 = $235.\nSince his original cost was $120, he earned $235 - $120 = $115 in profit.\nThe answer is 115',
#  'origin_tokens': 2365,
#  'compressed_tokens': 211,
#  'ratio': '11.2x',
#  'saving': ', Saving $0.1 in GPT-4.'}
## Or use the phi-2 model,
## Before that, you need to update the transformers to the github version, like pip install -U git https://github.com/huggingface/transformers.git
llm_lingua = PromptCompressor("microsoft/phi-2")
## Or use the quantation model, like TheBloke/Llama-2-7b-Chat-GPTQ, only need <8GB GPU memory.
## Before that, you need to pip install optimum auto-gptq
llm_lingua = PromptCompressor("TheBloke/Llama-2-7b-Chat-GPTQ", model_config={"revision": "main"})

項目推介

作為微軟發佈的開源項目,LLMLingua 是由經驗豐富的開發者團隊開發的,他們定期更新和維護此項目,致力於為 LLMLingua 提供最新的功能和改進。根據多篇學術論文的研究表明,LLMLingua 在壓縮提示、加速推斷、增強大規模語言模型的信息處理能力等方面取得了顯著的成果。

以下是該項目 Star 趨勢圖(代表項目的活躍程度):

更多項目詳情請查看如下鏈接。

開源項目地址:https://github.com/microsoft/LLMLingua

開源項目作者:microsoft

以下是參與項目建設的所有成員:

關註我們,一起探索有意思的開源項目。