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

Support for SentenceTransformers with deepsparse.sentence_transformers.SentenceTransformer #1301

Merged
merged 13 commits into from
Oct 19, 2023

Conversation

mgoin
Copy link
Member

@mgoin mgoin commented Oct 6, 2023

Direct replacement for SentenceTransformer implemented using DeepSparse and optimum-deepsparse

Performance optimizations with bucketing/batching will come in future work

Smoke Test:

pip install -e ".[sentence_transformers]"
# from sentence_transformers import SentenceTransformer
from deepsparse.sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", export=True)

# Sentences we'd like to encode
sentences = ['This framework generates embeddings for each input sentence',
    'Sentences are passed as a list of string.',
    'The quick brown fox jumps over the lazy dog.']

# Sentences are encoded by calling model.encode()
embeddings = model.encode(sentences)

# Print the embeddings
for sentence, embedding in zip(sentences, embeddings):
    print("Sentence:", sentence)
    print("Embedding:", embedding.shape)
    print("")

### Sentence: This framework generates embeddings for each input sentence
### Embedding: (384,)
### 
### Sentence: Sentences are passed as a list of string.
### Embedding: (384,)
### 
### Sentence: The quick brown fox jumps over the lazy dog.
### Embedding: (384,)

Full test through evaluation with MTEB

pip install mteb deepsparse-nightly[sentence_transformers] sentence-transformers
from mteb import MTEB

model_name = "TaylorAI/bge-micro-v2"

from deepsparse.sentence_transformers import SentenceTransformer
model = SentenceTransformer(model_name, export=True)
evaluation = MTEB(tasks=["Banking77Classification"])
results_ds = evaluation.run(model, output_folder=f"results/ds-{model_name}")
print(results_ds)

import sentence_transformers
model = sentence_transformers.SentenceTransformer(model_name)
evaluation = MTEB(tasks=["Banking77Classification"])
results_st = evaluation.run(model, output_folder=f"results/st-{model_name}")
print(results_st)

Output:

{'Banking77Classification': {'mteb_version': '1.1.1', 'dataset_revision': '0fd18e25b25c072e09e0d92ab615fda904d66300', 'mteb_dataset_name': 'Banking77Classification', 'test': {'accuracy': 0.8117207792207791, 'f1': 0.8109893836310513, 'accuracy_stderr': 0.007164150669501205, 'f1_stderr': 0.007346045502756079, 'main_score': 0.8117207792207791, 'evaluation_time': 26.05}}}

{'Banking77Classification': {'mteb_version': '1.1.1', 'dataset_revision': '0fd18e25b25c072e09e0d92ab615fda904d66300', 'mteb_dataset_name': 'Banking77Classification', 'test': {'accuracy': 0.8117207792207791, 'f1': 0.8109893836310513, 'accuracy_stderr': 0.007164150669501205, 'f1_stderr': 0.007346045502756079, 'main_score': 0.8117207792207791, 'evaluation_time': 12.21}}}

@mgoin mgoin marked this pull request as ready for review October 13, 2023 15:14
Copy link
Member

@bfineran bfineran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall - assuming a readme will come separately. will fit in nicely with the pipelines refactor - should be able to swap in optimum.deepsparse the way we swap in any other engine to our pipelines

bfineran
bfineran previously approved these changes Oct 16, 2023
Copy link
Member

@bfineran bfineran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending coment

setup.py Outdated Show resolved Hide resolved
@mgoin mgoin merged commit 869af57 into main Oct 19, 2023
12 of 13 checks passed
@mgoin mgoin deleted the sentence-transformer-support branch October 19, 2023 16:37
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

3 participants