AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


如何使用vLLM部署DeepSeek V2 Lite模型
发布日期:2024-09-06 08:40:33 浏览次数: 1746


最近在进行一些私有化部署大模型,很多企业会考虑数据隐私问题。因此常常会在企业内部部署自主部署大语言模型。常见的能够提供大语言模型的部署工具有,Ollama、vLLM、Xinference、SGLang和LM Studio。Ollama和LM Studio都是只支持GGUF类型量化的大语言模型,vLLM、Xinference和SGlang是支持pytorch或者说transformer类型的大模型,这些类型的模型通常在HuggingFace上都能找到。一般我们使用Ollama和LM Studio在桌面显卡领域部署,也就是个人电脑上部署使用。而在服务器领域,我们通常使用后三者。本篇我们主要关注于vLLM如何部署和量化DeepSeek大语言模型,机器环境是4卡Nvidia 2080Ti,大约48G显存。

1. 下载LLM模型

首先我们下载所需要的大语言模型,国内我们通常使用ModelScope[1]下载,它的速度较快也很稳定。我们使用modelscope官方提供的工具modelscope来下载,它有自动重连断点下载等功能。我们切换conda环境到base,然后安装modelscope。

conda activate base
pip install modelscope

然后我们访问modelscope找到我们要下载的模型,比如DeepSeek V2 Lite模型[2]

拷贝左上角模型的限定名称,然后使用如下命令,将其下载到当前目录。

modelscope download --model deepseek-ai/DeepSeek-V2-Lite-Chat --local_dir .

速度很快,可以达到30MB/s

2. 安装vLLM推理引擎

创建vLLM的虚拟环境并激活。

conda create -n vllm python=3.11
conda activate vllm

配置国内源,这样安装会快一点

conda config --show channels
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --set channel_priority flexible

接下来开始安装vLLM,根据官方文档[3]来操作。

pip install vllm

注意

使用pip默认安装的是基于CUDA 12.1编译的,它也兼容CUDA 12.2和12.4等环境。但如果你的CUDA版本是CUDA 11.8,你需要一些指定版本的操作。

# Install vLLM with CUDA 11.8.
export VLLM_VERSION=0.4.0
export PYTHON_VERSION=310
pip install https://github.com/vllm-project/vllm/releases/download/v${VLLM_VERSION}/vllm-${VLLM_VERSION}+cu118-cp${PYTHON_VERSION}-cp${PYTHON_VERSION}-manylinux1_x86_64.whl --extra-index-url https://download.pytorch.org/whl/cu118

3. 开始部署

使用如下命令,开始部署DeepSeek V2 Lite Chat模型。

CUAD_VISIBLE_DEVICES=0,1,2,3 python -m vllm.entrypoints.openai.api_server --model deepseek-ai/DeepSeek-V2-Lite-Chat --port 11434 --tensor-parallel-size 4 --gpu-memory-utilization 0.9 --max-model-len 8192 --trust-remote-code --enforce_eager --dtype=half 

有几个参数需要特别说明,更多的参数可以参考vLLM官方文档[4]

  • dtype - 数据类型,由于RTX20280Ti 只支持半精度类型的,因此dtype必须强制指定为half。
  • max-model-len - 指定上下文长度,它会自动预留KV Cache。虽然说DeepSeek V2支持128K上下文,但它也会导致占用大量的预留显存,因此一般我们一点一点尝试向上加,从而找到你显卡最佳上下文长度。我不确定是否有方法可以计算上下文长度所需显存。
  • gpu-memory-utilization - 指定显存利用率,默认0.9 假设48G,那么设置为0.9,意味着它最大可以使用48*0.9=43.2G
  • tensor-parallel-size - 张量并行推理,如果你是一机多卡,并且单张显卡显存无法承载大模型,那么你可以考虑启用该选项,大小根据你的显卡数量设置。

但我使用这里48G显存居然不够一个BF16的16B模型和KV Cache使用,报了如下错误。

