开源模型增量学习实现踩坑记录
最近在尝试对开源大模型进行增量学习时遇到了不少坑,分享一下踩坑心得。
背景
我们希望在已有的LLaMA模型基础上,通过增量学习的方式训练新的领域数据。使用HuggingFace的transformers库和PEFT库进行实现。
实现步骤
- 环境准备
pip install transformers peft accelerate bitsandbytes
- 加载模型并配置LoRA
from transformers import LlamaForCausalLM, LlamaTokenizer
from peft import get_peft_model, LoraConfig
model = LlamaForCausalLM.from_pretrained("llama-7b")
tokenizer = LlamaTokenizer.from_pretrained("llama-7b")
peft_config = LoraConfig(
r=8,
lora_alpha=32,
target_modules=["q_proj", "v_proj"],
lora_dropout=0.01,
bias="none",
task_type="CAUSAL_LM"
)
model = get_peft_model(model, peft_config)
- 训练设置
from transformers import TrainingArguments
training_args = TrainingArguments(
output_dir="./lora_finetuned",
per_device_train_batch_size=4,
gradient_accumulation_steps=8,
num_train_epochs=1,
learning_rate=1e-4,
logging_steps=10,
save_steps=100
)
坑点总结
- 显存问题:初始设置的batch_size太大导致OOM,需要通过gradient_accumulation_steps来平衡
- 参数选择:r值过大会消耗大量显存,建议从8开始尝试
- 数据格式:必须确保输入数据的tokenization正确,否则训练效果会很差
最佳实践
建议使用低资源模式训练,同时做好实验记录,便于复现和调优。

讨论