-
硬件需求:
最好GPU>=24G,(本项目使用a100演示),最小要求GPU显存8G才达到对轻量级7B参数微调
-
软件需求:
vscode(如有远程请自行搜索),Anaconda,python,cuda(配置自行搜索)
-
实践1-运行web-demo
-
环境配置
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
-
下载模型
#新建文件夹
mkdir -p ~/model cd ~/model
#从OpenXLab中获取权重
#安装 git-lfs 依赖
#如果下面命令报错则使用 apt install git git-lfs -yconda install git-lfs git-lfs install
#下载模型
git clone https://code.openxlab.org.cn/MrCat/Llama-3-8B-Instruct.git Meta-Llama-3-8B-Instruct
#或者软链接 InternStudio 中的模型
ln -s /root/share/new_models/meta-llama/Meta-Llama-3-8B-Instruct ~/model/Meta-Llama-3-8B-Instruct
-
web demo部署
cd ~ git clone https://github.com/SmartFlowAI/Llama3-Tutorial #或者 git clone https://github.com/SmartFlowAI/Llama3-XTuner-CN
#安装 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
-
在vscode配置转发端口(点击端口,转发端口8501)
-
结果展示
-
-
实践2-Xtuner微调Llama3个人小助手认知
-
上面配置过Tutorial,只需要将Xtuner克隆到本地即可(远程打开文件夹)
-
使用XTuner进行微调(本次微调使用的时QLoRA)
-
打开Llama3-Tutorial----tools-----gdata.py
-
修改name
-
运行生成json文件
#切换到该文件夹下 python tools/gdata.py
-
如需修改配置文件和对话模板—修改对应的路径(configs/assistant/llama3_8b_instruct_qlora_assistant.py)
-
训练模型
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(huggingface) 格式 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
-
推理验证(/root/llama3_hf_merged)
streamlit run ~/Llama3-Tutorial/tools/internstudio_web_demo.py \ /root/llama3_hf_merged
7.结果展示
-
-
-
实践3-llama3图片理解能力微调
-
环境配置—前面已经配置好了,直接进入环境中
conda activate llama3 cd XTuner pip install .[all]
-
图片理解训练数据准备
-
模型准备
准备 Llama3 权重
在微调开始前,我们首先来准备 Llama3-8B-Instruct 模型权重。
- InternStudio
mkdir -p ~/model cd ~/model ln -s /root/share/new_models/meta-llama/Meta-Llama-3-8B-Instruct .
- 非 InternStudio
我们选择从 OpenXLab 上下载 Meta-Llama-3-8B-Instruct 的权重。
mkdir -p ~/model cd ~/model git lfs install git clone https://code.openxlab.org.cn/MrCat/Llama-3-8B-Instruct.git Meta-Llama-3-8B-Instruct
准备 Visual Encoder 权重
我们接下来准备 Llava 所需要的 openai/clip-vit-large-patch14-336,权重,即 Visual Encoder 权重。
- InternStudio
mkdir -p ~/model cd ~/model ln -s /root/share/new_models/openai/clip-vit-large-patch14-336 .
- 非 InternStudio
可以访问 https://huggingface.co/openai/clip-vit-large-patch14-336 以进行下载。
准备 Image Projector 权重
然后我们准备 Llava 将要用到的 Image Projector 部分权重。
- InternStudio
mkdir -p ~/model cd ~/model ln -s /root/share/new_models/xtuner/llama3-llava-iter_2181.pth .
- 非 InternStudio
相关权重可以访问:https://huggingface.co/xtuner/llava-llama-3-8b 以及 https://huggingface.co/xtuner/llava-llama-3-8b-v1_1 。(已经过微调,并非 Pretrain 阶段的 Image Projector)
-
数据准备
我们按照 https://github.com/InternLM/Tutorial/blob/camp2/xtuner/llava/xtuner_llava.md 中的教程来准备微调数据。为了让大家可以快速上手,我们选择了使用过拟合的方式快速实现。
可以执行以下代码:
cd ~ git clone https://github.com/InternLM/tutorial -b camp2 python ~/tutorial/xtuner/llava/llava_data/repeat.py \ -i ~/tutorial/xtuner/llava/llava_data/unique_data.json \ -o ~/tutorial/xtuner/llava/llava_data/repeated_data.json \ -n 200
-
-
XTurner微调
-
训练启动
我们已经为大家准备好了可以一键启动的配置文件,主要是修改好了模型路径、对话模板以及数据路径。
我们使用如下指令以启动训练:
xtuner train ~/Llama3-Tutorial/configs/llama3-llava/llava_llama3_8b_instruct_qlora_clip_vit_large_p14_336_lora_e1_finetune.py --work-dir ~/llama3_llava_pth --deepspeed deepspeed_zero2_offload
训练过程所需显存约为44447 MiB,在单卡 A100 上训练所需时间为30分钟。
在训练好之后,我们将原始 image projector 和 我们微调得到的 image projector 都转换为 HuggingFace 格式,为了下面的效果体验做准备。
xtuner convert pth_to_hf ~/Llama3-Tutorial/configs/llama3-llava/llava_llama3_8b_instruct_qlora_clip_vit_large_p14_336_lora_e1_finetune.py \ ~/model/llama3-llava-iter_2181.pth \ ~/llama3_llava_pth/pretrain_iter_2181_hf xtuner convert pth_to_hf ~/Llama3-Tutorial/configs/llama3-llava/llava_llama3_8b_instruct_qlora_clip_vit_large_p14_336_lora_e1_finetune.py \ ~/llama3_llava_pth/iter_1200.pth \ ~/llama3_llava_pth/iter_1200_hf
-
提问
问题1:Describe this image. 问题2:What is the equipment in the image?
-
Pretrain模型—只会为图片打标签,并不能回答问题。
export MKL_SERVICE_FORCE_INTEL=1 xtuner chat /root/model/Meta-Llama-3-8B-Instruct \ --visual-encoder /root/model/clip-vit-large-patch14-336 \ --llava /root/llama3_llava_pth/pretrain_iter_2181_hf \ --prompt-template llama3_chat \ --image /root/tutorial/xtuner/llava/llava_data/test_img/oph.jpg
-
Finetune 后 模型—可以根据图片回答我们的问题了
export MKL_SERVICE_FORCE_INTEL=1 xtuner chat /root/model/Meta-Llama-3-8B-Instruct \ --visual-encoder /root/model/clip-vit-large-patch14-336 \ --llava /root/llama3_llava_pth/iter_1200_hf \ --prompt-template llama3_chat \ --image /root/tutorial/xtuner/llava/llava_data/test_img/oph.jpg
-
-
-
实践4-llama3agent能力+微调(Lagent)
-
环境配置如上
-
微调过程
-
模型准备
在微调开始前,我们首先来准备 Llama3-8B-Instruct 模型权重。
InternStudio
mkdir -p ~/model cd ~/model ln -s /root/share/new_models/meta-llama/Meta-Llama-3-8B-Instruct .
非 InternStudio
我们选择从 OpenXLab 上下载 Meta-Llama-3-8B-Instruct 的权重。
mkdir -p ~/model cd ~/model git lfs install git clone https://code.openxlab.org.cn/MrCat/Llama-3-8B-Instruct.git Meta-Llama-3-8B-Instruct
-
数据集
由于 HuggingFace 上的 Agent-FLAN 数据集暂时无法被 XTuner 直接加载,因此我们首先要下载到本地,然后转换成 XTuner 直接可用的格式。
- InternStudio
如果是在 InternStudio 上,我们已经准备好了一份转换好的数据,可以直接通过如下脚本准备好:
cd ~ cp -r /root/share/new_models/internlm/Agent-FLAN . chmod -R 755 Agent-FLAN
- 非 InternStudio
首先先来下载数据:
cd ~ git lfs install git clone https://huggingface.co/datasets/internlm/Agent-FLAN
我们已经在 SmartFlowAI/Llama3-Tutorial 仓库中已经准备好了相关转换脚本。
python ~/Llama3-Tutorial/tools/convert_agentflan.py ~/Agent-FLAN/data
在显示下面的内容后,就表示已经转换好了。转换好的数据位于 ~/Agent-FLAN/data_converted
Saving the dataset (1/1 shards): 100%|████████████| 34442/34442
-
微调
我们已经为大家准备好了可以一键启动的配置文件,主要是修改好了模型路径、对话模板以及数据路径。
我们使用如下指令以启动训练:
export MKL_SERVICE_FORCE_INTEL=1 xtuner train ~/Llama3-Tutorial/configs/llama3-agentflan/llama3_8b_instruct_qlora_agentflan_3e.py --work-dir ~/llama3_agent_pth --deepspeed deepspeed_zero2
在训练完成后,我们将权重转换为 HuggingFace 格式,并合并到原权重中。
# 转换权重 xtuner convert pth_to_hf ~/Llama3-Tutorial/configs/llama3-agentflan/llama3_8b_instruct_qlora_agentflan_3e.py \ ~/llama3_agent_pth/iter_18516.pth \ ~/llama3_agent_pth/iter_18516_hf
由于训练时间太长,我们也为大家准备好了已经训练好且转换为 HuggingFace 格式的权重,可以直接使用。路径位于
/share/new_models/agent-flan/iter_2316_hf
。如果要使用自己训练的权重,可以使用如下指令合并权重:
# 合并权重 export MKL_SERVICE_FORCE_INTEL=1 xtuner convert merge /root/model/Meta-Llama-3-8B-Instruct \ ~/llama3_agent_pth/iter_18516_hf \ ~/llama3_agent_pth/merged
如果要使用已经训练好的权重,可以使用如下指令合并权重:
export MKL_SERVICE_FORCE_INTEL=1 xtuner convert merge /root/model/Meta-Llama-3-8B-Instruct \ /share/new_models/agent-flan/iter_2316_hf \ ~/llama3_agent_pth/merged
-
-
启动-Llama3+Agent-FLAN ReAct Demo
pip install lagent streamlit run ~/Llama3-Tutorial/tools/agent_web_demo.py ~/llama3_agent_pth/merged
- 微调前 LLaMA3 路径:
/root/model/Meta-Llama-3-8B-Instruct
- 微调后 LLaMA3 路径:
/root/llama3_agent_pth/merged
- 微调前 LLaMA3 路径:
-
结果展示(记得转发端口)
-
感谢InternStudio提供的平台学习llama3,以及b站up主机智流教学https://www.bilibili.com/video/BV1Kr421u71u/?spm_id_from=333.788
-
-
总结
通过本次学习熟悉在云服务器上部署llama3-web-demo,使用Xtuner完成小助手认知微调,使用LMDeploy部署llama3模型等,掌握对大模型部署和微调,学会使用Xtuner工具辅助完成部署
本次活动截至5/12日,可以报名参加
llama3部署
最新推荐文章于 2024-05-29 19:39:34 发布