大模型高效微调-PEFT框架介绍

1、背景介绍

最近开源的大模型越来越多,但是针对我们个人来说,从零开始训练一个大模型的成本太高,因此我们介绍一个针对大模型的高效微调框架-PEFT

github地址:https://github.com/huggingface/peft/tree/main

PEFT的全称是Parameter-Efficient Fine-Tuning,是transform开发的一个参数高效微调的库,可以有效地使预训练语言模型 (PLM) 适应各种下游应用程序,而无需微调模型的所有参数。

2、支持的方法

目前PEFT支持如下几种参数微调的方法

LoRA: LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS

Prefix Tuning: Prefix-Tuning: Optimizing Continuous Prompts for GenerationP-Tuning v2: Prompt Tuning Can Be Comparable to Fine-tuning Universally Across Scales and Task

P-Tuning: GPT Understands, Too

Prompt Tuning: The Power of Scale for Parameter-Efficient Prompt Tuning

AdaLoRA: Adaptive Budget Allocation for Parameter-Efficient Fine-Tuning

针对上述方法原理的介绍,我们后期会分几个章节来进行讲解

3、支持的模型

PEFT目前支持的模型列表如下:

3-1、Causal Language Modeling

image.png

3-2、Conditional Generation

image.png

3-3、Sequence Classification

image.png

3-4、Token Classification

image.png

4、PEFT的简单使用

我们这里介绍,利用Lora来训练 1.2B 参数的 bigscience/mt0-large模型来生成分类标签

4-1、PeftConfig

每个peft方法都由一个PeftConfig类来定义,这个类存储了用于构建 PeftModel 的所有重要参数。 这里我们是Lora这个方法进行微调,所以我们创建一个LoraConfig的类,这个类里面包含的重要参数如下:

task_type,任务类型

inference_mode,是否使用模型进行推理

r,低质矩阵的维度

lora_alpha,低质矩阵的比例因子

lora_dropout,Lora层的dropout概率

from peft import LoraConfig, TaskType

peft_config = LoraConfig(task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1)

4-2、PeftModel

首先我们需要加载需要微调的模型

from transformers import AutoModelForSeq2SeqLM

model_name_or_path = "bigscience/mt0-large"
tokenizer_name_or_path = "bigscience/mt0-large"
model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)

然后,使用get_peft_model() 函数创建PeftModel,get_peft_model需要传入微调的model以及对应的PeftConfig。如果我们要了解模型中可训练参数的数量,我们可以使用 print_trainable_parameters 方法。通过打印的结果,我们可以看到我们只训练了模型参数的 0.19%,相对于原始的大模型,这个训练的参数量已经非常小了。

from peft import get_peft_model

model = get_peft_model(model, peft_config)
model.print_trainable_parameters()
"output: trainable params: 2359296 || all params: 1231940608 || trainable%: 0.19151053100118282"

接下来,我们就可以使用,Transformers Trainer,Accelerate 或者是 PyTorch training loop,来训练自己的模型,模型训练完成后,使用 save_pretrained 函数将模型保存到目录中。

model.save_pretrained("output_dir")

模型保存之后,保存了2个文件,adapter_model.bin的大小在几M到几十M之间,这个跟我们训练的参数量有关

adapter_config.json
adapter_model.bin

猜你喜欢

转载自blog.csdn.net/qq_38563206/article/details/133085084