forked from speechbrain/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BEiT model refactoring (speechbrain#9)
- Loading branch information
1 parent
b077e26
commit 6d937be
Showing
5 changed files
with
54 additions
and
1,228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,54 @@ | ||
import logging | ||
from typing import Iterable, Tuple | ||
|
||
import torch.nn as nn | ||
|
||
from ..layer import AdapterLayer | ||
from ..model_mixin import ModelAdaptersMixin, ModelWithHeadsAdaptersMixin | ||
from ..lora import Linear as LoRALinear | ||
from ..model_mixin import ModelBaseAdaptersMixin | ||
from ..prefix_tuning import PrefixTuningShim | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
class BeitSelfAttentionAdaptersMixin: | ||
def init_adapters(self, config): | ||
self.location_key = "self" | ||
|
||
# Wrap layers for LoRA | ||
self.query = LoRALinear.wrap(self.query, "selfattn", config, attn_key="q") | ||
self.key = LoRALinear.wrap(self.key, "selfattn", config, attn_key="k") | ||
self.value = LoRALinear.wrap(self.value, "selfattn", config, attn_key="v") | ||
|
||
self.prefix_tuning = PrefixTuningShim(self.location_key + "_prefix" if self.location_key else None, config) | ||
|
||
|
||
class BeitIntermediateAdaptersMixin: | ||
def init_adapters(self, config): | ||
# Wrap layers for LoRA | ||
self.dense = LoRALinear.wrap(self.dense, "intermediate", config) | ||
|
||
|
||
class BeitOutputAdaptersMixin: | ||
def init_adapters(self, config): | ||
# Wrap layers for LoRA | ||
self.dense = LoRALinear.wrap(self.dense, "output", config) | ||
|
||
|
||
class BeitLayerAdaptersMixin: | ||
"""Adds adapters to the BeitLayer module.""" | ||
|
||
def _init_adapter_modules(self): | ||
self.attention_adapters = AdapterLayer("mh_adapter", self.config) | ||
self.attention_adapters._init_adapter_modules() | ||
|
||
self.output_adapters = AdapterLayer("output_adapter", self.config) | ||
self.output_adapters._init_adapter_modules() | ||
def init_adapters(self, config): | ||
self.attention_adapters = AdapterLayer("mh_adapter") | ||
self.output_adapters = AdapterLayer("output_adapter") | ||
|
||
|
||
class BeitModelAdaptersMixin(ModelAdaptersMixin): | ||
class BeitModelAdaptersMixin(ModelBaseAdaptersMixin): | ||
"""Adds adapters to the BeitModel module.""" | ||
|
||
def init_adapters(self, config): | ||
super().init_adapters(config) | ||
|
||
def iter_layers(self) -> Iterable[Tuple[int, nn.Module]]: | ||
for i, layer in enumerate(self.encoder.layer): | ||
yield i, layer | ||
|
||
|
||
class BeitModelWithHeadsAdaptersMixin(ModelWithHeadsAdaptersMixin): | ||
pass | ||
def set_input_embeddings(self, value): | ||
self.embeddings.patch_embeddings = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.