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

fix: update auto_match #8

Merged
merged 1 commit into from
Mar 11, 2024
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ from retrievals import AutoModelForEmbedding

sentences = ["Hello world", "How are you?"]
model_name_or_path = "sentence-transformers/all-MiniLM-L6-v2"
model = AutoModelForEmbedding(model_name_or_path, pooling_method="cls")
sentence_embeddings = model.encode(sentences)
model = AutoModelForEmbedding(model_name_or_path, pooling_method="mean", normalize_embeddings=True)
sentence_embeddings = model.encode(sentences, convert_to_tensor=True)
print(sentence_embeddings)
```

Expand Down
5 changes: 3 additions & 2 deletions src/retrievals/models/embedding_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import torch
from numpy import ndarray
from peft import LoraConfig, TaskType, get_peft_model
from torch import Tensor, nn
from torch.utils.data import DataLoader, Dataset
from tqdm.autonotebook import trange
Expand Down Expand Up @@ -76,7 +75,7 @@ def __init__(
generation_args: Dict = None,
use_fp16: bool = False,
use_lora: bool = False,
peft_config: Optional[LoraConfig] = None,
peft_config=None,
device: Optional[str] = None,
trust_remote_code: bool = False,
):
Expand Down Expand Up @@ -110,6 +109,8 @@ def __init__(
self.model.half()
if use_lora:
# peft config and wrapping
from peft import LoraConfig, TaskType, get_peft_model

if not peft_config:
raise ValueError("If use_lora is true, please provide a valid peft_config")
self.model = get_peft_model(self.model, peft_config)
Expand Down
12 changes: 10 additions & 2 deletions src/retrievals/models/match_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ def __init__(self, method="cosine") -> None:
self.method = method

def similarity_search(
self, query_embed: torch.Tensor, passage_embed: torch.Tensor, top_k: int = 1, batch_size: int = 0, **kwargs
self,
query_embed: torch.Tensor,
passage_embed: torch.Tensor,
top_k: int = 1,
batch_size: int = 0,
convert_to_numpy: bool = True,
**kwargs,
):
if self.method == "knn":
neighbors_model = NearestNeighbors(n_neighbors=top_k, metric="cosine", n_jobs=-1)
Expand All @@ -25,7 +31,9 @@ def similarity_search(
return dists, indices

elif self.method == "cosine":
dists, indices = cosine_similarity_search(query_embed, passage_embed, top_k=top_k, batch_size=batch_size)
dists, indices = cosine_similarity_search(
query_embed, passage_embed, top_k=top_k, batch_size=batch_size, convert_to_numpy=convert_to_numpy
)
return dists, indices

else:
Expand Down
Loading