ksanman / AllMiniLML6v2Sharp

net standard 2.1 Library for running Sentence Transformers All-MiniLM-L6-v2 from C#.
MIT License
12 stars 6 forks source link

how to recreate the model.onnx file #1

Closed tranlm closed 6 months ago

tranlm commented 6 months ago

Hi,

I'm trying to re-create your model.onnx file to ensure that I understand the process correctly. While I've been able to correctly set up the required args (i.e. ["input_ids", "token_type_ids", "attention_mask"]), the embeddings aren't matching. While both sets of embeddings are the same dimensions, my onnx model's embedding are noticeably different than yours. I've checked the sentence-transformers/all-MiniLM-L6-v2 repo and the last update to the weights was 1 to 2 years ago.

Would you mind sharing the code you used to generate your file? Thank you! I used the following:

import torch
from transformers import AutoTokenizer, AutoModel
from torch.onnx import export

model_name = "sentence-transformers/all-MiniLM-L6-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)

# Sample input
input_text = "Convert this sentence to ONNX."
inputs = tokenizer(input_text, return_tensors="pt")

onnx_path = "all-MiniLM-L6-v2.onnx"
args = (inputs["input_ids"], inputs["token_type_ids"], inputs["attention_mask"])
input_names = ["input_ids", "token_type_ids", "attention_mask"]
export(model, args, onnx_path, opset_version=12, input_names=input_names)
tranlm commented 6 months ago

Answering my own question here: It looks like I just needed to reverse the indices of the token_type_ids and attention_mask.