dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneModel.Graph; using var session = Session.Load("double.onnx"); float[] input = [3f], output = new float[1]; for (int i = 0; i < 3; i++) { input[0] = i + 3; session.Run<float, float>( "x", input, [1], "y", output); Console.WriteLine( $"{input[0]} -> {output[0]}"); }
3 -> 6 4 -> 8 5 -> 10
The same input and output arrays serve all three calls. Each completed call overwrites the output with the new result; copy a result elsewhere if you need to retain it.
A wrong input shape or an output span that is too short throws SessionException with InvalidInput; the latter reports the required length.
double.onnx is in the download. Its complete graph is y = Add(x, x), with float32 input x[1] and output y[1].
Unzip the example, then dotnet run.
Variations
Reuse tensors for named inputs and outputs. This standalone alternative preallocates the tensors and argument arrays before the loop.
using RecursiveAI.OneModel.Graph; using RecursiveAI.OneModel.Tensors; using var session = Session.Load("double.onnx"); using var input = Tensor.FromSpan<float>([3f], Shape.Create(1)); using var output = Tensor.Zeros(DType.F32, Shape.Create(1)); string[] names = ["x"], wanted = ["y"]; Tensor[] inputs = [input], outputs = [output]; for (int i = 0; i < 3; i++) { input.MutableData<float>()[0] = i + 3; session.Run(names, inputs, wanted, outputs); Console.WriteLine(output.Data<float>()[0]); }
6 8 10
Related recipes
The short answer
Create the session and buffers once, update the input contents, then call Session.Run<float, float> with the same arrays on each iteration. Supply the input’s dimensions separately. For a model with multiple inputs, use the tensor overload and preallocate the output tensors as shown in the variation.
Buffer reuse is an ownership decision, not a promise that no copying occurs. The span path copies input into staging storage and copies the result into your output span. It lets your application keep its arrays while the session manages intermediate storage. The session contract describes these two layers separately.
How do I reuse inference input and output buffers in C#?
Move allocations outside the prediction loop. The example creates one input array and one output array, then changes only input[0]. The output array is not a history of predictions: it contains the most recent result after each synchronous call returns. Consume that result before the next call overwrites it.
For this fixture, the input and output each contain one float. The shape [1] is a one-dimensional vector, not a scalar. Array length alone does not express tensor rank. A different model could require a matrix or a batch dimension even when the total number of values looks familiar.
The generic span overload serves a model with exactly one input. Its input element type must match the model, and the supplied value count must equal the product of the input dimensions. The output span must have room for the requested output, and Run returns the output’s shape. Do not pass an oversized input array as though its unused capacity were part of the model input; pass a span over the intended values.
How do I reuse ONNX output buffers with multiple named inputs?
Use the tensor variation as the pattern for the named API. The input names and tensors occupy corresponding positions, as do the requested output names and output tensors. Allocate those argument arrays outside the loop too. Add an entry to each input array for every required input in your model.
The caller-owned tensor path requires contiguous storage with the correct output element type and shape. It writes into the tensor you provide rather than returning a new output collection. You own that tensor’s lifetime, so dispose it after the last run that uses it. Do not dispose it after each iteration if the next iteration needs the same storage.
For changing output shapes, choose storage based on the actual model contract. A tensor with the right element count but the wrong shape is not an interchangeable destination. If you cannot know the result shape beforehand, the ordinary returned-output API may be more appropriate than forcing a fixed destination.
A session can run concurrently, but that does not make a shared writable array safe. Give overlapping calls separate mutable input and output storage. Sequential calls can reuse the same storage after each call returns, as this program does. Reusing a session also lets it reuse plans for previously seen input types and shapes; changing only values does not require a new shape plan.