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

Updated IPEX embedder to work with new Haystack version (2.7) #74

Merged
merged 2 commits into from
Nov 25, 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
45 changes: 31 additions & 14 deletions fastrag/embedders/ipex_embedder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union

import torch
from haystack.components.embedders import (
SentenceTransformersDocumentEmbedder,
SentenceTransformersTextEmbedder,
Expand Down Expand Up @@ -33,7 +34,7 @@ def __init__(
import sentence_transformers

class _IPEXSTTransformers(sentence_transformers.models.Transformer):
def _load_model(self, model_name_or_path, config, cache_dir, **model_args):
def _load_model(self, model_name_or_path, config, cache_dir, backend, **model_args):
print("Loading IPEX ST Transformer model")
optimized_intel_import.check()
self.auto_model = IPEXModel.from_pretrained(
Expand Down Expand Up @@ -89,23 +90,39 @@ def _load_auto_model(
cache_folder: Optional[str],
revision: Optional[str] = None,
trust_remote_code: bool = False,
local_files_only: bool = False,
model_kwargs: Optional[Dict[str, Any]] = None,
tokenizer_kwargs: Optional[Dict[str, Any]] = None,
config_kwargs: Optional[Dict[str, Any]] = None,
):
"""
Creates a simple Transformer + Mean Pooling model and returns the modules
"""

shared_kwargs = {
"token": token,
"trust_remote_code": trust_remote_code,
"revision": revision,
"local_files_only": local_files_only,
}
model_kwargs = (
shared_kwargs if model_kwargs is None else {**shared_kwargs, **model_kwargs}
)
tokenizer_kwargs = (
shared_kwargs
if tokenizer_kwargs is None
else {**shared_kwargs, **tokenizer_kwargs}
)
config_kwargs = (
shared_kwargs if config_kwargs is None else {**shared_kwargs, **config_kwargs}
)

transformer_model = _IPEXSTTransformers(
model_name_or_path,
cache_dir=cache_folder,
model_args={
"token": token,
"trust_remote_code": trust_remote_code,
"revision": revision,
},
tokenizer_args={
"token": token,
"trust_remote_code": trust_remote_code,
"revision": revision,
},
model_args=model_kwargs,
tokenizer_args=tokenizer_kwargs,
config_args=config_kwargs,
)
pooling_model = sentence_transformers.models.Pooling(
transformer_model.get_word_embedding_dimension(), "mean"
Expand All @@ -114,7 +131,7 @@ def _load_auto_model(

@property
def device(self):
return "cpu"
return torch.device("cpu")

self.model = _IPEXSentenceTransformer(
model_name_or_path=model,
Expand All @@ -132,7 +149,7 @@ def ipex_model_warm_up(self):
"""
Initializes the component.
"""
if not hasattr(self, "embedding_backend"):
if not getattr(self, "embedding_backend", None):
self.embedding_backend = _IPEXSentenceTransformersEmbeddingBackend(
model=self.model,
device=self.device.to_torch_str(),
Expand Down
2 changes: 1 addition & 1 deletion fastrag/rankers/bi_encoder_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def run(
scores = torch.tensor(query_vector) @ doc_vectors.T ## perhaps need to break it into chunks
scores = scores.reshape(len(documents))
# Store scores in documents_with_vectors
for doc, score in zip(documents_with_vectors, scores.tolist()):
for doc, score in zip(documents_with_vectors, scores.tolist()):
doc.score = score

indices = scores.cpu().sort(descending=True).indices
Expand Down