PEFT documentation

用于 PEFT 集成的函数

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.21.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

用于 PEFT 集成的函数

一组可能对非 PeftModel 模型(例如 transformers 或 diffusers 集成)有用的函数

此处提供的函数可被视为 PEFT 的“公共 API”,因此提供 PEFT 集成的包可以安全地使用它们。

转换适配器权重的数据类型

peft.tuners.tuners_utils.cast_adapter_dtype

< >

( model: nn.Moduleadapter_name: strautocast_adapter_dtype: bool = True )

参数

  • adapter_name (str) — 适配器名称。
  • autocast_adapter_dtype (bool, 可选) — 是否自动转换适配器数据类型。默认为 True。

一个辅助方法,用于将适配器权重转换为正确的 dtype。如果某些对象引用了旧参数,则不会更新这些对象,而是将新参数重新分配给适配器层。

目前,这只会将浮点数数据类型提升为 float32。

从模型中删除 PEFT 适配器

peft.tuners.tuners_utils.delete_adapter

< >

( model: nn.Moduleadapter_name: strprefix: strlayer_cls: type[BaseTunerLayer] = <class 'peft.tuners.tuners_utils.BaseTunerLayer'> ) → new_adapter (list[str] | None)

参数

  • model (nn.Module) — 要从中删除适配器的模型。
  • adapter_name (str) — 要删除的适配器名称。
  • prefix (str) — PEFT 方法的前缀,例如 LoRA 的 “lora_”。
  • layer_cls(类型,可选)— 适配器层的类。默认为 BaseTunerLayer。

返回值

new_adapter (list[str] | None)

删除后剩余的适配器名称,如果没有剩余的活跃适配器则为 None。如有必要,请使用此值来设置模型的新活跃适配器。

删除现有的 PEFT 适配器。

注意:此函数不会删除模型上的 PEFT 配置(如果存在)。即使删除了最后一个 PEFT 适配器,它也不会完全清除 PEFT 层。为此,如果使用 PEFT 模型实例,请考虑使用 model.unload(),或者直接重新加载基础模型。

获取 PEFT 适配器的状态字典

peft.get_peft_model_state_dict

< >

( modelstate_dict = Noneadapter_name: str = 'default'unwrap_compiled: bool = Falsesave_embedding_layers: bool | Literal['auto'] = 'auto' )

参数

  • model (PeftModel) — Peft 模型。当使用 torch.nn.DistributedDataParallel、DeepSpeed 或 FSDP 时,模型应为底层模型/解包后的模型(即 model.module)。
  • state_dict (dict,可选,默认为 None) — 模型的 state dict。如果未提供,将使用传入模型的 state dict。
  • adapter_name (str,可选,默认为 "default") — 要返回其状态字典的适配器名称。
  • unwrap_compiled (bool, 可选,默认为 False) — 如果使用了 torch.compile,是否解包模型。
  • save_embedding_layers (Union[bool, str], , 可选,默认为 auto) — 如果为 True,则在保存适配器权重的同时保存嵌入层。如果为 auto,则在可用时检查配置中的 target_modules 是否包含常见的嵌入层 peft.utils.other.EMBEDDING_LAYER_NAMES,并据此设置布尔标志。此功能仅适用于 🤗 transformers 模型。

获取 PEFT 模型中指定适配器的状态字典。

这仅包含 PEFT 参数,不包括基础模型的参数。因此,返回的 state_dict 通常远小于完整模型的规模。要获取完整的 state_dict,只需调用 model.state_dict() 即可。

请注意,适配器名称会从 state_dict 中移除,因为这只是一个任意名称,可以在加载适配器时更改。因此,例如,如果适配器名称为 'default',且原始键为 'model.q_proj.lora_A.default.weight',则返回的键将是 'model.q_proj.lora_A.weight'。在加载权重时,请结合使用此函数和 set_peft_model_state_dict() 来处理适配器名称。

根据 PEFT 配置向模型注入 PEFT 适配器

peft.inject_adapter_in_model

< >

( peft_config: PeftConfigmodel: torch.nn.Moduleadapter_name: str = 'default'low_cpu_mem_usage: bool = Falsestate_dict: Optional[dict[str, torch.Tensor]] = None )

