Skip to content

Commit

Permalink
Fix output_embeddings get/ set for empty active head (#754)
Browse files Browse the repository at this point in the history
Fixes #742.
  • Loading branch information
calpt authored Oct 30, 2024
1 parent 470af8a commit 0c1039b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/adapters/heads/model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def _init_head_modules(self):
def get_output_embeddings(self) -> Union[nn.Module, List[nn.Module]]:
# Only gets the output embeddings for the currently active head
embeddings = []
if not self._active_heads:
return None
for head_name in self._active_heads:
if head_name in self.heads:
head = self.heads[head_name]
Expand All @@ -109,6 +111,8 @@ def get_output_embeddings(self) -> Union[nn.Module, List[nn.Module]]:

def set_output_embeddings(self, new_embeddings: Union[nn.Module, List[nn.Module]]):
# Only sets the output embeddings for the currently active head
if not self._active_heads:
return
if not isinstance(new_embeddings, list):
new_embeddings = [new_embeddings] * len(self._active_heads)
for head_name, emb in zip(self._active_heads, new_embeddings):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_adapter_heads.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ def test_delete_head(self):
self.assertFalse(name in model.config.prediction_heads)
self.assertNotEqual(name, model.active_head)

# add head again
self.add_head(model, name)
self.assertTrue(name in model.heads)
self.assertTrue(name in model.config.prediction_heads)
self.assertEqual(name, model.active_head)

def test_adapter_with_head(self):
model1, model2 = create_twin_models(self.model_class, self.config)

Expand Down

0 comments on commit 0c1039b

Please sign in to comment.