Skip to content
data

QParams

QParams(scale: Tensor, zero_point: Tensor, qscheme: QScheme, qdtype: QDtype, ch_axis: int | None = None)

Immutable bundle of everything that defines a quantized tensor's grid.

A quantized tensor in Lucid's sidecar representation is an ordinary integer lucid.Tensor plus the metadata needed to interpret its codes as real numbers. QParams is that metadata gathered into one frozen value object: the scale / zero_point produced by calibration, together with the lucid.quantization.QScheme (grid geometry) and lucid.quantization.QDtype (grid range) they were derived under, plus the channel axis for per-channel schemes.

It is the return-shape of an observer's calibration: an observer records a range, calls calculate_qparams to get (scale, zero_point), and packages the result — with the scheme / dtype it used — into a QParams so downstream quantize / dequantize / fake-quant calls have a single self-describing object to read from.

Attributes

scaleTensor
The quantization step size — how many real units one integer code spans. A scalar for per-tensor schemes; a length-C vector for per-channel schemes.
zero_pointTensor
The integer code that real 0 maps to, stored as an integer-valued float tensor. 0 (or the grid midpoint) for symmetric schemes; solved for under affine schemes. Same shape as scale.
qschemeQScheme
The mapping family (per-tensor / per-channel × affine / symmetric) the parameters were derived under.
qdtypeQDtype
The target integer grid (bit width + [quant_min, quant_max]) the codes clamp to.
ch_axisint or None, default None
The channel axis for per-channel schemes; None for per-tensor.

Notes

  • Frozen (@dataclass(frozen=True)): a QParams is a value, safe to share and compare; re-calibration produces a new instance rather than mutating one.
  • scale and zero_point always share a shape, and it is that shape (scalar vs length-C) — not qscheme alone — that a kernel broadcasts against.
  • The pair is consumed by lucid.quantization.quantize / lucid.quantization.dequantize / lucid.quantization.fake_quantize, which broadcast scale / zero_point along ch_axis.

Examples

>>> import lucid.quantization as Q
>>> scale, zp = Q.calculate_qparams(-2.0, 6.0, Q.per_tensor_affine, Q.quint8)
>>> p = Q.QParams(scale, zp, Q.per_tensor_affine, Q.quint8)
>>> p.qscheme.is_per_channel, p.ch_axis
(False, None)
>>> round(float(p.scale.item()), 4)        # (6 - (-2)) / 255
0.0314

See Also

Used by 1