参数

  • peft_config (PeftConfig) — 包含 PEFT 模型参数的配置对象。
  • model (torch.nn.Module) — 将注入适配器的输入模型。
  • adapter_name (str, optional, 默认为 "default") — 要注入的适配器名称。如果未提供,则使用默认适配器名称(“default”)。
  • low_cpu_mem_usage (bool, optional, defaults to False) — 在 meta 设备上创建空的适配器权重。有助于加快加载过程。
  • state_dict (dict,可选,默认为 None) — 如果在此处传入 state_dict,适配器将根据 state_dict 中的条目进行注入。 当 PEFT 方法的确切 target_modules 未知时,这可能会很有用,例如因为检查点在创建时未包含元数据。请注意,state_dict 中的值不会被使用,仅使用键来确定应进行适配的正确层。

创建 PEFT 层并将其就地注入模型。

目前,该 API 不支持提示学习方法、自适应提示,或那些在多个目标层之间共享适配器状态的调优器。对于后者,请使用 get_peft_model()。

此函数与 get_peft_model() 类似,但它不会返回 PeftModel 实例。相反,它返回传入模型的原始、已修改的实例。

设置模型的当前激活 PEFT 适配器

peft.tuners.tuners_utils.set_adapter

< >

( modeladapter_name: str | list[str]inference_mode: bool = Falselayer_cls: type[BaseTunerLayer] = <class 'peft.tuners.tuners_utils.BaseTunerLayer'> )

参数

  • model (nn.Module) — 需要设置适配器的模型。
  • adapter_name (str, list[str]) — 要设置为激活状态的适配器名称
  • inference_mode(bool,可选)— 是否冻结已激活的适配器(即 requires_grad=False)。默认值为 False。
  • layer_cls(类型,可选)— 适配器层的类。默认为 BaseTunerLayer。

设置模型的当前激活 PEFT 适配器。

Active adapters 是参与前向传播的适配器。如果您想在多个 PEFT 适配器之间切换,请使用此函数。

设置指定适配器的训练模式

peft.tuners.tuners_utils.set_training

< >

( modeladapter_names: str | Sequence[str]training: bool = True )

参数

  • model (nn.Module) — 需要更新其适配器模块训练模式的模型。
  • adapter_names (str 或 Sequence[str]) — 需要更新训练模式的适配器名称。
  • training (bool, 可选) — 是否启用(True,默认值)或禁用(False)训练模式。

在指定的适配器模块上启用或禁用训练模式。

设置指定适配器的 requires_grad 属性

peft.tuners.tuners_utils.set_requires_grad

< >

( modeladapter_names: str | Sequence[str]requires_grad: bool = True )

参数

  • model (nn.Module) — 需要更新其适配器梯度要求的模型。
  • adapter_names (str 或 Sequence[str]) — 需要启用/禁用梯度的适配器名称。
  • requires_grad (bool, 可选) — 是否启用(True,默认)或禁用(False)。

在指定的适配器上启用或禁用梯度。

将 PEFT 状态字典的权重加载到模型中

peft.set_peft_model_state_dict

< >

( modelpeft_model_state_dictadapter_name = 'default'ignore_mismatched_sizes: bool = Falselow_cpu_mem_usage: bool = False )

参数

  • model (PeftModel) — Peft 模型。
  • peft_model_state_dict (dict) — Peft 模型的状态字典。
  • adapter_name (str,可选,默认为 "default") — 要设置其状态字典的适配器名称。
  • ignore_mismatched_sizes (bool,可选,默认为 False) — 是否忽略状态字典中不匹配的大小。
  • low_cpu_mem_usage (bool, optional, defaults to False) — This argument must be True if the model was loaded with adapter weights on the meta device, e.g. after calling inject_adapter_in_model with low_cpu_mem_usage=True. Otherwise, leave it as False.

设置 PEFT 模型的 state dict。

给定一个 PEFT state_dict(由 get_peft_model_state_dict() 返回),将权重插入到模型中。模型需要已经配置好 PEFT 适配器(例如通过 inject_adapter_in_model())。

设置适配器权重时也会重新插入适配器名称。该名称可能与最初用于训练适配器的名称不同。

Update on GitHub