硬件环境
NVIDIA A100-SXM 40G
CUDA Version: 11.4
软件环境
python 3.8
conda create --name baichuan python=3.8
conda activate baichuan
微调步骤
下载百川2代码:
git clone https://github.com/baichuan-inc/Baichuan2.git
安装百川所需的安装包
pip install -r requirements.txt
微调
安装微调所需要的包
cd Baichuan2/fine-tune
pip install -r requirements.txt
微调数据
数据格式参考 Baichuan2/fine-tune/data/belle_chat_ramdon_10k.json
全参数微调
微调命令
hostfile=""
deepspeed --hostfile=$hostfile fine-tune.py \
--report_to "none" \
--data_path "data/belle_chat_ramdon_10k.json" \
--model_name_or_path "../Baichuan-13B-Chat" \
--output_dir "output" \
--model_max_length 512 \
--num_train_epochs 4 \
--per_device_train_batch_size 16 \
--gradient_accumulation_steps 1 \
--save_strategy epoch \
--learning_rate 2e-5 \
--lr_scheduler_type constant \
--adam_beta1 0.9 \
--adam_beta2 0.98 \
--adam_epsilon 1e-8 \
--max_grad_norm 1.0 \
--weight_decay 1e-4 \
--warmup_ratio 0.0 \
--logging_steps 1 \
--gradient_checkpointing True \
--deepspeed ds_config.json \
--bf16 True \
--tf32 True
如果出现显存不够的情况,可以适当降低per_device_train_batch_size
遇到的问题
如果遇到下面的问题,需要将transformers版本进行降级
执行如下命令
pip install transformers==4.33.1
pip install tokenizers==0.13.3
轻量级微调(lora)
需要安装peft包
pip install peft
微调命令
hostfile=""
deepspeed --hostfile=$hostfile fine-tune.py \
--report_to "none" \
--data_path "data/belle_chat_ramdon_10k.json" \
--model_name_or_path "../Baichuan-13B-Chat" \
--output_dir "output" \
--model_max_length 512 \
--num_train_epochs 4 \
--per_device_train_batch_size 16 \
--gradient_accumulation_steps 1 \
--save_strategy epoch \
--learning_rate 2e-5 \
--lr_scheduler_type constant \
--adam_beta1 0.9 \
--adam_beta2 0.98 \
--adam_epsilon 1e-8 \
--max_grad_norm 1.0 \
--weight_decay 1e-4 \
--warmup_ratio 0.0 \
--logging_steps 1 \
--gradient_checkpointing True \
--deepspeed ds_config.json \
--bf16 True \
--tf32 True \
--use_lora True
部署方式
正常推理
chat模型推理
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig
tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", use_fast=False, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan2-13B-Chat")
messages = []
messages.append({"role": "user", "content": "解释一下“温故而知新”"})
response = model.chat(tokenizer, messages)
print(response)
base模型推理
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Base", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Base", device_map="auto", trust_remote_code=True)
inputs = tokenizer('登鹳雀楼->王之涣\n夜雨寄北->', return_tensors='pt')
inputs = inputs.to('cuda:0')
pred = model.generate(**inputs, max_new_tokens=64, repetition_penalty=1.1)
print(tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))
在线量化
8bits 在线量化:
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(8).cuda()
4bits 在线量化:
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(4).cuda()
离线量化
下载量化好的模型,官方提供了离线量化好的 4bits 模型:Baichuan2-7B-Chat-4bits
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat-4bits", device_map="auto", trust_remote_code=True)
微调完的模型进行部署
部署命令
from peft import AutoPeftModelForCausalLM
model = AutoPeftModelForCausalLM.from_pretrained("output", trust_remote_code=True)
调参经验
待补充
总结
- 基座模型可能已经见过某些领域的大量数据,本身表现就很好,微调后提升比较明显。
- 数据质量非常重要,爬取的数据,可能很多类似问题答案不同,甚至南辕北辙的情况,严重影响模型学习。
- 数据分布非常重要,超参影响反而没那么大,可能只影响几个点,不明显,但是数据分布和数据质量不好,可能直接就学不出来,另外不平衡问题也存在。
- 全参模型遗忘比较严重,lora在某些数据不容易收敛