To convert the prdev/mini-gte model from Hugging Face to ONNX, you can use the transformers and torch libraries in Python. Below is a step-by-step guide and code snippet to achieve this conversion. Ensure you have the necessary libraries installed before proceeding. You can install them using pip if you haven't already:
pip install transformers torch
Here's a Python script that downloads the prdev/mini-gte model and converts it to ONNX format:
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
import torch.onnx as onnx
# Model and tokenizer loading
model_name = "prdev/mini-gte"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Dummy input for model conversion
dummy_input = "This is a dummy input for model conversion"
inputs = tokenizer(dummy_input, return_tensors="pt")
# ONNX conversion
onnx.export(
model,
args=inputs,
f=model_name + ".onnx",
opset_version=12, # You can adjust the opset version based on your needs
input_names=["input_ids", "attention_mask"],
output_names=["output"],
dynamic_axes={
"input_ids": {0: "batch_size", 1: "sequence_length"},
"attention_mask": {0: "batch_size", 1: "sequence_length"},
"output": {0: "batch_size"}
}
)
print(f"Model {model_name} has been successfully converted to ONNX and saved as {model_name}.onnx")
This script performs the following steps:
- Loads the
prdev/mini-gte model and its corresponding tokenizer.
- Creates a dummy input to be used for the model conversion process.
- Converts the model to ONNX format using
torch.onnx.export, specifying the dummy input, output file name, and other necessary parameters for the conversion.
- Saves the converted model as an ONNX file named after the original model.
Please note, the opset_version and dynamic_axes parameters might need adjustments based on your specific requirements or the version of ONNX you are targeting. Additionally, ensure that the model's architecture and the conversion process are compatible with your intended use case for the ONNX model.