Function Call实践(三)——微调Llama3-8B-Instruct模型 #163

Open
opened 2024-10-08 18:15:58 +08:00 by 12390900721cs · 0 comments

这次实验和之前的微调没有什么很大的区别,在这里把各行命令再熟悉一下,模糊的地方和词语也记录一下,把流程走一遍。

notebook配置

设置环境变量

由于我们在平台上进行实验,故每次都要执行如下操作:

#最经典的网络配置:
export http_proxy=http://10.10.9.50:3000
export https_proxy=http://10.10.9.50:3000
export no_proxy=localhost,127.0.0.1
#最经典的huggingface下载目录配置:
export HF_HOME=/code/huggingface-cache/
#由于原版huggingface要翻墙,平台上就使用镜像:
export HF_ENDPOINT=https://hf-mirror.com

下载模型

这里平台上用git给我们下好了。

但是一般我们还是用的huggingface网站上直接下载模型的,如之前下载qwen模型:

huggingface-cli download Qwen/Qwen1.5-0.5B-Chat --resume-download --local-dir-use-symlinks False --local-dir /dataset/Qwen1.5-0.5B-Chat

(这里模型不载在默认的huggingface目录下,而是指定路径为--local-dir /dataset/Qwen1.5-0.5B-Chat。)

下载数据集

huggingface-cli download internlm/Agent-FLAN --repo-type dataset --revision main --local-dir-use-symlinks False --local-dir /code/internlm_Agent-FLAN_data

(路径也指定了,为--local-dir /code/internlm_Agent-FLAN_data)

微调训练

NPROC_PER_NODE=4 xtuner train /code/llama3_8b_instruct_qlora_agentflan_3e.py --work-dir /userhome/llama3-8b-ft/agent-flan --deepspeed deepspeed_zero3_offload

这里使用4块DCU进行分布式训练。DCU是一种高性能的计算卡,广泛应用于需要大规模计算和高性能模拟的领域,如人工智能计算、科学计算等。

在分布式训练中,使用多块DCU可以显著提升训练速度和效率。通过调整上下文长度和使用DeepSpeed等优化技术,可以有效地训练大型模型。命令中的NPROC_PER_NODE=4表示每个节点使用4个进程,xtuner可能是一个训练脚本或者工具,用于启动训练任务。--deepspeed deepspeed_zero3_offload是DeepSpeed库的一个配置选项,用于优化分布式训练过程。

这种训练方式特别适合于大规模的深度学习任务,尤其是在处理大型数据集和复杂模型时,可以有效减少训练时间和资源消耗。

训练过程如下就表示正常运行:

模型转换

平台上已经训练好模型,放在huggingface-cache数据下,挂载该数据后目录位于:/dataset/llama3-8b-functioncall-ft/agent-flan:

将用pytorch训练得到的pth格式模型转换为hf格式,并与原模型合并:

# 模型转成hf格式
xtuner convert pth_to_hf /code/llama3_8b_instruct_qlora_agentflan_3e.py /dataset/llama3-8b-functioncall-ft/agent-flan/iter_4296.pth /code/llama3-8b-ft/agent-flan/iter_4296_hf

# lora hf格式模型合并
xtuner convert merge /dataset/Llama-3-8B-Instruct/ /code/llama3-8b-ft/agent-flan/iter_4296_hf/ /code/llama3-8b-ft/agent-flan/merged --device cpu

调用模型测试模型微调前后的区别:

结果对比如下:

微调前,没有准确给出function需要的json格式参数;

微调后,可以正确给出json格式function参数。

