fintuning chatglm3

chatglm3介绍

ChatGLM3-6B 是 ChatGLM 系列最新一代的开源模型,在保留了前两代模型对话流畅、部署门槛低等众多优秀特性的基础上,ChatGLM3-6B 引入了如下特性:

更强大的基础模型: ChatGLM3-6B 的基础模型 ChatGLM3-6B-Base 采用了更多样的训练数据、更充分的训练步数和更合理的训练策略。在语义、数学、推理、代码、知识等不同角度的数据集上测评显示,ChatGLM3-6B-Base 具有在 10B 以下的预训练模型中最强的性能。
更完整的功能支持: ChatGLM3-6B 采用了全新设计的 Prompt 格式,除正常的多轮对话外。同时原生支持工具调用(Function Call)、代码执行(Code Interpreter)和 Agent 任务等复杂场景。
更全面的开源序列: 除了对话模型 ChatGLM3-6B 外,还开源了基础模型 ChatGLM-6B-Base、长文本对话模型 ChatGLM3-6B-32K。以上所有权重对学术研究完全开放,在填写问卷进行登记后亦允许免费商业使用。

chatglm3调优

所有的调优的方式, 均参照了chatglm的官方手册:
需要至少准备拿没有足够的显存只能进行的lora 模型的调优

  • SFT 全量微调: 4张显卡平均分配,每张显卡占用 48346MiB 显存。
  • P-TuningV2 微调: 1张显卡,占用 18426MiB 显存。
  • LORA 微调: 1张显卡,占用 14082MiB 显存。

lora是使用一张3060ti的显卡就能进行
P-tuningV2 需要12G以上的显卡,建议是3080ti及以上

环境搭建:

使用了推荐的conda的方式,进行了依赖的安装

conda create -n chatglm python=3.10
conda activate chatglm3
pip install -r requirementss.txt
问题和修正

出现的问题点如下:

问题一:

import mpi4py 直接导入不报错

from mpi4py import MPI出现报错ImportError: libmpi.so.40: cannot open shared object file: No such file or directory

网上找了好久的方法,试了很多都不行
最后在这里找到了解决办法,在终端下载openmpi就可以了:

conda install -c conda-forge openmpi=4.1.2
问题二:
The Open MPI wrapper compiler was unable to find the specified compiler
      x86_64-conda-linux-gnu-cc in your PATH.

解决方案

conda install gxx_linux-64 gcc_linux-64

数据准备

这里以 AdvertiseGen 数据集为例, 您可以从 Google Drive 或者 Tsinghua Cloud 下载 AdvertiseGen 数据集。 将解压后的 AdvertiseGen 目录放到 data 目录下并自行转换为如下格式数据集。

数据转换脚本内容如下:

import json

def transform_data(input_file_path, output_file_path):

    datas = []
    # Read the content of the file
    with open(input_file_path, 'r', encoding='utf-8') as file:
        for line in file:
            conversations = []
            if line.strip():  # Check if line is not empty
                # Parse the JSON string
                item = json.loads(line)

                # Add user and assistant messages
                user_message = {
                    "role": "user",
                    "content": item["content"]
                }
                assistant_message = {
                    "role": "assistant",
                    "content": item["summary"]
                }

                # Append to conversations list
                conversations.extend([user_message, assistant_message])

    # Prepare the output structure
                datas.append({"conversations": conversations})

    # Write the output to a new file
    with open(output_file_path, 'w', encoding='utf-8') as out_file:
        json.dump(datas, out_file, ensure_ascii=False, indent=2)

# Define the input and output file paths
input_file_path = 'data/AdvertiseGen_back/dev.json'  # Update this path
output_file_path = 'data/AdvertiseGen/formatted_data_dev.json'  # Update this path

# Call the function to transform the data
transform_data(input_file_path, output_file_path)

print("Data transformation complete. The formatted data is saved to", output_file_path)

通过上面的脚本,把里面的内容汇总成可以用来进行训练的数据,转换完成之后将数据copy到data下面的AdvertiseGen 目录下面

调优

调优直接参照命令:

lora 方式

通过以下代码执行 单机多卡/多机多卡 运行,这是使用 deepspeed 作为加速方案的,您需要安装 deepspeed。

cd finetune_demo
OMP_NUM_THREADS=1 torchrun --standalone --nnodes=1 --nproc_per_node=8  finetune_hf.py  data/AdvertiseGen/  THUDM/chatglm3-6b  configs/lora.yaml

通过以下代码执行 单机单卡 运行。

cd finetune_demo
python finetune_hf.py  data/AdvertiseGen/  THUDM/chatglm3-6b  configs/lora.yaml

训练过程中是可以按照step继续的,具体参照官方的文档

P tunV2方式

和lora的不同,也就是把lora修改为ptun就行了

cd finetune_demo
python finetune_hf.py  data/AdvertiseGen/  THUDM/chatglm3-6b  configs/ptuning_v2.yaml
SFT 方式

24G显存跑不起来, 放弃了

测试ptuning_v2.yaml

在 inference_hf.py 中验证微调后的模型
可以在 finetune_demo/inference_hf.py 中使用我们的微调后的模型,仅需要一行代码就能简单的进行测试。
这里tunning出来的内容被存储在了output目录里面

python inference_hf.py your_finetune_path --prompt your prompt

测试代码如下

 python inference_hf.py output/checkpoint-3000  --prompt  "类型#裙*版型#显瘦*材质#网纱*风格#性感*裙型#百褶*裙下摆#压褶*裙长#连衣裙*裙衣门襟#拉链*裙衣门襟#套头*裙款式#拼接*裙款式#拉链*裙款式#木耳边*裙款式#抽褶*裙款式#不规则"

测试结果如下:
在这里插入图片描述

参考链接

https://github.com/THUDM/ChatGLM3/blob/main/finetune_demo/README.md

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zeloas

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

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

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

打赏作者

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

抵扣说明:

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

余额充值