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

Allow loading weights from local #15

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 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 @@ -18,6 +18,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 @@ -55,6 +60,7 @@
)
def main(
model_name,
model_weights,
strategy,
task_name,
eval_split,
Expand All @@ -68,7 +74,7 @@ def main(
except:
raise ValueError(f'Unknown task name: {task_name}')

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
Loading