Sub topic 9
Graph Optimization Levels and Operator Fusion
Prerequisites and the optimization pipeline
This chapter assumes basic Python, ONNX Runtime, and neural-network concepts. You should know that an ONNX model is a graph: operators are nodes, and tensors flow along edges.
ONNX Runtime (ORT) optimizes this graph when it creates an InferenceSession. It can remove unnecessary nodes, fold constant calculations, and fuse several operators into one implementation. The optimized graph is then assigned to the selected execution provider, such as CUDA or TensorRT.
Optimization levels
ORT exposes four optimization levels:
| Level | Typical behavior | Use |
|---|---|---|
| `ORT_DISABLE_ALL` | No graph rewrites | Debugging and comparison |
| `ORT_ENABLE_BASIC` | Safe simplifications and constant folding | Conservative optimization |
| `ORT_ENABLE_EXTENDED` | More fusions and provider-specific rewrites | General deployment |
| `ORT_ENABLE_ALL` | All optimizations supported by the installed version | Maximum performance testing |
Optimization names and available fusions can change between ORT versions. Treat the level as a policy, not as a guarantee that every model receives every transformation.
import onnxruntime as ort
options = ort.SessionOptions()
options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
options.optimized_model_filepath = "resnet50.optimized.onnx"
session = ort.InferenceSession(
"resnet50.onnx",
sess_options=options,
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
)
print(session.get_providers())Setting optimized_model_filepath saves the graph produced during session creation. Inspecting that file with an ONNX graph viewer can reveal whether expected transformations occurred.
Operator fusion
Operator fusion replaces a sequence of nodes with one node or one provider-specific kernel. For example, inference graphs often contain:
- A convolution.
- Batch normalization.
- ReLU activation.
A fused implementation can reduce memory traffic and kernel-launch overhead. It may also keep intermediate values in faster memory. The mathematical result should remain close, but floating-point operation order can change slightly.
Fusion depends on graph structure. A reshape, unsupported operator, dynamic behavior, or mismatched tensor layout can prevent it. A model exported in training mode can also contain nodes that block inference-specific transformations.
Provider-specific behavior
Graph optimization and provider partitioning are separate steps. ORT may optimize the complete graph first, then divide it among providers. TensorRT can receive a supported subgraph and apply additional fusions while building its engine. Consequently, the CUDA and TensorRT sessions may produce different optimized graphs.
Keep the ORT version, CUDA version, TensorRT version, and optimization settings fixed when comparing benchmarks. Recreate the session after changing settings, because optimization normally occurs during session construction.
Exercise
Why can a Conv-BatchNorm-ReLU sequence be faster after fusion?
Show answer
Fusion can reduce intermediate memory traffic and combine several kernel launches into one implementation, while usually preserving the model's result within a small floating-point tolerance.