Recursive AI

Cap the size of an ONNX model stream in C#

OneModel is a free, open-source neural-network library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

The download’s add-one.onnx is 78 bytes. This example rejects it with a 32-byte limit.

Terminal
dotnet add package RecursiveAI.OneModel

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneModel;
 
var limit = new ModelLoadOptions {
    MaxModelBytes = 32
};
using var stream =
    File.OpenRead("add-one.onnx");
using var model =
    Model.Load(stream, loadOptions: limit);

Unzip the example, then dotnet run.

The snippet throws OnnxImportException. Its Kind is Unsupported.

The full program

The program catches the error and prints its kind and message. The using statement closes the input stream; Model.Load leaves it open.

C#
using RecursiveAI.OneModel;
using RecursiveAI.OneModel.Onnx;
 
using var stream = File.OpenRead("add-one.onnx");
long maxBytes = args is ["accept"] ? 78 : 32;
var limit = new ModelLoadOptions
{
    MaxModelBytes = maxBytes
};
try
{
    using var model = Model.Load(stream, loadOptions: limit);
    Console.WriteLine(string.Join(", ", model.Run([2f, 4f])));
}
catch (OnnxImportException ex)
{
    Console.WriteLine(ex.Kind);
    Console.WriteLine(ex.Message);
}
Text
Unsupported
The stream holds more than 32 bytes (ModelLoadOptions.MaxModelBytes); load from a file path so it can be memory-mapped, or save ONNX weights as external data. (<stream>)

Variations

Set the limit to 78 bytes to accept this file. Make the change below or run dotnet run -- accept in the download. The program loads the model and runs it on 2 and 4.

C#
MaxModelBytes = 78
Text
3, 5

The short answer

Pass new ModelLoadOptions { MaxModelBytes = limit } as the named loadOptions argument to Model.Load. Give the limit in bytes, not in parameters or tensors. The default is long.MaxValue, but an ONNX protobuf message still has to fit below 2 GiB. OneModel rejects inputs over the limit while reading the stream. For ONNX, the exception is OnnxImportException with Kind set to Unsupported; for a stream recognized as OneModel’s own format, it is ModelFormatException with the same kind.

How big can an ONNX model loaded from a stream be?

Ordinary streams are buffered, so your application needs memory for those serialized bytes even before a model is ready to run. Pick a positive limit that fits the models you expect and your application’s memory budget. The 32-byte value in the example makes the failure repeatable; it isn’t a suggested production limit.

Don’t use this option as a quota on all memory used to run the model. It limits the serialized input, not the tensors and other allocations that inference may need. It also isn’t a promise that no more than that number of bytes will be pulled from the source: Model.Load can read up to eight bytes to identify the format, and a non-seekable stream is read in chunks before the accumulated length is checked. Use a separate transport limit if the exact number of downloaded bytes matters.

Can you load a large ONNX model with external weights?

Yes. ONNX lets you store tensor data outside the model’s protobuf. A stream has no directory of its own, so if your ONNX file references external weights, set ExternalDataDirectory to the folder that contains them. With a path instead of a stream, OneModel uses the model file’s directory by default. The stream limit covers the model stream, not a sum of its referenced file sizes. See ONNX external data for how those references are represented.

All onemodel recipes