跳到內容

GGUF

警告

請注意,vLLM 中對 GGUF 的支援目前仍處於高度實驗性和未最佳化階段,可能與其他功能不相容。目前,您可以將 GGUF 用作減少記憶體佔用的一種方式。如果您遇到任何問題,請向 vLLM 團隊報告。

警告

目前,vLLM 僅支援載入單檔案 GGUF 模型。如果您有多檔案 GGUF 模型,可以使用 gguf-split 工具將其合併為單檔案模型。

要使用 vLLM 執行 GGUF 模型,您可以從 TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF 下載並使用本地 GGUF 模型,命令如下:

wget https://huggingface.tw/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf
# We recommend using the tokenizer from base model to avoid long-time and buggy tokenizer conversion.
vllm serve ./tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf \
   --tokenizer TinyLlama/TinyLlama-1.1B-Chat-v1.0

您還可以新增 --tensor-parallel-size 2 以使用 2 個 GPU 啟用張量並行推理。

# We recommend using the tokenizer from base model to avoid long-time and buggy tokenizer conversion.
vllm serve ./tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf \
   --tokenizer TinyLlama/TinyLlama-1.1B-Chat-v1.0 \
   --tensor-parallel-size 2

警告

我們建議使用基礎模型中的分詞器,而不是 GGUF 模型中的。因為從 GGUF 進行分詞器轉換耗時且不穩定,特別是對於某些詞彙量大的模型。

GGUF 假設 Hugging Face 可以將元資料轉換為配置檔案。如果 Hugging Face 不支援您的模型,您可以手動建立配置檔案並將其作為 `hf-config-path` 傳遞。

# If you model is not supported by huggingface you can manually provide a huggingface compatible config path
vllm serve ./tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf \
   --tokenizer TinyLlama/TinyLlama-1.1B-Chat-v1.0 \
   --hf-config-path Tinyllama/TInyLlama-1.1B-Chat-v1.0

您還可以透過 LLM 入口點直接使用 GGUF 模型。

程式碼
from vllm import LLM, SamplingParams

# In this script, we demonstrate how to pass input to the chat method:
conversation = [
   {
      "role": "system",
      "content": "You are a helpful assistant"
   },
   {
      "role": "user",
      "content": "Hello"
   },
   {
      "role": "assistant",
      "content": "Hello! How can I assist you today?"
   },
   {
      "role": "user",
      "content": "Write an essay about the importance of higher education.",
   },
]

# Create a sampling params object.
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

# Create an LLM.
llm = LLM(model="./tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf",
         tokenizer="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
# Generate texts from the prompts. The output is a list of RequestOutput objects
# that contain the prompt, generated text, and other information.
outputs = llm.chat(conversation, sampling_params)

# Print the outputs.
for output in outputs:
   prompt = output.prompt
   generated_text = output.outputs[0].text
   print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")