书生大模型-L1-XTuner 微调个人小助手认知

基础任务:使用 XTuner 微调 InternLM2-Chat-1.8B 实现自己的小助手认知,记录复现过程并截图。

1.开发机准备:

1.1进入开发机后确保自己已经克隆了Tutorial仓库的资料到本地:

mkdir -p /root/InternLM/Tutorial
git clone -b camp3  https://github.com/InternLM/Tutorial /root/InternLM/Tutorial

2.创建虚拟环境 

使用 Anaconda 创建一个名为 xtuner0121 的虚拟环境,可以直接执行命令。

# 创建虚拟环境
conda create -n xtuner0121 python=3.10 -y

# 激活虚拟环境(注意:后续的所有操作都需要在这个虚拟环境中进行)
conda activate xtuner0121

# 安装一些必要的库
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y
# 安装其他依赖
pip install transformers==4.39.3
pip install streamlit==1.36.0

3.安装 XTuner 

从 Github 上下载源码,进入源码目录,执行安装。(使用魔法科学上网)

# 创建一个目录,用来存放源代码
mkdir -p /root/InternLM/code

cd /root/InternLM/code

git clone -b v0.1.21  https://github.com/InternLM/XTuner /root/InternLM/code/XTuner

# 进入到源码目录
cd /root/InternLM/code/XTuner
conda activate xtuner0121

# 执行安装
pip install -e '.[deepspeed]'

查看安装是否成功:

xtuner version

 4. 模型准备:使用 InternLM 推出的1.8B的小模型来完成此次微调

# 创建一个目录,用来存放微调的所有资料,后续的所有操作都在该路径中进行
mkdir -p /root/InternLM/XTuner

cd /root/InternLM/XTuner

mkdir -p Shanghai_AI_Laboratory

ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b Shanghai_AI_Laboratory/internlm2-chat-1_8b

使用tree命令来观察目录结构

apt-get install -y tree

tree -l

5.开始微调 

这里我们用 internlm2-chat-1_8b 模型,通过 QLoRA 的方式来微调一个自己的小助手认知作为案例来进行:

通过网页端的 Demo 来看看微调前 internlm2-chat-1_8b 的对话效果。

运行

conda activate xtuner0121

streamlit run /root/InternLM/Tutorial/tools/xtuner_streamlit_demo.py

端口映射到本地(最后部分替换成自己开发机的端口)

ssh -CNg -L 8501:127.0.0.1:8501 root@ssh.intern-ai.org.cn -p 43252

在本地通过浏览器访问:http://127.0.0.1:8501 来进行对话: 

5.1指令跟随微调 

下面我们对模型进行微调,让模型认识到自己的弟位,了解它自己是你的一个助手。

5.1.1 准数据文件

为了让模型能够认清自己的身份弟位,在询问自己是谁的时候按照我们预期的结果进行回复,我们就需要通过在微调数据集中大量加入这样的数据。我们准备一个数据集文件datas/assistant.json,文件内容为对话数据。

cd /root/InternLM/XTuner
mkdir -p datas
touch datas/assistant.json

可以通过脚本生成的方式来准备数据。创建一个脚本文件 xtuner_generate_assistant.py 

cd /root/InternLM/XTuner
touch xtuner_generate_assistant.py
cd /root/InternLM/XTuner
cp /root/InternLM/Tutorial/tools/xtuner_generate_assistant.py ./

也可以直接根据需要修改脚本后执行:

python xtuner_generate_assistant.py

5.1.2准备配置文件

xtuner list-cfg -p internlm2

由于我们是对internlm2-chat-1_8b模型进行指令微调,所以与我们的需求最匹配的配置文件是 internlm2_chat_1_8b_qlora_alpaca_e3,这里就复制该配置文件。

xtuner copy-cfg internlm2_chat_1_8b_qlora_alpaca_e3 .

目录结构正确:

 5.1.3 启动微调 

cd /root/InternLM/XTuner
conda activate xtuner0121

xtuner train ./internlm2_chat_1_8b_qlora_alpaca_e3_copy.py

微调训练后的目录结构如下:

 5.1.4模型格式转换

模型转换的本质其实就是将原本使用 Pytorch 训练出来的模型权重文件转换为目前通用的 HuggingFace 格式文件,那么我们可以通过以下命令来实现一键转换。

我们可以使用 xtuner convert pth_to_hf 命令来进行模型格式转换。

cd /root/InternLM/XTuner
conda activate xtuner0121

# 先获取最后保存的一个pth文件
pth_file=`ls -t ./work_dirs/internlm2_chat_1_8b_qlora_alpaca_e3_copy/*.pth | head -n 1`
export MKL_SERVICE_FORCE_INTEL=1
export MKL_THREADING_LAYER=GNU
xtuner convert pth_to_hf ./internlm2_chat_1_8b_qlora_alpaca_e3_copy.py ${pth_file} ./hf

还可以修改其他参数: 

参数名解释
--fp32代表以fp32的精度开启,假如不输入则默认为fp16
--max-shard-size {GB}代表每个权重文件最大的大小(默认为2GB)

最新的目录结构如下:

转换完成后,可以看到模型被转换为 HuggingFace 中常用的 .bin 格式文件,这就代表着文件成功被转化为 HuggingFace 格式了。

 5.1.5模型合并
cd /root/InternLM/XTuner
conda activate xtuner0121

export MKL_SERVICE_FORCE_INTEL=1
export MKL_THREADING_LAYER=GNU
xtuner convert merge /root/InternLM/XTuner/Shanghai_AI_Laboratory/internlm2-chat-1_8b ./hf ./merged --max-shard-size 2GB

 还可以修改其他参数:

参数名解释
--max-shard-size {GB}代表每个权重文件最大的大小(默认为2GB)
--device {device_name}这里指的就是device的名称,可选择的有cuda、cpu和auto,默认为cuda即使用gpu进行运算
--is-clip这个参数主要用于确定模型是不是CLIP模型,假如是的话就要加上,不是就不需要添加

模型合并完成后,我们的目录结构:

​​​​​​​

6. 微调后的模型对话 :

微调完成后,我们可以再次运行xtuner_streamlit_demo.py脚本来观察微调后的对话效果,不过在运行之前,我们需要将脚本中的模型路径修改为微调后的模型的路径。

# 直接修改脚本文件第18行
#model_name_or_path = "/root/InternLM/XTuner/Shanghai_AI_Laboratory/internlm2-chat-1_8b"
model_name_or_path = "/root/InternLM/XTuner/merged"

然后,我们可以直接启动应用

conda activate xtuner0121

streamlit run /root/InternLM/Tutorial/tools/xtuner_streamlit_demo.py

通过浏览器访问:http://127.0.0.1:8501 来进行对话了。

  • 28
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值