# `BB.Inference.Quantisation`
[🔗](https://github.com/beam-bots/bb_inference/blob/main/lib/bb/inference/quantisation.ex#L5)

How a quantised tensor's raw integers map to real values:
`real = (raw - zero_point) * scale`.

Per-tensor quantisation has one `scale` and one `zero_point`, and `axis` is
`nil`. Per-axis quantisation has a list of each, one per index along `axis`.

Runtimes report quantisation and pass raw values through; they never
convert. Converting a whole input or output tensor in Nx's binary backend is
slow - hundreds of milliseconds for a camera frame - so a codec quantises
its input while it is still an OpenCV matrix, and dequantises only the
output values it actually reads, with `value/5`.

# `t`

```elixir
@type t() :: %BB.Inference.Quantisation{
  axis: non_neg_integer() | nil,
  scale: float() | [float()],
  zero_point: integer() | [integer()]
}
```

# `affine`

```elixir
@spec affine(t() | nil) :: {float(), float()}
```

The affine step that quantises a real value: `raw = real * alpha + beta`,
as `{alpha, beta}`. `nil` quantisation is the identity.

Per-axis input quantisation isn't supported here; it raises.

# `dequantise`

```elixir
@spec dequantise(number(), t() | nil, non_neg_integer(), tuple() | nil) :: number()
```

The real value of `raw`, the element at flat `index` of a tensor of `shape`.

# `value`

```elixir
@spec value(binary(), non_neg_integer(), Nx.Type.t(), t() | nil, tuple() | nil) ::
  number()
```

Read element `index` of a tensor's raw binary and return its real value.

`quantisation` is the tensor's `BB.Inference.Quantisation`, or `nil` for a
tensor that isn't quantised, whose value is returned as it is. `shape` is
only needed for per-axis quantisation, to find which index along the axis
the element is at.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
