最近在进行一些私有化部署大模型,很多企业会考虑数据隐私问题。因此常常会在企业内部部署自主部署大语言模型。常见的能够提供大语言模型的部署工具有,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显存。
首先我们下载所需要的大语言模型,国内我们通常使用ModelScope[1]下载,它的速度较快也很稳定。我们使用modelscope官方提供的工具modelscope来下载,它有自动重连断点下载等功能。我们切换conda环境到base,然后安装modelscope。
condaactivatebase
pipinstallmodelscope
然后我们访问modelscope找到我们要下载的模型,比如DeepSeek V2 Lite模型[2]。
拷贝左上角模型的限定名称,然后使用如下命令,将其下载到当前目录。
modelscopedownload--modeldeepseek-ai/DeepSeek-V2-Lite-Chat--local_dir.
速度很快,可以达到30MB/s
创建vLLM的虚拟环境并激活。
condacreate-nvllmpython=3.11
condaactivatevllm
配置国内源,这样安装会快一点
condaconfig--showchannels
condaconfig--addchannelshttps://mirrors.ustc.edu.cn/anaconda/pkgs/main/
condaconfig--addchannelshttps://mirrors.ustc.edu.cn/anaconda/pkgs/free/
condaconfig--addchannelshttps://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
condaconfig--addchannelshttps://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
condaconfig--setchannel_priorityflexible
接下来开始安装vLLM,根据官方文档[3]来操作。
pip install vllm
注意
使用pip默认安装的是基于CUDA 12.1编译的,它也兼容CUDA 12.2和12.4等环境。但如果你的CUDA版本是CUDA 11.8,你需要一些指定版本的操作。
#InstallvLLMwithCUDA11.8.
exportVLLM_VERSION=0.4.0
exportPYTHON_VERSION=310
pipinstallhttps://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-urlhttps://download.pytorch.org/whl/cu118
使用如下命令,开始部署DeepSeek V2 Lite Chat模型。
CUAD_VISIBLE_DEVICES=0,1,2,3python-mvllm.entrypoints.openai.api_server--modeldeepseek-ai/DeepSeek-V2-Lite-Chat--port11434--tensor-parallel-size4--gpu-memory-utilization0.9--max-model-len8192--trust-remote-code--enforce_eager--dtype=half
有几个参数需要特别说明,更多的参数可以参考vLLM官方文档[4]
但我使用这里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_utilizationor decreasingmax_model_lenwhen initializing the engine.
所以8K上下文导致了显存不够用,然而没有地方能够设置KV Cache大小,只能通过gpu_memory_utilization 参数设置,目前是0.9,增大到0.95能够启动并支持8K上下文了。速度勉强能够接受,速度大约每秒15 tokens。
如果你在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的模型名称。
curlhttp://localhost:8000/v1/completions\
-H"Content-Type:application/json"\
-d'{
"model":"sql-lora",
"prompt":"SanFranciscoisa",
"max_tokens":7,
"temperature":0
}'|jq
量化模型,需要考虑显卡平台支持的量化类型,此处不考虑非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,所以执行
whichnvcc
然后根据获得地址手动设置CUDA_HOME安装
CUDA_HOME=/usr/local/cudapipinstallflash_attn
然后编译wheel时候,卡了一个多小时了,还没结束。注意量化有时候依赖和vllm不一致,可以考虑建立两个虚拟环境。接下来使用如下代码并执行即可开始量化。
fromawqimportAutoAWQForCausalLM
fromtransformersimportAutoTokenizer
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"}
#Loadmodel
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)
#Savequantizedmodel
model.save_quantized(quant_path)
tokenizer.save_pretrained(quant_path)
如果你的显卡支持FP8量化,可以使用AutoFP8进行离线量化。
gitclonehttps://github.com/neuralmagic/AutoFP8.git
pipinstall-eAutoFP8
然后使用动态激活规模因子进行离线量化,不损失精度。
fromauto_fp8importAutoFP8ForCausalLM,BaseQuantizeConfig
pretrained_model_dir="hub/deepseek-ai/DeepSeek-V2-Lite-Chat/"
quantized_model_dir="hub/deepseek-ai/DeepSeek-V2-Lite-Chat-FP8/"
#Definequantizationconfigwithstaticactivationscales
quantize_config=BaseQuantizeConfig(quant_method="fp8",activation_scheme="dynamic")
#Fordynamicactivationscales,thereisnoneedforcalbrationexamples
examples=[]
#Loadthemodel,quantize,andsavecheckpoint
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,速度有了比较明显的提升。下一篇我们将会尝试一下。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |