Transformers 后端集成在 vLLM 中
Hugging Face Transformers 库为庞大的模型架构生态系统提供了灵活、统一的接口。从研究到在自定义数据集上进行微调,Transformers 都是首选工具包。
但是,当涉及到大规模部署这些模型时,推理速度和效率往往成为核心关注点。vLLM 应运而生,它是一个专为高吞吐量推理而设计的库,能够从 Hugging Face Hub 拉取模型并对其进行优化,以实现生产就绪的性能。
vLLM 代码库最近新增了一项功能,使其能够利用 Transformers 作为后端来运行模型。因此,vLLM 将在现有 Transformers 架构的基础上优化吞吐量/延迟。在这篇博文中,我们将探讨 vLLM 如何利用 Transformers 后端,将灵活性与效率相结合,使您能够更快、更智能地部署最先进的模型。
更新
本节将包含自博文首次发布(2025年4月11日)以来发生的所有更新。
支持视觉语言模型(2025年7月21日)
带有 Transformers 后端的 vLLM 现在支持视觉语言模型。当用户添加 model_impl="transformers"
时,将自动推断并加载适用于纯文本和多模态的正确类。
以下是如何使用 Transformers 后端来服务多模态模型。
vllm serve llava-hf/llava-onevision-qwen2-0.5b-ov-hf \
--model_impl transformers \
要使用该模型,可以通过如下方式使用 openai
API
from openai import OpenAI
openai_api_key = "EMPTY"
openai_api_base = "https://:8000/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
chat_response = client.chat.completions.create(
model="llava-hf/llava-onevision-qwen2-0.5b-ov-hf",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": "http://images.cocodataset.org/val2017/000000039769.jpg",
},
},
],
}],
)
print("Chat response:", chat_response)
您也可以直接使用 LLM
API 初始化 vLLM 引擎。以下是使用 LLM
API 提供相同模型的示例。
from vllm import LLM, SamplingParams
from PIL import Image
import requests
from transformers import AutoProcessor
model_id = "llava-hf/llava-onevision-qwen2-0.5b-ov-hf"
hf_processor = AutoProcessor.from_pretrained(model_id) # required to dynamically update the chat template
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "dummy_image.jpg"},
{"type": "text", "text": "What is the content of this image?"},
],
},
]
prompt = hf_processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
image = Image.open(
requests.get(
"http://images.cocodataset.org/val2017/000000039769.jpg", stream=True
).raw
)
# initialize the vlm using the `model_impl="transformers"`
vlm = LLM(
model="llava-hf/llava-onevision-qwen2-0.5b-ov-hf",
model_impl="transformers",
)
outputs = vlm.generate(
{
"prompt": prompt,
"multi_modal_data": {"image": image},
},
sampling_params=SamplingParams(max_tokens=100)
)
for o in outputs:
generated_text = o.outputs[0].text
print(generated_text)
# OUTPUTS:
# In the tranquil setting of this image, two feline companions are enjoying a peaceful slumber on a
# cozy pink couch. The couch, adorned with a plush red fabric across the seating area, serves as their perfect resting place.
#
# On the left side of the couch, a gray tabby cat is curled up at rest, its body relaxed in a display
# of feline serenity. One paw playfully stretches out, perhaps in mid-jump or simply exploring its surroundings.
Transformers 与 vLLM:推理实战
让我们从一个简单的文本生成任务开始,使用 meta-llama/Llama-3.2-1B
模型,看看这些库的表现如何。
使用 Transformers 进行推理
Transformers 库以其简洁性和多功能性而著称。使用其 pipeline
API,推理变得轻而易举
from transformers import pipeline
pipe = pipeline("text-generation", model="meta-llama/Llama-3.2-1B")
result = pipe("The future of AI is")
print(result[0]["generated_text"])
这种方法非常适合原型设计或小规模任务,但未针对高吞吐量推理或低延迟部署进行优化。
使用 vLLM 进行推理
vLLM 则另辟蹊径,通过 PagedAttention
(一种内存高效的注意力机制)和动态批处理等功能优先考虑效率。以下是 vLLM 中完成相同任务的示例
from vllm import LLM, SamplingParams
llm = LLM(model="meta-llama/Llama-3.2-1B")
params = SamplingParams(max_tokens=20)
outputs = llm.generate("The future of AI is", sampling_params=params)
print(f"Generated text: {outputs[0].outputs[0].text}")
vLLM 的推理速度明显更快,资源效率更高,尤其是在高负载下。例如,它能够以更低的 GPU 内存使用量处理每秒数千个请求。
vLLM 的部署超能力:OpenAI 兼容性
除了原始性能外,vLLM 还提供了与 OpenAI 兼容的 API,使其成为外部服务的直接替代品。启动服务器
vllm serve meta-llama/Llama-3.2-1B
然后使用 curl 进行查询
curl https://:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{"model": "meta-llama/Llama-3.2-1B", "prompt": "San Francisco is a", "max_tokens": 7, "temperature": 0}'
或者使用 Python 的 OpenAI 客户端
from openai import OpenAI
client = OpenAI(api_key="EMPTY", base_url="https://:8000/v1")
completion = client.completions.create(
model="meta-llama/Llama-3.2-1B",
prompt="San Francisco is a",
max_tokens=7,
temperature=0
)
print("Completion result:", completion.choices[0].text)
这种兼容性降低了成本并增强了控制力,让您能够利用 vLLM 的优化在本地扩展推理。
为什么我们需要 Transformers 后端?
Transformers 库针对贡献和添加新模型进行了优化。另一方面,向 vLLM 添加新模型则更为复杂一些。
在理想情况下,新模型一添加到 Transformers,我们就能在 vLLM 中使用它。随着 Transformers 后端的集成,我们正朝着这个理想世界迈进。
这是关于如何使您的 Transformers 模型与 vLLM 兼容以启用集成的官方文档。我们遵循此方法,使 modeling_gpt2.py
与集成兼容!您可以在此Transformers 拉取请求中查看更改。
对于一个已在 Transformers 中(并与 vLLM 兼容)的模型,我们需要做的是
llm = LLM(model="new-transformers-model", model_impl="transformers")
注意
添加 model_impl
参数并非严格必要。如果模型在 vLLM 中不受原生支持,vLLM 会自行切换到 Transformers 实现。
或者对于 Hugging Face Hub 中的自定义模型
llm = LLM(model="custom-hub-model", model_impl="transformers", trust_remote_code=True)
这个后端充当一座桥梁,将 Transformers 的即插即用灵活性与 vLLM 的推理能力相结合。您将获得两全其美的优势:使用 Transformers 进行快速原型开发,以及使用 vLLM 进行优化部署。
案例研究:Helium
Kyutai 团队的 Helium 尚未得到 vLLM 的支持。您可能希望使用 vLLM 对该模型运行优化推理,而这正是 Transformers 后端大显身手之处。
让我们看看它的实际应用
vllm serve kyutai/helium-1-preview-2b --model-impl transformers
使用 OpenAI API 进行查询
from openai import OpenAI
openai_api_key = "EMPTY"
openai_api_base = "https://:8000/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
completion = client.completions.create(model="kyutai/helium-1-preview-2b", prompt="What is AI?")
print("Completion result:", completion)
在这里,vLLM 有效地处理输入,利用 Transformers 后端无缝加载 kyutai/helium-1-preview-2b
。与在 Transformers 中原生运行相比,vLLM 提供了更低的延迟和更好的资源利用率。
通过将 Transformers 的模型生态系统与 vLLM 的推理优化相结合,您将解锁一个既灵活又可扩展的工作流程。无论您是在原型化新模型、部署自定义创作,还是扩展多模态应用程序,这种组合都能加速您从研究到生产的进程。