5. 基础关卡-Xtuner微调个人小助手认知任务

5. 基础关卡-Xtuner微调个人小助手认知

基础任务(完成此任务即完成闯关并获得 100 算力点)

使用 XTuner 微调 InternLM2-Chat-7B 实现自己的小助手认知,如下图所示(图中的尖米需替换成自己的昵称),记录复现过程并截图。

1. 复制模型

在InternStudio开发机中的已经提供了微调模型,可以直接软链接即可。

本模型位于/root/share/new_models/Shanghai_AI_Laboratory/internlm2_5-7b-chat

mkdir /root/finetune/models

ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2_5-7b-chat /root/finetune/models/internlm2_5-7b-chat

2. 修改Config

获取官方写好的 config:

# cd {path/to/finetune}
cd /root/finetune
mkdir ./config
cd config
xtuner copy-cfg internlm2_5_chat_7b_qlora_alpaca_e3 ./

修改以下几行:

#######################################################################
#                          PART 1  Settings                           #
#######################################################################
- pretrained_model_name_or_path = 'internlm/internlm2_5-7b-chat'
+ pretrained_model_name_or_path = '/root/finetune/models/internlm2_5-7b-chat'

- alpaca_en_path = 'tatsu-lab/alpaca'
+ alpaca_en_path = '/root/finetune/data/assistant_Tuner_change.jsonl'


evaluation_inputs = [
-    '请给我介绍五个上海的景点', 'Please tell me five scenic spots in Shanghai'
+    '请介绍一下你自己', 'Please introduce yourself'
]

#######################################################################
#                      PART 3  Dataset & Dataloader                   #
#######################################################################
alpaca_en = dict(
    type=process_hf_dataset,
-   dataset=dict(type=load_dataset, path=alpaca_en_path),
+   dataset=dict(type=load_dataset, path='json', data_files=dict(train=alpaca_en_path)),
    tokenizer=tokenizer,
    max_length=max_length,
-   dataset_map_fn=alpaca_map_fn,
+   dataset_map_fn=None,
    template_map_fn=dict(
        type=template_map_fn_factory, template=prompt_template),
    remove_unused_columns=True,
    shuffle_before_pack=True,
    pack_to_max_length=pack_to_max_length,
    use_varlen_attn=use_varlen_attn)

3. 启动微调

微调命令:

cd /root/finetune
conda activate xtuner-env

xtuner train ./config/internlm2_5_chat_7b_qlora_alpaca_e3_copy.py --deepspeed deepspeed_zero2 --work-dir ./work_dirs/assistTuner

微调结果如下:

L1-Xtuner-basic

4. 权重转换

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

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

xtuner convert pth_to_hf 命令用于进行模型格式转换。该命令需要三个参数:CONFIG 表示微调的配置文件, PATH_TO_PTH_MODEL 表示微调的模型权重文件路径,即要转换的模型权重, SAVE_PATH_TO_HF_MODEL 表示转换后的 HuggingFace 格式文件的保存路径。

除此之外,我们其实还可以在转换的命令中添加几个额外的参数,包括:

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

conda activate xtuner-env

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

convert

转换后的目录文件,模型被转换为 HuggingFace 中常用的 .bin 格式文件。

(xtuner-env) root@intern-studio-50014188:~/finetune/work_dirs/assistTuner# ls -alh hf/
total 300M
drwxr-xr-x 2 root root 4.0K Dec 11 14:21 .
drwxr-xr-x 6 root root 4.0K Dec 11 14:21 ..
-rw-r--r-- 1 root root 5.0K Dec 11 14:21 README.md
-rw-r--r-- 1 root root  654 Dec 11 14:21 adapter_config.json
-rw-r--r-- 1 root root 300M Dec 11 14:21 adapter_model.bin
-rw-r--r-- 1 root root 6.7K Dec 11 14:21 xtuner_config.py

此时,hf 文件夹即为我们平时所理解的所谓 “LoRA 模型文件”。

说明
可以简单理解:LoRA 模型文件 = Adapter

5. 模型合并

对于 LoRA 或者 QLoRA 微调出来的模型其实并不是一个完整的模型,而是一个额外的层(Adapter),训练完的这个层最终还是要与原模型进行合并才能被正常的使用。

在 XTuner 中提供了一键合并的命令 xtuner convert merge,在使用前我们需要准备好三个路径,包括原模型的路径、训练好的 Adapter 层的(模型格式转换后的)路径以及最终保存的路径。

cd /root/finetune/work_dirs/assistTuner
conda activate xtuner-env

export MKL_SERVICE_FORCE_INTEL=1
export MKL_THREADING_LAYER=GNU
xtuner convert merge /root/finetune/models/internlm2_5-7b-chat ./hf ./merged --max-shard-size 2GB

model_merged

目录结构:

(xtuner-env) root@intern-studio-50014188:~/finetune/work_dirs/assistTuner# ll merged/
total 15120013
drwxr-xr-x 2 root root       4096 Dec 11 20:01 ./
drwxr-xr-x 7 root root       4096 Dec 11 20:00 ../
-rw-r--r-- 1 root root        980 Dec 11 20:00 config.json
-rw-r--r-- 1 root root       8843 Dec 11 20:00 configuration_internlm2.py
-rw-r--r-- 1 root root        123 Dec 11 20:00 generation_config.json
-rw-r--r-- 1 root root      80669 Dec 11 20:00 modeling_internlm2.py
-rw-r--r-- 1 root root 1949342720 Dec 11 20:00 pytorch_model-00001-of-00008.bin
-rw-r--r-- 1 root root 1946250748 Dec 11 20:00 pytorch_model-00002-of-00008.bin
-rw-r--r-- 1 root root 1979787782 Dec 11 20:00 pytorch_model-00003-of-00008.bin
-rw-r--r-- 1 root root 1946250812 Dec 11 20:00 pytorch_model-00004-of-00008.bin
-rw-r--r-- 1 root root 1979787846 Dec 11 20:01 pytorch_model-00005-of-00008.bin
-rw-r--r-- 1 root root 1946250812 Dec 11 20:01 pytorch_model-00006-of-00008.bin
-rw-r--r-- 1 root root 1979787846 Dec 11 20:01 pytorch_model-00007-of-00008.bin
-rw-r--r-- 1 root root 1748040704 Dec 11 20:01 pytorch_model-00008-of-00008.bin
-rw-r--r-- 1 root root      18179 Dec 11 20:01 pytorch_model.bin.index.json
-rw-r--r-- 1 root root        713 Dec 11 20:01 special_tokens_map.json
-rw-r--r-- 1 root root       8806 Dec 11 20:01 tokenization_internlm2.py
-rw-r--r-- 1 root root       7805 Dec 11 20:01 tokenization_internlm2_fast.py
-rw-r--r-- 1 root root    5753097 Dec 11 20:01 tokenizer.json
-rw-r--r-- 1 root root    1477754 Dec 11 20:01 tokenizer.model
-rw-r--r-- 1 root root       2511 Dec 11 20:01 tokenizer_config.json

模型 WebUI 对话

再次运行 xtuner_streamlit_demo.py 脚本来观察微调后的对话效果。修改脚本中的模型路径:

# 直接修改脚本文件第18行
- model_name_or_path = "Shanghai_AI_Laboratory/internlm2_5-7b-chat"
+ model_name_or_path = "/root/finetune/work_dirs/assistTuner/merged"

启动应用:

conda activate xtuner-env

pip install streamlit==1.31.0
streamlit run /root/Tutorial/tools/L1_XTuner_code/xtuner_streamlit_demo.py

浏览器访问应用:

finetune-model-web-chat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lldhsds

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值