1. vllm环境配置
(1) python环境创建
VLLM官方提供的CUDA对应的版本有两个,分别是11.8和12.1。
- 配置的时候,要先看一下服务器的cuda版本,向下兼容;
- 其次,安装的torch只使用11.8和12.1的其中之一的cu版本;
- 最后,python环境建议版本是3.10-3.12,我安装的是python 3.10。
(2) 指定版本的 torch
torch, vllm, xformers 三者的版本是相互依赖的,更换任意版本,其他两个包需要相应更换。
pip install torch==2.3.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html
torch whl: https://download.pytorch.org/whl/cu118
(3) 安装xformers
用于加速llm推理。
pip install xformers=0.0.26.post1 -i https://yuna.pypi.tsinghua.edu.cn/simple
(4) 安装 vLLM
下载了官网的whl文件进行的安装,安装包很多,速度相对较慢。
pip install vllm-0.5.1+cu118-cp310-cp310-manylinux1_x86_64.whl
vllm whl: https://github.com/vllm-project/vllm/releases/tag/v0.5.1
(5) 建议参考的链接
- vllm 中文手册:https://vllm.hyper.ai/docs/getting-started/installation/gpu/
- CSDN安装总结:https://blog.csdn.net/first_three_sun/article/details/142816229
- 知乎安装总结:https://zhuanlan.zhihu.com/p/706432104
- xformers与torch对应版本:https://blog.csdn.net/BigerBang/article/details/140301608
2. vllm使用案例
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1,3,5,7"
from vllm import LLM
model_dir = "/data/premodel/llama3.1-8b" # 采用了本地存储的模型
llm = LLM(
model=model_dir,
rope_scaling={"type": "linear", "factor": 2.0}, # 这个是为应对llama报错设置的,别的模型考虑注释掉这一行
tensor_parallel_size=4, # 设置gpu并行4块
max_model_len=2048, # 防止超过使用内存
gpu_memory_utilization=0.9, # 使用GPU90%的内存
disable_custom_all_reduce=True, # 如果服务器不支持P2P通讯则使用这条命令行)
prompts = ["Hello, my name is",]
outputs = llm.generate(prompts)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")