Step by step using Colab to train legal LLM, based on microsoft/phi-1_5, ChatGLM3-6B. Through this project you can manually understand fine-tuning LLM 0 costs. If you want to understand the specific code implementation of LLM fine-tuning, you can refer to the my_finetune project ?.
| name | Colab | Datasets |
|---|---|---|
| Self-cognitive lora-SFT fine-tuning | self_cognition.json | |
| Legal Q&A lora-SFT fine tune | DISC-LawLLM | |
| Legal Q&A full parameters-SFT fine-tuning* | DISC-LawLLM | |
| ChatGLM3-6B Self-cognitive lora-SFT fine-tuning* | self_cognition.json |
*If you are a Colab Pro member user, you can try full parameters - SFT fine-tuning, use high RAM+T4, and 1,000 pieces of data will take about 20+ hours.
*If you are a Colab Pro member user, ChatGLM3-6B self-cognitive lora-SFT fine-tuning, using high RAM+T4, it only takes a few minutes, and the effect is better
Use colab free T4 graphics card to complete legal Q&A command supervision fine tuning (SFT) microsoft/phi-1_5 model
Source of self-cognition data: self_cognition.json
80 pieces of data, use T4 lora to fine-tune phi-1_5, and you can fine-tune it in a few minutes.
Fine-tuning parameters , please refer to colab for details.
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
Effect
Legal Q&A Data Source: DISC-LawLLM
In order to reduce video memory, use deepspeed stage2, cutoff_len can reach up to 1792, and the video memory will be exploded if there are more video memory.
deepspeed configuration
{
"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
}
}
Fine-tuning parameters
1000 pieces of data, T4 takes about 60 minutes
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
You can view the memory required for deepspeed ZeRO stage through estimate_zero3_model_states_mem_needs_all_live.
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)
As shown in the figure, offload_optimizer -> Microsoft/phi-1_5 requires 32G memory after cpu, and 52G high memory of colab can meet the needs.
deepspeed configuration
{
"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
You can also consider using kaggle, which can be used for 30 hours a week, you can choose 2 T4s, and use ZeRO stage 3 to fine-tune the full parameters.
deepspeed configuration
{
"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
}
}