train_law_llm
1.0.0
一步一步使用Colab訓練法律LLM,基於microsoft/phi-1_5 ,ChatGLM3-6B。通過本項目你可以0成本手動了解微調LLM。如果想要了解LLM微調具體代碼實現,可以參考my_finetune 項目?。
| name | Colab | Datasets |
|---|---|---|
| 自我認知lora-SFT 微調 | self_cognition.json | |
| 法律問答lora-SFT 微調 | DISC-LawLLM | |
| 法律問答全參數-SFT 微調* | DISC-LawLLM | |
| ChatGLM3-6B 自我認知lora-SFT 微調* | self_cognition.json |
*如果是Colab Pro會員用戶,可以嘗試全參數-SFT微調,使用高RAM+T4,1000條數據大概需要20+小時
*如果是Colab Pro會員用戶,ChatGLM3-6B 自我認知lora-SFT 微調,使用高RAM+T4,只需要幾分鐘,效果比較好
使用colab免費的T4顯卡,完成法律問答指令監督微調(SFT) microsoft/phi-1_5 模型
自我認知數據來源:self_cognition.json
80條數據,使用T4 lora微調phi-1_5,幾分鐘就可以微調完畢
微調參數,具體步驟詳見colab
python src/train_bash.py
--stage sft
--model_name_or_path microsoft/phi-1_5
--do_train True
--finetuning_type lora
--template vanilla
--flash_attn False
--shift_attn False
--dataset_dir data
--dataset self_cognition
--cutoff_len 1024
--learning_rate 2e-04
--num_train_epochs 20.0
--max_samples 1000
--per_device_train_batch_size 6
--per_device_eval_batch_size 6
--gradient_accumulation_steps 1
--lr_scheduler_type cosine
--max_grad_norm 1.0
--logging_steps 5
--save_steps 100
--warmup_steps 0
--neft_alpha 0
--train_on_prompt False
--upcast_layernorm False
--lora_rank 8
--lora_dropout 0.1
--lora_target Wqkv
--resume_lora_training True
--output_dir saves/Phi1.5-1.3B/lora/my
--fp16 True
--plot_loss True
效果
法律問答數據來源:DISC-LawLLM
為了減省顯存,使用deepspeed stage2,cutoff_len可以最多到1792,再多就要爆顯存了
deepspeed配置
{
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"zero_allow_untested_optimizer": true,
"fp16": {
"enabled": "auto",
"loss_scale": 0,
"initial_scale_power": 16,
"loss_scale_window": 1000,
"hysteresis": 2,
"min_loss_scale": 1
},
"zero_optimization": {
"stage": 2,
"offload_optimizer": {
"device": "cpu",
"pin_memory": true
},
"allgather_partitions": true,
"allgather_bucket_size": 2e8,
"reduce_scatter": true,
"reduce_bucket_size": 2e8,
"overlap_comm": false,
"contiguous_gradients": true
}
}
微調參數
1000條數據,T4大概需要60分鐘
deepspeed --num_gpus 1 --master_port=9901 src/train_bash.py
--deepspeed ds_config.json
--stage sft
--model_name_or_path microsoft/phi-1_5
--do_train True
--finetuning_type lora
--template vanilla
--flash_attn False
--shift_attn False
--dataset_dir data
--dataset self_cognition,law_sft_triplet
--cutoff_len 1792
--learning_rate 2e-04
--num_train_epochs 5.0
--max_samples 1000
--per_device_train_batch_size 1
--per_device_eval_batch_size 1
--gradient_accumulation_steps 1
--lr_scheduler_type cosine
--max_grad_norm 1.0
--logging_steps 5
--save_steps 1000
--warmup_steps 0
--neft_alpha 0
--train_on_prompt False
--upcast_layernorm False
--lora_rank 8
--lora_dropout 0.1
--lora_target Wqkv
--resume_lora_training True
--output_dir saves/Phi1.5-1.3B/lora/law
--fp16 True
--plot_loss True
可以通過,estimate_zero3_model_states_mem_needs_all_live查看deepspeed各個ZeRO stage 所需要的內存。
from transformers import AutoModel, AutoModelForCausalLM
from deepspeed.runtime.zero.stage3 import estimate_zero3_model_states_mem_needs_all_live
model_name = "microsoft/phi-1_5"
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
estimate_zero3_model_states_mem_needs_all_live(model, num_gpus_per_node=1, num_nodes=1)
如圖所適offload_optimizer -> cpu 後microsoft/phi-1_5 需要32G內存,colab高內存有52G可以滿足需求。
deepspeed配置
{
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"zero_allow_untested_optimizer": true,
"fp16": {
"enabled": "auto",
"loss_scale": 0,
"initial_scale_power": 16,
"loss_scale_window": 1000,
"hysteresis": 2,
"min_loss_scale": 1
},
"zero_optimization": {
"stage": 2,
"offload_optimizer": {
"device": "cpu",
"pin_memory": true
},
"allgather_partitions": true,
"allgather_bucket_size": 2e8,
"reduce_scatter": true,
"reduce_bucket_size": 2e8,
"overlap_comm": false,
"contiguous_gradients": true
}
}
deepspeed --num_gpus 1 --master_port=9901 src/train_bash.py
--deepspeed ds_config.json
--stage sft
--model_name_or_path microsoft/phi-1_5
--do_train True
--finetuning_type full
--template vanilla
--flash_attn False
--shift_attn False
--dataset_dir data
--dataset self_cognition,law_sft_triplet
--cutoff_len 1024
--learning_rate 2e-04
--num_train_epochs 10.0
--max_samples 1000
--per_device_train_batch_size 1
--per_device_eval_batch_size 1
--gradient_accumulation_steps 1
--lr_scheduler_type cosine
--max_grad_norm 1.0
--logging_steps 5
--save_steps 1000
--warmup_steps 0
--neft_alpha 0
--train_on_prompt False
--upcast_layernorm False
--lora_rank 8
--lora_dropout 0.1
--lora_target Wqkv
--resume_lora_training True
--output_dir saves/Phi1.5-1.3B/lora/law_full
--fp16 True
--plot_loss True
也可以考慮使用kaggle,可以每週使用30個小時,可以選擇2張T4,使用ZeRO stage 3 全參微調
deepspeed配置
{
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"zero_allow_untested_optimizer": true,
"fp16": {
"enabled": "auto",
"loss_scale": 0,
"initial_scale_power": 16,
"loss_scale_window": 1000,
"hysteresis": 2,
"min_loss_scale": 1
},
"zero_optimization": {
"stage": 3,
"overlap_comm": false,
"contiguous_gradients": true,
"sub_group_size": 5e7,
"reduce_bucket_size": "auto",
"stage3_prefetch_bucket_size": "auto",
"stage3_param_persistence_threshold": "auto",
"stage3_max_live_parameters": 5e7,
"stage3_max_reuse_distance": 5e7,
"stage3_gather_16bit_weights_on_model_save": true
}
}