实践分享|书生·浦语 InternLM2.5 PyTorch模型微调的最佳实践

引言

2024年6月30日,上海人工智能实验室发布InternLM2.5-7B系列模型,包括InternLM2.5-7B、InternLM2.5-7B-Chat和InternLM2.5-7B-Chat-1M。具体介绍如下:

  • InternLM2.5-7B:在通用领域和领域增强语料进一步预训练,在评估中拥有良好的语言能力。
  • InternLM2.5-7B-Chat:通过监督微调(SFT)和在线RLHF在InternLM2.5的基础上进一步对齐。InternLM2.5-7B-Chat表现出更好的指令遵循、聊天体验和函数调用能力。
  • InternLM2.5-7B-Chat-1M:InternLM2.5-7B-Chat的1M长上下文版本。InternLM2.5-7B-Chat-1M支持百万字超长上下文推理,同时保持与InternLM2.5-7B-Chat相同的性能。

相比上一代模型,InternLM2.5具有三个突出亮点:

  • 出色的推理能力:数学推理能力达到世界先进水平,超越Llama3、Gemma2-9B等模型。
  • 1M上下文窗口: 采用业界流行的“大海捞针”来评估模型的长文本信息召回能力,InternLM2.5在1M token范围内实现了几乎完美的大海捞针召回。在LongBench等长上下文任务上具有领先性能。
  • 工具使用能力更强:具有强大的工具调用能力,比如可以针对复杂问题,搜索上百个网页并进行整合分析。

通过开源工具OpenCompass对InternLM2.5在几个重要的评测集上进行了评测,部分评测结果如下表所示,欢迎访问OpenCompass 榜单获取更多的评测结果。

Base Model

BenchmarkInternLM2.5-7BLLaMA-3-8BYi-1.5-9B
MMLU(5-shot)71.666.471.6
CMMLU(5-shot)79.151.074.1
BBH(3-shot)70.159.771.1
MATH(4-shot)34.016.431.9
GSM8K(4-shot)74.854.374.5
GPQA(0-shot)31.331.327.8

Chat Model

BenchmarkInternLM2.5-7B-ChatLlama3-8B-InstructGemma2-9B-ITYi-1.5-9B-ChatGLM-4-9B-ChatQwen2-7B-Instruct
MMLU (5-shot)72.868.470.971.071.470.8
CMMLU (5-shot)78.053.360.374.574.580.9
BBH (3-shot CoT)71.654.468.2*69.669.665.0
MATH (0-shot CoT)60.127.946.951.151.148.6
GSM8K (0-shot CoT)86.072.988.980.185.382.9
GPQA (0-shot)38.426.133.837.936.938.4
  • 以上评测结果基于OpenCompass获得(部分数据标注 * 代表数据来自原始论文),具体测试细节可参见OpenCompass中提供的配置文件。
  • 评测数据会因OpenCompass的版本迭代而存在数值差异,请以OpenCompass最新版的评测结果为主。

环境准备

安装Ascend CANN Toolkit和Kernels

安装方法请参考安装教程或使用以下命令

# 请替换 URL 为 CANN 版本和设备型号对应的 URL
# 安装 CANN Toolkit
wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run
bash Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run --install

# 安装 CANN Kernels
wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run
bash Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run --install

# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh

安装openMind Hub Client以及openMind Library

  • 安装openMind Hub Client
pip install openmind_hub
  • 安装openMind Library,并安装PyTorch框架及其依赖。
pip install openmind[pt]

更详细的安装信息请参考openMind官方的环境安装章节。

安装LLaMa Factory

git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch-npu,metrics]"

模型链接和下载

InternLM2.5模型系列由社区开发者在魔乐社区贡献,包括:

  • InternLM2.5-7B:https://modelers.cn/models/AI-Research/internlm2_5-7b
  • InternLM2.5-7B-Chat:https://modelers.cn/models/AI-Research/internlm2_5-7b-chat
  • InternLM2.5-7B-Chat-1M:https://modelers.cn/models/AI-Research/internlm2_5-7b-chat-1m

通过Git从魔乐社区下载模型的repo,以InternLM2.5-7B-Chat为例:

# 首先保证已安装git-lfs(https://git-lfs.com)
git lfs install
git clone https://modelers.cn/AI-Research/internlm2_5-7b-chat.git

模型推理

用户可以使用openMind Library或者LLaMa Factory进行模型推理,以InternLM2.5-7B-Chat为例,具体如下:

  • 使用openMind Library进行模型推理

新建推理脚本inference_internlm2_5_7b_chat.py,推理脚本内容为:

