vllmsglang 单端口多模型部署方案
在单个端口下部署多个模型并通过统一API接口调用,可以通过请求路由层或分布式框架实现。以下是基于 vLLM 和 SGLang 的具体方案:
一、vLLM 方案
vLLM 本身不支持单端口多模型,但可通过以下方式实现:
-
多实例 + 反向代理
- 为每个模型启动独立的 vLLM 服务实例,绑定不同端口(如
8000
、8001
)。 - 使用 Nginx 或 FastAPI 作为路由层,根据请求参数(如
model_name
)转发到对应端口。 - 示例配置(Nginx):
location /v1/completions {if ($arg_model = "model1") { proxy_pass http://localhost:8000; }if ($arg_model = "model2") { proxy_pass http://localhost:8001; } }
- 优点:简单易实现,隔离性好。
- 为每个模型启动独立的 vLLM 服务实例,绑定不同端口(如
-
Ray 集群 + 动态路由
- 使用 Ray 管理多模型实例,通过
--tensor-parallel-size
和--pipeline-parallel-size
分配 GPU 资源。 - 自定义 FastAPI 服务,调用 Ray 集群的模型实例。
- 示例代码片段:
from fastapi import FastAPI app = FastAPI() @app.post("/generate") async def generate(model: str, prompt: str):if model == "qwen": return call_vllm("localhost:8000", prompt)elif model == "llama": return call_vllm("localhost:8001", prompt)
- 优点:适合生产环境,扩展性强。
- 使用 Ray 管理多模型实例,通过
二、SGLang 方案
SGLang 支持更灵活的多模型部署:
-
多进程 + 路由服务
- 每个模型启动独立进程,通过
--port
指定不同端口(如30001
、30002
)。 - 使用 SGLang 内置的
sglang_router
统一路由请求:# 启动路由服务(端口 30000) python -m sglang_router.launch_router --port 30000 # 启动模型实例 python -m sglang.launch_server --model-path model1 --port 30001 python -m sglang.launch_server --model-path model2 --port 30002
- 请求时通过
model
参数指定目标模型。
- 每个模型启动独立进程,通过
-
分布式多节点部署
- 跨节点部署时,通过
--nnodes
和--node-rank
分配模型,例如:# 节点0(模型A) python -m sglang.launch_server --model-path modelA --tp 2 --nnodes 2 --node-rank 0 # 节点1(模型B) python -m sglang.launch_server --model-path modelB --tp 2 --nnodes 2 --node-rank 1
- 统一通过路由器的
30000
端口调用。
- 跨节点部署时,通过
三、对比与选型建议
方案 | 适用场景 | 优点 | 缺点 |
---|---|---|---|
vLLM + Nginx | 简单多模型隔离 | 部署简单,兼容性好 | 需维护多个实例和代理配置 |
vLLM + Ray | 大规模分布式推理 | 资源利用率高,支持动态扩展 | 复杂度高,需熟悉 Ray 生态 |
SGLang 路由 | 快速轻量级多模型服务 | 内置路由,低延迟 | 对显存管理要求较高 |
SGLang 分布式 | 超大规模模型跨节点部署 | 支持多机多卡,扩展性强 | 网络配置复杂 |
四、注意事项
- 显存管理:调整
--mem-fraction-static
(如0.7
)避免 OOM。 - 性能监控:使用
nvidia-smi
和日志分析吞吐量。 - API 兼容性:确保路由层遵循 OpenAI API 格式(如
/v1/completions
)。
如果需要更详细的配置示例,可参考 vLLM 多节点部署指南 或 SGLang 官方文档。