ChatGLM2-6B-PT微调 chatglm2-6b-int4模型

Github

-ChatGLM2-6B:https://github.com/THUDM/ChatGLM2-6B

git clone https://github.com/THUDM/ChatGLM2-6B.git

下载模型

模型文件比较大需要先提前下载好再通过 矩池云 客户端上传到GPU服务器

GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/THUDM/chatglm2-6b-int4

开发环境

矩池云免费5G网盘

  • https://www.matpool.com/user/matbox

可以在矩池云租用的任一机器里访问网盘里的数据,访问目录/mnt。 您也可以直接在/mnt目录下操作,/mnt下的操作等同于直接对网盘进行操作。

VS Code 远程连接矩池云机器教程

  • https://www.matpool.com/supports/doc-vscode-connect-matpool/

SSH 远程连接矩池云机器教程

  • https://www.matpool.com/supports/doc-xshell-connect-matpool/

GPU环境最低要求

安装依赖

将ChatGLM2-6B源码传到 矩池云 GPU服务器,执行以下命令安装相关依赖

cd ChatGLM2-6B
# 新建模型目录,将模型复制到model目录下
mkdir model
# 安装依赖
pip install -r requirements.txt -i https://mirror.sjtu.edu.cn/pypi/web/simple
# 运行微调除 ChatGLM2-6B 的依赖之外,还需要安装以下依赖
pip install rouge_chinese nltk jieba datasets transformers[torch] -i https://pypi.douban.com/simple/

推理测试

# 加载模型
model_path = "model/chatglm2-6b-int4"
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().cuda()
model = model.eval()

# 准备提示语
prompt = '''
如何制作宫保鸡丁
'''
print(prompt)

# 模型输出
current_length = 0
for response, history in model.stream_chat(tokenizer, prompt, history=[]):
    print(response[current_length:], end="", flush=True)
    current_length = len(response)
print("")
  • 执行程序
python test.py

微调前

# 加载模型
model_path = "model/chatglm2-6b-int4"
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().cuda()
model = model.eval()

# 准备提示语
prompt = "类型#上衣\*材质#牛仔布\*颜色#白色\*风格#简约\*图案#刺绣\*衣样式#外套\*衣款式#破洞"
print(prompt)

# 模型输出
current_length = 0
for response, history in model.stream_chat(tokenizer, prompt, history=[]):
    print(response[current_length:], end="", flush=True)
    current_length = len(response)
print("")
  • 执行程序
python test.py

在这里插入图片描述

下载ADGEN数据集

cd ptuning
# 复制训练数据集到ptuning目录中
cp -r /mnt/AdvertiseGen .

# 微调训练
# 训练集目录 ptuning/AdvertiseGen/
# 模型目录 ChatGLM2-6B/model/
# 模型训练输出目录 ptuning/output/
# max_steps 最大训练步数
# save_steps 保存步骤数
# logging_steps 记录日志的频率
# quantization_bit 控制量化的精度
# pre_seq_len 预先设定的序列长度
# learning_rate 使用的学习率
# gradient_accumulation_steps 连续计算梯度的步数
torchrun --standalone --nnodes=1 --nproc-per-node=1 main.py \
    --do_train \
    --train_file AdvertiseGen/train.json \
    --validation_file AdvertiseGen/dev.json \
    --preprocessing_num_workers 10 \
    --prompt_column content \
    --response_column summary \
    --overwrite_cache \
    --model_name_or_path /home/ChatGLM2-6B/model/chatglm2-6b-int4 \
    --output_dir output/adgen-chatglm2-6b-pt-$PRE_SEQ_LEN-$LR \
    --overwrite_output_dir \
    --max_source_length 64 \
    --max_target_length 128 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --gradient_accumulation_steps 16 \
    --predict_with_generate \
    --max_steps 3000 \
    --logging_steps 10 \
    --save_steps 1000 \
    --learning_rate 2e-2 \
    --pre_seq_len 128 \
    --quantization_bit 4

修改训练步数(缩短训练时间)

torchrun --standalone --nnodes=1 --nproc-per-node=1 main.py \
    --do_train \
    --train_file AdvertiseGen/train.json \
    --validation_file AdvertiseGen/dev.json \
    --preprocessing_num_workers 10 \
    --prompt_column content \
    --response_column summary \
    --overwrite_cache \
    --model_name_or_path /home/ChatGLM2-6B/model/chatglm2-6b-int4 \
    --output_dir output/adgen-chatglm2-6b-pt-$PRE_SEQ_LEN-$LR \
    --overwrite_output_dir \
    --max_source_length 64 \
    --max_target_length 128 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --gradient_accumulation_steps 16 \
    --predict_with_generate \
    --max_steps 100 \
    --logging_steps 10 \
    --save_steps 50 \
    --learning_rate 2e-2 \
    --pre_seq_len 128 \
    --quantization_bit 4

微调后

# 微调后
import os
import torch
from transformers import AutoConfig, AutoTokenizer, AutoModel

model_path = "model/chatglm2-6b-int4"
# 载入Tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)

# 微调后代码
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True, pre_seq_len=128)
model = AutoModel.from_pretrained(model_path, config=config, trust_remote_code=True)
# 加载根据ADGEN数据集微调后训练的模型
prefix_state_dict = torch.load(os.path.join("/home/ChatGLM2-6B/ptuning/output/adgen-chatglm2-6b-pt--/checkpoint-3000", "pytorch_model.bin"))
new_prefix_state_dict = {}
for k, v in prefix_state_dict.items():
    if k.startswith("transformer.prefix_encoder."):
        new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)

model = model.half().cuda()
model.transformer.prefix_encoder.float()
model = model.eval()

prompt = "类型#上衣\*材质#牛仔布\*颜色#白色\*风格#简约\*图案#刺绣\*衣样式#外套\*衣款式#破洞"
# 模型输出
current_length = 0
for response, history in model.stream_chat(tokenizer, prompt, history=[]):
    print(response[current_length:], end="", flush=True)
    current_length = len(response)
print("")
  • 执行程序
python test.py

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

逢生博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值