大模型lora微调-chatglm2

免部署模型对比

网址:https://gitclone.com/aiit/chat/

模型参数下载

网址:互链高科

模型框架列举

  •  chatglm2训练框架: https://github.com/hiyouga/ChatGLM-Efficient-Tuning 
  • 通用大模型训练(chatglm2失败,其他模型成功):https://github.com/hiyouga/LLaMA-Efficient-Tuning
  • 萤火大模型(未验证):https://github.com/yangjianxin1/Firefly/

通义千问大模型微调源码(chatglm2 微调失败,训练通义千问成功):

GitHub - hiyouga/LLaMA-Efficient-Tuning: Easy-to-use LLM fine-tuning framework (LLaMA-2, BLOOM, Falcon, Baichuan, Qwen, ChatGLM2)Easy-to-use LLM fine-tuning framework (LLaMA-2, BLOOM, Falcon, Baichuan, Qwen, ChatGLM2) - GitHub - hiyouga/LLaMA-Efficient-Tuning: Easy-to-use LLM fine-tuning framework (LLaMA-2, BLOOM, Falcon, Baichuan, Qwen, ChatGLM2)icon-default.png?t=N7T8https://github.com/hiyouga/LLaMA-Efficient-Tuning

训练数据准备

进入LLaMA-Efficient-Tuning根目录后,有一个data目录,创建一个自己的训练数据文件,录入内容格式如下:

[{"instruction":"阅读下列短文,从每题所给的四个选项《A、 B、 C和D)中。选出最佳选项。",

"input":"sdfdsg",

"output:"A"}]训练数据文件配置到dataset_info.json中

训练命令

vim run.sh

Model_path="model_path"
save_Model_dir="save_model"
dataset="qwentest"

CUDA_VISIBLE_DEVICES=0

torchrun --nproc_per_node 1 src/train_bash.py \
--model_name_or_path ${Model_path} \
--stage sft \

--do_train \
--dataset ${dataset} \
--dataset_dir data \
--finetuning_type lora \
--output_dir ${save_Model_dir} \
--overwrite_cache \
--per_device_train_batch_size 4 \
--gradient_accumulation_steps 1 \
--lr_scheduler_type cosine \
--logging_steps 10 \
--save_steps 1000 \

--learning_rate 1e-3 \
--num_train_epochs 10.0 \
--plot_loss \

--lora_target c_attn \
--template chatml \
--fp16

执行训练

3、执行nohup ./finetune_llm.sh &

训练完的模型调用

from transformers import AutoTokenizer,AutoModelForCausalLM, AutoConfig
from peft import PeftModel

from transformers.generation import GenerationConfig
import os

model_path = "原始模型"
ckpt_path = "lora微调后的模型"

save_pre_name = os.path.basename(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)

base_model = AutoModelForCausaLLM.from_pretrained(model_path,device_map={"":0},trust_remote_code=True)

base_model.generation_config = GenerationConfig.from_pretrained(model_path, trust_remote_code=True)

lora_model = PeftModel.from_pretrained(base_model, ckpt_path)

lora_model.to('cuda:4')
lora_model.eval()

input = ""
line_format=input.replace("######","\n")

llm_question="ddsfdsfddsfsdg\nA. 非常满意\nB. 满意\nC. 不满意\nD. 非常不满意\n答案:".format(line_format)

inputs = tokenizer(llm_question, return_tensors='pt')

inputs = inputs.to(lora_model.device)

pred = lora_model.generate(**inputs, max_new_tokens=500)

predict_txt = tokenizer.decode(pred.cpu()[0], skip_special_tokens=True).replace("\n", " ")

print(predict_txt)

关于chatglm2模型微调:(chatglm2成功):   https://github.com/hiyouga/ChatGLM-Efficient-Tuning 

训练数据准备

进入LLaMA-Efficient-Tuning根目录后,有一个data目录,创建一个自己的训练数据文件,录入内容格式如下:

[{"instruction":"阅读下列短文,从每题所给的四个选项《A、 B、 C和D)中。选出最佳选项。",

"input":"sdfdsg",

"output:"A"}]训练数据文件配置到dataset_info.json中

dataset_info.json 样式如下

{

"alpaca_zh":{

"filename":"yourpath"

},

"youtrainname":

{

"filename":"yourpath"

}

}

其中alpaca_zh不参加训练,但是在模型导出时,若不配会报错

训练命令vim run.sh

Model_path="model_path"
dataset="datasetname"
save_Model_dir="save_model"
CUDA_VISIBLE_DEVICES=0,1,2
torchrun --nproc_per_node 3  src/train_bash.py \
    --model_name_or_path ${Model_path} \
    --stage sft \
    --do_train \
    --dataset ${dataset} \
    --finetuning_type lora \
    --output_dir ${save_Model_dir} \
    --overwrite_cache \
    --per_device_train_batch_size 10 \
    --gradient_accumulation_steps 1 \
    --lr_scheduler_type cosine \
    --logging_steps 10 \
    --save_steps 200 \
    --learning_rate 5e-5 \
    --num_train_epochs 10.0 \
    --plot_loss \
    --fp16

模型导出(非必须,导出后可作为基础模型使用,无需模型参数拼接)

model_path="basemodelpath"

python export_model.py \

    --model_name_or_path ${model_path} \

    --finetuning_type lora \

    --checkpoint_dir lora_model_path

    --output_dir final_model_path

训练完的模型调用(未进行lora模型导出合并时使用)

from transformers import AutoTokenizer,AutoModelForCausalLM, AutoConfig
from peft import PeftModel

import os

model_path = "原始模型"
ckpt_path = "lora微调后的模型"

save_pre_name = os.path.basename(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)

base_model= AutoModel.from_pretrained(model_path, trust_remote_code=True)

lora_model = PeftModel.from_pretrained(base_model, ckpt_path)

lora_model.to('cuda:4')
lora_model.eval()

input = ""
line_format=input.replace("######","\n")

llm_question="ddsfdsfddsfsdg\nA. 非常满意\nB. 满意\nC. 不满意\nD. 非常不满意\n答案:".format(line_format)
predict_txt, history= lora_model.chat(tokenizer, llm_question, history=[])
print(predict_txt)

 若进行了模型导出时应用

tokenizer=AutoTokenizer.from_pretrained(model_path,trust_remote_code=True)

model = AutoModel.from_pretrained(model_path,trust_remote_code=True,device="cuda:0")

llm_question="你好"

predict,history = my_model.chat(tokenizer, llm_question, history=[])

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会发paper的学渣

您的鼓励和将是我前进的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值