链载Ai

标题: 使用 LLaMA-Factory 微调 llama3 模型 [打印本页]

作者: 链载Ai    时间: 4 小时前
标题: 使用 LLaMA-Factory 微调 llama3 模型

1. LLaMA-Factory模型介绍

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;margin: 1.2em 0px 24px;line-height: 22px;color: rgb(0, 0, 0);">https://github.com/hiyouga/LLaMA-Factory

LLaMA-Factory 是一个用于大型语言模型(LLM)微调的工具,它旨在简化大型语言模型的微调过程, 使得用户可以快速地对模型进行训练和优化,以提高模型在特定任务上的性能。

这个工具支持多种预训练的大型语言模型,例如 LLaMA、LLaVA、Mistral、Mixtral-MoE、Qwen、Yi、 Gemma、Baichuan、ChatGLM 和 Phi 等。

LLaMA-Factory 的特点包括:

  1. 支持多种微调方法:它集成了连续预训练、有监督微调(SFT)、偏好对齐(RLHF)等多种微调方 法。
  2. 高效的微调技术:与 ChatGLM 官方的 P-Tuning 微调相比,LLaMA Factory 的 LoRA 微调提供了 显著的加速比,并且在特定任务上取得了更高的性能分数。
  3. 易用性:LLaMA-Factory 提供了高层次的抽象接口,使得开发者可以开箱即用,快速上手操作。
  4. WebUI 支持:借鉴 Stable Diffusion WebUI,该项目提供了基于 gradio 的网页版工作台,方便初 学者可以迅速上手操作。
  5. 模型导出和推理:支持模型的导出和推理,包括动态合并 LoRA 模型进行推理。
  6. API Server:支持启动 API Server,使得训练好的模型可以通过网络接口被远程访问和调用。
  7. 评测 benchmark:提供了主流评测 benchmark 支持,如 mmlu、cmmlu、ceval 等,用于评估 模型的泛化能力。

LLaMA-Factory 旨在降低大型语言模型微调的门槛,使得更多的研究者和开发者能够利用这些强大的模 型来解决具体的实际问题。

1.1 环境配置

/mnt/workspace路径下执行

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;margin: 1.2em 0px 24px;line-height: 22px;color: rgb(0, 0, 0);">ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;background-color: rgba(0, 0, 0, 0.05);border-radius: 2px;padding: 0.5em;line-height: 22px;color: unset;display: block;text-size-adjust: none;overflow-x: auto;">git clone https://github.com/hiyouga/LLaMA-Factory.git
conda create -n llama_factory python=3.10
conda activate llama_factory
cd LLaMA-Factory
pip install -e .[metrics]

1.2 下载llama3模型

/mnt/workspace路径下执行

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;margin: 1.2em 0px 24px;line-height: 22px;color: rgb(0, 0, 0);">ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;background-color: rgba(0, 0, 0, 0.05);border-radius: 2px;padding: 0.5em;line-height: 22px;color: unset;display: block;text-size-adjust: none;overflow-x: auto;">mkdir models
cd models

/mnt/workspace/models路径下执行

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;margin: 1.2em 0px 24px;line-height: 22px;color: rgb(0, 0, 0);">ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;background-color: rgba(0, 0, 0, 0.05);border-radius: 2px;padding: 0.5em;line-height: 22px;color: unset;display: block;text-size-adjust: none;overflow-x: auto;">pip install modelscope
git clone https://www.modelscope.cn/LLM-Research/Meta-Llama-3-8B-Instruct.git

2 llama3中文增强大模型(lora微调)

使用预训练模型+LoRA适配器的方式, 可以在不重新训练整个模型的情况下, 快速且高效地将模型适应到 新的任务或领域, 如将英文模型适应到中文对话。这是一种常见且实用的微调方法。

2.1 模型训练

修改文件 /mnt/workspace/LLaMA-Factory/examples/train_lora/llama3_lora_sft.yaml

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;margin: 1.2em 0px 24px;line-height: 22px;color: rgb(0, 0, 0);">ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;background: rgb(40, 44, 52);border-radius: 2px;padding: 0.5em;line-height: 22px;color: rgb(171, 178, 191);display: block;text-size-adjust: none;overflow-x: auto;">!上传配置文件
llama3_lora_sft.yaml
ingFang SC", "Microsoft YaHei", sans-serif;font-size: 14px;margin: 1.2em 0px 24px;line-height: 22px;color: rgb(0, 0, 0);">### model
model_name_or_path:/mnt/workspace/models/Meta-Llama-3-8B-Instruct

### method
stage:sft
do_train:true
finetuning_type:lora
lora_target:all

### dataset
dataset:alpaca_gpt4_zh
template:llama3
cutoff_len:2048
max_samples:1000
overwrite_cache:true
preprocessing_num_workers:16

### output
output_dir:/mnt/workspace/models/llama3-lora-zh
logging_steps:10
save_steps:500
plot_loss:true
overwrite_output_dir:true

### train
per_device_train_batch_size:1
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

yaml文件中的主要参数:

model

以上就是这个yaml配置文件的主要参数解释。这些参数设置了一个使用LoRA对Meta-Llama-3-8BInstruct模型在alpaca_gpt4_zh数据集上进行微调的训练过程。

2.2 克隆数据集

在/mnt/workspace/LLaMA-Factory/data路径下使用下面的命令克隆alpaca_gpt4_zh数据集

git clone https://www.modelscope.cn/datasets/llamafactory/alpaca_gpt4_zh.git

然后,修改dataset_info.json文件

!上传配置文件
data_info.json

上传alpaca_gpt4_data_zh.json到data目录下

2.3 训练

llamafactory-cli train examples/train_lora/llama3_lora_sft.yaml

1.4 推理

修改examples/inference/llama3_lora_sft.yaml

model_name_or_path: /mnt/workspace/models/Meta-Llama-3-8B-Instruct
adapter_name_or_path: /mnt/workspace/models/llama3-lora-zh
template: llama3
finetuning_type: lora

/mnt/workspace/LLaMA-Factory路径下执行

llamafactory-clichatexamples/inference/llama3_lora_sft.yaml






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5