目前,OC支持了包括 TensorRT、PyTorch、Paddle 在内的超过30款的主流或热门推理框架、训练框架、应用框架及其WebUI应用。OC将推出「OC+AI上手」系列,教你如何从0到1,在OC 8上部署和训练相关模型及框架,从而获得高效、灵活、易用、稳定和可扩展的AI模型开发和部署体验,同时更好地利用硬件资源,提高你的整体计算效率。
一、环境准备
1、Docker 环境准备
安装 Docker
若有旧版本,需要删除旧版本,不然会有冲突。
yum remove docker* -y
安装 docker 包后。
yum install docker -y
启动 Docker daemon
注意: 为了防止后续运行容器时出现如下错误: ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 该错误是由于 Docker daemon 没有正确启动导致的,需要先开启 Docker daemon。 |
#开启Docker daemon (server)sudo systemctl start docker
#使用以下命令检查Docker daemon是否正在运行,此时应该看到Docker Client和Docker Server都在运行中sudo systemctl status docker
安装 NVIDIA Container Toolkit
在容器里运行模型,如果想要在 GPU 上运行,则需要安装 NVIDIA Container Toolkit,可参见 NVIDIA Container Toolkit 安装指引(https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)。
#配置安装所需要的库curl -s -L https://nvidia.github.io/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo | \sudo tee /etc/yum.repos.d/nvidia-container-toolkit.repo
#配置库以便可以使用各种包sudo yum-config-manager --enable nvidia-container-toolkit-experimental
#安装NVIDIA Container Toolkit包sudo yum install -y nvidia-container-toolkit
配置 Docker
#使用nvidia-ctk命令修改并更新/etc/docker/daemon.json主机上的文件,以便Docker可以使用NVIDIA容器运行。sudo nvidia-ctk runtime configure --runtime=docker
#重新启动Dockersudo systemctl restart docker
2、配置客户端与远程 GitHub 仓库的连接
注意: 为了防止后续拉取 GitHub 仓库代码出现如下错误: fatal: Could not read from remote repository. 则说明客户端还没有配置与远程 GitHub 仓库的连接,需要先配置 ssh 远程连接。 |
#生成密钥,其中youremail@example.com需替换成自己在GitHub上注册账号时所用的邮箱ssh-keygen -t rsa -C "youremail@example.com"
打开生成的 /.ssh/id_rsa,复制密钥,在 GitHub 网站登录自己的账户,在账户选项中选择Settings > SSH and GPG keys > New SSH key,把密钥填写到账户中(注意填写时的格式要求)。
#测试客户端是否和GitHub远程连接上,出现Success则配置成功ssh -T git@github.com
安装 git 包
(1)检查系统是否安装 git:
git
注意: 如果出现 -bash: git: command not found,则说明环境还没有安装 git 包。 |
(2)安装 git:
#安装gitsudo yum -y install git
#检查git版本git --version
如果此时正常出现 git 的版本号,则说明安装成功。
3、参考文档
https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
https://huggingface.co/
二、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、检查必要的包的版本
(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等)。 |
2、安装 vLLM
可以使用以下两种方法在容器里安装 vLLM 框架:
#使用pip安装pip install vllm
或者:
#使用vLLM GitHub仓库目录下的setup.py安装pip install .
以上两种方式都可以安装 vLLM,请务必确保网络环境良好,否则容易安装失败。
注意: 如果尝试多次仍无法安装,请将 pip 换为国内清华源后再次尝试安装(此方法会极大的加快下载速度,强烈推荐!)。
|
vLLM 安装完成后,可以通过以下命令确认是否安装完成以及查看 vLLM 版本:
#确认vLLM是否安装完成以及查看安装的vLLM版本python -c "import vllm; print(vllm.__version__)"
如果没有查找到 vLLM 库,请按照上述步骤重新安装。
三、运行模型
1、下载模型权重地址换源
(1) 由于中国大陆无法下载 Hugging Face(https://huggingface.co/) 网站模型,首先需要对下载网站换源,使用国内镜像网站的 HF-Mirror 模型。
说明: 如果 docker run 的时候加上了 |
#单次有效,退出容器且暂停容器运行后失效,再次进入容器需重新输入此条命令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.prompt generated_text = output.outputs[0].text print(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. '
参考文档
vllm GitHub(https://github.com/vllm-project/vllm/tree/main)
vllm 安装指引(https://docs.vllm.ai/en/stable/getting_started/installation.html)
清华 pipy 镜像源(https://mirrors.tuna.tsinghua.edu.cn/help/pypi/)
Hugging Face 镜像源(https://hf-mirror.com/)
Hugging Face opt-125模型(https://huggingface.co/facebook/opt-125m)
关注腾讯开源公众号
获取更多最新腾讯官方开源信息!
加微信进群即可了解更多“腾讯开源新动态”!
添加微信请备注:腾讯开源