文章目录
LLaMA Efficient Tuning 安装 数据准备 浏览器一体化界面 单 GPU 训练 train_bash 1、预训练 pt 2、指令监督微调 sft 3、奖励模型训练 rm 4、PPO 训练 ppo 5、DPO 训练 dpo 多 GPU 分布式训练 1、使用 Huggingface Accelerate 2、使用 DeepSpeed 导出微调后的模型 export_model.py API 服务 api_demo.py 命令行测试 cli_demo.py 浏览器测试 web_demo.py 指标评估(BLEU 分数和汉语 ROUGE 分数) 模型预测LLaMA Efficient Tuning
https://github.com/rancheng/LLaMA-Efficient-Tuning/blob/main/README_zh.md
支持模型:
LLaMA、LLaMA-2、BLOOM、BLOOMZ、Falcon、Baichuan、Baichuan2、InternLM、Qwen、XVERSE、ChatGLM2
--lora_target
参数的部分可选项。请使用 python src/train_bash.py -h
查看全部可选项。 对于所有“基座”(Base)模型,--template
参数可以是 default
, alpaca
, vicuna
等任意值。但“对话”(Chat)模型请务必使用对应的模板。 安装
软件依赖
Python 3.8+ 和 PyTorch 1.13.1+ ?Transformers, Datasets, Accelerate, PEFT 和 TRL sentencepiece 和 tiktoken jieba, rouge-chinese 和 nltk (用于评估) gradio 和 matplotlib (用于网页端交互) uvicorn, fastapi 和 sse-starlette (用于 API) 以及 强而有力的 GPU!git clone https://github.com/hiyouga/LLaMA-Efficient-Tuning.git conda create -n llama_etuning python=3.10 conda activate llama_etuning cd LLaMA-Efficient-Tuning pip install -r requirements.txt
如果要在 Windows 平台上开启量化 LoRA(QLoRA),需要安装预编译的 bitsandbytes 库, 支持 CUDA 11.1 到 12.1.
pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.39.1-py3-none-win_amd64.whl
数据准备
关于数据集文件的格式,请参考 data/example_dataset
文件夹的内容。
https://github.com/rancheng/LLaMA-Efficient-Tuning/tree/main/data/example_dataset
构建自定义数据集时,既可以使用单个 .json
文件,也可以使用一个数据加载脚本和多个文件。
注意:使用自定义数据集时,请更新 data/dataset_info.json
文件,该文件的格式请参考 data/README.md
。
https://github.com/rancheng/LLaMA-Efficient-Tuning/blob/main/data/dataset_info.json
https://github.com/rancheng/LLaMA-Efficient-Tuning/blob/main/data/README.md
浏览器一体化界面
CUDA_VISIBLE_DEVICES=0 python src/train_web.py
我们极力推荐新手使用浏览器一体化界面,因为它还可以自动生成运行所需的命令行脚本。
目前网页 UI 仅支持单卡训练。
单 GPU 训练 train_bash
1、预训练 pt
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --stage pt \ --model_name_or_path path_to_llama_model \ --do_train \ --dataset wiki_demo \ --template default \ --finetuning_type lora \ --lora_target q_proj,v_proj \ --output_dir path_to_pt_checkpoint \ --overwrite_cache \ --per_device_train_batch_size 4 \ --gradient_accumulation_steps 4 \ --lr_scheduler_type cosine \ --logging_steps 10 \ --save_steps 1000 \ --learning_rate 5e-5 \ --num_train_epochs 3.0 \ --plot_loss \ --fp16
2、指令监督微调 sft
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --stage sft \ --model_name_or_path path_to_llama_model \ --do_train \ --dataset alpaca_gpt4_zh \ --template default \ --finetuning_type lora \ --lora_target q_proj,v_proj \ --output_dir path_to_sft_checkpoint \ --overwrite_cache \ --per_device_train_batch_size 4 \ --gradient_accumulation_steps 4 \ --lr_scheduler_type cosine \ --logging_steps 10 \ --save_steps 1000 \ --learning_rate 5e-5 \ --num_train_epochs 3.0 \ --plot_loss \ --fp16
3、奖励模型训练 rm
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --stage rm \ --model_name_or_path path_to_llama_model \ --do_train \ --dataset comparison_gpt4_zh \ --template default \ --finetuning_type lora \ --lora_target q_proj,v_proj \ --resume_lora_training False \ --checkpoint_dir path_to_sft_checkpoint \ --output_dir path_to_rm_checkpoint \ --per_device_train_batch_size 2 \ --gradient_accumulation_steps 4 \ --lr_scheduler_type cosine \ --logging_steps 10 \ --save_steps 1000 \ --learning_rate 1e-6 \ --num_train_epochs 1.0 \ --plot_loss \ --fp16
4、PPO 训练 ppo
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --stage ppo \ --model_name_or_path path_to_llama_model \ --do_train \ --dataset alpaca_gpt4_zh \ --template default \ --finetuning_type lora \ --lora_target q_proj,v_proj \ --resume_lora_training False \ --checkpoint_dir path_to_sft_checkpoint \ --reward_model path_to_rm_checkpoint \ --output_dir path_to_ppo_checkpoint \ --per_device_train_batch_size 2 \ --gradient_accumulation_steps 4 \ --lr_scheduler_type cosine \ --logging_steps 10 \ --save_steps 1000 \ --learning_rate 1e-5 \ --num_train_epochs 1.0 \ --plot_loss
5、DPO 训练 dpo
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --stage dpo \ --model_name_or_path path_to_llama_model \ --do_train \ --dataset comparison_gpt4_zh \ --template default \ --finetuning_type lora \ --lora_target q_proj,v_proj \ --resume_lora_training False \ --checkpoint_dir path_to_sft_checkpoint \ --output_dir path_to_dpo_checkpoint \ --per_device_train_batch_size 2 \ --gradient_accumulation_steps 4 \ --lr_scheduler_type cosine \ --logging_steps 10 \ --save_steps 1000 \ --learning_rate 1e-5 \ --num_train_epochs 1.0 \ --plot_loss \ --fp16
多 GPU 分布式训练
1、使用 Huggingface Accelerate
accelerate config # 首先配置分布式环境 accelerate launch src/train_bash.py # 参数同上
使用 DeepSpeed ZeRO-2 进行全参数微调的 Accelerate 配置示例
compute_environment: LOCAL_MACHINE deepspeed_config: gradient_accumulation_steps: 4 gradient_clipping: 0.5 offload_optimizer_device: none offload_param_device: none zero3_init_flag: false zero_stage: 2 distributed_type: DEEPSPEED downcast_bf16: 'no' machine_rank: 0 main_training_function: main mixed_precision: fp16 num_machines: 1 num_processes: 4 rdzv_backend: static same_network: true tpu_env: [] tpu_use_cluster: false tpu_use_sudo: false use_cpu: false
2、使用 DeepSpeed
deepspeed --num_gpus 8 --master_port=9901 src/train_bash.py \ --deepspeed ds_config.json \ ... # 参数同上
使用 DeepSpeed ZeRO-2 进行全参数微调的 DeepSpeed 配置示例
{ "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, "allgather_partitions": true, "allgather_bucket_size": 5e8, "reduce_scatter": true, "reduce_bucket_size": 5e8, "overlap_comm": false, "contiguous_gradients": true } }
导出微调后的模型 export_model.py
python src/export_model.py \ --model_name_or_path path_to_llama_model \ --template default \ --finetuning_type lora \ --checkpoint_dir path_to_checkpoint \ --output_dir path_to_export
API 服务 api_demo.py
python src/api_demo.py \ --model_name_or_path path_to_llama_model \ --template default \ --finetuning_type lora \ --checkpoint_dir path_to_checkpoint
关于 API 文档请见 http://localhost:8000/docs
。
命令行测试 cli_demo.py
python src/cli_demo.py \ --model_name_or_path path_to_llama_model \ --template default \ --finetuning_type lora \ --checkpoint_dir path_to_checkpoint
浏览器测试 web_demo.py
python src/web_demo.py \ --model_name_or_path path_to_llama_model \ --template default \ --finetuning_type lora \ --checkpoint_dir path_to_checkpoint
指标评估(BLEU 分数和汉语 ROUGE 分数)
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --stage sft \ --model_name_or_path path_to_llama_model \ --do_eval \ --dataset alpaca_gpt4_zh \ --template default \ --finetuning_type lora \ --checkpoint_dir path_to_checkpoint \ --output_dir path_to_eval_result \ --per_device_eval_batch_size 8 \ --max_samples 100 \ --predict_with_generate
我们建议在量化模型的评估中使用 --per_device_eval_batch_size=1
和 --max_target_length 128
。
模型预测
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --stage sft \ --model_name_or_path path_to_llama_model \ --do_predict \ --dataset alpaca_gpt4_zh \ --template default \ --finetuning_type lora \ --checkpoint_dir path_to_checkpoint \ --output_dir path_to_predict_result \ --per_device_eval_batch_size 8 \ --max_samples 100 \ --predict_with_generate
codellamapythonbashtpugitapigpugptgithubgpt4alpacaweb浏览器clijson命令行mac分布式数据集分布式训练api 服务clip自定义模型训练预训练奖励模型windowschat数据准备huggingfacefastapitransformerapi 文档urlclonedoccpucreatepytorchtokentiktok