Skip to content
class

MinMaxObserver

extendsObserverBase
MinMaxObserver(qscheme: QScheme = per_tensor_affine, qdtype: QDtype = quint8, eps: float = 1e-08)

Per-tensor observer tracking the global running min / max of the input.

The simplest and most common calibration statistic: on every batch it folds the tensor's overall min / max into a cumulative running range, and calculate_qparams distills that single [min_val, max_val] into one per-tensor (scale, zero_point) pair. It is the natural default for activations and a serviceable choice for weights, but because the running range only ever grows, a single spike in any calibration batch permanently widens it — wasting codes on a range that most values never occupy.

When to pick it. Reach for MinMaxObserver when the tensor's distribution is well-behaved (roughly bounded, light-tailed) and you want the cheapest possible calibration. If a few calibration batches carry outliers, prefer MovingAverageMinMaxObserver, whose EMA lets the range relax back toward the bulk of the data; for heavy-tailed activations where clipping the tails buys real accuracy, prefer HistogramObserver. For weights, the per-channel PerChannelMinMaxObserver is almost always tighter.

Per batch the running range is the cumulative min/max:

min_val←min⁡(min_val, min⁡ixi),max_val←max⁡(max_val, max⁡ixi)\text{min\_val} \leftarrow \min(\text{min\_val},\ \min_i x_i),\qquad \text{max\_val} \leftarrow \max(\text{max\_val},\ \max_i x_i)

seeded from (+∞,−∞)(+\infty, -\infty), then reduced to qparams by the affine map of lucid.quantization.calculate_qparams (range first widened to include 0):

s=max_val−min_valqmax⁡−qmin⁡,z=clip⁡ ⁣(qmin⁡−round⁡(min_val/s), qmin⁡, qmax⁡)s = \frac{\text{max\_val} - \text{min\_val}}{q_{\max} - q_{\min}},\qquad z = \operatorname{clip}\!\bigl(q_{\min} - \operatorname{round}(\text{min\_val}/s),\ q_{\min},\ q_{\max}\bigr)

(or the symmetric form of the base class if a symmetric qscheme is passed).

Parameters

qschemeQScheme= per_tensor_affine
Target quantization scheme. Affine spends a free zero_point to fit an asymmetric range tightly (e.g. post-ReLU activations).
qdtypeQDtype= quint8
Target quantized dtype; quint8 gives the unsigned grid [0, 255].
epsfloat= 1e-8
Lower floor on the derived scale — avoids a zero scale on a constant input (min_val == max_val).

Attributes

min_valTensor
Scalar (shape ()) running minimum, seeded +inf.
max_valTensor
Scalar (shape ()) running maximum, seeded -inf.

Notes

  • Cumulative, not per-batch. min_val / max_val are monotone: they only widen across calibration, so the final range reflects the extreme of all data seen, not any single batch. This is the source of the outlier sensitivity.
  • No autograd history. The range is folded in under lucid.no_grad and written in place, per the ObserverBase contract.
  • Symmetric vs affine. With a symmetric qscheme the base-class math pins zero_point (0 for a signed dtype, mid-range for unsigned); with an affine scheme zero_point is free.
  • Uncalibrated. Before the first forward the range is [+inf, -inf]; calling calculate_qparams on it yields a meaningless (infinite) scale — always calibrate first.
  • Not the default in lucid.quantization.get_default_qconfig (which uses HistogramObserver for activations); it is the plain building block the EMA and per-channel variants specialise.

Examples

>>> import lucid
>>> import lucid.quantization as Q
>>> obs = Q.MinMaxObserver()
>>> for _ in range(4):                       # feed several calibration batches
...     _ = obs(lucid.randn(32, 256))
>>> scale, zero_point = obs.calculate_qparams()
>>> bool(scale.item() > 0)
True
An *uncalibrated* observer still holds the [+inf, -inf] seed, so its range is
degenerate until it has seen data:
>>> fresh = Q.MinMaxObserver()
>>> import math
>>> math.isinf(fresh.min_val.item()), math.isinf(fresh.max_val.item())
(True, True)

See Also

Used by 1

Constructors

1
dunder

__init__

→None
__init__(qscheme: QScheme = per_tensor_affine, qdtype: QDtype = quint8, eps: float = 1e-08)

Instance methods

2
fn

calculate_qparams

→tuple[Tensor, Tensor]
calculate_qparams()

Return (scale, zero_point) from the accumulated statistics.

fn

forward

→Tensor
forward(x: Tensor)

Fold x's global min/max into the running statistics.