import torch
from openmind import AutoTokenizer, AutoModelForCausalLM

# 若模型已下载,可替换成模型本地路径
tokenizer = AutoTokenizer.from_pretrained("AI-Research/internlm2_5-7b-chat", trust_remote_code=True)
# `torch_dtype=torch.float16`可以令模型以float16精度加载,否则transformers会将模型加载为float32,导致显存不足
model = AutoModelForCausalLM.from_pretrained("AI-Research/internlm2_5-7b-chat", torch_dtype=torch.float16, trust_remote_code=True).npu()
model = model.eval()
response, history = model.chat(tokenizer, "你好,请提供三个管理时间的建议。", history=[])
print(response)

执行推理脚本:

python inference_internlm2_5_7b_chat.py

推理结果如下:

在这里插入图片描述

  • 使用LLaMa Factory与模型交互

在LLaMa Factory路径下新建examples/inference/internlm2_5_7b_chat.yaml推理配置文件,文件内容为:

model_name_or_path: xxx # 当前仅支持本地加载,填写InternLM2.5-7B-Chat本地权重路径
template: intern2

使用以下命令与模型进行交互:

llamafactory-cli chat examples/inference/internlm2_5_7b_chat.yaml

交互结果如下:

在这里插入图片描述

模型微调

我们使用单张昇腾NPU,基于LLaMa Factory框架,采用LLaMa Factory提供的自我认知数据集进行Lora微调,改变模型对自己和作者的认知。

数据集

自我认知数据集在LLaMa Factory仓的data/identity.json路径下,这里使用文本编辑器将数据集的{{name}}{{author}}分别替换为“小昇”和"openMind"。替换后的示例如下:

在这里插入图片描述

微调

在LLaMa Factory路径下新建examples/train_lora/internlm2_5_7b_chat_lora_sft.yaml微调配置文件,微调配置文件如下:

### model
model_name_or_path: xxx # 当前仅支持本地加载,填写InternLM2.5-7B-Chat本地权重路径

### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all

### dataset
dataset: identity
template: intern2
cutoff_len: 128
preprocessing_num_workers: 16

### output
output_dir: saves/internlm2_5_7b_chat/lora/sft
logging_steps: 5
save_steps: 20 
plot_loss: true
overwrite_output_dir: true

### train
per_device_train_batch_size: 8
gradient_accumulation_steps: 1
learning_rate: 1.0e-4
num_train_epochs: 5.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000

通过下面的命令启动微调:

export ASCEND_RT_VISIBLE_DEVICES=0
llamafactory-cli train examples/train_lora/internlm2_5_7b_chat_lora_sft.yaml

微调可视化

在这里插入图片描述

微调结果

微调结束后,在LLaMa Factory路径下新建examples/inference/internlm2_5_7b_chat_lora_sft.yaml推理配置文件,配置文件内容为:

model_name_or_path: xxx # 当前仅支持本地加载,填写InternLM2.5-7B-Chat本地权重路径
adapter_name_or_path: saves/internlm2_5_7b_chat/lora/sft/checkpoint-60/ 
template: intern2
finetuning_type: lora

通过下面的命令启动推理:

llamafactory-cli chat examples/inference/internlm2_5_7b_chat_lora_sft.yaml

推理结果为:

  • 训练前生成样例

    • 问题:您好,请介绍一下你自己

      在这里插入图片描述

  • 训练后生成样例

    • 问题:您好,请介绍一下你自己

      在这里插入图片描述

  • 微调后通用能力测试

    • 问题1:请介绍一下故宫
      *在这里插入图片描述

    • 问题2:请问怎么做糖醋排骨?

    在这里插入图片描述

结语

本次实践采用的是一款应用使能开发套件openMind,在调用数据集训练、推理、微调等方面挺方便顺畅的。朋友们可以试试,也欢迎分享你们的经验,一起交流:

https://modelers.cn

openMind,一款应用使能开发套件,为各大模型社区提供支持,提供海量模型/数据托管能力、在线推理体验服务,同时具备模型训练、微调、评估、推理等全流程开发能力。开发者通过简单的API接口即可实现微调、推理等任务,极大缩短开发周期,助力AI技术的创新发展。目前,openMind已支持欢迎在魔乐等AI生态社区,欢迎体验。

相关链接:

[1] openMind Library介绍:https://modelers.cn/docs/zh/openmind-library/overview.html

[2] openMind Hub Client介绍:https://modelers.cn/docs/zh/openmind-hub-client/overview.html

[3] 一文教你10分钟体验魔乐社区:https://modelers.cn/docs/zh/community/quick_start.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值