实验环境
- OS: Ubuntu 24.04
- Python: 3.11
- GPU: NVIDIA GeForce RTX 4090 (2个)
- CUDA Version: 12.6
vLLM安装
模型下载
预先使用huggingface-cli下载Qwen/Qwen2.5-14B-Instruct。
Qwen2.5-14B-Instruct部署
启动为兼容OpenAI的API服务。
单机双卡设置CUDA_VISIBLE_DEVICES环境变量。
export CUDA_VISIBLE_DEVICES=0,1设置了HF_HUB_OFFLINE=1将不会向Hugging Face Hub发起任何HTTP调用。加快加载时间,这也特别适合服务器没有外网访问时。
export HF_HUB_OFFLINE=1启动服务:
vllm serve Qwen/Qwen2.5-14B-Instruct \
--served-model-name qwen2.5-14b-instruct \
--enable-auto-tool-choice \
--tool-call-parser hermes \
--max-model-len=32768 \
--tensor-parallel-size 2 \
--port 8000--tensor-parallel-size 2
--tensor-parallel-size 2表示使用Tensor Parallelism技术来分配模型跨两个GPU
Tensor Parallelism是一种分布式深度学习技术,用于处理大型模型。
当--tensor-parallel-size 设置为 2 时,模型的参数和计算会被分割成两部分,分别在两个GPU上进行处理。
这种方法可以有效地减少每个GPU上的内存使用,使得能够加载和运行更大的模型。
同时,它还可以在一定程度上提高计算速度,因为多个GPU可以并行处理模型的不同部分。
Tensor Parallelism对于大型语言模型(如 Qwen2.5-14B-Instruct)特别有用,因为这些模型通常太大,无法完全加载到单个GPU的内存中。
测试兼容OpenAI的API服务
通过curl 命令查看当前的模型列表:
curl -s http://localhost:8000/v1/models | jq .
{
"object": "list",
"data": [
{
"id": "qwen2.5-14b-instruct",
"object": "model",
"created": 1728454502,
"owned_by": "vllm",
"root": "Qwen/Qwen2.5-14B-Instruct",
"parent": null,
"max_model_len": 32768,
"permission": [
{
"id": "modelperm-e269177fea994b4aa7364bfc40992219",
"object": "model_permission",
"created": 1728454502,
"allow_create_engine": false,
"allow_sampling": true,
"allow_logprobs": true,
"allow_search_indices": false,
"allow_view": true,
"allow_fine_tuning": false,
"organization": "*",
"group": null,
"is_blocking": false
}
]
}
]
}通过curl命令测试chat completions API:
curl -s http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
"model": "qwen2.5-14b-instruct",
"messages": [
{"role": "system", "content": "你是一个数学家."},
{"role": "user", "content": "9.11和9.8这两个小数谁比较大?"}
],
"max_tokens": 512
}' | jq '.choices[0].message.content'
"比较两个小数9.11和9.8的大小,可以遵循以下步骤:\n\n1. **比较整数部分**:9.11和9.8的整数部分都是9,所以需要比较小数部分。\n2. **比较小数部分**:9.11的小数部分是0.11,而9.8的小数部分是0.8。\n\n为了更容易比较,可以将0.8写成0.80,这样两个数的小数部分就都有两位了。\n- 9.11的小数部分是0.11。\n- 9.8的小数部分是0.80。\n\n显然,0.80 > 0.11,因此9.8 > 9.11。\n\n所以,9.8比9.11大。"通过curl命令测试tool calling:
curl -s http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
"model": "qwen2.5-14b-instruct",
"messages": [
{ "role": "user", "content": "What is 3 * 12? Also, what is 11 + 49?" }
],
"parallel_tool_calls": false,
"tools": [
{
"type": "function",
"function": {
"name": "add",
"description": "Add two integers.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "integer"},
"b": {"type": "integer"}
},
"required": ["a", "b"]
}
}
},
{
"type": "function",
"function": {
"name": "multiply",
"description": "Multiply two integers.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "integer"},
"b": {"type": "integer"}
},
"required": ["a", "b"]
}
}
}
]
}' | jq '.choices[0].message.tool_calls'[
{
"id": "chatcmpl-tool-ef9f47970bbb40539df865e89fb6a347",
"type": "function",
"function": {
"name": "multiply",
"arguments": "{\"a\": 3, \"b\": 12}"
}
},
{
"id": "chatcmpl-tool-c37a4dadc5d94d0a9daa7fc4d9a3f7a4",
"type": "function",
"function": {
"name": "add",
"arguments": "{\"a\": 11, \"b\": 49}"
}
}
]使用systemd配置为系统服务
使用systemd将前面部署的qwen2.5-14b-instruct配置为系统服务。
/etc/systemd/system/qwen2.5-14b-instruct.service:
[Unit]
Description=qwen2.5-14b-instruct
After=network.target
[Service]
Type=simple
Environment="CUDA_VISIBLE_DEVICES=0,1"
Environment="HF_HUB_OFFLINE=1"
WorkingDirectory=/home/<thuser>/vllm
User=<theuser>
ExecStart=/bin/bash -c 'source .venv/bin/activate && \
vllm serve Qwen/Qwen2.5-14B-Instruct \
--served-model-name qwen2.5-14b-instruct \
--enable-auto-tool-choice \
--tool-call-parser hermes \
--max-model-len=32768 \
--tensor-parallel-size 2 \
--port 8000'
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.targetsystemctl enable qwen2.5-14b-instruct启动服务:
systemctl start qwen2.5-14b-instruct查看启动日志:
journalctl -u qwen2.5-14b-instruct -f