Skip to content

Commit

Permalink
Adding logic for model not handling vocabulary resizing (as jina-colb…
Browse files Browse the repository at this point in the history
…ert-v2)
  • Loading branch information
NohTow committed Sep 12, 2024
1 parent bd02ff9 commit 730c6a5
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pylate/models/colbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,15 @@ def __init__(
self.to(device)
self.is_hpu_graph_enabled = False

self.tokenizer.add_tokens([self.query_prefix, self.document_prefix])
self._first_module().auto_model.resize_token_embeddings(len(self.tokenizer))
# Try adding the prefixes to the tokenizer. We call resize_token_embeddings twice to ensure the tokens are added only if resize_token_embeddings works. There should be a better way to do this.
try:
self._first_module().auto_model.resize_token_embeddings(len(self.tokenizer))
self.tokenizer.add_tokens([self.query_prefix, self.document_prefix])
self._first_module().auto_model.resize_token_embeddings(len(self.tokenizer))
except NotImplementedError:
logger.warning(
"The tokenizer does not support resizing the token embeddings, the prefixes token have not been added to vocabulary."
)

self.document_prefix_id = self.tokenizer.convert_tokens_to_ids(
self.document_prefix
Expand Down

0 comments on commit 730c6a5

Please sign in to comment.