Colabを使用して、Microsoft/Phi-1_5、ChatGlm3-6Bに基づいてLegal LLMをトレーニングするために段階的に段階的に使用します。このプロジェクトを通じて、微調整LLM 0コストを手動で理解できます。 LLM微調整の特定のコード実装を理解したい場合は、 my_finetuneプロジェクトを参照できますか?
| 名前 | colab | データセット |
|---|---|---|
| 自己認知lora-sft微調整 | self_cognition.json | |
| 法律Q&Aロラスフツ微調整 | disc-lawllm | |
| リーガルQ&Aフルパラメーター-SFT微調整* | disc-lawllm | |
| chatglm3-6b自己認知lora-sft微調整* | self_cognition.json |
*Colab Proメンバーのユーザーの場合、完全なパラメーターを試すことができます - SFT微調整、High RAM+ T4を使用し、1,000個のデータには約20時間以上かかります。
*Colab Proメンバーのユーザーである場合、Chatglm3-6Bの自己認知LORA-SFT微調整、High RAM+T4を使用して、数分しかかかりません。
Colab Free T4 Graphics Cardを使用して、法的Q&Aコマンド監督微調整(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
効果
リーガルQ&Aデータソース:disc-lawllm
ビデオメモリを減らすために、deepspeed stage2を使用して、cutoff_lenが1792年までに到達することができ、ビデオメモリが増えるとビデオメモリが爆発します。
ディープスピード構成
{
"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
atmatiate_zero3_model_states_mem_needs_all_liveを使用して、deepspeedゼロステージに必要なメモリを表示できます。
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-> Microsoft/Phi -1_5にはCPU後に32Gメモリが必要であり、Colabの高さ52Gがニーズを満たすことができます。
ディープスピード構成
{
"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
また、週に30時間使用できるKaggleの使用を検討することもできます。2つのT4を選択し、ステージ3を使用して完全なパラメーターを微調整できます。
ディープスピード構成
{
"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
}
}