大模型LLM-微調(diào)經(jīng)驗(yàn)分享&總結(jié)
作者:劉聰NLP
鏈接:https://zhuanlan.zhihu.com/p/620885226
寫在前面
ChatGLM-6B模型微調(diào)
Freeze方法
for name, param in model.named_parameters():
if not any(nd in name for nd in ["layers.27", "layers.26", "layers.25", "layers.24", "layers.23"]):
param.requires_grad = False
CUDA_VISIBLE_DEVICES=0 deepspeed finetuning_freeze.py --num_train_epochs 5 --train_batch_size 2
PT方法
-
P-Tuning(https://arxiv.org/abs/2103.10385),僅對大模型的Embedding加入新的參數(shù)。 -
P-Tuning-V2(https://arxiv.org/abs/2110.07602),將大模型的Embedding和每一層前都加上新的參數(shù)。
config = ChatGLMConfig.from_pretrained(args.model_dir)
config.pre_seq_len = args.pre_seq_len
config.prefix_projection = args.prefix_projection
model = ChatGLMForConditionalGeneration.from_pretrained(args.model_dir, config=config)
for name, param in model.named_parameters():
if not any(nd in name for nd in ["prefix_encoder"]):
param.requires_grad = False
CUDA_VISIBLE_DEVICES=0 deepspeed finetuning_pt.py --num_train_epochs 5 --train_batch_size 2 --pre_seq_len 16
Lora方法
-
Lora論文:https://arxiv.org/abs/2106.09685 -
官方代碼:https://github.com/microsoft/LoRA -
HuggingFace封裝的peft庫:https://github.com/huggingface/peft
model = ChatGLMForConditionalGeneration.from_pretrained(args.model_dir)
config = LoraConfig(r=args.lora_r,
lora_alpha=32,
target_modules=["query_key_value"],
lora_dropout=0.1,
bias="none",
task_type="CAUSAL_LM",
inference_mode=False,
)
model = get_peft_model(model, config)
CUDA_VISIBLE_DEVICES=0 deepspeed finetuning_lora.py --num_train_epochs 5 --train_batch_size 2 --lora_r 8
三元組抽取實(shí)驗(yàn)結(jié)果
-
模型訓(xùn)練時(shí),最大長度為768,Batch大小為2,訓(xùn)練輪數(shù)為5,fp16訓(xùn)練,采用DeepSpeed的Zero-1訓(xùn)練; -
PT為官方的P-Tuning V2訓(xùn)練方法,PT-Only-Embedding表示僅對Embedding進(jìn)行soft-prompt,F(xiàn)reeze僅訓(xùn)練模型后五層參數(shù),Lora采用低秩矩陣方法訓(xùn)練,秩為8; -
由于之前訓(xùn)練PT在48G-A40顯卡上會(huì)出現(xiàn)OOM,因此之前進(jìn)行PT實(shí)驗(yàn)時(shí)對模型開啟了gradient_checkpointing_enable,使得模型顯存占用變小,但訓(xùn)練時(shí)長增加。 -
訓(xùn)練示例:
prompt_text:你現(xiàn)在是一個(gè)信息抽取模型,請你幫我抽取出關(guān)系內(nèi)容為\"性能故障\", \"部件故障\", \"組成\"和 \"檢測工具\(yùn)"的相關(guān)三元組,三元組內(nèi)部用\"_\"連接,三元組之間用\\n分割。文本:
輸入:故障現(xiàn)象:發(fā)動(dòng)機(jī)水溫高,風(fēng)扇始終是低速轉(zhuǎn)動(dòng),高速檔不工作,開空調(diào)尤其如此。
輸出:發(fā)動(dòng)機(jī)_部件故障_水溫高\(yùn)n風(fēng)扇_部件故障_低速轉(zhuǎn)動(dòng)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
效果為PT>Freeze>Lora>PT-Only-Embedding; -
速度為PT-Only-Embedding>Lora>Freeze>PT; -
PT-Only-Embedding效果很不理想,發(fā)現(xiàn)在訓(xùn)練時(shí),最后的loss僅能收斂到2.幾,而其他機(jī)制可以收斂到0.幾。分析原因?yàn)?,輸出?nèi)容形式與原有語言模型任務(wù)相差很大,僅增加額外Embedding參數(shù),不足以改變復(fù)雜的下游任務(wù); -
PT方法占用顯存更大,因?yàn)橐苍黾恿撕芏喽鈪?shù); -
測試耗時(shí),采用float16進(jìn)行模型推理,由于其他方法均增加了額外參數(shù),因此其他方法的推理耗時(shí)會(huì)比Freeze方法要高。當(dāng)然由于是生成模型,所以生成的長度也會(huì)影響耗時(shí); -
模型在指定任務(wù)上微調(diào)之后,并沒有喪失原有能力,例如生成“幫我寫個(gè)快排算法”,依然可以生成-快排代碼; -
由于大模型微調(diào)都采用大量instruction進(jìn)行模型訓(xùn)練,僅采用單一的指令進(jìn)行微調(diào)時(shí),對原來其他的指令影響不大,因此并沒導(dǎo)致原來模型的能力喪失; -
上面測試僅代表個(gè)人測試結(jié)果。
-
翻譯任務(wù)
-
代碼任務(wù)
-
問答任務(wù)
中文開源大模型&項(xiàng)目
中文開源大模型
-
ChatGLM-6B:https://huggingface.co/THUDM/chatglm-6b -
ChatYuan-large-v2:https://huggingface.co/ClueAI/ChatYuan-large-v2
-
BloomZ:https://huggingface.co/bigscience/bloomz -
LLama:https://github.com/facebookresearch/llama -
Flan-T5:https://huggingface.co/google/flan-t5-xxl -
OPT:https://huggingface.co/facebook/opt-66b
中文開源指令數(shù)據(jù)
-
[1]:https://github.com/LC1332/Chinese-alpaca-lora -
[2]:https://github.com/hikariming/alpaca_chinese_dataset -
[3]:https://github.com/carbonz0/alpaca-chinese-dataset -
[4]:https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM -
[5]:https://github.com/LianjiaTech/BELLE -
[6]:https://huggingface.co/datasets/JosephusCheung/GuanacoDataset
開源項(xiàng)目
-
BELLE:https://github.com/LianjiaTech/BELLE -
ChatGLM:https://github.com/THUDM/ChatGLM-6B -
Luotuo-Chinese-LLM:https://github.com/LC1332/Luotuo-Chinese-LLM -
stanford_alpaca:https://github.com/tatsu-lab/stanford_alpaca
總結(jié)
END
評論
圖片
表情
