PEFT 文档
LoKr
并获得增强的文档体验
开始使用
LoKr
低秩 Kronecker 积 (LoKr) 是一种 LoRA 变体方法,它使用两个低秩矩阵逼近大型权重矩阵,并将它们与 Kronecker 积结合起来。LoKr 还提供可选的第三个低秩矩阵,以便在微调期间提供更好的控制。
LoKrConfig
class peft.LoKrConfig
< 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 rank_pattern: Optional[dict] = <factory> alpha_pattern: Optional[dict] = <factory> r: int = 8 alpha: int = 8 rank_dropout: float = 0.0 module_dropout: float = 0.0 use_effective_conv2d: bool = False decompose_both: bool = False decompose_factor: int = -1 rank_dropout_scale: bool = False target_modules: Optional[Union[list[str], str]] = None exclude_modules: Optional[Union[list[str], str]] = None init_weights: Union[bool, Literal['lycoris']] = True layers_to_transform: Optional[Union[list[int], int]] = None layers_pattern: Optional[Union[list[str], str]] = None modules_to_save: Optional[list[str]] = None )
参数
- r (
int
) — LoKr 秩。 - alpha (
int
) — LoKr 缩放的 alpha 参数。 - rank_dropout (
float
) — 训练期间秩维度的 dropout 概率。 - module_dropout (
float
) — 训练期间禁用 LoKr 模块的 dropout 概率。 - use_effective_conv2d (
bool
) — 对 ksize > 1 的 Conv2d 使用参数有效分解(来自 FedPara 论文的“Proposition 3”)。 - decompose_both (
bool
) — 执行左 Kronecker 积矩阵的秩分解。 - decompose_factor (
int
) — Kronecker 积分解因子。 - rank_dropout_scale (‘bool) — 是否在训练时缩放秩 dropout,默认为
False
。 - target_modules (
Optional[Union[List[str], str]]
) — 要将适配器应用于的模块的名称。如果指定了此项,则只会替换具有指定名称的模块。当传递字符串时,将执行正则表达式匹配。当传递字符串列表时,将执行完全匹配,或者检查模块的名称是否以任何传递的字符串结尾。如果指定为“all-linear”,则将选择所有 linear/Conv1D 模块,但不包括输出层。如果未指定,将根据模型架构选择模块。如果架构未知,则会引发错误 — 在这种情况下,您应手动指定目标模块。 - exclude_modules (
Optional[Union[List[str], str]]
) — 不应用适配器的模块的名称。当传递字符串时,将执行正则表达式匹配。当传递字符串列表时,将执行完全匹配,或者检查模块的名称是否以任何传递的字符串结尾。 - init_weights (
bool
) — 是否执行适配器权重的初始化。此项默认为True
。使用“lycoris”以 LYCORIS 仓库的风格初始化权重。不建议传递False
。 - layers_to_transform (
Union[List[int], int]
) — 要转换的层索引。如果传递一个整数列表,它将把适配器应用到列表中指定的层索引。如果传递一个整数,它将把转换应用到这个索引的层。 - layers_pattern (
Optional[Union[List[str], str]]
) — 层模式名称,仅当layers_to_transform
与None
不同时使用。这应该指向模型的nn.ModuleList
,通常称为'layers'
或'h'
。 - rank_pattern (
dict
) — 从层名称或正则表达式到与r
指定的默认秩不同的秩的映射。例如,{'^model.decoder.layers.0.encoder_attn.k_proj': 16}
。 - alpha_pattern (
dict
) — 从层名称或正则表达式到与alpha
指定的默认 alpha 不同的 alpha 的映射。例如,{'^model.decoder.layers.0.encoder_attn.k_proj': 16}
。 - modules_to_save (
Optional[List[str]]
) — 除了适配器层之外,要设置为可训练并在最终检查点中保存的模块列表。
LoKrModel 的配置类。
LoKrModel
class peft.LoKrModel
< source >( model config adapter_name low_cpu_mem_usage: bool = False ) → torch.nn.Module
参数
- model (
torch.nn.Module
) — 适配器调谐器层将附加到的模型。 - config (LoKrConfig) — LoKr 模型的配置。
- adapter_name (
str
) — 适配器的名称,默认为"default"
。 - low_cpu_mem_usage (
bool
,optional
, 默认为False
) — 在 meta 设备上创建空适配器权重。有助于加速加载过程。
返回
torch.nn.Module
LoKr 模型。
从预训练模型创建低秩 Kronecker 积模型。原始方法部分描述于 https://arxiv.org/abs/2108.06098 和 https://arxiv.org/abs/2309.14859。当前实现大量借鉴了 https://github.com/KohakuBlueleaf/LyCORIS/blob/eb460098187f752a5d66406d3affade6f0a07ece/lycoris/modules/lokr.py
示例
>>> from diffusers import StableDiffusionPipeline
>>> from peft import LoKrModel, LoKrConfig
>>> config_te = LoKrConfig(
... r=8,
... lora_alpha=32,
... target_modules=["k_proj", "q_proj", "v_proj", "out_proj", "fc1", "fc2"],
... rank_dropout=0.0,
... module_dropout=0.0,
... init_weights=True,
... )
>>> config_unet = LoKrConfig(
... r=8,
... lora_alpha=32,
... target_modules=[
... "proj_in",
... "proj_out",
... "to_k",
... "to_q",
... "to_v",
... "to_out.0",
... "ff.net.0.proj",
... "ff.net.2",
... ],
... rank_dropout=0.0,
... module_dropout=0.0,
... init_weights=True,
... use_effective_conv2d=True,
... )
>>> model = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
>>> model.text_encoder = LoKrModel(model.text_encoder, config_te, "default")
>>> model.unet = LoKrModel(model.unet, config_unet, "default")
属性:
- model (
~torch.nn.Module
) — 要适配的模型。 - peft_config (LoKrConfig): LoKr 模型的配置。