这次实验和之前的微调没有什么很大的区别,在这里把各行命令再熟悉一下,模糊的地方和词语也记录一下,把流程走一遍。 ## notebook配置 ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1727258394532-c0cbdcde-2d49-45d5-9d1f-71d7a283c9ed.png) ## 设置环境变量 由于我们在平台上进行实验,故每次都要执行如下操作: ```plain #最经典的网络配置: export http_proxy=http://10.10.9.50:3000 export https_proxy=http://10.10.9.50:3000 export no_proxy=localhost,127.0.0.1 #最经典的huggingface下载目录配置: export HF_HOME=/code/huggingface-cache/ #由于原版huggingface要翻墙,平台上就使用镜像: export HF_ENDPOINT=https://hf-mirror.com ``` <font style="color:#000000;"></font> ## <font style="color:#000000;">下载模型</font> 这里平台上用git给我们下好了。 但是一般我们还是用的huggingface网站上直接下载模型的,如之前下载qwen模型: ```plain huggingface-cli download Qwen/Qwen1.5-0.5B-Chat --resume-download --local-dir-use-symlinks False --local-dir /dataset/Qwen1.5-0.5B-Chat ``` <font style="color:#000000;">(这里模型不载在默认的huggingface目录下,而是指定路径为--local-dir /dataset/Qwen1.5-0.5B-Chat。)</font> ## <font style="color:#000000;">下载数据集</font> ```plain huggingface-cli download internlm/Agent-FLAN --repo-type dataset --revision main --local-dir-use-symlinks False --local-dir /code/internlm_Agent-FLAN_data ``` <font style="color:#000000;">(路径也指定了,为--local-dir /code/internlm_Agent-FLAN_data)</font> <font style="color:#000000;"></font> ## <font style="color:#000000;">微调训练</font> ```plain NPROC_PER_NODE=4 xtuner train /code/llama3_8b_instruct_qlora_agentflan_3e.py --work-dir /userhome/llama3-8b-ft/agent-flan --deepspeed deepspeed_zero3_offload ``` <font style="color:#000000;">这里使用4块DCU进行分布式训练。DCU</font><font style="color:rgb(6, 6, 7);">是一种高性能的计算卡,广泛应用于需要大规模计算和高性能模拟的领域,如人工智能计算、科学计算等。</font> <font style="color:rgb(6, 6, 7);">在分布式训练中,使用多块DCU可以显著提升训练速度和效率。通过调整上下文长度和使用DeepSpeed等优化技术,可以有效地训练大型模型。命令中的</font>`NPROC_PER_NODE=4`<font style="color:rgb(6, 6, 7);">表示每个节点使用4个进程,</font>`xtuner`<font style="color:rgb(6, 6, 7);">可能是一个训练脚本或者工具,用于启动训练任务。</font>`--deepspeed deepspeed_zero3_offload`<font style="color:rgb(6, 6, 7);">是DeepSpeed库的一个配置选项,用于优化分布式训练过程。</font> <font style="color:rgb(6, 6, 7);">这种训练方式特别适合于大规模的深度学习任务,尤其是在处理大型数据集和复杂模型时,可以有效减少训练时间和资源消耗。</font> <font style="color:#333333;">训练过程如下就表示正常运行:</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1728379713968-31e95088-d8fd-42b2-b175-5ffdca373923.png) ## 模型转换 <font style="color:#333333;">平台上已经训练好模型,放在huggingface-cache数据下,挂载该数据后目录位于:</font><font style="color:#333333;background-color:#ffff00;">/dataset/llama3-8b-functioncall-ft/agent-flan:</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1728381325798-edbd3626-8c46-433b-ac7c-e5396251fe05.png) 将用pytorch训练得到的pth格式模型转换为hf格式,并与原模型合并: ```plain # 模型转成hf格式 xtuner convert pth_to_hf /code/llama3_8b_instruct_qlora_agentflan_3e.py /dataset/llama3-8b-functioncall-ft/agent-flan/iter_4296.pth /code/llama3-8b-ft/agent-flan/iter_4296_hf # lora hf格式模型合并 xtuner convert merge /dataset/Llama-3-8B-Instruct/ /code/llama3-8b-ft/agent-flan/iter_4296_hf/ /code/llama3-8b-ft/agent-flan/merged --device cpu ``` <font style="color:#333333;">调用模型测试模型微调前后的区别:</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1728382275739-4d764052-23ac-48da-8d77-e25e59776e82.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1728382406849-03dd4bca-74c8-4289-99de-03b34a3fee3f.png) 结果对比如下: <font style="color:#333333;">微调前,没有准确给出function需要的json格式参数;</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1728382423060-175b9359-4a2f-4ecc-a408-d745a0f1f5d7.png) <font style="color:#333333;">微调后,可以正确给出json格式function参数。</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1728382439425-995cc4d7-3edc-4f42-998f-50348fe5f0b4.png)
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: HswOAuth/llm_course#163
No description provided.