Skip to content

Commit

Permalink
feat: allow loading weights from local (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
violenil authored Oct 7, 2024
1 parent a79df8b commit d196d82
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion chunked_pooling/wrappers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import List, Optional, Union

import torch
Expand Down Expand Up @@ -138,7 +139,7 @@ def wrapper(self, *args, **kwargs):
return wrapper


def load_model(model_name, **model_kwargs):
def load_model(model_name, model_weights=None, **model_kwargs):
if model_name in MODEL_WRAPPERS:
model = MODEL_WRAPPERS[model_name](model_name, **model_kwargs)
if hasattr(MODEL_WRAPPERS[model_name], 'has_instructions'):
Expand All @@ -149,6 +150,9 @@ def load_model(model_name, **model_kwargs):
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
has_instructions = False

if model_weights and os.path.exists(model_weights):
model._model.load_state_dict(torch.load(model_weights, device=model.device))

# encode functions of various models do not support all sentence transformers kwargs parameter
if model_name in MODELS_WITHOUT_PROMPT_NAME_ARG:
ENCODE_FUNC_NAMES = ['encode', 'encode_queries', 'encode_corpus']
Expand Down
8 changes: 7 additions & 1 deletion run_chunked_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
default='jinaai/jina-embeddings-v2-small-en',
help='The name of the model to use.',
)
@click.option(
'--model-weights',
default=None,
help='The path to the model weights to use, e.g. in case of finetuning.',
)
@click.option(
'--strategy',
default=DEFAULT_CHUNKING_STRATEGY,
Expand Down Expand Up @@ -70,6 +75,7 @@
)
def main(
model_name,
model_weights,
strategy,
task_name,
eval_split,
Expand All @@ -91,7 +97,7 @@ def main(
f'Truncation is disabled because Long Late Chunking algorithm is enabled.'
)

model, has_instructions = load_model(model_name)
model, has_instructions = load_model(model_name, model_weights)

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)

Expand Down

0 comments on commit d196d82

Please sign in to comment.