This PR adds a new, slightly modified FeatureExtractionPipeline from Transformers that allows us to use it with sentence-transformers models. When using the pipeline object from optimum,, the library checks if the requested model for feature-extraction is a sentence-transformers model and if so, it would return the sentence_embeddings instead of the first hidden state.
Thats is done by adding a new is_sentence_transformer_model that checks if the requested model is a transformers or sentence-transformers model. If it is a sentence-transformers model, it uses NeuronModelForSentenceTransformers and the FeatureExtractionPipeline returns _model_outputs.sentence_embedding[0] instead of model_outputs[0]
sentence-transformers models will now always return the sentence_embeddings when initialized with the FeatureExtractionPipeline pipeline.
Alternatives options:
Instead of modifying the feature-extraction pipeline, we could also introduce a new task sentence-embeddings to optimum, but that might hinder more general adoption since it is unique to optimum-neuron.
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
What does this PR do?
This PR adds a new, slightly modified
FeatureExtractionPipeline
from Transformers that allows us to use it withsentence-transformers
models. When using thepipeline
object fromoptimum,,
the library checks if the requested model forfeature-extraction
is asentence-transformers
model and if so, it would return thesentence_embeddings
instead of the first hidden state.Thats is done by adding a new
is_sentence_transformer_model
that checks if the requested model is atransformers
orsentence-transformers
model. If it is asentence-transformers
model, it usesNeuronModelForSentenceTransformers
and theFeatureExtractionPipeline
returns_model_outputs.sentence_embedding[0]
instead ofmodel_outputs[0]
Example:
Validated with
torch.allclose
Implications:
sentence-transformers
models will now always return thesentence_embeddings
when initialized with theFeatureExtractionPipeline
pipeline.Alternatives options:
feature-extraction
pipeline, we could also introduce a new tasksentence-embeddings
to optimum, but that might hinder more general adoption since it is unique to optimum-neuron.