Run int8.onnx from the zip with Run<sbyte, int>: signed 8-bit input, 32-bit output.
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneModel; using var model = Model.Load("int8.onnx"); sbyte[] x = [4, -2]; int[] y = model.Run<sbyte, int>(x, [1, 2]); Console.WriteLine(y[0]);
Unzip the example, then dotnet run.
The model multiplies the input row [4, -2] by the signed INT8 column [3, -5] and prints the INT32 result:
22If your model has one input and one output, replace int8.onnx with its path and match the types and shape it declares. If you ask for float output from the sample graph, OneModel throws SessionException: the output tensor holds INT32, not float32.
Variations
To run a different row through the same model, replace the input values. For example, [1, 2] produces -7:
sbyte[] x = [1, 2];If the model declares the full input shape, you can leave the shape argument out. For int8.onnx, OneModel gets [1, 2] from the graph:
int[] y = model.Run<sbyte, int>(x);Related recipes
The short answer
Load the quantized .onnx with Model.Load, then call Run<TIn, TOut> with the model’s declared input and output types. This example runs on the CPU. Its input is signed INT8 (sbyte in C#), but its output is INT32 (int). You don’t have to dequantize the model before loading it.
What does INT8 quantization mean in ONNX?
Quantization represents values using integers with a scale and a zero point. The zero point is the integer that represents real zero; the scale tells you how much one integer step represents. Different parts of an ONNX graph can use different types. You might have INT8 weights, INT32 accumulation, and float32 inputs and outputs in the same model.
The sample uses MatMulInteger. With no zero points supplied, it multiplies 4 × 3 and −2 × −5, adds the products, and returns 22. There is no float conversion or output scale in this graph. Other operators, such as QLinearMatMul, use scale and zero point inputs to turn the accumulator back into a quantized value. The INT8 operator table lists what OneModel accepts.
How do you run a quantized ONNX file?
Use model.Session.Inputs and model.Session.Outputs to inspect the model’s inputs and outputs. Each entry gives you the tensor’s Name, DType (element type), and Dims (declared dimensions). Match the types at the graph boundary, not the filename. Use sbyte for a signed INT8 input or byte for UINT8; they are different types even though both take up one byte. If the graph declares float32 input and quantizes internally, feed float values instead. A model with multiple inputs or outputs needs Session and named tensors rather than the single-input, single-output Model.Run used here.
Flatten each tensor in row-major order. For the sample, [1, 2] means one row of two elements, not a batch of two rows. The graph declares that shape, so the shorter call in Variations works too. If a graph leaves too many dimensions unspecified for OneModel to infer them from the number of values, pass the shape explicitly.
If import fails on a real quantized model, check the operators and attributes used in that export. The INT8 documentation excludes blocked quantization and INT4/UINT4 for the operators it lists. An “INT8” filename doesn’t tell you whether your model uses those features.