Close Menu
techwavedispatch.com

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Deep Q-Learning with Space Invaders

    November 15, 2026

    Director of Machine Learning Insights [Part 3: Finance Edition]

    November 15, 2026

    Convert Transformers to ONNX with Hugging Face Optimum

    November 15, 2026
    Facebook X (Twitter) Instagram
    Trending
    • Deep Q-Learning with Space Invaders
    • Director of Machine Learning Insights [Part 3: Finance Edition]
    • Convert Transformers to ONNX with Hugging Face Optimum
    • Getting Started With Embeddings
    • Accelerate Large Model Training using DeepSpeed
    • Announcing Evaluation on the Hub
    • Liftoff! How to get started with your first ML project 🚀
    • Policy Gradient with PyTorch
    Facebook X (Twitter) Instagram
    techwavedispatch.com
    Subscribe
    Sunday, November 15
    • Home
    • AI Tools & Software
    • Machine Learning
    • Generative AI
    • Robotics & Automation
    • Future Tech & Hardware
    • AI in Business
    • Ethics & Regulations
    • More
      • Gadgets & Smart Devices
      • Cybersecurity & AI
      • Tech Breakthroughs
    techwavedispatch.com
    Home»AI Tools & Software»Convert Transformers to ONNX with Hugging Face Optimum
    AI Tools & Software

    Convert Transformers to ONNX with Hugging Face Optimum

    adminBy adminNovember 15, 2026No Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Convert Transformers to ONNX with Hugging Face Optimum
    Share
    Facebook Twitter LinkedIn Pinterest Email

    There are currently three ways to convert your Hugging Face Transformers models to ONNX. In this section, you will learn how to export distilbert-base-uncased-finetuned-sst-2-english for text-classification using all three methods going from the low-level torch API to the most user-friendly high-level API of optimum. Each method will do exactly the same



    Export with torch.onnx (low-level)

    torch.onnx enables you to convert model checkpoints to an ONNX graph by the export method. But you have to provide a lot of values like input_names, dynamic_axes, etc.

    You’ll first need to install some dependencies:

    pip install transformers torch
    

    exporting our checkpoint with export

    import torch
    from transformers import AutoModelForSequenceClassification, AutoTokenizer
    
    
    model_id = "distilbert-base-uncased-finetuned-sst-2-english"
    model = AutoModelForSequenceClassification.from_pretrained(model_id)
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    dummy_model_input = tokenizer("This is a sample", return_tensors="pt")
    
    
    torch.onnx.export(
        model, 
        tuple(dummy_model_input.values()),
        f="torch-model.onnx",  
        input_names=['input_ids', 'attention_mask'], 
        output_names=['logits'], 
        dynamic_axes={'input_ids': {0: 'batch_size', 1: 'sequence'}, 
                      'attention_mask': {0: 'batch_size', 1: 'sequence'}, 
                      'logits': {0: 'batch_size', 1: 'sequence'}}, 
        do_constant_folding=True, 
        opset_version=13, 
    )
    



    Export with transformers.onnx (mid-level)

    transformers.onnx enables 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 dynamic_axes etc.

    You’ll first need to install some dependencies:

    pip install transformers[onnx] torch
    

    Exporting our checkpoint with the transformers.onnx.

    from pathlib import Path
    import transformers
    from transformers.onnx import FeaturesManager
    from transformers import AutoConfig, AutoTokenizer, AutoModelForSequenceClassification
    
    
    model_id = "distilbert-base-uncased-finetuned-sst-2-english"
    feature = "sequence-classification"
    model = AutoModelForSequenceClassification.from_pretrained(model_id)
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    
    
    model_kind, model_onnx_config = FeaturesManager.check_supported_model_or_raise(model, feature=feature)
    onnx_config = model_onnx_config(model.config)
    
    
    onnx_inputs, onnx_outputs = transformers.onnx.export(
            preprocessor=tokenizer,
            model=model,
            config=onnx_config,
            opset=13,
            output=Path("trfs-model.onnx")
    )
    



    Export with Optimum (high-level)

    Optimum Inference includes methods to convert vanilla Transformers models to ONNX using the ORTModelForXxx classes. To convert your Transformers model to ONNX you simply have to pass from_transformers=True to the from_pretrained() method and your model will be loaded and converted to ONNX leveraging the transformers.onnx package under the hood.

    You’ll first need to install some dependencies:

    pip install optimum[onnxruntime]
    

    Exporting our checkpoint with ORTModelForSequenceClassification

    from optimum.onnxruntime import ORTModelForSequenceClassification
    
    model = ORTModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english",from_transformers=True)
    

    The best part about the conversion with Optimum is that you can immediately use the model to run predictions or load it inside a pipeline.

    Convert Face Hugging ONNX Optimum Transformers
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    admin
    • Website

    Related Posts

    Deep Q-Learning with Space Invaders

    November 15, 2026

    Director of Machine Learning Insights [Part 3: Finance Edition]

    November 15, 2026

    Getting Started With Embeddings

    November 15, 2026
    Leave A Reply Cancel Reply

    Top Posts

    NVIDIA Is Investigating Reports That CEO Jensen Huang Was The Target Of Chinese Spy Efforts

    November 8, 202619 Views

    ADIndex: Layer-Adaptive Sparse Attention Indexing via Group-Level Distillation

    November 11, 20265 Views

    Anthropic releases new Mythos and Fable models amid RSI concerns

    November 9, 20265 Views

    One Policy for 100 Household Tasks: Our Solution for the 2026 BEHAVIOR Challenge

    November 11, 20264 Views
    Don't Miss
    AI Tools & Software

    Deep Q-Learning with Space Invaders

    By adminNovember 15, 20260

    ⚠️ A new updated version of this article is available here 👉 https://huggingface.co/deep-rl-course/unit1/introduction This article…

    Director of Machine Learning Insights [Part 3: Finance Edition]

    November 15, 2026

    Convert Transformers to ONNX with Hugging Face Optimum

    November 15, 2026

    Getting Started With Embeddings

    November 15, 2026
    About Us

    Welcome to TechWave Dispatch

    At TechWave Dispatch, we bring you the latest news, insights, and developments from the ever-evolving world of technology. Our mission is to keep readers informed about the innovations shaping our digital future.

    From artificial intelligence and machine learning to cybersecurity, robotics, smart devices, and groundbreaking technologies, we cover the topics that matter in today's fast-moving tech landscape.

    Our Picks

    Deep Q-Learning with Space Invaders

    November 15, 2026

    Director of Machine Learning Insights [Part 3: Finance Edition]

    November 15, 2026

    Convert Transformers to ONNX with Hugging Face Optimum

    November 15, 2026
    Most Popular

    After blocking Muse, Amazon now lets users text Alexa to help them shop

    November 6, 20260 Views

    A controlled world for robot learning and evaluation

    November 6, 20260 Views

    Perplexity now has its own always-on AI agent

    November 6, 20260 Views
    Facebook X (Twitter) Instagram Pinterest
    • Terms and Conditions
    • Privacy Policy
    • Contact Us
    • About Us
    © 2026 TechWave Dispatch. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.