Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 5 min read How-to mediumAdept

Convert Transformers to ONNX with Hugging Face Optimum

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.

Assumes: PyTorch basicsHugging Face Transformers

Related posts

  • Accelerated Inference with Optimum and Transformers Pipelines

    Hugging Face mediumAdept

    Optimum 1.2 adds inference support via ONNX Runtime with ORTModelForXxx classes that drop into Hugging Face Transformers pipelines in place of AutoModelForXxx. An end-to-end tutorial converts deepset/roberta-base-squad2 to ONNX, applies graph optimization (operator fusion, constant folding) with ORTOptimizer, then dynamic int8 quantization with ORTQuantizer targeting AVX512-VNNI on an m5.xlarge. Quantization shrinks the model from 473MB to 292MB while keeping 99.61% of original accuracy on SQuAD v2 (F1 81.83 vs 82.15), and latency is then benchmarked at sequence length 128.

  • Optimum+ONNX Runtime - Easier, Faster training for your Hugging Face models

    Hugging Face mediumAdept

    Hugging Face Optimum's ORTTrainer swaps ONNX Runtime in as the training backend for Transformers models. It reports throughput gains of 39–130% over a PyTorch baseline on an 8×A100 node when combined with DeepSpeed ZeRO Stage 1, and up to 40% with ONNX Runtime alone. The speedups come from memory planning, kernel optimizations, multi-tensor-apply fused Adam, an FP16 optimizer that avoids device-to-host copies, mixed precision and graph fusions. Adopting it mostly means replacing Trainer and TrainingArguments with ORTTrainer and ORTTrainingArguments and optionally selecting the adamw_ort_fused optimizer.

  • Introducing Optimum: The Optimization Toolkit for Transformers at Scale

    Hugging Face easyNovice

    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. Its first integration uses Intel Neural Compressor to quantize Transformers for Intel Xeon CPUs. Quantization is hard to do by hand: ops must be swapped for quantized kernels, quantize/dequantize nodes inserted, observers, schemes and int8/uint8/int16 types chosen, and accuracy loss balanced. Neural Compressor handles this through post-training, quantization-aware and dynamic quantization driven by a YAML config that can be stored on the Model Hub.

  • Transformers.js v4: Now Available on NPM!

    Hugging Face mediumAdept

    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. Models are re-exported operation by operation using ONNX Runtime contrib operators such as GroupQueryAttention, MatMulNBits and MultiHeadAttention, giving about a 4x speedup for BERT embedding models. The release also adds Mamba, MLA and MoE architectures such as GPT-OSS 20B, which runs at about 60 tokens/s on an M4 Pro Max. Other changes include a move from Webpack to esbuild (10x faster builds, 53% smaller default bundle), a pnpm monorepo, a ModelRegistry API for inspecting and caching pipeline assets, and a standalone 8.8kB tokenizers library.

  • Accelerate your models with 🤗 Optimum Intel and OpenVINO

    Hugging Face mediumAdept

    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). A walkthrough applies post-training static quantization to a ViT image classifier fine-tuned on food101, using 300 calibration samples, then exports to OpenVINO IR and loads the result via OVModelForImageClassification pipelines. The quantized model shrinks 3.8x (344MB to 90MB) and cuts per-sample latency 2.4x (98ms to 41ms) with no measurable accuracy loss (87.6). Warm up the model before benchmarking, because compilation happens on the first inference. Encoder-decoder quantization is not yet supported.

The quest for software engineering knowledge.

Look:
Layout: