---
title: "Run an INT8 ONNX model in C#"
url: https://recursiveai.org/onemodel/cookbook/run-int8-onnx-model/
description: "Run a quantized INT8 ONNX model on the CPU in C#. Load an example with signed 8-bit inputs and read the 32-bit result without converting to float."
version: "1.3.0"
---

# Run an INT8 ONNX model in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModel) · [GitHub](https://github.com/recursive-ai-oss/onemodel-csharp) · [Docs](https://recursiveai.org/onemodel/docs/) · [Download example (.zip)](https://recursiveai.org/onemodel/cookbook/run-int8-onnx-model/run-int8-onnx-model.zip)

Run `int8.onnx` from the zip with `Run<sbyte, int>`: signed 8-bit input, 32-bit output.

```shell
dotnet add package RecursiveAI.OneModel
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onemodel/cookbook/run-int8-onnx-model/run-int8-onnx-model.zip), then `dotnet run`.

The model multiplies the input row `[4, -2]` by the signed INT8 column `[3, -5]` and prints the INT32 result:

```text
22
```

If 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`:

```csharp
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:

```csharp
int[] y = model.Run<sbyte, int>(x);
```

## Related recipes

- [Run an ONNX model in C#](https://recursiveai.org/onemodel/cookbook/run-onnx-model/)
- [Run an INT8 model on the GPU](https://recursiveai.org/onemodel/cookbook/run-int8-model-on-gpu/)
- [Run ONNX with named inputs and outputs](https://recursiveai.org/onemodel/cookbook/named-inputs-and-outputs/)

## 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`](https://onnx.ai/onnx/operators/onnx__MatMulInteger.html). 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](https://recursiveai.org/onemodel/docs/int8/#operators) 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.
