推測解碼¶
警告
請注意,vLLM 中的推測解碼尚未最佳化,通常無法針對所有提示資料集或採樣引數實現token間延遲的降低。最佳化工作正在進行中,您可以在此處關注: 問題 #4630
警告
目前,vLLM 中的推測解碼與流水線並行不相容。
本文件展示瞭如何在 vLLM 中使用 推測解碼。推測解碼是一種提高記憶體受限 LLM 推理中token間延遲的技術。
使用草稿模型進行推測¶
以下程式碼配置 vLLM 以離線模式使用推測解碼和草稿模型,每次推測 5 個 token。
程式碼
from vllm import LLM, SamplingParams
prompts = [
"The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
llm = LLM(
model="facebook/opt-6.7b",
tensor_parallel_size=1,
speculative_config={
"model": "facebook/opt-125m",
"num_speculative_tokens": 5,
},
)
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
要在線上模式下執行相同操作,請啟動伺服器
python -m vllm.entrypoints.openai.api_server \
--host 0.0.0.0 \
--port 8000 \
--model facebook/opt-6.7b \
--seed 42 \
-tp 1 \
--gpu_memory_utilization 0.8 \
--speculative_config '{"model": "facebook/opt-125m", "num_speculative_tokens": 5}'
警告
注意:請使用 --speculative_config
來設定所有與推測解碼相關的配置。之前透過 --speculative_model
指定模型並單獨新增相關引數(例如 --num_speculative_tokens
)的方法現已棄用。
然後使用客戶端
程式碼
from openai import OpenAI
# Modify OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = "https://:8000/v1"
client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key=openai_api_key,
base_url=openai_api_base,
)
models = client.models.list()
model = models.data[0].id
# Completion API
stream = False
completion = client.completions.create(
model=model,
prompt="The future of AI is",
echo=False,
n=1,
stream=stream,
)
print("Completion results:")
if stream:
for c in completion:
print(c)
else:
print(completion)
透過匹配提示中的 n-gram 進行推測¶
以下程式碼配置 vLLM 使用推測解碼,其中提議是透過匹配提示中的 n-gram 生成的。欲瞭解更多資訊,請閱讀 此主題帖。
程式碼
from vllm import LLM, SamplingParams
prompts = [
"The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
llm = LLM(
model="facebook/opt-6.7b",
tensor_parallel_size=1,
speculative_config={
"method": "ngram",
"num_speculative_tokens": 5,
"prompt_lookup_max": 4,
},
)
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
使用 MLP 推測器進行推測¶
以下程式碼配置 vLLM 使用推測解碼,其中提議是由草稿模型生成的,這些草稿模型根據上下文向量和取樣 token 來調整草稿預測。欲瞭解更多資訊,請參閱 這篇部落格 或 這份技術報告。
程式碼
from vllm import LLM, SamplingParams
prompts = [
"The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
llm = LLM(
model="meta-llama/Meta-Llama-3.1-70B-Instruct",
tensor_parallel_size=4,
speculative_config={
"model": "ibm-ai-platform/llama3-70b-accelerator",
"draft_tensor_parallel_size": 1,
},
)
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
請注意,這些推測模型目前需要在沒有張量並行的情況下執行,儘管主模型可以使用張量並行執行(參見上例)。由於推測模型相對較小,我們仍然可以看到顯著的加速。然而,此限制將在未來版本中修復。
HF hub 上有多種此類推測模型可供選擇
- llama-13b-accelerator
- llama3-8b-accelerator
- codellama-34b-accelerator
- llama2-70b-accelerator
- llama3-70b-accelerator
- granite-3b-code-instruct-accelerator
- granite-8b-code-instruct-accelerator
- granite-7b-instruct-accelerator
- granite-20b-code-instruct-accelerator
使用基於 EAGLE 的草稿模型進行推測¶
以下程式碼配置 vLLM 使用推測解碼,其中提議由基於 EAGLE (Extrapolation Algorithm for Greater Language-model Efficiency) 的草稿模型生成。一個更詳細的離線模式示例,包括如何提取請求級別接受率,可以在 此處找到。
程式碼
from vllm import LLM, SamplingParams
prompts = [
"The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
llm = LLM(
model="meta-llama/Meta-Llama-3-8B-Instruct",
tensor_parallel_size=4,
speculative_config={
"model": "yuhuili/EAGLE-LLaMA3-Instruct-8B",
"draft_tensor_parallel_size": 1,
"num_speculative_tokens": 2,
},
)
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
使用基於 EAGLE 的草稿模型時需要考慮的幾個重要事項
-
在 EAGLE 模型 HF 倉庫 中可用的 EAGLE 草稿模型在 拉取請求 #12304 之後應該能夠直接被 vLLM 載入和使用。如果您使用的 vllm 版本在 拉取請求 #12304 之前,請使用 指令碼 轉換推測模型,並在
speculative_config
中指定"model": "path/to/modified/eagle/model"
。如果在使用最新版本的 vLLM 時仍然出現權重載入問題,請留言或提出問題。 -
基於 EAGLE 的草稿模型需要在沒有張量並行的情況下執行(即
speculative_config
中draft_tensor_parallel_size
設定為 1),儘管主模型可以使用張量並行執行(參見上例)。 -
在使用基於 EAGLE 的推測器與 vLLM 配合時,觀察到的加速比參考實現 此處 中報告的要低。此問題正在調查中,並在此處跟蹤: 問題 #9565。
Hugging Face hub 上有多種 EAGLE 草稿模型可供選擇
基礎模型 | Hugging Face 上的 EAGLE 模型 | # EAGLE 引數 |
---|---|---|
Vicuna-7B-v1.3 | yuhuili/EAGLE-Vicuna-7B-v1.3 | 0.24B |
Vicuna-13B-v1.3 | yuhuili/EAGLE-Vicuna-13B-v1.3 | 0.37B |
Vicuna-33B-v1.3 | yuhuili/EAGLE-Vicuna-33B-v1.3 | 0.56B |
LLaMA2-Chat 7B | yuhuili/EAGLE-llama2-chat-7B | 0.24B |
LLaMA2-Chat 13B | yuhuili/EAGLE-llama2-chat-13B | 0.37B |
LLaMA2-Chat 70B | yuhuili/EAGLE-llama2-chat-70B | 0.99B |
Mixtral-8x7B-Instruct-v0.1 | yuhuili/EAGLE-mixtral-instruct-8x7B | 0.28B |
LLaMA3-Instruct 8B | yuhuili/EAGLE-LLaMA3-Instruct-8B | 0.25B |
LLaMA3-Instruct 70B | yuhuili/EAGLE-LLaMA3-Instruct-70B | 0.99B |
Qwen2-7B-Instruct | yuhuili/EAGLE-Qwen2-7B-Instruct | 0.26B |
Qwen2-72B-Instruct | yuhuili/EAGLE-Qwen2-72B-Instruct | 1.05B |
推測解碼的無損保證¶
在 vLLM 中,推測解碼旨在提高推理效率同時保持準確性。本節闡述了推測解碼的無損保證,將這些保證分為三個關鍵領域
-
理論無損性 - 推測解碼取樣在理論上是無損的,直至硬體數值精度限制。浮點錯誤可能導致輸出分佈的輕微變化,如 使用推測取樣加速大型語言模型解碼 中所述。
-
演算法無損性 - vLLM 的推測解碼實現經過演算法驗證是無損的。關鍵驗證測試包括
- 拒絕取樣器收斂性:確保 vLLM 的拒絕取樣器生成的樣本與目標分佈一致。 檢視測試程式碼
- 貪婪取樣等效性:確認使用推測解碼的貪婪取樣與不使用推測解碼的貪婪取樣結果一致。這驗證了 vLLM 的推測解碼框架在與 vLLM 前向傳遞和 vLLM 拒絕取樣器整合時,提供了無損保證。幾乎所有位於 tests/spec_decode/e2e 的測試都使用 此斷言實現 來驗證此屬性。
-
vLLM 對數機率穩定性 - vLLM 目前不保證穩定的 token 對數機率(logprobs)。這可能導致同一請求在不同執行中產生不同的輸出。有關更多詳細資訊,請參閱 常見問題 中題為 vLLM 中同一提示的輸出在不同執行中是否會變化? 的常見問題部分。
雖然 vLLM 致力於確保推測解碼的無損性,但使用和不使用推測解碼生成的輸出可能因以下因素而異
- 浮點精度:硬體數值精度的差異可能導致輸出分佈的輕微偏差。
- 批處理大小和數值穩定性:批處理大小的變化可能導致對數機率和輸出機率的波動,這可能是由於批處理操作中的非確定性行為或數值不穩定造成的。
有關緩解策略,請參閱 常見問題 中 vLLM 中同一提示的輸出在不同執行中是否會變化? 的常見問題條目。