【LLaMa3微调】使用 LLaMA-Factory 微调LLaMA3
- 实验环境
1.1 机器- 操作系统:Windows 10 或 Ubuntu
- PyTorch 版本:2.1.0
- Python 版本:3.10(针对Ubuntu 22.04)
- Cuda 版本:12.1
- GPU 配置:p100 (16GB) * 2
- CPU 配置:12 vCPU Intel® Xeon® Platinum 8352V CPU @ 2.10GHz
1.2 基座模型
- 基于中文数据训练过的 LLaMA3 8B 模型:shenzhi-wang/Llama3-8B-Chinese-Chat
- 可选配置:hf 国内镜像站,使用以下命令安装和配置:
pip install -U huggingface_hub export HF_ENDPOINT=https://hf-mirror.com huggingface-cli download --resume-download shenzhi-wang/Llama3-8B-Chinese-Chat --local-dir /root/autodl-tmp/models/Llama3-8B-Chinese-Chat1
- LLaMA-Factory 框架
2.1 安装
- 使用以下命令克隆并安装LLaMA-Factory:
git clone https://github.com/hiyouga/LLaMA-Factory.git cd LLaMA-Factory pip install -e .
2.2 准备训练数据
- 训练数据示例:fruozhiba_qaswift_train.json,可以去魔搭社区下载
- 将训练数据放置在 LLaMA-Factory/data/ 目录下,例如:LLaMA-Factory/data/ruozhiba_qaswift_train.json
- 修改数据注册文件:LLaMA-Factory/data/dataset_info.json,添加数据集的配置信息,例如:
"fintech": {"file_name": "ruozhiba_qaswift_train.json","columns": {"prompt": "instruction","query": "input","response": "output","history": "history"} }
2.3 启动 Web UI
在 LLaMA-Factory 目录下执行以下命令启动 Web UI:
bash cd LLaMA-Factory llamafactory-cli webui
2.4 微调模型
使用 Web UI 或命令行进行微调。
命令行微调示例:构建配置文件 cust/train_llama3_lora_sft.yaml,内容示例如下:
cutoff_len: 1024dataset: ruozhiba_qaswift_traindataset_dir: datado_train: truefinetuning_type: loraflash_attn: autofp16: truegradient_accumulation_steps: 8learning_rate: 0.0002logging_steps: 5lora_alpha: 16lora_dropout: 0lora_rank: 8lora_target: q_proj,v_projlr_scheduler_type: cosinemax_grad_norm: 1.0max_samples: 1000model_name_or_path: /root/autodl-tmp/models/Llama3-8B-Chinese-Chatnum_train_epochs: 10.0optim: adamw_torchoutput_dir: saves/LLaMA3-8B-Chinese-Chat/lora/train_XXXX-XX-XX-XX-XX-XXpacking: falseper_device_train_batch_size: 2plot_loss: truepreprocessing_num_workers: 16report_to: nonesave_steps: 100stage: sfttemplate: llama3use_unsloth: truewarmup_steps: 0
使用以下命令执行微调
```bashllamafactory-cli train cust/train_llama3_lora_sft.yaml```