Skip to content
class

ObserverBase

extendsModule
ObserverBase(qscheme: QScheme, qdtype: QDtype, ch_axis: int | None = None, eps: float = 1e-08)

Abstract base for calibration observers that yield (scale, zero_point).

An observer is the calibration workhorse of the quantization subsystem: an lucid.nn.Module that watches a tensor stream during a calibration pass, accumulates a summary statistic (a running min/max, a channel-wise range, or a value histogram), and on demand distills that statistic into the (scale, zero_point) pair a quantized tensor needs. Its forward is the identity — it observes x and returns it untouched — so an observer can be spliced inline into a prepared graph or wrapped by a FakeQuantize without perturbing numerics. Concrete subclasses override forward (to fold each batch into the running statistic) and calculate_qparams (to reduce that statistic to qparams).

What calculate_qparams computes. Every min/max-family subclass funnels its observed range through the shared affine / symmetric calibration in lucid.quantization.calculate_qparams. The range is first widened to include real zero (so 0.0 is always representable), then mapped onto the integer grid [q_min, q_max] of qdtype:

affine:s=max⁡−min⁡qmax⁡−qmin⁡,z=clip⁡ ⁣(qmin⁡−round⁡(min⁡/s), qmin⁡, qmax⁡)\text{affine:}\quad s = \frac{\max - \min}{q_{\max} - q_{\min}},\qquad z = \operatorname{clip}\!\bigl(q_{\min} - \operatorname{round}(\min/s),\, q_{\min},\, q_{\max}\bigr) symmetric:s=max⁡(∣min⁡∣, ∣max⁡∣)(qmax⁡−qmin⁡)/2,z={0signed⌊(qmax⁡+qmin⁡+1)/2⌋unsigned\text{symmetric:}\quad s = \frac{\max(|\min|,\,|\max|)}{(q_{\max} - q_{\min})/2},\qquad z = \begin{cases} 0 & \text{signed} \\ \lfloor (q_{\max}+q_{\min}+1)/2 \rfloor & \text{unsigned} \end{cases}

with a floor s←max⁡(s,ε)s \leftarrow \max(s, \varepsilon) guarding a degenerate (constant-input) range. Affine spends a free zero_point to pack an asymmetric range tightly (ideal for post-ReLU activations); symmetric pins zero_point so real 0 lands exactly on a code (the standard choice for signed weights).

Choosing a subclass. Pick along three axes. Granularity — per-tensor (MinMaxObserver, one pair for the whole tensor) vs per-channel (PerChannelMinMaxObserver, one pair per output channel, far tighter for conv / linear weights whose channels differ in range). Outlier robustness — a hard running min/max grabs every spike, an EMA variant (MovingAverageMinMaxObserver) smooths per-batch outliers, and HistogramObserver clips the tails to minimise L2 error on heavy-tailed activations. Fixed-range — ops whose output range is known a priori use FixedQParamsObserver, and dtype-only carriers use PlaceholderObserver / NoopObserver.

Parameters

qschemeQScheme
Target quantization scheme — one of per_tensor_affine / per_tensor_symmetric / per_channel_affine / per_channel_symmetric. Selects the affine-vs-symmetric branch above and whether qparams are scalar or per-channel vectors.
qdtypeQDtype
Target quantized dtype supplying the grid bounds quant_min / quant_max (e.g. quint8 → [0, 255], qint8 → [-128, 127]) and the on-device storage dtype that physically holds the integer codes.
ch_axisint= None
Channel axis quantized independently. Forced to None for per-tensor schemes regardless of the value passed.
epsfloat= 1e-8
Lower floor on the derived scale, forwarded to lucid.quantization.calculate_qparams to avoid division by zero.

Attributes

qschemeQScheme
The stored target scheme.
qdtypeQDtype
The stored target dtype.
ch_axisint or None
The effective channel axis (None unless the scheme is per-channel).
epsfloat
The stored scale floor.

Notes

  • Identity forward. forward returns its input unchanged; only the side effect of updating the running buffers matters. This is what lets an observer be spliced into a prepared model transparently.
  • Statistics carry no graph. A statistics-gathering forward is decorated with _observes, which runs it under lucid.no_grad on buffers aligned to x's device, and it writes each statistic through _store_running_stat: in place when shape, dtype and device agree, re-registered otherwise (the scalar-seed → per-channel-vector change on the first observation). Gradients reach x only through whatever consumes the returned tensor — a FakeQuantize straight-through estimator — never through the buffers, so a training loop does not keep one step's graph alive per iteration.
  • Not instantiated directly. ObserverBase is abstract; its calculate_qparams raises NotImplementedError. Use a concrete subclass, or with_args to defer construction into a lucid.quantization.QConfig.
  • H4 purity. Every statistic is computed on the public lucid.* op surface — no external numeric libraries — so the same code path is correct on both the Accelerate (CPU) and MLX (GPU) streams.

Examples

>>> import lucid
>>> import lucid.quantization as Q
>>> obs = Q.MinMaxObserver()            # a concrete ObserverBase subclass
>>> _ = obs(lucid.randn(64, 128))       # forward observes and returns x unchanged
>>> scale, zero_point = obs.calculate_qparams()
>>> scale.shape, zero_point.shape
((), ())
Deferring construction into a lucid.quantization.QConfig recipe:
>>> factory = Q.PerChannelMinMaxObserver.with_args(ch_axis=0)
>>> obs2 = factory()                    # zero-arg factory builds a fresh observer
>>> type(obs2).__name__
'PerChannelMinMaxObserver'

See Also

Used by 4

Constructors

1
dunder

__init__

→None
__init__(qscheme: QScheme, qdtype: QDtype, ch_axis: int | None = None, eps: float = 1e-08)

Class methods

1
cls

with_args

→functools.partial[ObserverBase]
with_args(kwargs: object = {})

Return a zero-arg factory that builds this observer with kwargs.

Mirrors the reference framework's Observer.with_args ergonomic so a lucid.quantization.QConfig can carry an observer recipe rather than a live instance.

Instance methods

1
fn

calculate_qparams

→tuple[Tensor, Tensor]
calculate_qparams()

Return (scale, zero_point) from the accumulated statistics.