ValueError: The model's max seq len (8192) is larger than the maximum number of tokens that can be stored in KV cache (7104). Try increasing gpu_memory_utilization or decreasing max_model_len when initializing the engine.

所以8K上下文导致了显存不够用,然而没有地方能够设置KV Cache大小,只能通过gpu_memory_utilization 参数设置,目前是0.9,增大到0.95能够启动并支持8K上下文了。速度勉强能够接受,速度大约每秒15 tokens。

4. 使用Lora

如果你在base模型基础上微调,那么可以通过以下方式指定Lora模型。比如

vllm serve meta-llama/Llama-2-7b-hf \
--enable-lora \
--lora-modules sql-lora=$HOME/.cache/huggingface/hub/models--yard1--llama-2-7b-sql-lora-test/snapshots/0dfa347e8877a4d4ed19ee56c140fa518470028c/

其中我们使用--enable-lora --lora-modules {name}={lora-path}来指定lora模型。在使用OAI兼容的接口请求时候,必须将模型名称指定为lora的模型名称。

curl http://localhost:8000/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "sql-lora",
        "prompt": "San Francisco is a",
        "max_tokens": 7,
        "temperature": 0
    }'
 | jq

5. 量化DeepSeek Lite Chat模型

量化模型,需要考虑显卡平台支持的量化类型,此处不考虑非N卡。由于RTX 2080Ti是Turing架构,它的计算能力是7.5,因此它不支持FP8量化。

此处这里使用AWQ进行4bit量化

pip install autoawq

还有一个依赖要单独安装,不然会报错。

ImportError: This modeling file requires the following packages that were not found in your environment: flash_attn. Run

pip install flash_attn

但安装总说找不到nvcc,所以执行

which nvcc

然后根据获得地址手动设置CUDA_HOME安装

CUDA_HOME=/usr/local/cuda pip install flash_attn

然后编译wheel时候,卡了一个多小时了,还没结束。注意量化有时候依赖和vllm不一致,可以考虑建立两个虚拟环境。接下来使用如下代码并执行即可开始量化。

from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer

model_path = 'hub/deepseek-ai/DeepSeek-V2-Lite-Chat/'
quant_path = 'hub/deepseek-ai/DeepSeek-V2-Lite-Chat-awq-int4/'
quant_config = { "zero_point"True"q_group_size"128"w_bit"4"version""GEMM" }

# Load model
model = AutoAWQForCausalLM.from_pretrained(model_path, **{"low_cpu_mem_usage"True})
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)

# Quantize
model.quantize(tokenizer, quant_config=quant_config)

# Save quantized model
model.save_quantized(quant_path)
tokenizer.save_pretrained(quant_path)

如果你的显卡支持FP8量化,可以使用AutoFP8进行离线量化。

git clone https://github.com/neuralmagic/AutoFP8.git
pip install -e AutoFP8

然后使用动态激活规模因子进行离线量化,不损失精度。

from auto_fp8 import AutoFP8ForCausalLM, BaseQuantizeConfig

pretrained_model_dir = "hub/deepseek-ai/DeepSeek-V2-Lite-Chat/"
quantized_model_dir = "hub/deepseek-ai/DeepSeek-V2-Lite-Chat-FP8/"

# Define quantization config with static activation scales
quantize_config = BaseQuantizeConfig(quant_method="fp8", activation_scheme="dynamic")
# For dynamic activation scales, there is no need for calbration examples
examples = []

# Load the model, quantize, and save checkpoint
model = AutoFP8ForCausalLM.from_pretrained(pretrained_model_dir, quantize_config)
model.quantize(examples)
model.save_quantized(quantized_model_dir)

总结

本文主要记录了我在RTX 4080Ti上部署DeepSeek V2 16B模型的过程。希望给大家一个参考,更多的参数还得看vLLM官方文档。此外,对于DeepSeek V2模型,它使用的MLA( Multi-head Latent Attention)目前vLLM尚未实现,但sglang最近实现了MLA,速度有了比较明显的提升。下一篇我们将会尝试一下。


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询