TRL 文档

使用 peft 和 trl 微调 8 位模型与低秩适配 (LoRA) 的示例

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

使用 peft 和 trl 微调 8 位模型与低秩适配 (LoRA) 的示例

这些示例中的 notebook 和脚本展示了如何使用低秩适配 (LoRA) 以内存高效的方式微调模型。peft 库中支持大多数 PEFT 方法,但请注意,某些 PEFT 方法(如 Prompt tuning)不受支持。有关 LoRA 的更多信息,请参阅原始论文

以下是 trl 仓库中启用 peft 的 notebook 和脚本的概述

文件 任务 描述
stack_llama/rl_training.py RLHF 使用学习到的奖励模型和 peft 对 7b 参数 LLaMA 模型进行分布式微调。
stack_llama/reward_modeling.py 奖励建模 使用 peft 对 7b 参数 LLaMA 奖励模型进行分布式训练。
stack_llama/supervised_finetuning.py SFT 使用 peft 对 7b 参数 LLaMA 模型进行分布式指令/监督式微调。

安装

注意:peft 正在积极开发中,因此我们直接从其 Github 页面安装。Peft 也依赖于最新版本的 transformers。

pip install trl[peft]
pip install bitsandbytes loralib
pip install git+https://github.com/huggingface/transformers.git@main
#optional: wandb
pip install wandb

注意:如果您不想使用 wandb 记录日志,请删除 scripts/notebooks 中的 log_with="wandb"。您也可以将其替换为您喜欢的受 accelerate 支持的实验跟踪器。

如何使用它?

只需在您的脚本中声明一个 PeftConfig 对象,并通过 .from_pretrained 传递它以加载 TRL+PEFT 模型。

from peft import LoraConfig
from trl import AutoModelForCausalLMWithValueHead

model_id = "edbeeching/gpt-neo-125M-imdb"
lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
)

model = AutoModelForCausalLMWithValueHead.from_pretrained(
    model_id, 
    peft_config=lora_config,
)

如果您想以 8 位精度加载模型

pretrained_model = AutoModelForCausalLMWithValueHead.from_pretrained(
    config.model_name, 
    load_in_8bit=True,
    peft_config=lora_config,
)

……或者以 4 位精度

pretrained_model = AutoModelForCausalLMWithValueHead.from_pretrained(
    config.model_name, 
    peft_config=lora_config,
    load_in_4bit=True,
)

启动脚本

trl 库由 accelerate 提供支持。因此,最好使用以下命令配置和启动训练

accelerate config # will prompt you to define the training configuration
accelerate launch examples/scripts/ppo.py --use_peft # launch`es training

使用 trl + peft 和数据并行

您可以根据需要扩展到任意数量的 GPU,只要您能够将训练过程放在单个设备中即可。您需要应用的唯一调整是按如下方式加载模型

from peft import LoraConfig
...

lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
)

pretrained_model = AutoModelForCausalLMWithValueHead.from_pretrained(
    config.model_name, 
    peft_config=lora_config,
)

如果您想以 8 位精度加载模型

pretrained_model = AutoModelForCausalLMWithValueHead.from_pretrained(
    config.model_name, 
    peft_config=lora_config,
    load_in_8bit=True,
)

……或者以 4 位精度

pretrained_model = AutoModelForCausalLMWithValueHead.from_pretrained(
    config.model_name, 
    peft_config=lora_config,
    load_in_4bit=True,
)

最后,确保奖励也在正确的设备上计算,为此您可以使用 ppo_trainer.model.current_device

大型模型的朴素流水线并行 (NPP)(>60B 模型)

trl 库还支持大型模型(>60B 模型)的朴素流水线并行 (NPP)。这是一种在多个 GPU 上并行化模型的简单方法。这种范例称为“朴素流水线并行” (NPP),是一种在多个 GPU 上并行化模型的简单方法。我们在多个 GPU 上加载模型和适配器,激活和梯度将在 GPU 之间朴素地通信。这支持 int8 模型以及其他 dtype 模型。

如何使用 NPP?

只需在 from_pretrained 上使用自定义 device_map 参数加载您的模型,即可在多个设备上拆分您的模型。请查看此教程,了解如何为您的模型正确创建 device_map

另请确保将 lm_head 模块放在第一个 GPU 设备上,因为如果它不在第一个设备上,可能会引发错误。在撰写本文时,您需要安装 acceleratemain 分支:pip install git+https://github.com/huggingface/accelerate.git@mainpeftmain 分支:pip install git+https://github.com/huggingface/peft.git@main

启动脚本

虽然 trl 库由 accelerate 提供支持,但您应该在单个进程中运行您的训练脚本。请注意,我们尚不支持数据并行与 NPP 一起使用。

python PATH_TO_SCRIPT

微调 Llama-2 模型

您可以使用 SFTTrainer 和官方脚本轻松微调 Llama2 模型!例如,要在 Guanaco 数据集上微调 llama2-7b,请运行(在单个 NVIDIA T4-16GB 上测试)

python trl/scripts/sft.py --output_dir sft_openassistant-guanaco  --model_name meta-llama/Llama-2-7b-hf --dataset_name timdettmers/openassistant-guanaco --load_in_4bit --use_peft --per_device_train_batch_size 4 --gradient_accumulation_steps 2
< > 更新 在 GitHub 上