Skip to main content

Overview

ONNX Runtime provides extensive performance tuning options to optimize model inference and training. This guide covers the key configuration options and best practices for achieving optimal performance.

Session Configuration

Creating an Optimized Session

Use SessionOptions to configure performance settings:

Graph Optimization Levels

ONNX Runtime provides different optimization levels:
  • ORT_DISABLE_ALL: No optimizations applied
  • ORT_ENABLE_BASIC: Basic optimizations like constant folding, redundant node elimination
  • ORT_ENABLE_EXTENDED: Extended optimizations including node fusion, layout optimizations
  • ORT_ENABLE_ALL: All available optimizations (recommended for production)

Execution Providers

Selecting Execution Providers

Execution providers enable hardware acceleration:

Common Execution Provider Options

CUDA Provider

  • device_id: GPU device ID
  • arena_extend_strategy: Memory allocation strategy
  • gpu_mem_limit: Maximum GPU memory usage
  • cudnn_conv_algo_search: Algorithm selection (DEFAULT, EXHAUSTIVE, HEURISTIC)

TensorRT Provider

  • trt_fp16_enable: Enable FP16 precision
  • trt_int8_enable: Enable INT8 quantization
  • trt_max_workspace_size: Maximum workspace size for TensorRT
  • trt_engine_cache_enable: Cache compiled engines

Intra-Op and Inter-Op Parallelism

Thread Configuration

Control parallelism for optimal CPU utilization:

Execution Modes

  • ORT_SEQUENTIAL: Operators are executed sequentially (lower overhead)
  • ORT_PARALLEL: Operators can be executed in parallel (better for models with independent ops)

Model Optimization

Offline Optimization

Save optimized models for faster startup:

Optimization Configuration

Fine-tune specific optimizations:

Memory Management

Memory Pattern Optimization

Arena Configuration

I/O Binding for Zero-Copy

Reduce memory copies with I/O binding:

GPU I/O Binding

Profiling and Analysis

Enable Profiling

Analyze Performance

The profile file contains:
  • Operator execution times
  • Memory usage patterns
  • Data transfer overhead
  • Kernel launch times

Best Practices

1. Choose the Right Execution Provider

  • Use GPU providers (CUDA, TensorRT, DirectML) for compute-intensive models
  • Use CPU provider for smaller models or edge devices
  • Test multiple providers to find the best fit

2. Optimize Thread Configuration

3. Use I/O Binding

  • Reduces memory allocation overhead
  • Enables zero-copy for GPU inference
  • Best for high-throughput scenarios

4. Enable All Optimizations

5. Warm Up the Session

Common Performance Issues

Issue: Slow First Inference

Solution: Model optimization and kernel compilation happen on first run. Use warm-up iterations or save optimized models.

Issue: High Memory Usage

Solution:
  • Limit GPU memory with gpu_mem_limit
  • Use smaller batch sizes
  • Enable memory pattern optimization

Issue: Poor CPU Utilization

Solution:
  • Adjust intra_op_num_threads and inter_op_num_threads
  • Try different execution modes
  • Build ONNX Runtime with OpenMP support

Advanced Configuration

Custom Execution Provider Configuration

Session Configuration File

See Also