Open batman-do opened 1 year ago
I have no experience with ONNX myself, so I can't really comment on what could cause this. However, perhaps @nbertagnolli has some clue of what this could be? That is, if he has some time :)
Happy to take a look : ). @batman-do could you send over a small working example and I'll poke around and see what I can find?
I use setfit's version == 0.6.0 and use export_onnx in the repo, I export onnx following examples work with checkpoints sentence-transformers/paraphrase-albert-small-v2
but when using my checkpoint with body roberta above or sentence-transformers/paraphrase-multilingual-mpnet-base-v2
don't work @nbertagnolli
I was running into the same error with a roberta-based model body. After some debugging I noticed the following:
In setfit/exporters/onnx.py(51) forward()
we have a token_type_ids
tensor that looks like this:
tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
And a attention_mask
tensor that looks like this:
tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In my opinion, these tensors look as if they were switched. The error then occurs because we are trying to look up both the token_type_id embedding with index 0 and the one with index 1, but there is only one embedding in the matrix (at least for the model stsb-xlm-roberta-base
). I created a pull request but without testing the solution for any other model yet. I will do so once I have some more time. For my use case, it is now working. I made a pull request with my changes. If you have the same issue, this should hopefully get it working again 😃
I ran into this problem too with sentence-transformers/paraphrase-multilingual-mpnet-base-v2
, and can also confirm that, as @rolshoven said, the 2 tensors in setfit/exporters/onnx.py(51)
seem switched. The changes in the PR solved that issue for me, too. However, I also get a different error when using distiluse-base-multilingual-cased-v2
, namely: RuntimeError: The size of tensor a (12) must match the size of tensor b (128) at non-singleton dimension 1
.
I'm adding here a short code snippet to reproduce the issues (as inspired from one of @nbertagnolli 's replies ).
from setfit import SetFitModel
from sentence_transformers import SentenceTransformer
from setfit import SetFitHead, SetFitHead, SetFitModel
from setfit.exporters.onnx import export_onnx
# Uncomment for different scenarios
model_id = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2" # Not working, "index out of range"
# model_id = "sentence-transformers/distiluse-base-multilingual-cased-v2" # Not working, tensor sizes error
# model_id = "sentence-transformers/all-MiniLM-L6-v2" # Working
# Load pretrained transformer and create a head
model_body = SentenceTransformer(model_id)
model_head = SetFitHead(in_features = model_body.get_sentence_embedding_dimension(), out_features = 4)
model = SetFitModel(model_body = model_body, model_head = model_head)
# Export model
output_path = "setfit_onnx"
export_onnx(model.model_body,
model.model_head,
opset=12,
output_path=output_path)
Any updates on this? I'm facing the same issue but for the instructor-large model. There seems to be an error when loading the attention weights.
@lucasalvarezlacasa how configure export_onnx for gpu and cuda for: sentence-transformers/all-MiniLM-L6-v2
for model head and model body?
help me fix issue @tomaarsen , thanks u guys