These Python scripts convert Hugging Face models into the ONNX format for optimized inference.
These scripts handle two primary use cases:
- Feature extraction models (e.g.,
sentence-transformers). - Token classification models (e.g., Named Entity Recognition - NER).
It automatically downloads the model and organizes the exported files in a structured subdirectory.
Before running the script, make sure you have the following Python packages installed:
pip install torch transformers onnx onnxruntime optimumpython3 models/hf_model_to_onnx.py -m="dbmdz/bert-large-cased-finetuned-conll03-english" -o="bert-large-cased-finetuned-conll03-english"python3 models/hf_model_to_onnx.py -m="sentence-transformers/all-MiniLM-L6-v2" -o="sentence-transformers/all-MiniLM-L6-v2"./models/
├── hf_extract_model.py
├── hf_model_to_onnx.py
├── sentence-transformers/all-MiniLM-L6-v2/
│ ├── model.onnx (via optimum)
│ └── tokenizer/
└── dslim/bert-base-NER/
├── model.onnx
├── label_map.json
└── tokenizer/