Inspect which ONNX operations remain after folding constants and combining operations.
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
fold.onnx is in the zip.
using RecursiveAI.OneModel; using RecursiveAI.OneModel.Graph; using RecursiveAI.OneModel.Tensors; using var x = Tensor.Scalar(10f) .Reshape(1, 1); using var m = Model.Load("fold.onnx"); var inputs = new Dictionary<string, Tensor> { ["x"] = x }; var nodes = m.Session.GetDiagnostics(inputs).Nodes; Console.WriteLine(nodes.Length); Console.WriteLine(m.Run([10f])[0]);
Prints:
1 25
Unzip the example, then dotnet run.
The full program
fold.onnx is an original sample included in the zip. Its complete computation is sum = 2 + 3; product = x × 2; y = Relu(product + sum), with x shaped [1, 1]. It contains Add, MatMul, Add and Relu nodes; its constants are a = [2], b = [3] and w = [[2]]. The snippet uses x = [[10]].
using RecursiveAI.OneModel; using RecursiveAI.OneModel.Graph; using RecursiveAI.OneModel.Tensors; using var input = Tensor.FromSpan<float>([10f], Shape.Create([1, 1])); var inputs = new Dictionary<string, Tensor> { ["x"] = input }; foreach (var level in new[] { OptimizationLevel.None, OptimizationLevel.Basic, OptimizationLevel.All }) { using var model = Model.Load("fold.onnx", new ModelOptions { OptimizationLevel = level }); var nodes = model.Session.GetDiagnostics(inputs).Nodes; Console.WriteLine($"{level}: {nodes.Length} node(s), output {model.Run([10f])[0]}"); Console.WriteLine(string.Join(", ", nodes.Select(n => n.OpType))); }
dotnet run prints:
None: 3 node(s), output 25 MatMul, Add, Relu Basic: 3 node(s), output 25 MatMul, Add, Relu All: 1 node(s), output 25 MatMul
Invalid ONNX files throw OnnxImportException; unsupported operators throw SessionException listing the operators.
Variations
Use exact arithmetic
Use exact arithmetic with all eligible rewrites:
using RecursiveAI.OneModel; using RecursiveAI.OneModel.Graph; using var model = Model.Load("fold.onnx", new ModelOptions { OptimizationLevel = OptimizationLevel.All, Mode = ArithmeticMode.Exact }); Console.WriteLine(model.Run([10f])[0]);
25Related recipes
The short answer
Set ModelOptions.OptimizationLevel when loading the model, supply tensors with the intended input shapes to Session.GetDiagnostics, and inspect its Nodes. Compare the output as well as the node count. Constant folding is not the same as fusion, and a smaller plan does not establish a speed improvement.
How do you inspect ONNX constant folding in C#?
Use a graph whose constant-only computation feeds a runtime node. The sample adds two initializers to form a bias, then adds that bias to a matrix product involving the runtime input. It finally applies Relu. The input is positive, so the sample output is twenty-five with every tested level.
The graph passes reference says Basic enables constant-fold. It evaluates nodes whose inputs are all constants, using the session’s arithmetic mode. The pass skips data-dependent shapes, nodes feeding a graph output, and results larger than both their inputs and 64 KiB. That scope matters: a constant-looking expression need not disappear in every context.
There is another layer: plans still evaluate constant subgraphs at None. Consequently the full program reports three diagnostic nodes for both None and Basic, not four versus three. The constant Add is absent from the executed operator list in both cases. Equal counts here do not mean the Basic option was ignored. Diagnostics describe execution for supplied shapes, not a dump of every original ONNX node or a pass-by-pass rewrite log.
What does ONNX graph optimization change?
All, the default, enables eligible fusion and packing passes in addition to constant folding. In this sample, the constant-weight matrix product, bias addition and activation combine into one executed node. Diagnostics label it MatMul; the output remains twenty-five. Read that as one executed operator covering the chain, not as evidence that the bias or activation vanished from the mathematical computation.
Options apply at session creation. Create separate model instances to compare levels, as the full program does, rather than modifying an existing session. Use the same file and identical runtime values for each comparison. Supply concrete input shapes to diagnostics because execution plans are shape-specific; a node count without those shapes is incomplete context.
Exact mode runs only eligible bit-preserving rewrites. For example, the documented Conv–BatchNormalization fold is excluded because moving a scale before a sum changes rounding. The explicit exact-mode variation runs this small graph and prints twenty-five. It does not assert that every optimized graph is bit-identical to every unoptimized fast-mode graph.
Does graph optimization work with ONNX FP16 weights?
Distinguish an imported half-precision graph from storage conversion requested by an option. The reference says a model containing its own half-precision types, initializers or casts is not rewritten, because these passes use binary32 kernels. Do not expect the sample’s fusion count to carry over to such a model.
With WeightStorage set to Binary16 or BFloat16, qualifying weights stay compact and are not packed. Passes pack and fold only binary32 constants, while operations around compact weights can still be rewritten. A batch-normalization fold needing binary32 weights is skipped for compact weights; a convolution’s activation can still fuse. Weights below the documented compaction threshold of 1024 elements remain binary32 and can be packed. This differs from importing a graph already expressed in half precision. The sample intentionally uses float32 throughout so that constant evaluation and fusion are visible without numeric conversion as a second variable.