-
Notifications
You must be signed in to change notification settings - Fork 62
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
[Inference] Add SentenceTransformers
support to pipeline
for feature-extration
#583
Conversation
@tomaarsen can you also do a review? |
SentenceTransformers
support to pipeline
for feature-extration
SentenceTransformers
support to pipeline
for feature-extration
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several pipeline tests are failing. It is unclear to me if they should be triggered or not.
|
||
return preprocess_params, {}, postprocess_params | ||
|
||
def preprocess(self, inputs, **tokenize_kwargs) -> Dict[str, GenericTensor]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My primary concern at this time is that the Sentence Transformer tokenizer uses this max_seq_length
as the "correct" maximum length as opposed to the value defined in the tokenizer_config.json
.
Here, we are relying on the tokenizer defined in the Pipeline
, which won't use the max_seq_length. As a result, I think this ST pipeline component will perform differently (worse, to be precise) for longer input texts. A solution is to use model_inputs = self.model.tokenize(inputs)
instead.
Do note that the ST tokenize method does not allow for extra tokenize kwargs such as truncation, return_tensors, or padding. These are unfortunately hardcoded at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yet another solution is to rely exclusively on self.model.encode(...)
in def _forward
, but I recognize that this might clash with some requirements of the Pipeline
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomaarsen for inferentia models need to be traced to a sequence length before running inference since we have static shapes. You always need to specify a sequence_length
and batch_size
before you can compile a model which is then used.
This abstracted away by the user in the NeuronModelForSentenceTransformers class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no "sentence-transformers" used at all at the end. since the model is traced and it happens with transformers. We should be good here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See here:
optimum-neuron/optimum/neuron/modeling.py
Line 201 in 18460aa
class NeuronModelForSentenceTransformers(NeuronBaseModel): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, I see! Thanks for the heads up. I figured it was more like Intel Gaudi, which does just work with regular Sentence Transformers (as long as the padding is "max_length" to also get static shapes & the device is "hpu").
Then my concern still stands: I think the max_seq_length
might not be taken into account correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sentence_bert_config.json
is not taken into consideration during the export, and maybe we should. @tomaarsen where can we usually find the max_seq_length
? Is there a specific name / path that it's stored, if so could we add it to config.json
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi, max_seq_length
is not taken into account at all by the export of Neuron model. Shall we prevent users from setting static seq len higher than this value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Sentence Transformer models, max_seq_length
should have priority. In 99% of cases, this is stored in the sentence_bert_config.json
file in the root of the model directory/repository. You might indeed want to store it in a config.json
when exporting for Neuron, or override model_max_length
in tokenizer_config.json
as that should work in a more "expected" fashion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disclaimer: I don't know sentence transformers, so this review is on the general outlook of the code and test, which looks good to me. @JingyaHuang and @tomaarsen reviews will be more relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @philschmid, left some small nits.
|
||
return preprocess_params, {}, postprocess_params | ||
|
||
def preprocess(self, inputs, **tokenize_kwargs) -> Dict[str, GenericTensor]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sentence_bert_config.json
is not taken into consideration during the export, and maybe we should. @tomaarsen where can we usually find the max_seq_length
? Is there a specific name / path that it's stored, if so could we add it to config.json
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @philschmid for adding it!
And thanks @tomaarsen for raising the concern of max_seq_length
, it's on the backlog, will improve it in a coming PR.
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.