本指导适用于在 TencentOS Server 3 上使用 vLLM 推理框架运行 opt 模型的官方 Demo,以 Docker 方式启动。
vLLM 环境准备
1. 从 GitHub 下载 vLLM 开源仓库到本地。
#下载vLLM开源仓库到本地git clone https://github.com/vllm-project/vllm.gitcd vllm
2. 启动 vLLM 容器。
docker run --gpus all -it -e HF_ENDPOINT="https://hf-mirror.com" -v $PWD:/workspace --name=vllm --ipc=host nvcr.io/nvidia/pytorch:23.10-py3 /bin/bash
此时会从 nvcr 拉取 docker 镜像,请确保网络环境较好,直到镜像里所有层下载成功。成功后会直接进入容器内部。
检查必要的包的版本
1. 确保使用较新的 pip 包,并使用命令更新。
#更新pip包 python3 -m pip install --upgrade pip
2. 此外,vLLM 需要确保 Python 版本在3.8以上才能正常工作,请检查 Python 版本。
python
本指导中安装的 Python 版本为3.10,如发现版本不在 3.8 – 3.11 之间,请重新安装 Python。
注意:
vLLM 需要 GPU compute capability 7.0 或更高(例如 V100,T4,RTX20xx,A100,L4,H100等)。
安装 vLLM
可以使用以下两种方法在容器里安装 vLLM 框架:
#使用pip安装pip install vllm
或者:
#使用vLLM GitHub仓库目录下的setup.py安装pip install .
以上两种方式都可以安装 vLLM,请务必确保网络环境良好,否则容易安装失败。
注意:
如果尝试多次仍无法安装,请将 pip 换为国内清华源后再次尝试安装(此方法会极大的加快下载速度,强烈推荐!)。
#将pip换成清华源,以下方法二选一#临时使用pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package#设为默认,永久有效(推荐)pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
vLLM 安装完成后,可以通过以下命令确认是否安装完成以及查看 vLLM 版本:
#确认vLLM是否安装完成以及查看安装的vLLM版本python -c "import vllm; print(vllm.__version__)"
如果没有查找到 vLLM 库,请按照上述步骤重新安装。
运行模型
下载模型权重地址换源
1. 由于中国大陆无法下载 Hugging Face 网站模型,首先需要对下载网站换源,使用国内镜像网站的 HF-Mirror 模型。
说明:
如果 docker run 的时候加上了
-e HF_ENDPOINT="https://hf-mirror.com",则此步可以跳过。#单次有效,退出容器且暂停容器运行后失效,再次进入容器需重新输入此条命令 export HF_ENDPOINT="https://hf-mirror.com"#设为默认,永久有效,即便退出容器且暂停容器运行,再次进入容器后也可直接运行模型(推荐使用此方法)echo 'export HF_ENDPOINT="https://hf-mirror.com"' >> ~/.bashrc
2. 运行 opt-125m 的官方 Demo 位于 examples/offline_inference.py,运行该 Python 文件,则会自动开始下载模型并开始推理。
#运行Demopython examples/offline_inference.py
offline_inference.py 代码如下:
from vllm import LLM, SamplingParams# Sample prompts.prompts = ["Hello, my name is","The president of the United States is","The capital of France is","The future of AI is",]# Create a sampling params object.sampling_params = SamplingParams(temperature=0.8, top_p=0.95)# Create an LLM.llm = LLM(model="facebook/opt-125m")# Generate texts from the prompts. The output is a list of RequestOutput objects# that contain the prompt, generated text, and other information.outputs = llm.generate(prompts, sampling_params)# Print the outputs.for output in outputs:prompt = output.promptgenerated_text = output.outputs[0].textprint(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
该 Demo 会输入4条 prompts,通过 opt-125m 模型在 vLLM 推理框架下进行推理,最后给出生成的文字的结果。
生成的结果如下(参考):
Prompt: 'Hello, my name is', Generated text: ' Joel, my dad is my friend and we are in a relationship. I am'Prompt: 'The president of the United States is', Generated text: ' speaking out against the release of some State Department documents which show the Russians were involved'Prompt: 'The capital of France is', Generated text: ' known as the “Proud French capital”. What is this city'Prompt: 'The future of AI is', Generated text: ' literally in danger of being taken by any other company.\\nAgreed. '
注意事项
说明:
由于 OpenCloudOS 是 TencentOS Server 的开源版本,理论上上述文档当中的所有操作同样适用于 OpenCloudOS。
参考文档