sentence bert model in onnx format
See original GitHub issueI would like to convert sentence bert model from pytorch to tensorflow use onnx, and tried to follow the standard onnx procedure for converting a pytorch model. But I’m having difficulty determining the onnx input arguments for sentence bert model, I encounter TypeError: forward() takes 2 positional arguments but 4 were given.
Suggestions appreciated!
model = SentenceTransformer('output/continue_training_model')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
dummy_input0 = torch.LongTensor(batch_size, max_seq_length).to(device)
dummy_input1 = torch.LongTensor(batch_size, max_seq_length).to(device)
dummy_input2 = torch.LongTensor(batch_size, max_seq_length).to(device)
torch.onnx.export(model,(dummy_input0, dummy_input1,dummy_input2), onnx_file_name, verbose=True)
Issue Analytics
- State:
- Created 4 years ago
- Comments:25 (5 by maintainers)
Hi,
I had some trouble converting the
sentence-transformers/all-mpnet-base-v2
model with onnx format so I’ll share with you a class and a function that I have made with @yuanzhoulvpi2017 tutorial (it was helpful, thank you).I’ve done some tests and I tend to measure a 4x speedup using onnx format. I’m not sure my code is fully optimised.
The
sentence_transformers_onx
function returns a model with a method encode that behave like SentenceTransformers models.Raphaël
Hi @ycgui I started to add the models to HuggingFace Models Hub: https://huggingface.co/sentence-transformers
Huggingface also provides methods / scripts to convert models to ONNX.
I hope this helps.