BB.Inference.Quantisation (bb_inference v0.1.0)

Copy Markdown View Source

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.

Summary

Functions

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

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

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

Types

t()

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

Functions

affine(quantisation)

@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(raw, quantisation, index, shape)

@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(binary, index, type, quantisation, shape \\ nil)

@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.