人机对话之chatglm模型-lora微调实战


基于上篇chatglm-ptuning微调,继续lora微调讲解,其中第一二节和上篇一致,详情观看 chatglm-ptuning微调实战

chatglm-lora微调源代码下载

git clone https://gitcode.com/mymusise/ChatGLM-Tuning.git

依赖安装

下载源码后,当前路径中会出现一个ChatGLM-Tuning文件夹,cd至该文件夹

pip install -r requirements.txt

微调

与ptuning微调不同,需要先进行数据预处理操作,数据格式与官网data保持一致
在这里插入图片描述

数据预处理

  1. 转化json数据集为jsonl,以官网数据为例:
python cover_alpaca2jsonl.py \
    --data_path data/alpaca_data.json \ #修改为自己路径
    --save_path data/alpaca_data.jsonl \ #修改为自己路径
  1. tokenization
python tokenize_dataset_rows.py \
    --jsonl_path data/alpaca_data.jsonl \
    --save_path data/alpaca \
    --max_seq_length 200 \
    --skip_overlength  False \
    --chatglm_path /homebak/home_new/heyiwei/ChatGLM2-6B/chatglm2-6b \
    --version v2    

运行完毕后会生成data/alpaca路径

  • –jsonl_path 微调的数据路径, 格式jsonl, 对每行的[‘context’]和[‘target’]字段进行encode
  • –save_path 输出路径
  • –max_seq_length 样本的最大长度
  • –chatglm_path 导入模型的路径(可以选择chatglm或chatglm2的不同路径)
  • –version 模型的版本(v1指chatglm,v2指chatglm2)
  1. 训练
python finetune.py \
    --dataset_path data/alpaca \
    --lora_rank 8 \
    --per_device_train_batch_size 6 \
    --gradient_accumulation_steps 1 \
    --max_steps 52000 \
    --save_steps 1000 \
    --save_total_limit 2 \
    --learning_rate 1e-4 \
    --fp16 \
    --remove_unused_columns false \
    --logging_steps 50 \
    --output_dir output \
    --chatglm_path /homebak/home_new/heyiwei/ChatGLM2-6B/chatglm2-6b

ps:这个只能适用于6b模型,不适用于量化,对量化模型训练则会报错

RuntimeError: Only Tensors of floating point and complex dtype can require gradients

推理

from transformers import AutoModel,AutoTokenizer
import torch
from peft import PeftModel
import json
from cover_alpaca2jsonl import format_example

device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")

#加载模型
model = AutoModel.from_pretrained("xxx/chatglm-6b", trust_remote_code=True, load_in_8bit=True, device_map='auto', revision="")
tokenizer = AutoTokenizer.from_pretrained("xxx/chatglm-6b", trust_remote_code=True,  revision="")
model = PeftModel.from_pretrained(model, "微调后的模型路径")


# 加载数据
instructions = json.load(open("xxx/alpaca_data_cleaned.json"))
answers = []


with torch.no_grad():
    for idx, item in enumerate(instructions[:3]):
        feature = format_example(item)
        input_text = feature['context']
        ids = tokenizer.encode(input_text)
        input_ids = torch.LongTensor([ids])
        input_ids = input_ids.to(device)
        out = model.generate(
            input_ids=input_ids,
            max_length=150,
            do_sample=False,
            temperature=0
        )
        out_text = tokenizer.decode(out[0])
        answer = out_text.replace(input_text, "").replace("\nEND", "").strip()
        item['infer_answer'] = answer
        print(out_text)
        print(f"### {idx+1}.Answer:\n", item.get('output'), '\n\n')
        answers.append({'index': idx, **item})
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要对 chatglm2-6b 进行微调以适应 LORA(低功耗广域网)环境,可以按照以下步骤进行操作: 1. 首先,执行 chatglm2_6b_lora.py 脚本来启动微调过程。可以使用命令行输入以下命令:`python chatglm2_6b_lora.py`。 2. 接下来,修改配置文件中的几个值。可以使用以下命令在配置文件中替换一些字符串: ``` sed -i 's/THUDM\/chatglm2-6b--//g' ./chatglm2_6b_lora/config.json sed -i 's/THUDM\/chatglm2-6b/\.\.\/chatglm2_6b_lora/g' ./chatglm2_6b_lora/config.json sed -i 's/THUDM\/chatglm2-6b--//g' ./chatglm2_6b_lora/tokenizer_config.json sed -i 's/THUDM\/ChatGLM2-6B/\.\.\/chatglm2_6b_lora/g' ./chatglm2_6b_lora/modeling_chatglm.py sed -i 's/THUDM\/chatglm2-6b/\.\.\/chatglm2_6b_lora/g' ./chatglm2_6b_lora/modeling_chatglm.py ``` 这些命令将修改配置文件中的一些路径,以使其适应 LORA 微调环境。 3. 最后,调用导出的模型进行微调。具体操作可以参考 ChatGLM-Efficient-Tuning 项目的 README 文件,首先克隆该项目的仓库,然后创建并激活一个新的 Python 环境,安装所需的依赖,并根据提供的数据集说明进行微调。可以使用以下命令执行这一步骤: ``` git clone https://github.com/hiyouga/ChatGLM-Efficient-Tuning.git conda create -n chatglm_etuning python=3.10 conda activate chatglm_etuning cd ChatGLM-Efficient-Tuning pip install -r requirements.txt ``` 然后,根据项目提供的数据集说明进行微调。 通过按照上述步骤进行操作,您可以对 chatglm2-6b 进行 LORA 微调。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [修改 ChatGLM2-6B 自我认知的 Lora 微调教程](https://blog.csdn.net/engchina/article/details/131492403)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值