我爱普洱茶 发表于 2024-9-6 15:33:56

Llama 3 Web Demo 部署- XTuner 小助手微调- LMDeploy 部署 Llama 3

1 Llama 3 Web Demo 部署

本博客为基于机智流、Datawhale、ModelScope:Llama3-Tutorial(Llama 3 超级课堂)的作业。
1.1 环境部署

使用VSCode远程连接InterStudio开发机,并设置 VSCode 端口映射
使用conda创建虚拟环境,并安装对应的库
conda create -n llama3 python=3.10
conda activate llama3
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia
1.2 实践教程(InternStudio 版)

新建文件夹
mkdir -p ~/model
cd ~/model 从OpenXLab中获取权重(开发机中不需要使用此步)
安装 git-lfs 依靠
# 如果下面命令报错则使用 apt install git git-lfs -y
conda install git-lfs
git-lfs install
下载模子或软链接 InternStudio 中的模子(建议使用软链接方式)
#下载模型
git clone https://code.openxlab.org.cn/MrCat/Llama-3-8B-Instruct.git Meta-Llama-3-8B-Instruct
#软链接方式
ln -s /root/share/new_models/meta-llama/Meta-Llama-3-8B-Instruct ~/model/Meta-Llama-3-8B-Instruct
 
1.3 WebDemo部署

下载课程代码
cd ~
git clone https://github.com/SmartFlowAI/Llama3-Tutorial
安装 XTuner (会主动安装其他依靠)
cd ~
git clone -b v0.1.18 https://github.com/InternLM/XTuner
cd XTuner
pip install -e .
运行 web_demo.py
streamlit run ~/Llama3-Tutorial/tools/internstudio_web_demo.py \
~/model/Meta-Llama-3-8B-Instruct
 设置端口转发
https://img-blog.csdnimg.cn/direct/c60ccd4964bd486586b19f49f2ddc556.png
最终效果
 https://img-blog.csdnimg.cn/direct/807dbf6b680a4d1e9d66a39f63b2affd.png
2 XTuner 微调

2.1 自我认知练习数据集准备

cd ~/Llama3-Tutorial
python tools/gdata.py
2.2 XTuner设置文件准备

使用 configs/assistant/llama3_8b_instruct_qlora_assistant.py 设置文件 
2.3 练习模子

cd ~/Llama3-Tutorial

# 开始训练,使用 deepspeed 加速,A100 40G显存 耗时24分钟
xtuner train configs/assistant/llama3_8b_instruct_qlora_assistant.py --work-dir /root/llama3_pth

# Adapter PTH 转 HF 格式
xtuner convert pth_to_hf /root/llama3_pth/llama3_8b_instruct_qlora_assistant.py \
/root/llama3_pth/iter_500.pth \
/root/llama3_hf_adapter

# 模型合并
export MKL_SERVICE_FORCE_INTEL=1
xtuner convert merge /root/model/Meta-Llama-3-8B-Instruct \
/root/llama3_hf_adapter\
/root/llama3_hf_merged
#注意,路径前面要加空格,否则merge.py会报错,识别不到save_dir.
2.4 验证

streamlit run ~/Llama3-Tutorial/tools/internstudio_web_demo.py \
/root/llama3_hf_merged
https://img-blog.csdnimg.cn/direct/a7a7d885208b4baaaaeae4a8a354f3a1.png 
3 LMDeploy 部署 Llama 3 

3.1  环境设置

# studio-conda -t lmdeploy -o pytorch-2.1.2
# 初始化环境
conda create -n lmdeploy python=3.10
conda activate lmdeploy
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia 安装lmdeploy最新版
pip install -U lmdeploy 3.2 LMDeploy Chat CLI 工具

在终端运行
conda activate lmdeploy
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct https://img-blog.csdnimg.cn/direct/ab6b4696f0944e2cbd59e691830688c1.png 
3.3 LMDeploy模子量化(lite)

3.3.1 设置最大KV Cache缓存巨细

模子在运行时,占用的显存可大致分为三部分:模子参数自己占用的显存、KV Cache占用的显存,以及中心运算效果占用的显存。LMDeploy的KV Cache管理器可以通过设置--cache-max-entry-count参数,控制KV缓存占用剩余显存的最大比例。默认的比例为0.8。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct/
# 如果你是InternStudio 就使用
# studio-smi
nvidia-smi  https://img-blog.csdnimg.cn/direct/866a44c0774b4ced9c36f26509872e98.png
此时模子的占用为33236M。下面,改变--cache-max-entry-count参数,设为0.5。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct/ --cache-max-entry-count 0.5  https://img-blog.csdnimg.cn/direct/c4dac61c779542bfbd451fa50bd8b26a.png
新建一个终端运行
# 如果你是InternStudio 就使用
# studio-smi
nvidia-smi 把--cache-max-entry-count参数设置为0.01,约等于克制KV Cache占用显存。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct/ --cache-max-entry-count 0.01 https://img-blog.csdnimg.cn/direct/f7b352e9fba74d7c9a571d106fceb2dd.png 
3.3.2 使用W4A16量化

lmdeploy lite auto_awq \
   /root/model/Meta-Llama-3-8B-Instruct \
--calib-dataset 'ptb' \
--calib-samples 128 \
--calib-seqlen 1024 \
--w-bits 4 \
--w-group-size 128 \
--work-dir /root/model/Meta-Llama-3-8B-Instruct_4bit 下面使用Chat功能运行W4A16量化后的模子。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct_4bit --model-format awq 将KV Cache比例再次调为0.01,检察显存占用环境。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct_4bit --model-format awq --cache-max-entry-count 0.01 可以看到,显存占用变为6738MB,明显低落。
3.3.3 在线量化 KV

自 v0.4.0 起,LMDeploy KV 量化方式有原来的离线改为在线。并且,支持两种数值精度 int4、int8。量化方式为 per-head per-token 的非对称量化。
3.4 LMDeploy服务(serve)

3.4.1 启动API服务器

lmdeploy serve api_server \
    /root/model/Meta-Llama-3-8B-Instruct \
    --model-format hf \
    --quant-policy 0 \
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1 https://img-blog.csdnimg.cn/direct/1074d3fe828b485182d6fb193c3472bd.png
3.4.2 命令行客户端连接API服务器

通过VS Code新建一个终端: 激活conda环境
conda activate lmdeploy 运行命令行客户端
lmdeploy serve api_client http://localhost:23333 3.4.3 网页客户端连接API服务器

关闭刚刚的VSCode终端,但服务器端的终端不要关闭。 运行之前确保自己的gradio版本低于4.0.0。
pip install gradio==3.50.2 新建一个VSCode终端,激活conda环境。使用Gradio作为前端,启动网页客户端。
conda activate lmdeploy
lmdeploy serve gradio http://localhost:23333 \
    --server-name 0.0.0.0 \
    --server-port 6006

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Llama 3 Web Demo 部署- XTuner 小助手微调- LMDeploy 部署 Llama 3