# ONNX Runtime — Cross-Platform High-Performance ML Inference > A cross-platform inference and training accelerator from Microsoft that runs ONNX models on CPUs, GPUs, and specialized hardware with optimized execution providers for production deployment. ## Install Save as a script file and run: # ONNX Runtime — Cross-Platform High-Performance ML Inference ## Quick Use ```bash pip install onnxruntime-gpu # or onnxruntime for CPU-only ``` ```python import onnxruntime as ort session = ort.InferenceSession("model.onnx", providers=["CUDAExecutionProvider"]) inputs = {"input": input_array} outputs = session.run(None, inputs) ``` ## Introduction ONNX Runtime (ORT) is Microsoft's production inference engine for machine learning models in the ONNX (Open Neural Network Exchange) format. It provides hardware-accelerated inference across different platforms and devices, serving as the bridge between training frameworks like PyTorch and TensorFlow and optimized deployment environments. ## What ONNX Runtime Does - Runs ONNX models with automatic graph optimizations for faster inference - Supports 20+ execution providers including CUDA, TensorRT, DirectML, and CoreML - Provides C, C++, Python, C#, Java, JavaScript, and Objective-C APIs - Enables on-device inference for mobile (Android/iOS) and edge deployments - Includes training acceleration with optimized gradient computation via ORTModule ## Architecture Overview ORT loads an ONNX graph, applies graph optimizations (constant folding, operator fusion, memory planning), partitions nodes across available execution providers, and executes the optimized graph. Each execution provider implements a subset of ONNX operators using hardware-specific APIs. The session-based API allows concurrent inference with thread-safe model execution. ## Self-Hosting & Configuration - Install platform-specific wheels via pip or language-specific package managers - Export models to ONNX from PyTorch using torch.onnx.export or from TensorFlow via tf2onnx - Configure execution providers and session options for hardware targeting - Use ONNX Runtime Model Optimizer (ORT Model Optimizer) for further quantization and pruning - Deploy on serverless platforms, containers, or edge devices with the same model file ## Key Features - Automatic graph optimizations that accelerate inference without model changes - Quantization support (INT8, FP16) with minimal accuracy loss for deployment - Cross-platform binary size optimization for mobile deployment (under 1MB core) - ONNX Runtime Extensions for pre/post-processing operators in the same graph - Broad hardware support from NVIDIA GPUs to Apple Neural Engine to Qualcomm NPUs ## Comparison with Similar Tools - **TensorRT** — NVIDIA-only with deeper GPU optimization; ORT is cross-platform with TensorRT as one backend - **TFLite** — mobile-focused for TensorFlow models; ORT handles any ONNX model on mobile - **PyTorch (eager)** — training-first with higher inference overhead; ORT is optimized for production serving - **Triton Inference Server** — orchestrates multiple models; ORT is the inference engine underneath - **OpenVINO** — Intel-optimized; ORT supports Intel hardware plus many other targets ## FAQ **Q: Which frameworks can export to ONNX?** A: PyTorch, TensorFlow, scikit-learn, XGBoost, LightGBM, Keras, and many others have ONNX exporters. **Q: Does ONNX Runtime support transformer models?** A: Yes. It includes specialized optimizations for transformer architectures including attention fusion and Flash Attention integration. **Q: Can I use it for training, not just inference?** A: Yes. ORTModule wraps PyTorch modules to accelerate training with the same graph optimizations used for inference. **Q: What is the performance improvement over native PyTorch?** A: Typical speedups range from 1.5x to 4x for inference depending on the model architecture and hardware target. ## Sources - https://github.com/microsoft/onnxruntime - https://onnxruntime.ai --- Source: https://tokrepo.com/en/workflows/asset-919bbca2 Author: Script Depot