Skip to main content
Graph optimization is a key feature of ONNX Runtime that improves inference performance by transforming the computational graph without changing its semantics. These optimizations reduce computation, memory usage, and improve hardware utilization.

What are Graph Optimizations?

Graph optimizations are transformations applied to the ONNX computational graph:
  • Constant folding: Pre-compute constant expressions
  • Operator fusion: Combine multiple operators into a single kernel
  • Redundancy elimination: Remove unnecessary computations
  • Layout transformations: Optimize data layouts for hardware
Optimizations are semantics-preserving - they produce the same results while improving performance.

Optimization Levels

ONNX Runtime organizes optimizations into hierarchical levels:
No optimizations applied. Useful for:
  • Debugging
  • Validating optimization correctness
  • Ensuring bit-exact reproducibility
Higher optimization levels increase session creation time but improve inference performance. Use ORT_ENABLE_ALL for production.

Graph Transformer Architecture

ONNX Runtime uses a transformer-based architecture for optimizations:

Transformer Categories

Rule-Based

Pattern matching and replacement
  • EliminateIdentity
  • ConstantFolding
  • CommonSubexpressionElimination

Fusion

Combine multiple operators
  • ConvBatchNormFusion
  • MatMulAddFusion
  • GELUFusion

Layout

Data layout transformations
  • NCHWToNHWC
  • TransposeOptimizer

EP-Specific

Hardware-specific optimizations
  • CUDA kernel fusions
  • TensorRT subgraph compilation

Common Optimizations

Constant Folding

Pre-compute operations with constant inputs:
Shape operations computed at runtime
Constant folding is especially effective for models with dynamic shapes that use shape manipulation operations.

Operator Fusion

Combine multiple operators into a single fused kernel:
Benefits:
  • Reduces memory bandwidth
  • Fewer kernel launches
  • Can fold BN parameters into Conv weights
Implementation:
Benefits:
  • Single kernel call
  • Better cache utilization
  • BLAS optimization (GEMM)
Common patterns:
  • Conv + Relu → ConvRelu
  • MatMul + Relu → GemmRelu
  • Add + Relu → AddRelu
  • LayerNorm + GELU → LayerNormGELU
Example:
Fuse multi-headed attention pattern:Benefits:
  • Massive reduction in memory transfers
  • Optimized attention kernels (FlashAttention)
  • Better GPU utilization

Redundancy Elimination

Remove unnecessary operations:

Shape Inference

Propagate shape information through the graph:
Shape inference is automatic in ONNX Runtime but can be pre-computed for faster session initialization.

Layout Optimizations

Transform data layouts for optimal hardware execution:

NCHW vs NHWC

Best for:
  • CUDA GPU operations
  • Standard ONNX format
  • Most deep learning frameworks

Automatic Layout Optimization

Memory Optimizations

Memory Reuse Planning

ONNX Runtime plans memory reuse to minimize peak memory:
Memory reuse reduces peak usage from 3MB to 2MB.

In-Place Operations

Some operations can modify tensors in-place:
In-place operations require careful analysis to ensure correctness. ONNX Runtime automatically detects safe in-place opportunities.

Execution Provider Optimizations

EPs can provide hardware-specific optimizations:

CUDA EP Optimizations

CUDA-specific optimizations:
  • Kernel fusion (multiple ops in one CUDA kernel)
  • Memory coalescing
  • Shared memory utilization
  • cuDNN algorithm tuning

TensorRT EP Optimizations

TensorRT optimizations:
  • Layer fusion (vertical and horizontal)
  • Precision calibration (FP16, INT8)
  • Kernel auto-tuning
  • Dynamic tensor memory management

Custom Graph Transformers

You can implement custom optimizations:
Custom transformers require C++ implementation and building ONNX Runtime from source. See the Custom Operators guide for implementing custom functionality.

Inspecting Optimizations

Save Optimized Model

Verbose Logging

Performance Impact

Typical performance improvements from optimizations:

Computer Vision

ResNet-50:
  • Basic: 5-10% faster
  • Extended: 20-40% faster
  • All: 30-50% faster
Key optimizations:
  • Conv+BN fusion
  • Activation fusions
  • Layout optimization

NLP Models

BERT:
  • Basic: 10-15% faster
  • Extended: 40-60% faster
  • All: 50-70% faster
Key optimizations:
  • Attention fusion
  • LayerNorm fusion
  • Embedding optimization
Actual speedup depends on model architecture, hardware, and input shapes. Always benchmark your specific use case.

Best Practices

The initialization overhead is amortized over many inferences.
Pre-optimized models load faster.

Troubleshooting

Optimization Increases Latency

Numerical Differences

Optimizations are semantics-preserving but may have small numerical differences:
Small numerical differences (1e-6) are normal due to different operation orders. Larger differences indicate a bug.

Session Creation Too Slow

Next Steps

Quantization

Further optimize models with quantization

Model Optimization

End-to-end model optimization workflow

Performance Tuning

Complete performance tuning guide

Performance Tuning

Profile and analyze model performance

Additional Resources