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

performance: Optimize ColBERT index free search with torch.topk #219

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Diegi97
Copy link
Contributor

@Diegi97 Diegi97 commented Jun 4, 2024

The following line is a bottleneck when using the index free search:

sorted_scores = sorted(enumerate(scores), key=lambda x: x[1], reverse=True)

The change I introduce reduces the search time over 25k documents from 5.571s to 0.023s in my local setup with an intel i7 and a RTX 4090.

Script to reproduce:

from ragatouille import RAGPretrainedModel
from datasets import load_dataset

# Load the pretrained model
r = RAGPretrainedModel.from_pretrained('colbert-ir/colbertv2.0')

# Load the dataset
dataset = load_dataset('mteb/scidocs', 'corpus')
docs = dataset['corpus']['text']
print(f"Number of documents: {len(docs)}")

# Encode the documents
encodings = r.encode(docs, bsize=256)

# Perform search on encoded documents
import timeit

def search():
    return r.search_encoded_docs('Recurrent Neural Networks', k=5)

# Timing the searches
rnn_time = timeit.timeit(search, number=7)

print(f"Search 'Recurrent Neural Networks': {rnn_time / 7:.3f} s per loop")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant