speechbrain.nnet.adapters 模块
SpeechBrain 实现的各种预训练模型适配器,例如 LoRA, Houlsby
- 作者
Titouan Parcollet 2024
Peter Plantinga 2024
摘要
类
给定任何 torch 模型(例如 asr_brain.modules.Transformer)和一个适配器类(例如 HoulsbyAdapter),此类将用这个新的适配器类替换目标层(同时保留参数)。 |
|
此类实现了 Houlsby 适配器,如论文“Parameter-Efficient Transfer Learning for NLP” https://arxiv.org/abs/1902.00751 中所述。 |
|
此类实现了 LoRA 适配器,如论文“LoRA: Low-Rank Adaptation of Large Language Models” https://arxiv.org/abs/2106.09685 中所述。 |
函数
检查层是否在要适配的层列表中。 |
|
根据父级分配用新模块替换层。 |
参考
- class speechbrain.nnet.adapters.AdaptedModel(model_to_adapt: Module, adapter_class: Module, all_linear: bool = False, all_conv: bool = False, target_layers: list = [], unfrozen_layers: list = [], adapter_kwargs: dict = {}, manual_adapter_insertion: bool = False)[source]
基础类:
Module
给定任何 torch 模型(例如 asr_brain.modules.Transformer)和一个适配器类(例如 HoulsbyAdapter),此类将用这个新的适配器类替换目标层(同时保留参数)。
- 参数:
model_to_adapt (nn.Module) – 要添加适配器的基础 PyTorch 模型。
adapter_class (class) – 此 SpeechBrain 库的(未初始化)适配器。
all_linear (bool) – 是否将适配器添加到所有线性层(默认值: False)
all_conv (bool) – 是否将适配器添加到所有卷积层(默认值: False)
target_layers (list of str) – 给定模型中应被替换的模块名称列表。支持 Unix shell 风格的通配符
(*, ?, [seq], [!seq])
,使用fnmatch
。unfrozen_layers (list of str) – 训练期间要解冻的层列表。支持 Unix shell 风格的通配符
(*, ?, [seq], [!seq])
,使用fnmatch
。adapter_kwargs (dict) – 应传递给适配器的参数集合。
manual_adapter_insertion (bool) – 默认值 (
False
) 会在初始化时插入适配器。然而,在某些情况下,最好等待参数加载后再插入适配器,例如当需要加载预训练参数时。在这种情况下,可以将此设置为True
并在参数加载后手动调用insert_adapters
。
示例
>>> from collections import OrderedDict >>> model = torch.nn.Sequential( ... OrderedDict([ ... ("layer1", torch.nn.Linear(10, 20)), ... ("layer2", torch.nn.Linear(20, 20)), ... ("layer3", torch.nn.Linear(20, 10)), ... ]) ... ) >>> lora_model = AdaptedModel( ... model_to_adapt=model, ... adapter_class=LoRA, ... target_layers=["layer[13]"], ... unfrozen_layers=["layer2"], ... adapter_kwargs={"rank": 2}, ... ) >>> lora_model AdaptedModel( (adapted_model): Sequential( (layer1): LoRA( (pretrained_module): Linear(in_features=10, out_features=20, bias=True) (adapter_down_proj): Linear(in_features=10, out_features=2, bias=False) (adapter_up_proj): Linear(in_features=2, out_features=20, bias=False) ) (layer2): Linear(in_features=20, out_features=20, bias=True) (layer3): LoRA( (pretrained_module): Linear(in_features=20, out_features=10, bias=True) (adapter_down_proj): Linear(in_features=20, out_features=2, bias=False) (adapter_up_proj): Linear(in_features=2, out_features=10, bias=False) ) ) )
- speechbrain.nnet.adapters.is_layer_adaptable(name, module, all_linear, all_conv, target_layers)[source]
检查层是否在要适配的层列表中。
- speechbrain.nnet.adapters.replace_module(model: Module, name: str, new_module: Module)[source]
根据父级分配用新模块替换层。这用于用一个封装了原始层的适配器层替换层。因此,旧参数被保留,并添加新参数。
- 参数:
model (nn.Module) – 包含要替换模块的模型。
name (str) – 要替换的目标模块名称。
new_module (nn.Module) – 由旧参数和新参数构成的新模块。
- class speechbrain.nnet.adapters.HoulsbyAdapterLinear(target_linear, projection_size, activation=<class 'speechbrain.nnet.activations.Swish'>, bias=True)[source]
基础类:
Module
此类实现了 Houlsby 适配器,如论文“Parameter-Efficient Transfer Learning for NLP” https://arxiv.org/abs/1902.00751 中所述。
- 参数:
示例
>>> import torch >>> x = torch.rand((8, 60, 64)) >>> base_linear = nn.Linear(64, 64) >>> adapt = HoulsbyAdapterLinear(base_linear, 8) >>> output = adapt(x) >>> output.shape torch.Size([8, 60, 64])
- class speechbrain.nnet.adapters.LoRA(target_module, rank=16, alpha=1.0)[source]
基础类:
Module
此类实现了 LoRA 适配器,如论文“LoRA: Low-Rank Adaptation of Large Language Models” https://arxiv.org/abs/2106.09685 中所述。
- 参数:
示例
>>> import torch >>> x = torch.rand((8, 60, 64)) >>> base_linear = nn.Linear(64, 64) >>> adapt = LoRA(base_linear, 64, 4) >>> output = adapt(x) >>> output.shape torch.Size([8, 60, 64])