Skip to content
class

MovingAverageMinMaxObserver

extendsMinMaxObserver
MovingAverageMinMaxObserver(qscheme: QScheme = per_tensor_affine, qdtype: QDtype = quint8, averaging_constant: float = 0.01, eps: float = 1e-08)

Per-tensor observer with an exponential moving average of min / max.

An outlier-tolerant refinement of MinMaxObserver. Instead of taking a hard cumulative min/max, it tracks an exponential moving average of each batch's range, so a single anomalous batch nudges the running range only slightly and is soon forgotten as later batches pull it back toward the bulk of the data. The result is a tighter, more representative grid than a plain min/max on noisy activation streams — at the cost of one hyperparameter, the averaging constant.

When to pick it. Prefer this over MinMaxObserver whenever calibration data may contain occasional spikes (most real activation streams do) and you want a range that reflects the typical extreme rather than the absolute worst case. It is the activation observer wrapped by FakeQuantize in lucid.quantization.get_default_qat_qconfig, where smoothing also stabilises training. For heavy-tailed distributions where you would rather clip the tail than average it, HistogramObserver is stronger; for weights, use MovingAveragePerChannelMinMaxObserver.

The first batch seeds the range directly (the ±inf seed cannot be averaged); every later batch applies the EMA update with averaging constant cc:

min_val←(1−c) min_val+c (min⁡ixi)=min_val+c (min⁡ixi−min_val),\text{min\_val} \leftarrow (1-c)\,\text{min\_val} + c\,(\min_i x_i) = \text{min\_val} + c\,(\min_i x_i - \text{min\_val}), max_val←(1−c) max_val+c (max⁡ixi)=max_val+c (max⁡ixi−max_val).\text{max\_val} \leftarrow (1-c)\,\text{max\_val} + c\,(\max_i x_i) = \text{max\_val} + c\,(\max_i x_i - \text{max\_val}).

Smaller cc means heavier smoothing (slower to react, more outlier-robust); c=1c = 1 degenerates to using only the latest batch. The reduction to (scale, zero_point) is the same affine / symmetric map inherited from MinMaxObserver.

Parameters

qschemeQScheme= per_tensor_affine
Target quantization scheme.
qdtypeQDtype= quint8
Target quantized dtype (unsigned [0, 255] grid).
averaging_constantfloat= 0.01
EMA weight c∈(0,1]c \in (0, 1] of each new batch's range; smaller is smoother and more outlier-robust, larger reacts faster.
epsfloat= 1e-8
Lower floor on the derived scale.

Attributes

min_valTensor
Scalar (shape ()) EMA of the batch minimum, seeded +inf.
max_valTensor
Scalar (shape ()) EMA of the batch maximum, seeded -inf.
averaging_constantfloat
The stored EMA weight cc.

Notes

  • First-batch seeding. forward detects the +inf seed and copies the first batch's range in verbatim; the EMA recurrence applies only from the second batch on. Without this the average would be corrupted by the infinite seed.
  • Not cumulative. Unlike MinMaxObserver, the range can shrink between batches — this is exactly why a lone outlier does not permanently widen the grid.
  • No autograd history. The EMA update runs under lucid.no_grad and is written back in place, per the ObserverBase contract.
  • Symmetric vs affine behaves as in the base class; averaging_constant only affects the running range, not the range → qparams reduction.
  • Default activation observer of lucid.quantization.get_default_qat_qconfig (wrapped in FakeQuantize).

Examples

>>> import lucid
>>> import lucid.quantization as Q
>>> obs = Q.MovingAverageMinMaxObserver(averaging_constant=0.1)
>>> for _ in range(8):
...     _ = obs(lucid.randn(16, 128))
>>> scale, zero_point = obs.calculate_qparams()
>>> bool(scale.item() > 0)
True
A lone outlier batch barely moves the smoothed range (contrast the hard-min/max
observer, which would absorb the full spike):
>>> obs = Q.MovingAverageMinMaxObserver(averaging_constant=0.01)
>>> _ = obs(lucid.zeros(1, 4))               # seed range ~ [0, 0]
>>> _ = obs(lucid.full((1, 4), 1000.0))      # one huge batch
>>> bool(obs.max_val.item() < 20.0)          # EMA absorbed only ~1% of the spike
True

See Also

Used by 3

Constructors

1
dunder

__init__

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

Instance methods

1
fn

forward

→Tensor
forward(x: Tensor)

EMA-update the running min/max (seeded on the first batch).