跳到内容

GGUF

警告

请注意,vLLM 对 GGUF 的支持目前处于高度实验性阶段且尚未优化,可能与其他功能不兼容。目前,您可以利用 GGUF 来减少内存占用。如果遇到任何问题,请向 vLLM 团队报告。

警告

目前,vLLM 仅支持加载单文件 GGUF 模型。如果您有多个文件的 GGUF 模型,可以使用 gguf-split 工具将其合并为单个模型文件。

要在 vLLM 中运行 GGUF 模型,您可以使用 repo_id:quant_type 格式直接从 HuggingFace 加载。例如,从 unsloth/Qwen3-0.6B-GGUF 加载 Q4_K_M 量化模型:

# We recommend using the tokenizer from base model to avoid long-time and buggy tokenizer conversion.
vllm serve unsloth/Qwen3-0.6B-GGUF:Q4_K_M --tokenizer Qwen/Qwen3-0.6B

您还可以添加 --tensor-parallel-size 2 来启用使用 2 个 GPU 的张量并行推理。

vllm serve unsloth/Qwen3-0.6B-GGUF:Q4_K_M \
   --tokenizer Qwen/Qwen3-0.6B \
   --tensor-parallel-size 2

或者,您也可以下载并使用本地的 GGUF 文件。

wget https://hugging-face.cn/unsloth/Qwen3-0.6B-GGUF/resolve/main/Qwen3-0.6B-Q4_K_M.gguf
vllm serve ./Qwen3-0.6B-Q4_K_M.gguf --tokenizer Qwen/Qwen3-0.6B

警告

我们建议使用基础模型的分词器(tokenizer)而不是 GGUF 模型自带的分词器。因为从 GGUF 转换分词器既耗时又不稳定,特别是对于某些具有庞大词汇量的模型。

GGUF 假设 HuggingFace 可以将元数据转换为配置文件。如果 HuggingFace 不支持您的模型,您可以手动创建一个配置文件,并通过 hf-config-path 参数传入。

# If your model is not supported by HuggingFace you can manually provide a HuggingFace compatible config path
vllm serve unsloth/Qwen3-0.6B-GGUF:Q4_K_M \
   --tokenizer Qwen/Qwen3-0.6B \
   --hf-config-path Qwen/Qwen3-0.6B

您也可以通过 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 using repo_id:quant_type format.
llm = LLM(
   model="unsloth/Qwen3-0.6B-GGUF:Q4_K_M",
   tokenizer="Qwen/Qwen3-0.6B",
)
# 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}")