Can you write code to onnx this? https://huggingface.co/prdev/mini-gte
- User
Can you write code to onnx this? https://huggingface.co/prdev/mini-gte
- Assistant
Below is a minimal, end-to-end example that
- pulls prdev/mini-gte from the Hub
- exports it to ONNX (op-set 17, dynamic batch & sequence lengths)
- sanity-checks the exported model with ONNX Runtime.
The same snippet works on CPU or GPU.
Feel free to copy-paste it into a ‑-> python export_mini_gte_to_onnx.py file and run.# 0. Dependencies (≈ 1 min) pip install -U "transformers>=4.36" "optimum[onnxruntime]>=1.16" \ onnxruntime sentence-transformers# export_mini_gte_to_onnx.py from pathlib import Path import numpy as np import onnxruntime as ort from transformers import AutoTokenizer from optimum.onnxruntime import ORTModelForFeatureExtraction MODEL_ID = "prdev/mini-gte" # HF repo ONNX_DIR = Path("mini_gte_onnx") # where to save exported model MAX_LEN = 256 # pick whatever sequence length you usually use OPSET = 17 print("1) Downloading + exporting to ONNX …") tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) ort_model = ORTModelForFeatureExtraction.from_pretrained( MODEL_ID, export=True, # <- triggers the PyTorch → ONNX graph conversion opset=OPSET, provider="CPUExecutionProvider", # change to "CUDAExecutionProvider" if you want ) ort_model.save_pretrained(ONNX_DIR) tokenizer.save_pretrained(ONNX_DIR) print(f"✅ Saved ONNX model to {ONNX_DIR.resolve()}\n") print("2) Quick round-trip test …") sess = ort.InferenceSession(str((ONNX_DIR / "model.onnx").resolve()), providers=["CPUExecutionProvider"]) text = "The quick brown fox jumps over the lazy dog." encoded = tokenizer(text, return_tensors="np", padding="max_length", truncation=True, max_length=MAX_LEN) # ONNX Runtime expects pure numpy, so we pass the dict directly outputs = sess.run(None, { "input_ids": encoded["input_ids"], "attention_mask": encoded["attention_mask"] }) # the model returns a single tensor: last_hidden_state last_hidden_state = outputs[0] # (batch, seq_len, hidden_size) cls_embedding = last_hidden_state[:, 0] # often used as sentence repr print("last_hidden_state:", last_hidden_state.shape) print("CLS embedding :", cls_embedding.shape)Typical console output
1) Downloading + exporting to ONNX … … lots of export logs … ✅ Saved ONNX model to ~/mini_gte_onnx 2) Quick round-trip test … last_hidden_state: (1, 256, 384) CLS embedding : (1, 384)That’s it ! You now have an ONNX version of
prdev/mini-gtesitting inmini_gte_onnx/with:• model.onnx – the graph
• config.json / tokenizer.* – original HF artefactsYou can drop those files into any ONNX Runtime, Triton, or NVIDIA TensorRT pipeline.
If you want an even smaller model, passquantize=Trueinfrom_pretrained(…, export=True, quantize=True)(weight-only 8-bit QDQ quantization).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.