---
title: "Convert Transformers to ONNX with Hugging Face Optimum"
description: "Hugging Face Transformers models can be exported to ONNX in three ways, shown here on a DistilBERT text-classification checkpoint."
url: "https://swe.quest/articles/convert-transformers-to-onnx-with-hugging-face-optimum-86f6804aa2e19f43"
canonical: "https://huggingface.co/blog/convert-transformers-to-onnx"
source: "Hugging Face"
published: "2022-06-22"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["inference","machine-learning","python"]
---

# Convert Transformers to ONNX with Hugging Face Optimum

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-06-22, 5 min read, How-to. Original: [https://huggingface.co/blog/convert-transformers-to-onnx](https://huggingface.co/blog/convert-transformers-to-onnx)

## Summary

Hugging Face Transformers models can be exported to ONNX in three ways, shown here on a DistilBERT text-classification checkpoint. The low-level torch.onnx.export requires manually specifying input/output names and dynamic axes. transformers.onnx derives these from per-architecture config objects. Optimum's ORTModelForXxx classes convert in one call with from\_transformers=True and return a model usable directly in pipelines. Once a model is in ONNX it can go on to ONNX Runtime acceleration, static quantization (~3x latency gains claimed) or TensorRT conversion.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- PyTorch basics
- Hugging Face Transformers

## Keywords

ONNX, Hugging Face Optimum, ONNX Runtime, Transformers, DistilBERT, torch.onnx

## Related posts

- [Accelerated Inference with Optimum and Transformers Pipelines](https://swe.quest/articles/accelerated-inference-with-optimum-and-transformers-pipelines-9ff6245f88a35ca0.md): Hugging Face · 2022-05-10 · Medium · 12 min read · Inference & serving, Python, Performance engineering, Machine learning
  Optimum 1.2 adds inference support via ONNX Runtime with ORTModelForXxx classes that drop into Hugging Face Transformers pipelines in place of AutoModelForXxx.
- [Optimum+ONNX Runtime - Easier, Faster training for your Hugging Face models](https://swe.quest/articles/optimum-onnx-runtime-easier-faster-training-for-your-hugging-face-models-99e01d7695af970a.md): Hugging Face · 2023-01-24 · Medium · 6 min read · Model training, Performance engineering, Python
  Hugging Face Optimum's ORTTrainer swaps ONNX Runtime in as the training backend for Transformers models.
- [Introducing Optimum: The Optimization Toolkit for Transformers at Scale](https://swe.quest/articles/introducing-optimum-the-optimization-toolkit-for-transformers-at-scale-a074b17981960e1a.md): Hugging Face · 2021-09-14 · Easy · 4 min read · Inference & serving, Machine learning, Open source, Hardware
  Optimum is a new open source Hugging Face library that targets production performance for Transformer models by wrapping hardware-specific acceleration tooling built with hardware partners.
- [Transformers.js v4: Now Available on NPM!](https://swe.quest/articles/transformers-js-v4-now-available-on-npm-60e5d316d3e108bd.md): Hugging Face · 2026-02-09 · Medium · 7 min read · Inference & serving, JavaScript, LLMs, Browsers, Build systems
  Transformers.js v4 ships on NPM with a new C++ WebGPU runtime built with the ONNX Runtime team, enabling WebGPU-accelerated models in browsers as well as Node, Bun and Deno.
- [Accelerate your models with 🤗 Optimum Intel and OpenVINO](https://swe.quest/articles/accelerate-your-models-with-optimum-intel-and-openvino-6481f69a8bf224aa.md): Hugging Face · 2022-11-02 · Medium · 5 min read · Inference & serving, Python, Hardware, Computer vision
  Optimum Intel now integrates OpenVINO, letting Transformers models run on OpenVINO Runtime across Intel processors and be quantized with the Neural Network Compression Framework (NNCF).

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
