对于象meta的开源大模型,如llama3,由于都是用通用数据进行预训练,对想使用其模型的公司来说,可能会不适用,因为这大模型对公司的数据不熟悉,因此引入微调(fine-tunning)。
通过喂给大模型大量数据,1万条起步,使得大模型也能对公司的数据熟悉,进而用于各种对话场景。
1.克隆并安装llama factory库,install-llamafactory.sh
bin_path=$(cd `dirname $0`; pwd)
cd $bin_path/../
pwd
git clone --depth 1 https://github.com/hiyouga/llama-factory.git
cd llama-factory
pip install -e ".[torch,metrics,bitsandbytes,modelscope]"
2.设置环境变量
export use_modelscope_hub=1 #使用modelscop模型库,非huggingface的
export cuda_visible_devices=0 #设置使用gpu
export hf_endpoint=https://hf-mirror.com #设置huggingface的替代地址
export modelscope_cache=/root/autodl-tmp/models/modelscope #设置modelscope中的大模型保存路径
export llamafactory_home=/root/autodl-tmp/llama-factory
3.准备数据
#在data/dataset_info.json中加入此数据
"docker_command_nl": {
"hf_hub_url": "mattcoddity/dockernlcommands"
},
在data目录中加入训练数据,mattcoddity/dockernlcommands.json
数据格式为:
[
{
"input": "give me a list of containers that have the ubuntu image as their ancestor.",
"instruction": "translate this sentence in docker command",
"output": "docker ps --filter 'ancestor=ubuntu'"
},
] 4.训练大模型
训练的参数文件:llama3_lora_sft_docker_command.yaml
### model
#md model id
model_name_or_path: llm-research/meta-llama-3-8b-instruct
#huggingface model id
#model_name_or_path: meta-llama/meta-llama-3-8b-instruct
### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all
### dataset
dataset: docker_command_nl
template: llama3
cutoff_len: 1024
max_samples: 1000
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: /root/autodl-tmp/my-test/saves/llama3-8b/lora/sft/docker-commnad-nlp/sft
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true
### train
per_device_train_batch_size: 4
gradient_accumulation_steps: 8
learning_rate: 1.0e-4
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000
### eval
val_size: 0.1
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 500
训练命令:lora-train-docker-command.sh
bin_path=$(cd `dirname $0`; pwd)
cd $bin_path/
pwd
cd $llamafactory_home
pwd
llamafactory-cli train $bin_path/conf/llama3_lora_sft_docker_command.yaml
执行此命令即可开始训练大模型。
5.合并大模型
合并用的参数文件,llama3_lora_export_docker_command.yaml
### model
#md model id
model_name_or_path: llm-research/meta-llama-3-8b-instruct
#huggingface model id
#model_name_or_path: meta-llama/meta-llama-3-8b-instruct
adapter_name_or_path: /root/autodl-tmp/my-test/saves/llama3-8b/lora/docker-commnad-nlp/sft
template: llama3
export_dir: /root/autodl-tmp/my-test/saves/llama3-8b/lora/docker-commnad-nlp/export
finetuning_type: lora
export_size: 2
export_device: gpu
export_legacy_format: false
合并命令,lora-export-docker-command.sh
bin_path=$(cd `dirname $0`; pwd)
cd $bin_path/
pwd
llamafactory-cli export conf/llama3_lora_export_docker_command.yaml