本教程出自元壤教育AIGC提示工程教研团队,未经授权,禁止转载,如需系统学习AIGC提示工程系列课程,请关注元壤教育公众号进入知识商城系统学习。

元壤教育:AIGC提示工程企业培训领导品牌,致力于通过AIGC企业培训赋能企业,助力千万家企业10倍效能提升。

下载项目代码

# 拉取仓库
git clone https://github.com/mymusise/ChatGLM-Tuning.git

# 进入目录
cd ChatGLM-Tuning

使用 conda 配置 python 环境

conda create -p lora-chatglm  python=3.8

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

激活环境,根据安装完成的提示,红框的内容进行激活

conda activate /home/ubuntu/ChatGLM-Tuning/lora-chatglm

安装依赖

pip install -r requirements.txt

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

数据集处理

下载数据集

采用Stanford Alpaca中提供的alpaca_data.json 指令数据集进行参数高效微调,但是在Alpaca-LoRA中提到该数据集存在一些噪声,因此,他们对该数据集做了清洗后得到了 alpaca_data_cleaned_archive.json 文件。采用该数据集进行训练大概率会得到更好结果。

cd data/
wget https://img.chengxuka.com/alpaca_data_cleaned_archive.json

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

数据预处理

cd ..

python cover_alpaca2jsonl.py \
  --data_path data/alpaca_data_cleaned_archive.json \
  --save_path data/alpaca_data_cleaned_archive.jsonl

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

对预处理后的语料进行分词并保存。

python tokenize_dataset_rows.py \
    --jsonl_path data/alpaca_data_cleaned_archive.jsonl \
    --save_path data/alpaca \
    --max_seq_length 128

参数说明:

  • --jsonl_path 微调的数据路径, 格式jsonl, 对每行的['context']和['target']字段进行encode
  • --save_path 输出路径
  • --max_seq_length 样本的最大长度

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

模型训练

python finetune.py \
    --dataset_path data/alpaca \
    --lora_rank 8 \
    --per_device_train_batch_size 6 \
    --gradient_accumulation_steps 1 \
    --max_steps 5200 \
    --save_steps 1000 \
    --save_total_limit 2 \
    --learning_rate 1e-4 \
    --fp16 \
    --remove_unused_columns false \
    --logging_steps 50 \
    --output_dir output

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

推理

新建一个 inference.py

vi inference.py

输入 i,复制下面的代码,然后按下esc,输入 :wq,保存内容并退出

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("THUDM/chatglm-6b", trust_remote_code=True, load_in_8bit=True, device_map='auto')
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)

model = PeftModel.from_pretrained(model, "./output/")

# TODO
instructions = json.load(open("data/alpaca_data_cleaned_archive.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})

运行 inference.py

python inference.py

推理结果:
【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

问题集锦

AttributeError: /home/ubuntu/ChatGLM-Tuning/lora-chatglm/lib/python3.8/site-packages/bitsandbytes/libbitsandbytes_cpu.so: undefined symbol: cget_col_row_stats

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调
解决方法:
首先查看 cuda 版本,

conda list | grep cuda

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调
下载 cudatoolkit=11.7

conda install cudatoolkit=11.7 -c nvidia

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调
首先查看 cuda 版本,就可以看到 cudatoolkit

conda list | grep cuda

【清华AI大模型ChatGLM-6B系列教程:三】基于ChatGLM-6B使用LoRA进行参数高效微调

系列教程

相关新闻

联系我们

联系我们

010 - 82964070

 

邮件:yuanrang@cldy.org

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部