I am using Apple Mac M1:
OS: MacOS Monterey
Python 3.9.13
I want to implement a semantic search using SentenceTransformer
.
Here is my code:
from sentence_transformers import SentenceTransformer
import faiss
from pprint import pprint
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def load_index():
index = faiss.read_index("movie_plot.index")
return index
def fetch_movie_info(dataframe_idx):
info = df.iloc[dataframe_idx]
meta_dict = dict()
meta_dict['Title'] = info['Title']
meta_dict['Plot'] = info['Plot'][:500]
return meta_dict
def search(query, top_k, index, model):
print("starting search!")
t=time.time()
query_vector = model.encode([query])
top_k = index.search(query_vector, top_k)
print('>>>> Results in Total Time: {}'.format(time.time()-t))
top_k_ids = top_k[1].tolist()[0]
top_k_ids = list(np.unique(top_k_ids))
results = [fetch_movie_info(idx) for idx in top_k_ids]
return results
def main():
# GET MODEL
model = SentenceTransformer('msmarco-distilbert-base-dot-prod-v3')
print("model set!")
#GET INDEX
index = load_index()
print("index loaded!")
query="Artificial Intelligence based action movie"
results=search(query, top_k=5, index=index, model=model)
print("n")
for result in results:
print('t',result)
main()
when i run the code above it gets stuck at this error
/opt/homebrew/Caskroom/miniforge/base/envs/searchapp/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn(‘resource_tracker: There appear to be %d ‘
what is causing this and how can I fix it?
2
Answers
I had the same problem, upgrading to Python 3.9 solved it in my case.
It looks like it is an official bug: https://bugs.python.org/issue45209
Check your memory utilisation. You might be out of memory.
Since you’re using a pretrained model for inference/prediction, you can reduce the memory requirements of the model by avoiding computation of the gradients. Gradients are only used for training the model typically.
You can wrap your model call with
torch.no_grad
like so:You can also reduce the memory utilisation a model by reducing the batch size (i.e. number of samples passed to the model at one time), but that doesn’t seem to apply here.