Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unfreeze_adapters param to train_fusion() #156

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/transformers/adapters/model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def train_adapter(self, adapter_setup: Union[list, AdapterCompositionBlock]):
pass

@abstractmethod
def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock]):
def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock], unfreeze_adapters=False):
"""Sets the model into mode for training of adapter fusion determined by a list of adapter names."""
pass

Expand Down Expand Up @@ -470,9 +470,9 @@ def train_adapter(self, adapter_setup: Union[list, AdapterCompositionBlock]):
"""Sets the model into mode for training the given adapters."""
self.base_model.train_adapter(adapter_setup)

def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock]):
def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock], unfreeze_adapters=False):
"""Sets the model into mode for training of adapter fusion determined by a list of adapter names."""
self.base_model.train_fusion(adapter_setup)
self.base_model.train_fusion(adapter_setup, unfreeze_adapters=unfreeze_adapters)

def _add_adapter(self, adapter_name):
self.base_model._add_adapter(adapter_name)
Expand Down
6 changes: 3 additions & 3 deletions src/transformers/adapters/models/bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ def train_adapter(self, adapter_setup: Union[list, AdapterCompositionBlock]):
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)

def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock]):
def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock], unfreeze_adapters=False):
"""Sets the model into mode for training of adapter fusion determined by a list of adapter names."""
self.train()
self.freeze_model(True)
adapter_setup = parse_composition(adapter_setup)
if hasattr(self, "encoder"):
self.encoder.enable_adapters(adapter_setup, False, True)
self.decoder.enable_adapters(adapter_setup, False, True)
self.encoder.enable_adapters(adapter_setup, unfreeze_adapters, True)
self.decoder.enable_adapters(adapter_setup, unfreeze_adapters, True)
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)

Expand Down
4 changes: 2 additions & 2 deletions src/transformers/adapters/models/bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ def train_adapter(self, adapter_setup: Union[list, AdapterCompositionBlock]):
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)

def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock]):
def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock], unfreeze_adapters=False):
"""Sets the model into mode for training of adapter fusion determined by a list of adapter names."""
self.train()
self.freeze_model(True)
adapter_setup = parse_composition(adapter_setup)
self.encoder.enable_adapters(adapter_setup, False, True)
self.encoder.enable_adapters(adapter_setup, unfreeze_adapters, True)
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)
# TODO implement fusion for invertible adapters
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/adapters/models/distilbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def train_adapter(self, adapter_setup: Union[list, AdapterCompositionBlock]):
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)

def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock]):
def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock], unfreeze_adapters=False):
"""Sets the model into mode for training of adapter fusion determined by a list of adapter names."""
self.train()
self.freeze_model(True)
adapter_setup = parse_composition(adapter_setup)
self.transformer.enable_adapters(adapter_setup, False, True)
self.transformer.enable_adapters(adapter_setup, unfreeze_adapters, True)
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)

Expand Down
4 changes: 2 additions & 2 deletions src/transformers/adapters/models/gpt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ def train_adapter(self, adapter_setup: Union[list, AdapterCompositionBlock]):
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)

def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock]):
def train_fusion(self, adapter_setup: Union[list, AdapterCompositionBlock], unfreeze_adapters=False):
self.train()
self.freeze_model(True)
adapter_setup = parse_composition(adapter_setup)
self.enable_adapters(adapter_setup, False, True)
self.enable_adapters(adapter_setup, unfreeze_adapters, True)
# use the adapters to be trained by default in every forward pass
self.set_active_adapters(adapter_setup)

Expand Down