---
title: "Convert Transformers to ONNX with Hugging Face Optimum"
description: "Exporting Hugging Face Transformers models to the ONNX format can be accomplished through three different abstraction levels. The low-level approach utilizes torch.onnx.export, requiring manual specification of dummy inputs, input names, output names, opset versions, and dynamic axes configurations. At the intermediate level, the transformers.onnx package simplifies the conversion process by relying on FeaturesManager and prebuilt configuration objects to handle dynamic axis definitions automatically. The high-level method uses Hugging Face Optimum classes such as ORTModelForSequenceClassification by setting the from_transformers flag to True inside from_pretrained. This Optimum export leverages transformers.onnx internally and produces a model ready for immediate inference execution or integration into pipelines."
---

# Convert Transformers to ONNX with Hugging Face Optimum

huggingface.co · Philipp Schmid · Jun 22, 2022

**Type:** Tutorial

## Summary

Exporting Hugging Face Transformers models to the ONNX format can be accomplished through three different abstraction levels. The low-level approach utilizes torch.onnx.export, requiring manual specification of dummy inputs, input names, output names, opset versions, and dynamic axes configurations. At the intermediate level, the transformers.onnx package simplifies the conversion process by relying on FeaturesManager and prebuilt configuration objects to handle dynamic axis definitions automatically. The high-level method uses Hugging Face Optimum classes such as ORTModelForSequenceClassification by setting the from\_transformers flag to True inside from\_pretrained. This Optimum export leverages transformers.onnx internally and produces a model ready for immediate inference execution or integration into pipelines.

## Context

Developers need methods to export Hugging Face Transformers models, such as distilbert-base-uncased-finetuned-sst-2-english for text classification, into ONNX graphs across different levels of API abstraction.

## Approach / What changed

The post demonstrates three conversion workflows: low-level export using torch.onnx.export with explicit dynamic axes and dummy inputs, mid-level export using transformers.onnx with FeaturesManager configuration objects, and high-level export using Optimum ORTModelForSequenceClassification with from\_transformers=True.

## Takeaways

- The low-level torch.onnx.export API requires explicit configuration of dummy inputs, input\_names, output\_names, dynamic\_axes mappings, and opset\_version.
- The transformers.onnx package eliminates manual dynamic axis mapping by using FeaturesManager and configuration objects to export models.
- Optimum ORTModelForXxx classes convert models automatically via from\_transformers=True in from\_pretrained, enabling immediate prediction and pipeline usage.

**Tags:** [Machine Learning](https://yomu.fyi/topic/machine-learning), [Python](https://yomu.fyi/topic/python)

- Source: [huggingface.co](https://huggingface.co/blog/convert-transformers-to-onnx)
- Source URL: https://huggingface.co/blog/convert-transformers-to-onnx
- Ingested by Yomu: 2026-08-27T15:10:35.816Z

[Read original post](https://huggingface.co/blog/convert-transformers-to-onnx)
