{
  "source_file": "0930",
  "source_platform": "个人博客",
  "author": "Philipp Schmid",
  "title_listed": "Convert Transformers to ONNX",
  "keywords": "ONNX,Transformers,转换",
  "domain": "机器学习",
  "content_type": "教程",
  "is_paid_flag": "否",
  "url": "https://www.philschmid.de/convert-transformers-to-onnx",
  "publish_date": "2022-06",
  "corpus_stage": "推理部署",
  "notes": "Transformers转ONNX优化",
  "id": "47fb0290f3fe",
  "collected_at": "2026-10-02T14:29:18",
  "title": "Convert Transformers to ONNX with Hugging Face Optimum",
  "body_markdown": "Convert Transformers to ONNX with Hugging Face Optimum\nJune 21, 2022\n6\nminute read\nHundreds of Transformers experiments and models are uploaded to the\nHugging Face Hub\nevery single day. Machine learning engineers and students conducting those experiments use a variety of frameworks like PyTorch, TensorFlow/Keras, or others. These models are already used by thousands of companies and form the foundation of AI-powered products.\nIf you deploy Transformers models in production environments, we recommend exporting them first into a serialized format that can be loaded, optimized, and executed on specialized runtimes and hardware.\nIn this guide, you'll learn about:\nWhat is ONNX?\nWhat is Hugging Face Optimum?\nWhat Transformers architectures are supported?\nHow can I convert a Transformers model (BERT) to ONNX?\nWhat's next?\nLet's get started! 🚀\nIf you are interested in optimizing your models to run with maximum efficiency, check out the\n🤗 Optimum library\n.\n1. What is ONNX?\nThe\nONNX (Open Neural Network eXchange)\nis an open standard and format to represent machine learning models. ONNX defines a common set of operators and a common file format to represent deep learning models in a wide variety of frameworks, including PyTorch and TensorFlow.\nPseudo ONNX Graph. Visualized with\nNetron\nWhen a model is exported to the ONNX format, these operators are used to construct a computational graph (often called an\nintermediate representation\n) which represents the flow of data through the neural network.\nImportant:\nONNX Is not a Runtime ONNX is only the representation that can be used with runtimes like ONNX Runtime. You can find a list of supported accelerators\nhere\n.\n➡️\nLearn more about ONNX.\n2. What is Hugging Face Optimum?\nHugging Face Optimum\nis an open-source library and an extension of\nHugging Face Transformers\n, that provides a unified API of performance optimization tools to achieve maximum efficiency to train and run models on accelerated hardware, including toolkits for optimized performance on\nGraphcore IPU\nand\nHabana Gaudi\n.\nOptimum can be used for converting, quantization, graph optimization, accelerated training & inference with support for\ntransformers pipelines\n.\nBelow you can see a typical customer journey of how you can leverage Optimum with ONNX.\n➡️ Learn more about Optimum\n3. What Transformers architectures are supported?\nA list of all supported Transformers architectures can be found in the\nONNX section of the Transformers documentation\n. Below is an excerpt of the most commonly used architectures which can be converted to ONNX and optimized with\nHugging Face Optimum\nALBERT\nBART\nBERT\nDistilBERT\nELECTRA\nGPT Neo\nGPT-J\nGPT-2\nRoBERT\nT5\nViT\nXLM\n…\n➡️ All supported architectures\n4. How can I convert a Transformers model (BERT) to ONNX?\nThere are currently three ways to convert your Hugging Face Transformers models to ONNX. In this section, you will learn how to export\ndistilbert-base-uncased-finetuned-sst-2-english\nfor\ntext-classification\nusing all three methods going from the low-level\ntorch\nAPI to the most user-friendly high-level API of\noptimum\n. Each method will do exactly the same\nExport with\ntorch.onnx\n(low-level)\ntorch.onnx\nenables you to convert model checkpoints to an ONNX graph by the\nexport\nmethod. But you have to provide a lot of values like\ninput_names\n,\ndynamic_axes\n, etc.\nYou’ll first need to install some dependencies:\nPython\npip install transformers torch\nexporting our checkpoint with\nexport\nPython\nimport\ntorch\nfrom\ntransformers\nimport\nAutoModelForSequenceClassification, AutoTokenizer\n# load model and tokenizer\nmodel_id =\n\"distilbert-base-uncased-finetuned-sst-2-english\"\nmodel = AutoModelForSequenceClassification.\nfrom_pretrained\n(model_id)\ntokenizer = AutoTokenizer.\nfrom_pretrained\n(model_id)\ndummy_model_input =\ntokenizer\n(\n\"This is a sample\"\n,\nreturn_tensors\n=\n\"pt\"\n)\n# export\ntorch.onnx.\nexport\n(\nmodel,\ntuple(dummy_model_input.\nvalues\n()),\nf\n=\n\"torch-model.onnx\"\n,\ninput_names\n=[\n'input_ids'\n,\n'attention_mask'\n],\noutput_names\n=[\n'logits'\n],\ndynamic_axes\n={\n'input_ids'\n: {\n0\n:\n'batch_size'\n,\n1\n:\n'sequence'\n},\n'attention_mask'\n: {\n0\n:\n'batch_size'\n,\n1\n:\n'sequence'\n},\n'logits'\n: {\n0\n:\n'batch_size'\n,\n1\n:\n'sequence'\n}},\ndo_constant_folding\n=\nTrue\n,\nopset_version\n=\n13\n,\n)\nExport with\ntransformers.onnx\n(mid-level)\ntransformers.onnx\nenables you to convert model checkpoints to an ONNX graph by leveraging configuration objects. That way you don’t have to provide the complex configuration for\ndynamic_axes\netc.\nYou’ll first need to install some dependencies:\nPython\npip install transformers[onnx] torch\nExporting our checkpoint with the\ntransformers.onnx\n.\nPython\nfrom\npathlib\nimport\nPath\nimport\ntransformers\nfrom\ntransformers.onnx\nimport\nFeaturesManager\nfrom\ntransformers\nimport\nAutoConfig, AutoTokenizer, AutoModelForSequenceClassification\n# load model and tokenizer\nmodel_id =\n\"distilbert-base-uncased-finetuned-sst-2-english\"\nfeature =\n\"sequence-classification\"\nbase_model = AutoModelForSequenceClassification.\nfrom_pretrained\n(model_id)\ntokenizer = AutoTokenizer.\nfrom_pretrained\n(model_id)\n# load config\nmodel_kind, model_onnx_config = FeaturesManager.\ncheck_supported_model_or_raise\n(model,\nfeature\n=feature)\nonnx_config =\nmodel_onnx_config\n(model.config)\n# export\nonnx_inputs, onnx_outputs = transformers.onnx.\nexport\n(\npreprocessor\n=tokenizer,\nmodel\n=model,\nconfig\n=onnx_config,\nopset\n=\n13\n,\noutput\n=\nPath\n(\n\"trfs-model.onnx\"\n)\n)\nExport with Optimum (high-level)\nOptimum\nInference includes methods to convert vanilla Transformers models to ONNX using the\nORTModelForXxx\nclasses. To convert your Transformers model to ONNX you simply have to pass\nfrom_transformers=True\nto the\nfrom_pretrained()\nmethod and your model will be loaded and converted to ONNX leveraging the\ntransformers.onnx\npackage under the hood.\nYou’ll first need to install some dependencies:\nPython\npip install optimum[onnxruntime]\nExporting our checkpoint with\nORTModelForSequenceClassification\nPython\nfrom\noptimum.onnxruntime\nimport\nORTModelForSequenceClassification\nmodel = ORTModelForSequenceClassification.\nfrom_pretrained\n(\n\"distilbert-base-uncased-finetuned-sst-2-english\"\n,\nfrom_transformers\n=\nTrue\n)\nThe best part about the conversion with Optimum is that you can immediately use the\nmodel\nto run predictions or load it\ninside a pipeline.\n5. What's next?\nSince you successfully convert your Transformers model to ONNX the whole set of optimization and quantization tools is now open to use. Potential next steps can be:\nUse the onnx model for\nAccelerated Inference with Optimum and Transformers Pipelines\nApply\nstatic quantization to your model\nfor ~3x latency improvements\nUse ONNX runtime for\ntraining\nConvert your ONNX model to\nTensorRT\nto improve GPU performance\n…\nIf you are interested in optimizing your models to run with maximum efficiency, check out the\n🤗 Optimum library\n.\nThanks for reading! If you have any questions, feel free to contact me, through\nGithub\n, or on the\nforum\n. You can also connect with me on\nTwitter\nor\nLinkedIn\n.",
  "metadata": {
    "title": "Convert Transformers to ONNX with Hugging Face Optimum",
    "description": "Introduction guide about ONNX and Transformers. Learn how to convert transformers like BERT to ONNX and what you can do with it.",
    "keywords": null,
    "author": "Philipp Schmid",
    "og:title": "Convert Transformers to ONNX with Hugging Face Optimum",
    "og:description": "Introduction guide about ONNX and Transformers. Learn how to convert transformers like BERT to ONNX and what you can do with it.",
    "og:url": "https://www.philschmid.de/convert-transformers-to-onnx",
    "og:image": "https://www.philschmid.de/api/og?title=Convert+Transformers+to+ONNX+with+Hugging+Face+Optimum",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:image:alt": "Convert Transformers to ONNX with Hugging Face Optimum",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "Convert Transformers to ONNX with Hugging Face Optimum",
    "twitter:description": "Introduction guide about ONNX and Transformers. Learn how to convert transformers like BERT to ONNX and what you can do with it.",
    "twitter:image": "https://www.philschmid.de/api/og?title=Convert+Transformers+to+ONNX+with+Hugging+Face+Optimum",
    "article:published_time": "2022-06-21"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/47fb0290f3fe/code/body-000.txt",
      "chars": 30,
      "language": "text"
    },
    {
      "file": "attachments/47fb0290f3fe/code/body-001.txt",
      "chars": 805,
      "language": "text"
    },
    {
      "file": "attachments/47fb0290f3fe/code/body-002.txt",
      "chars": 36,
      "language": "text"
    },
    {
      "file": "attachments/47fb0290f3fe/code/body-003.txt",
      "chars": 799,
      "language": "text"
    },
    {
      "file": "attachments/47fb0290f3fe/code/body-004.txt",
      "chars": 32,
      "language": "text"
    },
    {
      "file": "attachments/47fb0290f3fe/code/body-005.txt",
      "chars": 199,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://www.philschmid.de/static/blog/convert-transformers-to-onnx/graph.png",
      "bytes": 16954,
      "reused": false,
      "local_path": "attachments/47fb0290f3fe/images/body-4e929a20b5cf79f865cf.png",
      "src": "https://www.philschmid.de/static/blog/convert-transformers-to-onnx/graph.png",
      "alt": "graph",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://www.philschmid.de/static/blog/convert-transformers-to-onnx/user-journey.png",
      "bytes": 20948,
      "reused": false,
      "local_path": "attachments/47fb0290f3fe/images/body-3b7b8c631b3f4cf8af9f.png",
      "src": "https://www.philschmid.de/static/blog/convert-transformers-to-onnx/user-journey.png",
      "alt": "user-journey.png",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://huggingface.co/",
      "label": "Hugging Face Hub",
      "type": "a"
    },
    {
      "href": "https://github.com/huggingface/optimum",
      "label": "🤗 Optimum library",
      "type": "a"
    },
    {
      "href": "http://onnx.ai/",
      "label": "ONNX (Open Neural Network eXchange)",
      "type": "a"
    },
    {
      "href": "https://github.com/lutzroeder/Netron",
      "label": "Netron",
      "type": "a"
    },
    {
      "href": "https://onnx.ai/supported-tools.html",
      "label": "here",
      "type": "a"
    },
    {
      "href": "https://onnx.ai/about.html",
      "label": "Learn more about ONNX.",
      "type": "a"
    },
    {
      "href": "https://github.com/huggingface/transformers",
      "label": "Hugging Face Transformers",
      "type": "a"
    },
    {
      "href": "https://github.com/huggingface/optimum-graphcore",
      "label": "Graphcore IPU",
      "type": "a"
    },
    {
      "href": "https://github.com/huggingface/optimum-habana",
      "label": "Habana Gaudi",
      "type": "a"
    },
    {
      "href": "https://huggingface.co/docs/transformers/main/en/main_classes/pipelines",
      "label": "transformers pipelines",
      "type": "a"
    },
    {
      "href": "https://huggingface.co/blog/hardware-partners-program",
      "label": "➡️ Learn more about Optimum",
      "type": "a"
    },
    {
      "href": "https://huggingface.co/docs/transformers/serialization",
      "label": "ONNX section of the Transformers documentation",
      "type": "a"
    },
    {
      "href": "https://huggingface.co/docs/optimum/index",
      "label": "Hugging Face Optimum",
      "type": "a"
    },
    {
      "href": "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english",
      "label": "distilbert-base-uncased-finetuned-sst-2-english",
      "type": "a"
    },
    {
      "href": "https://pytorch.org/docs/stable/onnx.html",
      "label": "torch.onnx",
      "type": "a"
    },
    {
      "href": "https://huggingface.co/docs/optimum/onnxruntime/modeling_ort",
      "label": "Optimum",
      "type": "a"
    },
    {
      "href": "https://huggingface.co/blog/optimum-inference",
      "label": "Accelerated Inference with Optimum and Transformers Pipelines",
      "type": "a"
    },
    {
      "href": "https://www.philschmid.de/static-quantization-optimum",
      "label": "static quantization to your model",
      "type": "a"
    },
    {
      "href": "https://github.com/huggingface/optimum/tree/main/examples/onnxruntime/training",
      "label": "training",
      "type": "a"
    },
    {
      "href": "https://docs.nvidia.com/deeplearning/tensorrt/api/python_api/",
      "label": "TensorRT",
      "type": "a"
    },
    {
      "href": "https://discuss.huggingface.co/c/optimum/59",
      "label": "forum",
      "type": "a"
    },
    {
      "href": "https://twitter.com/_philschmid",
      "label": "Twitter",
      "type": "a"
    },
    {
      "href": "https://www.linkedin.com/in/philipp-schmid-a6a2bb196/",
      "label": "LinkedIn",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:52:53Z",
  "source_url": "https://www.philschmid.de/convert-transformers-to-onnx",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 7010,
  "expected_body_images": 2,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/47fb0290f3fe/pages/source.html",
  "local_html": "attachments/47fb0290f3fe/pages/article.html",
  "body_base_path": "attachments/47fb0290f3fe/pages/",
  "body_markdown_path": "attachments/47fb0290f3fe/pages/article.md"
}