PEFT 文档
IA3
并获得增强的文档体验
开始使用
IA3
Infused Adapter by Inhibiting and Amplifying Inner Activations,或 IA3,是一种向自注意力机制和编码器-解码器注意力层的键和值,以及逐位置前馈网络的中间激活添加三个学习向量以重新缩放的方法。
论文摘要如下:
少样本上下文学习 (ICL) 使预训练语言模型能够执行以前未见过的任务,而无需任何基于梯度的训练,方法是将少量训练示例作为输入的一部分馈送。ICL 会产生大量的计算、内存和存储成本,因为它涉及每次进行预测时处理所有训练示例。参数高效微调 (PEFT)(例如,适配器模块、提示调优、稀疏更新方法等)提供了一种替代范例,其中训练一小组参数以使模型能够执行新任务。在本文中,我们严格比较了少样本 ICL 和 PEFT,并证明后者提供了更好的准确性以及显着降低的计算成本。在此过程中,我们引入了一种新的 PEFT 方法,称为 (IA)^3,它通过学习到的向量缩放激活,在仅引入相对少量新参数的同时获得更强的性能。我们还基于 T0 模型提出了一个简单的配方,称为 T-Few,它可以应用于新任务,而无需特定于任务的调整或修改。我们通过将其应用于 RAFT 基准来验证 T-Few 在完全未见过的任务上的有效性,首次获得了超人的性能,并以 6% 的绝对优势超越了最先进水平。我们实验中使用的所有代码都是公开可用的.
IA3Config
class peft.IA3Config
< source >( task_type: typing.Union[str, peft.utils.peft_types.TaskType, NoneType] = None peft_type: typing.Union[str, peft.utils.peft_types.PeftType, NoneType] = None auto_mapping: typing.Optional[dict] = None base_model_name_or_path: typing.Optional[str] = None revision: typing.Optional[str] = None inference_mode: bool = False target_modules: Optional[Union[list[str], str]] = None exclude_modules: Optional[Union[list[str], str]] = None feedforward_modules: Optional[Union[list[str], str]] = None fan_in_fan_out: bool = False modules_to_save: Optional[list[str]] = None init_ia3_weights: bool = True )
参数
- target_modules (
Optional[Union[List[str], str]]
) — 要应用适配器的模块的名称。如果指定了此项,则仅替换具有指定名称的模块。当传递字符串时,将执行正则表达式匹配。当传递字符串列表时,将执行精确匹配,或者检查模块的名称是否以任何传递的字符串结尾。如果指定为“all-linear”,则选择所有线性/Conv1D 模块,但不包括输出层。如果未指定,则将根据模型架构选择模块。如果架构未知,则会引发错误——在这种情况下,您应该手动指定目标模块。 - exclude_modules (
Optional[Union[List[str], str]]
) — 不应用适配器的模块的名称。当传递字符串时,将执行正则表达式匹配。当传递字符串列表时,将执行精确匹配,或者检查模块的名称是否以任何传递的字符串结尾。 - feedforward_modules (
Optional[Union[List[str], str]]
) — 要被视为前馈模块的模块的名称,如原始论文中所述。这些模块的 (IA)³ 向量将乘以输入,而不是输出。feedforward_modules
必须是target_modules
中存在的名称或名称的子集。 - fan_in_fan_out (
bool
) — 如果要替换的层存储的权重类似 (fan_in, fan_out),则设置为 True。例如,gpt-2 使用Conv1D
,它存储的权重类似于 (fan_in, fan_out),因此应将其设置为True
。 - modules_to_save (
Optional[List[str]]
) — 除了 (IA)³ 层之外,要设置为可训练并在最终检查点中保存的模块列表。 - init_ia3_weights (
bool
) — 是否初始化 (IA)³ 层中的向量,默认为True
。不建议将其设置为False
。
这是用于存储 IA3Model 配置的配置类。
IA3Model
class peft.IA3Model
< source >( model config adapter_name low_cpu_mem_usage: bool = False ) → torch.nn.Module
参数
- model (PreTrainedModel) — 要适配的模型。
- config (IA3Config) — (IA)^3 模型的配置。
- adapter_name (
str
) — 适配器的名称,默认为"default"
。 - low_cpu_mem_usage (
bool
,optional
, defaults toFalse
) — 在 meta 设备上创建空的适配器权重。有助于加速加载过程。
返回
torch.nn.Module
(IA)^3 模型。
从预训练的 transformers 模型创建 Infused Adapter by Inhibiting and Amplifying Inner Activations ((IA)^3) 模型。该方法在 https://arxiv.org/abs/2205.05638 中详细描述。
示例
>>> from transformers import AutoModelForSeq2SeqLM, ia3Config
>>> from peft import IA3Model, IA3Config
>>> config = IA3Config(
... peft_type="IA3",
... task_type="SEQ_2_SEQ_LM",
... target_modules=["k", "v", "w0"],
... feedforward_modules=["w0"],
... )
>>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
>>> ia3_model = IA3Model(config, model)
属性:
- model (PreTrainedModel) — 要适配的模型。
- peft_config (
ia3Config
): (IA)^3 模型的配置。
add_weighted_adapter
< source >( adapters: list[str] weights: list[float] adapter_name: str )
此方法通过合并给定的适配器和给定的权重来添加新的适配器。
删除现有的适配器。
merge_and_unload
< source >( safe_merge: bool = False adapter_names: Optional[list[str]] = None )
此方法将 IA³ 层合并到基础模型中。如果有人想将基础模型用作独立模型,则需要这样做。
示例
>>> from transformers import AutoModelForCausalLM
>>> from peft import PeftModel
>>> base_model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-40b")
>>> peft_model_id = "smangrul/falcon-40B-int4-peft-lora-sfttrainer-sample"
>>> model = PeftModel.from_pretrained(base_model, peft_model_id)
>>> merged_model = model.merge_and_unload()
set_adapter
< source >( adapter_name: str | list[str] )
设置活动的适配器。
此外,此函数会将指定的适配器设置为可训练的 (即,requires_grad=True)。如果不需要这样做,请使用以下代码。
通过移除所有 IA³ 模块但不合并,返回基础模型。这将返回原始的基础模型。